使用说明
MCP工具服务器
一个用Go编写的模型上下文协议(MCP)服务器,它提供了三个有用的工具:求和数字、乘法数字和计算句子中的字母。
特性
- 总数:将多个数字相加
- 将数字相乘:将多个数字相乘
- 数字母:计算句子中的字母数
快速开始
先决条件
- 达到1.21或更高
- Git(可选,用于克隆)
安装与运行
-
安装依赖项:
go mod tidy -
运行服务器:
go run .默认情况下,服务器将在端口8084上启动。您应该看到:
MCP Server starting on port 8084 Health check available at: http://localhost:8084/health MCP endpoint available at: http://localhost:8084/mcp Available tools: sum_numbers, multiply_numbers, count_letters -
健康检查: 访问http://localhost:8084/health验证服务器是否正在运行。
API使用
服务器实现MCP(模型上下文协议)规范。所有请求应以POST方式发送至 /mcp JSON-RPC 2.0格式。
初始化连接
curl -X POST http://localhost:8084/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {},
"clientInfo": {"name": "test-client", "version": "1.0.0"}
}
}'
列出可用工具
curl -X POST http://localhost:8084/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list",
"params": {}
}'
工具示例
总数
curl -X POST http://localhost:8084/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "sum_numbers",
"arguments": {
"numbers": [1, 2, 3, 4, 5]
}
}
}'
将数字相乘
curl -X POST http://localhost:8084/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 4,
"method": "tools/call",
"params": {
"name": "multiply_numbers",
"arguments": {
"numbers": [2, 3, 4]
}
}
}'
数字母
curl -X POST http://localhost:8084/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 5,
"method": "tools/call",
"params": {
"name": "count_letters",
"arguments": {
"sentence": "Hello, World! 123"
}
}
}'
配置
环境变量
PORT:服务器端口(默认值:8084)
例子:
PORT=9000 go run .
项目结构
mcp-server/
├── go.mod # Go module file
├── main.go # Server entry point
├── server.go # MCP server implementation
├── tools.go # Tool implementations
├── types.go # MCP protocol types
└── README.md # This file
发展
建筑
go build -o mcp-server
./mcp-server
测试工具
使用上面的curl示例或任何支持JSON-RPC 2.0的HTTP客户端。
MCP协议支持
此服务器实现了模型上下文协议(MCP)规范,支持:
initialize-初始化MCP连接tools/list-列出可用工具tools/call-使用参数执行工具
错误处理
服务器为以下对象提供详细的错误消息:
- JSON-RPC请求无效
- 参数缺失或无效
- 工具执行错误
- 未知的方法或工具
许可证
这个项目是开源的,可以在MIT许可证下使用。