计算器协议服务器

一个基于Go语言实现的Model Context Protocol (MCP)服务器,提供计算和问候功能,遵循JSON-RPC 2.0规范。

  • by akhidasTech
  • Go
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 akhidastech/mcpcalculator

使用说明

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": <result_value>
}

或者对于错误:

{
    "jsonrpc": "2.0",
    "id": 1,
    "error": {
        "code": <error_code>,
        "message": "error message",
        "data": <additional_data>
    }
}

协议实现

此实现遵循模型上下文协议规范:

  1. JSON-RPC 2.0:所有通信都使用JSON-RPC 2.0协议
  2. 能力:服务器在初始化期间通告其功能
  3. 工具:实现工具注册和执行协议
  4. 资源:实现资源访问协议
  5. 错误处理:使用标准错误代码和格式

安全

此实现遵循MCP安全指南:

  1. 使用者同意:工具和资源需要显式调用
  2. 数据隐私:未经明确请求,不得共享任何数据
  3. 工具安全:工具执行受到控制和验证
  4. 错误处理:正确的错误报告和处理

贡献

欢迎投稿!请随时提交拉取请求。