一个用于构建模型上下文协议(MCP)服务器的Go框架,使大型语言模型(LLMs)能够安全地访问工具和数据源。
go get github.com/gomcpgo/mcp
这是一个创建MCP服务器的最小示例:
package main
import (
"context"
"log"
"github.com/gomcpgo/mcp/pkg/handler"
"github.com/gomcpgo/mcp/pkg/server"
"github.com/gomcpgo/mcp/pkg/protocol"
)
func main() {
// 创建处理器注册表
registry := handler.NewHandlerRegistry()
// 注册你的工具处理器
registry.RegisterToolHandler(&MyToolHandler{})
// 创建并启动服务器
srv := server.New(server.Options{
Name: "my-server",
Version: "1.0.0",
Registry: registry,
})
if err := srv.Run(); err != nil {
log.Fatal(err)
}
}
// MyToolHandler 实现了 ToolHandler 接口
type MyToolHandler struct{}
func (h *MyToolHandler) ListTools(ctx context.Context) (*protocol.ListToolsResponse, error) {
return &protocol.ListToolsResponse{
Tools: []protocol.Tool{
{
Name: "my-tool",
Description: "我的工具描述",
InputSchema: json.RawMessage(`{
"type": "object",
"properties": {
"param1": {
"type": "string",
"description": "参数1的描述"
}
},
"required": ["param1"]
}`),
},
},
}, nil
}
func (h *MyToolHandler) CallTool(ctx context.Context, req *protocol.CallToolRequest) (*protocol.CallToolResponse, error) {
// 处理工具执行
result := "工具执行结果"
return &protocol.CallToolResponse{
Content: []protocol.ToolContent{
{
Type: "text",
Text: result,
},
},
}, nil
}
该框架支持三种类型的处理器:
用于实现LLMs可以执行的工具:
type ToolHandler interface {
ListTools(ctx context.Context) (*ListToolsResponse, error)
CallTool(ctx context.Context, req *CallToolRequest) (*CallToolResponse, error)
}
用于暴露LLMs可以读取的数据:
type ResourceHandler interface {
ListResources(ctx context.Context) (*ListResourcesResponse, error)
ReadResource(ctx context.Context, req *ReadResourceRequest) (*ReadResourceResponse, error)
}
用于提供提示模板:
type PromptHandler interface {
ListPrompts(ctx context.Context) (*ListPromptsResponse, error)
GetPrompt(ctx context.Context, req *GetPromptRequest) (*GetPromptResponse, error)
}
这里是一个更完整的示例,展示了所有处理器类型:
package main
import (
"context"
"encoding/json"
"log"
"github.com/gomcpgo/mcp/pkg/handler"
"github.com/gomcpgo/mcp/pkg/server"
"github.com/gomcpgo/mcp/pkg/protocol"
)
type MyServer struct {
handler.ToolHandler
handler.ResourceHandler
handler.PromptHandler
}
func (s *MyServer) ListTools(ctx context.Context) (*protocol.ListToolsResponse, error) {
return &protocol.ListToolsResponse{
Tools: []protocol.Tool{
{
Name: "my-tool",
Description: "我的工具描述",
InputSchema: json.RawMessage(`{
"type": "object",
"properties": {
"param1": {"type": "string"}
}
}`),
},
},
}, nil
}
func (s *MyServer) CallTool(ctx context.Context, req *protocol.CallToolRequest) (*protocol.CallCallToolResponse, error) {
return &protocol.CallToolResponse{
Content: []protocol.ToolContent{
{
Type: "text",
Text: "工具结果",
},
},
}, nil
}
func (s *MyServer) ListResources(ctx context.Context) (*protocol.ListResourcesResponse, error) {
return &protocol.ListResourcesResponse{
Resources: []protocol.Resource{
{
URI: "file:///example.txt",
Name: "示例文件",
Description: "一个示例资源",
MimeType: "text/plain",
},
},
}, nil
}
func (s *MyServer) ReadResource(ctx context.Context, req *protocol.ReadResourceRequest) (*protocol.ReadResourceResponse, error) {
return &protocol.ReadResourceResponse{
Contents: []protocol.ResourceContent{
{
URI: req.URI,
Text: "资源内容",
MimeType: "text/plain",
},
},
}, nil
}
func (s *MyServer) ListPrompts(ctx context.Context) (*protocol.ListPromptsResponse, error) {
return &protocol.ListPromptsResponse{
Prompts: []protocol.Prompt{
{
Name: "my-prompt",
Description: "一个提示模板",
Arguments: []protocol.PromptArgument{
{
Name: "arg1",
Description: "第一个参数",
Required: true,
},
},
},
},
}, nil
}
func (s *MyServer) GetPrompt(ctx context.Context, req *protocol.GetPromptRequest) (*protocol.GetPromptResponse, error) {
return &protocol.GetPromptResponse{
Messages: []protocol.Message{
{
Role: "user",
Content: protocol.MessageContent{
Type: "text",
Text: "提示内容",
},
},
},
}, nil
}
func main() {
myServer := &MyServer{}
registry := handler.NewHandlerRegistry()
registry.RegisterToolHandler(myServer)
registry.RegisterResourceHandler(myServer)
registry.RegisterPromptHandler(myServer)
srv := server.New(server.Options{
Name: "complete-server",
Version: "1.0.
0",
Registry: registry,
})
if err := srv.Run(); err != nil {
log.Fatal(err)
}
}
要将你的MCP服务器与Claude Desktop一起使用,请将其添加到claude_desktop_config.json中:
{
"mcpServers": {
"my-server": {
"command": "path/to/your/server",
"args": ["arg1", "arg2"]
}
}
}
配置文件的位置:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json该框架包括用于测试你的MCP服务器的实用程序。这里是一个示例:
func TestMyServer(t *testing.T) {
myServer := &MyServer{}
registry := handler.NewHandlerRegistry()
registry.RegisterToolHandler(myServer)
// 测试工具列表
tools, err := myServer.ListTools(context.Background())
if err != nil {
t.Fatalf("ListTools失败:%v", err)
}
// 测试工具执行
resp, err := myServer.CallTool(context.Background(), &protocol.CallToolRequest{
Name: "my-tool",
Arguments: map[string]interface{}{
"param1": "test",
},
})
if err != nil {
t.Fatalf("CallTool失败:%v", err)
}
}
错误处理
配置
安全性
测试