此仓库是一个使用 Model Context Protocol (MCP) 提供天气信息的简单服务。可以获取多个城市(如福冈、东京、大阪、莫斯科、纽约等)的天气信息。
<a href="https://glama.ai/mcp/servers/@terisuke/my-weather-mcp"> <img width="380" height="200" src="https://gips2.baidu.com/it/u=535482579,4149246163&fm=3081&app=3081&f=PNG?w=760&h=400" alt="Weather Service MCP server" /> </a> ## 许可证本项目在 MIT许可证 下发布。
运行此项目需要以下软件:
node -v
npm -v
git --version
mkdir my-weather-mcp
cd my-weather-mcp
npm init -y
npm install @modelcontextprotocol/sdk axios zod typescript @types/node https-proxy-agent
npm install --save-dev ts-node
npx tsc --init
tsconfig.json 文件如下:{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"outDir": "./build",
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
}
package.json 文件中的脚本部分如下:"scripts": {
"build": "tsc && node -e \"import('fs').then(fs => fs.default.chmodSync('build/index.js', '755'))\"",
"start": "node build/index.js",
"inspect": "npx @modelcontextprotocol/inspector build/index.js",
"dev": "ts-node src/index.ts"
}
mkdir -p src test
如果要使用现有的项目,请按照以下步骤克隆:
git clone https://github.com/terisuke/my-weather-mcp.git
cd my-weather-mcp
npm install
要构建并运行项目,请使用以下命令:
npm run build && npm run start
要使用 MCP 检查器测试天气服务,请执行以下命令:
npm run build && npm run inspect
检查器启动后,可以在浏览器中访问 http://127.0.0.1:6274 并与天气服务交互。
使用检查器,可以获取以下城市的天气信息:
也可以指定其他城市,但必须是 Open-Meteo API 能识别的城市名称。
my-weather-mcp/
├── build/ # 编译后的 JavaScript 文件
├── src/ # TypeScript 源代码
│ └── index.ts # 主应用程序代码
├── test/ # 测试文件
├── package.json # 项目配置
├── tsconfig.json # TypeScript 配置
└── README.md # 此文件
src/index.ts 文件以添加或修改功能。npm run build && npm run inspect
my-weather-mcp),根据需要添加描述。git init
git add .
git commit -m "初始提交:MCP天气服务实现"
git remote add origin https://github.com/用户名/my-weather-mcp.git
git push -u origin main
git add .
git commit -m "更改内容说明"
git push
ERR_PACKAGE_PATH_NOT_EXPORTED 错误此错误发生在 @modelcontextprotocol/sdk 包的导入路径不正确时。请按以下方式修正:
// 错误的导入
import { McpServer } from "@modelcontextprotocol/sdk";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/transports";
// 正确的导入
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
如果在 API 调用过程中发生网络错误,请检查代理设置。通过设置环境变量 HTTP_PROXY 或 HTTPS_PROXY 可以使用代理:
export HTTP_PROXY=http://代理服务器:端口
export HTTPS_PROXY=https://代理服务器:端口
如果问题无法解决,请创建一个包含以下信息的问题: