JSON-RPC工具调用

一个基于JSON-RPC 2.0协议的工具调用服务,支持初始化、工具列表查询和工具调用功能。

  • by pcingola
  • Jupyter Notebook
  • 本地部署
  • 更新于
在 GitHub 查看 pcingola/mcp-server-demo

使用说明

MCP示例和演示

示例:在STDIN上运行

运行服务器

mcp run server.py

1.初始化请求

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "2025-03-26",
    "capabilities": {
      "roots": {
        "listChanged": true
      },
      "sampling": {}
    },
    "clientInfo": {
      "name": "ExampleClient",
      "version": "1.0.0"
    }
  }
}

注意:它必须在一行中!

{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-03-26", "capabilities": {"roots": {"listChanged": true}, "sampling": {}}, "clientInfo": {"name": "ExampleClient", "version": "1.0.0"}}}

2.初始化正常

{"jsonrpc": "2.0", "method": "notifications/initialized" }

3.平

{"jsonrpc": "2.0", "id":"123", "method":"ping"}

4.列出工具

{"jsonrpc" :"2.0", "id" :1, "method" :"tools/list"}

5.调用工具: Add

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "add",
    "arguments": {
      "a": 40,
      "b": 2
    }
  }
}
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 40 ,"b": 2}}}

6.调用工具: Add,出现错误

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "add",
    "arguments": {
      "a": 40,
      "z": "2"
    }
  }
}
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 40 ,"z": "2"}}}

示例2:使用MCP检查器

在“dev”模式下运行:

 mcp dev server.py