MCP Go —— MCP 的 Go 实现
Model Context Protocol (MCP) 的 Go 实现,实现 LLM 应用程序与外部数据源和工具之间的无缝集成。
package main import ( "context" "errors" "fmt" "github.com/mark3labs/mcp-go/mcp" "github.com/mark3labs/mcp-go/server" ) func main() { // Create MCP server s := server.NewMCPServer( "Demo 🚀", "1.0.0", ) // 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, ok := request.Params.Arguments["name"].(string) if !ok { return nil, errors.New("name must be a string") } return mcp.NewToolResultText(fmt.Sprintf("Hello, %s!", name)), nil }
MCP Go 处理所有复杂的协议细节和服务器管理,因此你可以专注于构建出色的工具。它旨在实现高级且易于使用。
主要特点:
- 快速:高级接口意味着更少的代码和更快的开发速度
- 简单:使用最少的样板构建 MCP 服务器
- 完整:MCP Go 旨在提供核心 MCP 规范的完整实现