Go MCP SDK
这是一个Go SDK,用于构建符合模型上下文协议(MCP)的服务器。它提供了创建MCP服务器、注册工具和处理请求的基本构建块。
免责声明
此SDK目前正在积极开发中,应被视为正在进行中的工作。API可能会更改,某些功能可能无法完全实现。请谨慎使用,并随时为其发展做出贡献。
特性
- MCP服务器:可以处理MCP请求的服务器。
- 工具注册:注册工具及其处理程序的简单方法。
- JSON模式生成:自动生成用于工具输入的JSON模式。
- 结构化日志记录:结构化日志记录,便于调试。
入门指南
本教程将指导您完成使用Go MCP SDK创建简单计算器服务器的过程。
先决条件
- 在您的系统上安装1.21或更高版本。
使用SDK
由于此SDK尚未发布到公共存储库,因此您需要将其用作本地模块。为此,您可以使用 replace 指令在你 go.mod 文件指向您的本地副本 go-mcp-sdk 存储库。
例如:
replace go-mcp-sdk => /path/to/your/local/go-mcp-sdk创建服务器
以下是创建新MCP服务器的步骤:
1.初始化服务器
首先,创建一个新的服务器实例,包括其名称、版本和功能。
package main
import (
"context"
"fmt"
"log"
"go-mcp-sdk/pkg/mcp"
"go-mcp-sdk/pkg/protocol"
)
func main() {
server := mcp.NewServer("GoCalculatorServer", "1.0.0", protocol.ServerCapabilities{
Tools: &protocol.ServerToolCapabilities{},
})
// ...
}2.定义刀具参数结构
对于每个工具,定义一个表示其输入参数的结构。SDK将根据这些结构自动生成JSON模式。
// AddParams defines the input for our "add" tool.
// The `description` tag is used to generate the schema description for each property.
type AddParams struct {
A float64 `json:"a" description:"The first number to add."`
B float64 `json:"b" description:"The second number to add."`
}
// SubtractParams defines the input for our "subtract" tool.
type SubtractParams struct {
A float64 `json:"a" description:"The number to subtract from (minuend)."`
B float64 `json:"b" description:"The number to subtract (subtrahend)."`
}3.定义和注册工具
接下来,定义您的工具及其处理程序。处理程序是一个强类型函数,它接受上下文和指向参数结构的指针。
toolsToRegister := []mcp.ToolRegistration{
{
Definition: protocol.Tool{
Name: "calculator/add",
Title: "Add Numbers",
Description: "Calculates the sum of two numbers, a and b.",
},
Handler: func(ctx context.Context, params *AddParams) (string, error) {
result := params.A + params.B
return fmt.Sprintf("The sum of %f and %f is %f.", params.A, params.B, result), nil
},
},
{
Definition: protocol.Tool{
Name: "calculator/subtract",
Title: "Subtract Numbers",
Description: "Calculates the difference between two numbers, a - b.",
},
Handler: func(ctx context.Context, params *SubtractParams) (string, error) {
result := params.A - params.B
return fmt.Sprintf("The difference of %f minus %f is %f.", params.A, params.B, result), nil
},
},
}
if err := server.RegisterTools(toolsToRegister); err != nil {
log.Fatalf("Failed to register tools: %v", err)
}4.启动服务器
最后,启动服务器并监听连接。
log.Println("Starting calculator server on :8080")
if err := server.ListenAndServe(":8080"); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}完整示例: calculator-server
以下是位于中的示例服务器的完整代码 examples/calculator-server/main.go:
package main
import (
"context"
"fmt"
"log"
"go-mcp-sdk/pkg/mcp"
"go-mcp-sdk/pkg/protocol"
)
// AddParams defines the input for our "add" tool.
type AddParams struct {
A float64 `json:"a" description:"The first number to add."`
B float64 `json:"b" description:"The second number to add."`
}
// SubtractParams defines the input for our "subtract" tool.
type SubtractParams struct {
A float64 `json:"a" description:"The number to subtract from (minuend)."`
B float64 `json:"b" description:"The number to subtract (subtrahend)."`
}
func main() {
server := mcp.NewServer("GoCalculatorServer", "1.0.0", protocol.ServerCapabilities{
Tools: &protocol.ServerToolCapabilities{},
})
toolsToRegister := []mcp.ToolRegistration{
{
Definition: protocol.Tool{
Name: "calculator/add",
Title: "Add Numbers",
Description: "Calculates the sum of two numbers, a and b.",
},
Handler: func(ctx context.Context, params *AddParams) (string, error) {
result := params.A + params.B
return fmt.Sprintf("The sum of %f and %f is %f.", params.A, params.B, result), nil
},
},
{
Definition: protocol.Tool{
Name: "calculator/subtract",
Title: "Subtract Numbers",
Description: "Calculates the difference between two numbers, a - b.",
},
Handler: func(ctx context.Context, params *SubtractParams) (string, error) {
result := params.A - params.B
return fmt.Sprintf("The difference of %f minus %f is %f.", params.A, params.B, result), nil
},
},
}
if err := server.RegisterTools(toolsToRegister); err != nil {
log.Fatalf("Failed to register tools: %v", err)
}
log.Println("Starting calculator server on :8080")
if err := server.ListenAndServe(":8080"); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}贡献
欢迎投稿!请随时打开问题或提交拉取请求。
