项目简介

这是一个基于 Go 语言实现的 Model Context Protocol (MCP) 服务器构建工具。它利用 Go 的反射机制,能够自动扫描并导出 Go 代码中的函数,将它们注册为 MCP 工具,供大型语言模型 (LLM) 客户端(如 Claude Desktop)调用。极大地简化了创建 MCP 服务器后端的过程,减少了大量手动注册工具所需的样板代码。

主要功能点

  • 自动发现函数: 无需手动注册,只需编写 Go 函数并遵循简单的命名约定(函数名首字母大写),工具会自动找到并注册它们。
  • 零样板代码: 摆脱繁琐的工具定义和参数处理逻辑,专注于编写核心业务逻辑。
  • 动态生成 Schema: 根据 Go 函数的参数和返回值类型,自动生成符合 MCP 规范的 JSON Schema。
  • 实时执行代码: 支持通过工具调用来编译和运行 Go 代码片段。
  • 类型安全转换: 自动处理常见的 Go 类型(如 string, int, float, bool, slice 等)与 JSON 类型之间的转换。
  • 热重载: 在开发过程中,修改 Go 函数代码后,服务器会自动重新加载并更新可用的工具,无需手动重启。

安装步骤

  1. 初始化 Go Modules: 在你的项目目录下执行 'go mod init your-project' (将 'your-project' 替换为你的项目名称)。
  2. 获取依赖: 执行 'go get github.com/mark3labs/mcp-go'。
  3. 克隆仓库: 执行 'git clone https://github.com/sullrich/go-mcp-reflect.git'。
  4. 进入仓库目录: 执行 'cd go-mcp-reflect'。
  5. 编译项目: 执行 'go build .'。这将在当前目录下生成一个可执行文件(在 Windows 上是 'go-mcp-reflect.exe')。

服务器配置(为 MCP 客户端准备)

MCP 服务器通过标准输入输出 (Stdio) 与客户端通信。要让 MCP 客户端(如 Claude Desktop)连接到此服务器,你需要在客户端的配置文件中指定如何启动这个服务器程序。

以 Claude Desktop 为例,其 MCP 配置通常位于用户目录下的特定文件(macOS: '~/Library/Application Support/Claude/claude_desktop_config.json',Windows: '%APPDATA%\Claude\claude_desktop_config.json')。你需要在此配置文件的 'mcpServers' 部分添加一个条目,例如:

{
  "mcpServers": {
    "go-mcp-reflect-server": { // 给你的服务器起一个名称,客户端将用这个名称来引用它
      "command": "/full/path/to/go-mcp-reflect", // **必填:** MCP服务器可执行文件的完整路径。请替换为你的实际路径。
      "args": [], // 启动 MCP 服务器时所需的命令行参数列表。此项目目前通常不需要额外参数。
      "env": {} // 启动 MCP 服务器时设置的环境变量。通常无需修改。
    }
  }
}

重要提示:

  • 确保 'command' 指向的是你编译生成的 'go-mcp-reflect' 可执行文件的正确完整路径。
  • 保存客户端配置文件后,重启 MCP 客户端(如 Claude Desktop)以加载新的服务器配置。

基本使用方法

  1. 编写 Go 函数: 在你的 Go 项目中(例如在 'main.go' 或一个单独的服务文件中),定义一个结构体,并在结构体上添加方法。确保你想导出为 MCP 工具的方法名称首字母大写。 这些方法通常需要接收一个 'context.Context' 作为第一个参数,并返回 '(结果, error)' 的形式。例如:

    package main
    
    import (
    	"context"
    	"fmt"
        "strings"
    )
    
    type MyAgentService struct{}
    
    // This function will be automatically exported as tool "greet_user"
    func (s *MyAgentService) GreetUser(ctx context.Context, name string) (string, error) {
    	return fmt.Sprintf("Hello, %s! This is your custom agent.", name), nil
    }
    
    // This function will be automatically exported as tool "add_numbers"
    func (s *MyAgentService) AddNumbers(ctx context.Context, a int, b int) (map[string]interface{}, error) {
        return map[string]interface{}{
            "sum": a + b,
            "input_a": a,
            "input_b": b,
        }, nil
    }
    
    // This function will be automatically exported as tool "count_words"
    func (s *MyAgentService) CountWords(ctx context.Context, text string) (map[string]interface{}, error) {
        words := strings.Fields(text)
        return map[string]interface{}{
            "text": text,
            "word_count": len(words),
        }, nil
    }
  2. 创建并运行 MCP 服务器: 在你的 'main' 函数中,使用 'go-mcp-reflect' 的 'AutoExporter' 将你的服务注册到 MCP 服务器上,然后启动服务器:

    package main
    
    import (
        "context"
        "fmt"
        "log"
        // 导入你的服务包,例如如果 MyAgentService 在 main 包,则无需导入
        // 如果在其他包,需要导入该包
    
        "github.com/mark3labs/mcp-go/server"
        "github.com/sullrich/go-mcp-reflect/exporter" // 导入 go-mcp-reflect 中的 exporter
        // "github.com/sullrich/go-mcp-reflect/hotreload" // 如果需要热重载
    )
    
    // MyAgentService 定义 (如上所示) ...
    type MyAgentService struct{}
    func (s *MyAgentService) GreetUser(ctx context.Context, name string) (string, error) { /* ... */ return "", nil }
    func (s *MyAgentService) AddNumbers(ctx context.Context, a int, b int) (map[string]interface{}, error) { /* ... */ return nil, nil }
    func (s *MyAgentService) CountWords(ctx context.Context, text string) (map[string]interface{}, error) { /* ... */ return nil, nil }
    
    
    func main() {
        // 1. 创建 MCP 服务器实例
        mcpServer := server.NewMCPServer(
            "my-custom-agent", // 给服务器起一个标识符
            "1.0.0",          // 版本号
            server.WithToolCapabilities(true), // 开启工具能力声明
            // 其他配置选项...
        )
    
        // 2. 创建你的服务实例
        myService := &MyAgentService{} // 使用你的服务结构体
    
        // 3. 创建 AutoExporter 并注册函数
        autoExporter := exporter.NewAutoExporter(mcpServer, myService)
        err := autoExporter.RegisterExportedFunctions()
        if err != nil {
            log.Fatalf("Failed to register functions: %v", err)
        }
    
        // 4. (可选) 启用热重载 - 需要在 go-mcp-reflect 的 main.go 中集成或使用其提供的 HotReloader
        // hotReloader := hotreload.NewHotReloader(autoExporter)
        // if err := hotReloader.EnableHotReload(); err != nil {
        //    log.Printf("Hot reload not available: %v", err)
        // } else {
        //    defer hotReloader.Close()
        // }
    
        // 5. 启动服务器 (通常使用 Stdio)
        log.Println("Starting MCP server...")
        if err := server.ServeStdio(mcpServer); err != nil {
            log.Fatalf("Server failed: %v", err)
        }
    }

    请注意,上述 'main' 函数示例是为了展示如何使用 'go-mcp-reflect' 的核心逻辑。'go-mcp-reflect' 仓库自带的 'main.go' 已经包含了这些逻辑和一个 'ExampleService',你可以直接编译并运行它。

  3. 连接 MCP 客户端: 根据前面“服务器配置”部分的说明,配置你的 MCP 客户端(如 Claude Desktop),使其能够找到并启动你编译好的 MCP 服务器程序。

  4. 在客户端中调用函数: 重启客户端后,客户端应该能发现你的服务器导出的工具(例如 'greet_user', 'add_numbers', 'count_words' 等),你现在可以在客户端中通过对话等方式调用这些工具了。

信息

分类

开发者工具