MCP计算器
具有计算器和问候功能的模型上下文协议(MCP)服务器的Go实现。此实现遵循官方MCP规范。
特性
- 完整的JSON-RPC 2.0实现
- MCP协议支持
- 工具注册和执行
- 资源处理
- 服务器能力协商
- 根据规范进行错误处理
项目结构
mcpcalculator/
├── mcp/
│ └── protocol/
│ ├── types.go # Protocol types and structures
│ ├── server.go # MCP server implementation
│ └── client.go # MCP client implementation
├── main.go # Example usage
├── go.mod # Go module file
└── README.md # Documentation需求
- 达到1.21或更高
安装
git clone https://github.com/akhidasTech/mcpcalculator.git
cd mcpcalculator
go mod download用法
启动服务器
go run main.go服务器将在端口8080上启动。
提出请求
工具和资源遵循JSON-RPC 2.0规范:
添加工具
curl -X POST http://localhost:8080 \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "add",
"params": {"a": 5, "b": 3}
}'问候资源
curl -X POST http://localhost:8080 \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "greeting",
"params": {"name": "John"}
}'响应格式
所有响应均遵循JSON-RPC 2.0格式:
{
"jsonrpc": "2.0",
"id": 1,
"result":
}或者对于错误:
{
"jsonrpc": "2.0",
"id": 1,
"error": {
"code": ,
"message": "error message",
"data":
}
}协议实现
此实现遵循模型上下文协议规范:
- JSON-RPC 2.0:所有通信都使用JSON-RPC 2.0协议
- 能力:服务器在初始化期间通告其功能
- 工具:实现工具注册和执行协议
- 资源:实现资源访问协议
- 错误处理:使用标准错误代码和格式
安全
此实现遵循MCP安全指南:
- 使用者同意:工具和资源需要显式调用
- 数据隐私:未经明确请求,不得共享任何数据
- 工具安全:工具执行受到控制和验证
- 错误处理:正确的错误报告和处理
贡献
欢迎投稿!请随时提交拉取请求。
