气象MCP服务器(PHP)
模型上下文协议(MCP)服务器的PHP实现,该服务器使用Open-Meteo API(无需API密钥)提供天气信息。
需求
- PHP 8.1或更高版本
allow_url_fopen在php.ini中启用
安装
php composer.phar install测试
使用PHPUnit运行测试套件:
php phpunit.phar该项目包括对两个核心组件的测试:
- McpServerTest -测试MCP协议处理,包括工具注册、调用、JSON-RPC响应和错误处理
- 天气服务测试 -测试天气数据处理,包括罗盘方向转换、天气代码映射和API响应处理(使用模拟来避免HTTP请求)
用法
运行服务器
php server.php服务器使用JSON-RPC 2.0通过stdio进行通信。
Claude桌面配置
添加到您的 claude_desktop_config.json:
{
"mcpServers": {
"weather": {
"command": "php",
"args": ["/full/path/to/server.php"]
}
}
}Claude代码配置
该项目包括 .mcp.json 在中打开项目时自动配置天气MCP服务器的文件 克劳德代码。只需克隆存储库并使用Claude Code打开即可,天气工具将立即可用。
要全局添加此服务器(在所有项目中都可用),请使用Claude Code CLI:
claude mcp add weather php /full/path/to/server.php或者将其添加到您的用户级配置中 ~/.claude/settings.json:
{
"mcpServers": {
"weather": {
"command": "php",
"args": ["/full/path/to/server.php"]
}
}
}DevoxxGenie(IntelliJ IDEA)配置
使用此MCP服务器 DevoxxGenie 在IntelliJ IDEA或其他JetBrains IDE中:
- 打开DevoxxGenie设置(设置>工具>DevoxxGene)
- 导航到MCP服务器配置
- 使用以下设置添加新服务器:
| 设置 | 值 |
|---|---|
| 名字 | php-weather |
| 传输类型 | STDIO |
| 命令 | php |
| 参数 | /full/path/to/server.php |
或使用提供的 mcp-servers-config.json 文件:
{
"mcpServers": {
"php-weather": {
"command": "php",
"args": [
"/full/path/to/server.php"
],
"env": {}
}
}
}注: 替换 /full/path/to/server.php 实际绝对路径为 server.php 在您的系统上。
配置后,DevoxxGenie将发现 get_current_weather 该工具和AI助手可以使用它来获取天气信息。
可用工具
get_current_weather
获取某个位置的当前天气状况。
参数:
location(必填):城市名称或地点(例如“伦敦”、“纽约”、“东京”)
示例响应:
Current Weather for London, United Kingdom
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Conditions: Overcast
Temperature: 9.3°C (feels like 7.8°C)
Humidity: 88%
Wind: 6.0 km/h from SSE
Coordinates: 51.5085, -0.1257
Updated: 2026-01-19T10:00get_forecast
获取某个地点的天气预报。
参数:
location(必填):城市名称或地点days(可选):要预测的天数(1-16,默认值:5)
示例响应:
3-Day Forecast for Tokyo, Japan
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2026-01-19
Overcast
High: 11.1°C | Low: 2.2°C
Precipitation: 0% | Wind: 4.5 km/h
2026-01-20
Partly cloudy
High: 7.1°C | Low: 0.5°C
Precipitation: 0% | Wind: 13.3 km/h项目结构
├── .mcp.json # Claude Code MCP configuration (auto-loads server)
├── composer.json # Project dependencies
├── server.php # Main entry point
├── mcp-servers-config.json # MCP server configuration for clients
├── phpunit.xml # PHPUnit configuration
├── phpunit.phar # PHPUnit executable
├── src/
│ ├── McpServer.php # MCP protocol implementation
│ └── WeatherService.php # Weather API client
├── tests/
│ ├── McpServerTest.php # MCP server tests
│ └── WeatherServiceTest.php # Weather service tests
└── README.mdAPI来源
天气数据由 开放天气,一个不需要API密钥的免费天气API。
