使用说明
简易MCP服务器
使用流式HTTP传输的MCP服务器实现(协议版本2025-11-25)。
特性
- 协议:MCP流式HTTP(2025-11-25)
- 运输:HTTP发布/获取/删除
- 工具:
hello-world,add_two_numbers(加上副驾驶别名add-two-numbers,welcome,greeting)add_two_numbers/add-two-numbers:接受{ "prompt": string },提取前两个数字,返回总和
- 监控:健康和信息端点
设置
-
安装依赖项
npm install -
构建
npm run build -
启动服务器
npm start服务器运行在
http://127.0.0.1:8080默认情况下。
API终点
- POST/mcp:JSON-RPC请求(初始化、调用工具等)
- GET/mcp:服务器发送事件(SSE)流
- 删除/mcp:终止会话
- GET/健康:健康检查
- 获取/信息:服务器信息
工具(名称+参数)
这些工具契约是有意明确的,因此MCP客户端(包括Copilot插件)可以直接调用工具,而不依赖于即席解析。
add_two_numbers (首选)/ add-two-numbers (别名)
- 目的:将从中提取的两个数字相加 完整用户提示.
- 输入:
{ "prompt": string }
- 行为:
- 提取 前两个数字 从
prompt(支持负数和小数)。 - 如果存在少于2个数字,则出错。
- 提取 前两个数字 从
客户指导:
- 如果用户消息包含至少两个数字并要求添加/sum/plus/total,请调用此工具。
- 将完整的用户消息逐字传递为
prompt(例如:please add following number 10 and 7). - 输出:
structuredContent:{ "sum": number }content:包含总和的文本
JSON-RPC示例 tools/call:
{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/call",
"params": {
"name": "add_two_numbers",
"arguments": { "prompt": "please add 10 and 20" }
}
}
hello-world
- 目的:返回来自的欢迎信息
resources/hello-world/welcome.md. - 别名:
welcome,greeting(行为与hello-world) - 输入:
{}(无参数)
- 输出:
structuredContent:{ "message": string, "mcpSdkVersion": string, "clientName": string, "clientVersion": string }content:包含欢迎消息及其版本的文本
JSON-RPC示例 tools/call:
{
"jsonrpc": "2.0",
"id": 3,
"method": "tools/call",
"params": {
"name": "hello-world",
"arguments": {}
}
}
序列图
sequenceDiagram
participant Client
participant Server
Note over Client, Server: Initialization
Client->>Server: POST /mcp (method: initialize)
Server-->>Client: 200 OK (Mcp-Session-Id: uuid)
Note over Client, Server: Connection
Client->>Server: GET /mcp (Header: Mcp-Session-Id)
Server-->>Client: 200 OK (SSE Stream Open)
Note over Client, Server: Tool Execution
Client->>Server: POST /mcp (method: tools/call)
Server-->>Client: 202 Accepted
Server->>Client: SSE Event (jsonrpc response)
Note over Client, Server: Termination
Client->>Server: DELETE /mcp
Server-->>Client: 200 OK
测试
运行单元测试:
npm test
目录结构
src/core/:核心服务器逻辑和传输。src/tools/:工具实施。src/index.ts:入口点。
