使用说明
MCP服务器模板(TypeScript)
使用TypeScript和Express.js的模型上下文协议(MCP)服务器模板实现。
概述
此服务器为实现MCP工具和集成提供了基础。它目前包括一个简单的回声工具作为占位符,可以通过其他功能进行扩展。
特性
- MCP协议合规性:全面实施模型上下文协议规范
- 可流式HTTP传输:具有会话管理的现代基于HTTP的传输
- Express.js集成:基于Express.js构建,用于熟悉的web服务器模式
- TypeScript:完全支持TypeScript,具有类型安全性
- 占位符回声工具:用于测试和演示的简单工具
安装
# Install dependencies
pnpm install
# Build the project
pnpm build
用法
启动服务器
# Start the server
node dist/server.js
服务器将在端口3000(或 PORT 环境变量)。
服务器端点
- POST/mcp:客户端请求的主MCP协议端点
- GET/mcp:通过服务器发送事件(SSE)发送服务器到客户端的通知
- 删除/mcp:会话终止端点
测试服务器
您可以使用curl命令测试服务器:
- 初始化会话:
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test-client", "version": "1.0.0"}}}' \
-v
会话ID将在 mcp-session-id 响应标头。寻找一条如下的线:
< mcp-session-id: fa178930-3286-443c-ac8e-395b63cf649f
- 列出可用工具:
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "mcp-session-id: YOUR_SESSION_ID" \
-d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'
或者使用这一行初始化并立即列出工具:
SESSION_ID=$(curl -s -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test-client", "version": "1.0.0"}}}' \
-D - | grep -i "mcp-session-id:" | cut -d' ' -f2 | tr -d '\r') && \
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "mcp-session-id: $SESSION_ID" \
-d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list"}'
- 调用回声工具:
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "mcp-session-id: YOUR_SESSION_ID" \
-d '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"message": "Hello!"}}}'
或者使用这一行初始化并立即调用echo工具:
SESSION_ID=$(curl -s -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test-client", "version": "1.0.0"}}}' \
-D - | grep -i "mcp-session-id:" | cut -d' ' -f2 | tr -d '\r') && \
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-H "mcp-session-id: $SESSION_ID" \
-d '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "echo", "arguments": {"message": "Hello!"}}}'
发展
可用脚本
pnpm build:将TypeScript编译为JavaScriptpnpm lint:在源文件上运行ESLintpnpm format:使用Prettier格式化所有文件pnpm test:运行Jest测试pnpm test:watch:在监视模式下运行Jest
项目结构
src/
├── server.ts # Main server implementation
└── __tests__/ # Test files
└── example.test.ts
dist/ # Compiled JavaScript output
添加新工具
要向服务器添加新工具,请修改 createMcpServer() 功能在 src/server.ts:
// Add a new tool
server.tool(
'tool-name',
'Tool description',
{
/* schema definition if needed */
},
async (args: any) => ({
content: [
{
type: 'text',
text: `Result: ${args.someParameter}`,
},
],
}),
);
MCP协议特性
此服务器支持以下MCP功能:
- 工具:执行函数并返回结果
- 会话管理:具有会话ID的有状态连接
- 流式HTTP:使用SSE进行通知的现代HTTP传输
后续步骤
此服务器为构建更复杂的MCP集成提供了基础。考虑添加:
- 具有适当输入验证的更复杂的工具
- 用于公开数据的资源端点
- LLM交互的提示模板
- 与外部API或服务集成
与Cline一起使用(VSCode扩展)
将此MCP服务器与 Cline VSCode扩展,将以下配置添加到VSCode设置JSON中:
{
"mcpServers": {
"localhost": {
"url": "http://localhost:3000/mcp",
"disabled": false,
"type": "streamableHttp",
"autoApprove": []
}
}
}
在将Cline连接到MCP服务器之前,请确保它在端口3000上运行。