一个使用TypeScript构建的简单Model Context Protocol (MCP)服务器实现。该服务器演示了基本的MCP功能,包括资源、提示和工具。
npm install
npm run build
npm run start:http
这将在http://localhost:3000启动服务器,并提供以下端点:
/sse/messagesnpm run start
这将以stdio模式运行服务器,适用于命令行集成。
启动HTTP服务器:
npm run start:http
在终端窗口中连接到SSE端点:
curl -N http://localhost:3000/sse
应该看到类似以下响应:
event: endpoint
data: /messages?sessionId=YOUR_SESSION_ID
在另一个终端窗口中发送请求以调用回显工具:
curl -X POST \
"http://localhost:3000/messages?sessionId=YOUR_SESSION_ID" \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/invoke",
"params": {
"name": "echo",
"parameters": {
"message": "Testing the MCP server!"
}
}
}'
应该在SSE终端窗口中看到如下响应:
event: message
data: {"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"Hello Testing the MCP server!"}]}}
尝试调试工具查看可用的服务器方法:
curl -X POST \
"http://localhost:3000/messages?sessionId=YOUR_SESSION_ID" \
-H 'Content-Type: application/json' \
-d '{
"jsonrpc": "2.0",
"id": 2,
"method": "tools/invoke",
"params": {
"name": "debug",
"parameters": {}
}
}'
对于可视化界面,可以使用MCP Inspector工具:


hello://world - 一个静态的“Hello World”资源greeting://{name} - 带有名字参数的动态问候echo - 回显带有“Hello”前缀的消息debug - 列出所有可用的工具和方法helpful-assistant - 一个基本的帮助助手提示MIT </中文翻译>