使用说明
Hello World MCP服务器
一个学习项目 模型上下文协议 使用TypeScript SDK(@modelcontextprotocol/sdk v1.27.x)。演示两种运输模式 hello_world 工具。
设置
npm install
运输方式
stdio--本地/进程生成
客户端将服务器作为子进程生成,并通过stdin/stdout进行通信。最适合本地开发和桌面应用程序集成(例如Kiro、Claude desktop)。
npm start
要将其连接到Kiro,请添加 .kiro/settings/mcp.json:
{
"mcpServers": {
"hello-world": {
"command": "node",
"args": ["<absolute-path>/src/mcp-stdio.js"],
"disabled": false
}
}
}
可流式HTTP--远程/托管
通过HTTP公开MCP协议并具有会话管理的Express服务器。最适合共享或云部署。
npm run start:hosted
收听 http://localhost:3000/mcp (用覆盖 PORT 有人)。
终点:
POST /mcp--初始化会话+发送请求GET /mcp--服务器→客户端SSE流(现有会话)DELETE /mcp--结束会话GET /healthz--健康检查
测试客户端
一个连接到托管服务器、列出工具和调用的小脚本 hello_world:
# start the hosted server first, then in another terminal:
node test-streamable-http.js
项目结构
src/
mcp-stdio.js # stdio transport (McpServer)
mcp-streamable-http.js # Streamable HTTP transport (McpServer + Express)
test-streamable-http.js # test client for the hosted server
SDK参考
两台服务器都使用高级 McpServer 来自的API @modelcontextprotocol/sdk/server/mcp.js.关键概念:
server.registerTool(name, config, handler)--注册工具(功能自动声明)server.registerResource(name, uri, config, handler)--公开只读数据server.registerPrompt(name, config, handler)--可重用提示模板server.connect(transport)--绑定到运输工具并开始服务
文件:https://github.com/modelcontextprotocol/typescript-sdk/tree/v1.x