一个简单的模型上下文协议(MCP)服务器,向AI代理(如GitHub Copilot)提供实时天气数据。
克隆仓库:
git clone https://github.com/debs-obrien/mcp-weather-server-demo.git
cd mcp-weather-server-demo
npm install
使用MCP Inspector测试:
npx -y @modelcontextprotocol/inspector npx -y tsx main.ts
.vscode文件夹中的mcp.json文件询问GitHub Copilot:
该服务器提供了一个get-weather工具,它:
// 创建带有天气工具的MCP服务器
const server = new McpServer({
name: "Weather Server",
version: "1.0.0"
});
// 定义get-weather工具
server.tool('get-weather', '获取城市天气的工具', ...);
// 通过stdio传输连接
const transport = new StdioServerTransport();
server.connect(transport);
@modelcontextprotocol/sdk - MCP服务器框架zod - 模式验证