基本数学MCP服务器
一个简单的模型上下文协议(MCP)服务器,提供基本的数学运算。此服务器演示了如何通过MCP协议创建和公开工具。
特性
此MCP服务器提供四种基本的数学运算:
- 添加:添加两个数字
- 减去:减去两个数字
- 乘:将两个数字相乘
- 分割:除以两个数字
先决条件
- Node.js
- npm
安装
npm install用法
服务器使用JSON-RPC 2.0协议通过stdin/stdout进行通信。下面是如何使用命令行工具与服务器交互的示例。
初始化服务器
在使用服务器之前,您应该初始化它以建立协议版本和功能:
echo '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test-client", "version": "1.0.0"}}}' | node mcp.js | jq示例响应:
{
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {
"listChanged": true
}
},
"serverInfo": {
"name": "basic-math-mcp",
"version": "1.0.0",
"description": "A basic math MCP server"
}
},
"jsonrpc": "2.0",
"id": 1
}列出可用工具
要查看所有可用工具及其模式:
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}' | node mcp.js | jq示例响应:
{
"result": {
"tools": [
{
"name": "add",
"title": "Addition tool",
"description": "Add two numbers",
"inputSchema": {
"type": "object",
"properties": {
"a": {
"type": "number"
},
"b": {
"type": "number"
}
},
"required": ["a", "b"],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
},
{
"name": "subtract",
"title": "Subtraction tool",
"description": "Subtract two numbers",
"inputSchema": {
"type": "object",
"properties": {
"a": {
"type": "number"
},
"b": {
"type": "number"
}
},
"required": ["a", "b"],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
},
{
"name": "multiply",
"title": "Multiplication tool",
"description": "Multiply two numbers",
"inputSchema": {
"type": "object",
"properties": {
"a": {
"type": "number"
},
"b": {
"type": "number"
}
},
"required": ["a", "b"],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
},
{
"name": "divide",
"title": "Division tool",
"description": "Divide two numbers",
"inputSchema": {
"type": "object",
"properties": {
"a": {
"type": "number"
},
"b": {
"type": "number"
}
},
"required": ["a", "b"],
"additionalProperties": false,
"$schema": "http://json-schema.org/draft-07/schema#"
}
}
]
},
"jsonrpc": "2.0",
"id": 1
}调用工具
要执行特定工具,请使用 tools/call 具有工具名称和参数的方法:
添加示例
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 10, "b": 2}}}' | node mcp.js | jq示例响应:
{
"result": {
"content": [
{
"type": "text",
"text": "12"
}
]
},
"jsonrpc": "2.0",
"id": 1
}减法示例
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "subtract", "arguments": {"a": 10, "b": 2}}}' | node mcp.js | jq乘法示例
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "multiply", "arguments": {"a": 10, "b": 2}}}' | node mcp.js | jq部门示例
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "divide", "arguments": {"a": 10, "b": 2}}}' | node mcp.js | jq技术细节
依赖项
@modelcontextprotocol/sdk:用于构建MCP服务器的MCP SDKzod:架构验证库
协议
此服务器使用以下方式实现模型上下文协议(MCP):
- 运输:stdio(标准输入/标准输出)
- 协议:JSON-RPC 2.0
- 协议版本: 2024-11-05
输入架构
所有数学运算都接受两个参数:
a(数字,必填):第一个操作数b(数字,必填):第二个操作数
输出格式
所有工具都以以下格式返回结果:
{
"result": {
"content": [
{
"type": "text",
"text": ""
}
]
},
"jsonrpc": "2.0",
"id":
}许可证
麻省理工学院
