公告

任何建议或需求可联系我!


Skip to content

入门指南

简介

什么是 MCP?

模型上下文协议(Model Context Protocol,MCP)是一个开放标准,支持 AI 应用与外部数据源和工具之间建立安全、可控的连接。它为大型语言模型(LLM)提供了一种标准化的方式来访问和交互外部系统,同时保持安全性和用户控制力。

为什么选择 MCP Go?

MCP-Go 旨在让使用 Go 构建 MCP 服务器变得快速、简单且功能完整:

  • 快速:高效的 Go 实现,开销极小
  • 简单:简洁直观的 API,最少的样板代码
  • 完整:完全支持 MCP 规范,包括工具、资源和提示

核心特性

  • 高层接口:专注于业务逻辑,而非协议细节
  • 最少样板代码:只需几行代码即可上手
  • 完整 MCP 规范支持:工具、资源、提示以及所有传输方式
  • 类型安全:利用 Go 的类型系统构建健壮的 MCP 服务器
  • 多种传输方式:支持 Stdio、StreamableHTTP、Server-Sent Events 和进程内传输

安装

将 MCP-Go 添加到你的 Go 项目中:

bash
go get github.com/mark3labs/mcp-go

MCP-Go 让使用 Go 构建模型上下文协议(MCP)服务器变得简单。本指南将帮助你在几分钟内创建你的第一个 MCP 服务器。

你的第一个 MCP 服务器

让我们创建一个带有 "hello world" 工具的简单 MCP 服务器:

go
package main

import (
    "context"
    "fmt"

    "github.com/mark3labs/mcp-go/mcp"
    "github.com/mark3labs/mcp-go/server"
)

func main() {
    // Create a new MCP server
    s := server.NewMCPServer(
        "Demo 🚀",
        "1.0.0",
        server.WithToolCapabilities(false),
    )

    // Add tool
    tool := mcp.NewTool("hello_world",
        mcp.WithDescription("Say hello to someone"),
        mcp.WithString("name",
            mcp.Required(),
            mcp.Description("Name of the person to greet"),
        ),
    )

    // Add tool handler
    s.AddTool(tool, helloHandler)

    // Start the stdio server
    if err := server.ServeStdio(s); err != nil {
        fmt.Printf("Server error: %v\n", err)
    }
}

func helloHandler(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
    name, err := request.RequireString("name")
    if err != nil {
        return mcp.NewToolResultError(err.Error()), nil
    }

    return mcp.NewToolResultText(fmt.Sprintf("Hello, %s!", name)), nil
}

运行你的服务器

  1. 将上面的代码保存到文件中(例如 main.go
  2. 运行:
    bash
    go run main.go

你的 MCP 服务器现在已经运行,并准备好通过 stdio 接受连接!

接下来做什么?

现在你已经有一个基本的服务器在运行,你可以:

  • 添加更多工具 - 创建用于计算、文件操作、API 调用等的工具
  • 添加资源 - 暴露文件、数据库或 API 等数据源
  • 添加提示 - 创建可复用的提示模板,以改善 LLM 交互
  • 探索示例 - 查看 examples/ 目录了解更多复杂用例

核心概念

工具(Tools)

工具让 LLM 能够通过你的服务器执行操作。它们就像 LLM 可以调用的函数:

go
calculatorTool := mcp.NewTool("calculate",
    mcp.WithDescription("Perform basic arithmetic operations"),
    mcp.WithString("operation",
        mcp.Required(),
        mcp.Enum("add", "subtract", "multiply", "divide"),
    ),
    mcp.WithNumber("x", mcp.Required()),
    mcp.WithNumber("y", mcp.Required()),
)

资源(Resources)

资源向 LLM 暴露数据。它们可以是静态文件或动态数据:

go
resource := mcp.NewResource(
    "docs://readme",
    "Project README",
    mcp.WithResourceDescription("The project's README file"),
    mcp.WithMIMEType("text/markdown"),
)

服务器选项

使用各种选项自定义你的服务器:

go
s := server.NewMCPServer(
    "My Server",
    "1.0.0",
    server.WithToolCapabilities(true),
    server.WithRecovery(),
    server.WithHooks(myHooks),
)

传输选项

MCP-Go 支持多种传输方式:

  • Stdio(最常用):server.ServeStdio(s)
  • StreamableHTTPserver.NewStreamableHTTPServer(s).Start(":8080")
  • Server-Sent Eventsserver.ServeSSE(s, ":8080")
  • 进程内client.NewInProcessClient(server)

需要帮助?