TheresaQAQ/trae-weather-mcp-server
If you are the rightful owner of trae-weather-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
This project is a Model Context Protocol (MCP) based weather query system, consisting of a server and a client. The server provides city code queries and weather forecast functionalities, while the client implements the MCP protocol to connect to the server and interact using large models.
天气查询 MCP 服务器和客户端
这是一个基于 Model Context Protocol (MCP) 的天气查询项目,包含服务器和客户端两部分。服务器提供城市编码查询和天气预报功能,客户端实现了MCP协议,可以连接到服务器并使用大模型进行交互。
功能特性
- 获取所有城市编码列表:提供中国所有城市及其对应的行政区划代码
- 根据城市编码查询天气预报:调用高德天气 API 获取实时天气和预报信息
技术栈
- TypeScript
- Node.js
- MCP TypeScript SDK
- Axios
- Zod
- Dotenv
快速开始
1. 安装依赖
npm install
2. 配置环境变量
复制 .env.example
文件并重命名为 .env
,然后填入有效的高德地图 API 密钥:
# 高德地图API密钥
AMAP_API_KEY=your-amap-api-key
# 服务器端口(可选)
PORT=3000
3. 开发模式运行
npm run dev
4. 构建生产版本
npm run build
5. 生产模式运行
npm start
MCP 工具接口
1. getAllCityAdcodes
描述:获取所有城市及其编码的列表
参数:无
返回:城市编码列表,每个元素包含 city
(城市名称)和 adcode
(行政区划代码)
2. getWeatherByAdcode
描述:根据城市编码查询天气预报
参数:
adcode
(string): 城市编码,例如:110000(北京市)extensions
(string, 可选): 返回结果控制,base
返回实况天气,all
返回预报天气
返回:高德天气 API 返回的数据结构
注意事项
- 请确保您已获得有效的高德地图 API 密钥,否则天气查询功能将无法使用
- 高德天气 API 有调用次数限制,请合理使用
- 本项目使用的城市编码数据来源于
city_adcode.json
文件
MCP Client 使用指南
功能特性
- MCP协议兼容:完全遵循MCP协议规范
- 多模型支持:使用OpenAI SDK,支持OpenAI和阿里通义千问等大模型
- 交互式界面:提供命令行交互式界面,方便用户操作
- 配置灵活:通过配置文件管理MCP服务器连接
环境配置
- 复制
.env.client.example
文件并重命名为.env
,然后填入有效的API密钥:
# OpenAI API 配置
OPENAI_API_KEY=your-openai-api-key
OPENAI_BASE_URL=https://api.openai.com/v1
# 阿里通义千问 API 配置(二选一)
QWEN_API_KEY=your-qwen-api-key
QWEN_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1
# 默认模型设置
DEFAULT_MODEL=gpt-4o
- 修改
mcp-servers.json
文件,配置MCP服务器连接信息:
{
"mcpServers": {
"weather-mcp-server": {
"command": "node",
"args": ["dist/index.js"],
"env": {
"AMAP_API_KEY": "your-amap-api-key"
}
}
}
}
安装依赖
npm install
构建客户端
npm run build:client
运行客户端
开发模式运行:
npm run dev:client
生产模式运行:
npm run build
npm run start:client
使用流程
- 运行客户端后,首先会提示输入MCP服务器配置文件路径(默认:
./mcp-servers.json
) - 选择要连接的MCP服务器
- 选择要使用的大模型
- 输入您的查询内容,客户端会处理并返回结果
- 输入
exit
或quit
退出程序
License
MIT