天气数据MCP服务器

一个基于Spring AI的天气数据模型上下文协议(MCP)服务器,提供天气预测和警报功能。

  • by suraj0223
  • Java
  • 本地部署
  • 更新于
在 GitHub 查看 suraj0223/weather-mcp

使用说明

气象MCP服务器

使用Spring AI的天气数据模型上下文协议(MCP)服务器。

🚀 快速开始

# Build the JAR file first
./mvnw clean package

# Start the MCP server
docker-compose up

# The server will be available at:
# - HTTP: http://localhost:8080
# - MCP Endpoint: http://localhost:8080/sse
# - Health: http://localhost:8080/actuator/health

🔌 光标集成

添加到您的 ~/.cursor/mcp.json:

{
  "mcpServers": {
    "weather": {
      "command": "<ABSOLUTE_PATH_OF_REPO_ON_LOCAL_MACHINE>/weather/start-mcp-server.sh"
    }
  }
}

🛠️ 可用工具

  • getWeatherForecastByLocation(latitude, longitude) -获取天气预报
  • getAlerts(state) -获取美国各州的天气警报

🐳 Docker命令

# Build the JAR (required before running docker-compose)
./mvnw clean package

# Start in background
docker-compose up -d

# View logs
docker-compose logs -f

# Stop
docker-compose down

# Rebuild and start
./mvnw clean package && docker-compose up --build

🧑‍💻 发展

先决条件

  • Java 21(例如OpenJDK 21或Temurin 21)
  • Maven 3.9+
  • Docker&Docker编写

本地建设

# Compile & run unit tests
./mvnw clean verify

# Build the runnable JAR (output in target/)
./mvnw clean package

在本地运行

# Run with the JVM directly
java -jar target/weather-0.0.1-SNAPSHOT.jar

# Or use Spring Boot devtools for hot-reload
./mvnw spring-boot:run

# Or run with Docker Compose
./mvnw clean package && docker-compose up

服务器启动于 http://localhost:8080 默认情况下。

环境变量

名称 默认值 描述
SERVER_PORT 8080 要绑定的HTTP端口
SPRING_AI_MCP_SERVER_STDIO false 通过stdio运行MCP服务器(对编辑器集成很有用)
SPRING_PROFILES_ACTIVE (无) 以逗号分隔的Spring配置文件列表

变量可以在中定义 src/main/resources/application.yml,通过以下方式覆盖 -D 标志,或导出到您的shell中。


📡 api参考

这个项目展示了API的两种风格:

  1. MCP工具端点 –用于支持IDE(例如Cursor)。
  2. REST端点 –可以调用的普通HTTP端点 curl邮递员等。

MCP工具

工具 签名 描述
getWeatherForecastByLocation (latitude: number, longitude: number) 返回所提供坐标的人类可读多日预报。
getAlerts (state: string) 返回所提供的美国州的活动警报(两个字母的代码)。

REST端点

方法 路径 示例
得到 /forecast?lat={lat}&lon={lon} /forecast?lat=37.7749&lon=-122.4194
得到 /alerts/{state} /alerts/CA

注意:REST层只是代理支持MCP工具的相同服务方法。