模型通信协议SDK

用于构建模型通信协议(MCP)工具和服务器的Go SDK,支持与AI应用(如Cursor IDE)集成。

  • by dentaku7
  • Go
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 dentaku7/mcp-go-sdk

使用说明

MCP Go SDK

用于构建模型通信协议(MCP)工具和服务器的Go SDK。此SDK提供了实现MCP兼容工具的构建块,这些工具可用于Cursor IDE等AI应用程序。

快速开始

查看中的示例服务器 servers/example 查看回显消息的MCP工具的最小实现:

package main

import (
    "encoding/json"
    "mcp-go/server"
    "mcp-go/transport"
)

// EchoTool implements a simple echo tool
type EchoTool struct{}

func (t *EchoTool) Name() string {
    return "echo"
}

func (t *EchoTool) Description() string {
    return "A simple echo tool that returns the input message"
}

func (t *EchoTool) Schema() json.RawMessage {
    return json.RawMessage(`{
        "type": "object",
        "properties": {
            "message": {
                "type": "string",
                "description": "The message to echo back"
            }
        },
        "required": ["message"]
    }`)
}

func (t *EchoTool) Execute(params json.RawMessage) (interface{}, error) {
    var input struct {
        Message string `json:"message"`
    }
    if err := json.Unmarshal(params, &input); err != nil {
        return nil, err
    }
    
    return map[string]interface{}{
        "content": []map[string]interface{}{
            {
                "type": "text",
                "text": input.Message,
            },
        },
        "metadata": map[string]interface{}{
            "length": len(input.Message),
        },
    }, nil
}

func main() {
    // Create a new server with stdin/stdout transport
    srv := server.NewServer(transport.NewStdioTransport())
    
    // Register your tool
    if err := srv.RegisterTool(&EchoTool{}); err != nil {
        panic(err)
    }
    
    // Start the server
    if err := srv.Start(); err != nil {
        panic(err)
    }
}

## Core Concepts

### 1. Tools

A Tool in MCP is a service that can be called by AI applications. Each tool must implement the `mcp.Tool` interface:

```go
type Tool interface {
    // Name returns the unique identifier for this tool
    Name() string
    
    // Description returns a human-readable description
    Description() string
    
    // Schema returns the JSON schema for the tool's parameters
    Schema() json.RawMessage
    
    // Execute runs the tool with the given parameters
    Execute(params json.RawMessage) (interface{}, error)
}

2.响应格式

工具应以MCP格式返回响应:

{
    "content": [
        {
            "type": "text",
            "text": "Your response text"
        }
    ],
    "metadata": {
        // Optional metadata about the response
    }
}

3.传输层

SDK通过以下方式提供了一个灵活的传输层 Transport 接口:

type Transport interface {
    Send(data interface{}) error
    Receive() ([]byte, error)
    Close() error
}

默认情况下,SDK包含stdio传输(transport.NewStdioTransport())用于命令行工具。

4.配置

要将MCP工具与Cursor IDE一起使用,请创建 .cursor/mcp.json 在项目根目录中:

{
    "mcpServers": {
        "mytool": {
            "command": "mytool",
            "args": [],
            "env": {}
        }
    }
}

贡献

  1. 分叉存储库
  2. 创建要素分支
  3. 添加新功能的测试
  4. 提交拉取请求

请确保您的代码:

  • 遵循Go最佳实践
  • 包括适当的文件
  • 具有测试覆盖率
  • 适当处理错误