天气MCP服务器

一个基于Spring Boot的MCP服务器,提供流式HTTP传输和JSON-RPC 2.0协议支持,包含模拟天气服务功能。

  • by needy-nomad
  • Java
  • 本地部署
  • 更新于
在 GitHub 查看 needy-nomad/streamable-http-mcp

使用说明

气象MCP服务器

基于Maven的Spring Boot应用程序实现了MCP(模型上下文协议)服务器,使用 可流式传输的HTTP 使用Spring WebFlux进行传输。

特性

  • 可流式HTTP传输:单人 /mcp 同时支持GET(SSE)和POST(JSON-RPC)的端点
  • JSON-RPC 2.0:完整的MCP协议实施
  • 天气工具:模拟天气服务返回城市天气数据
  • 响应式:使用Spring WebFlux(Netty)构建,用于异步/非阻塞操作
  • 会话管理:可选会话ID支持,通过 Mcp-Session-Id 头球

快速开始

构建

mvn clean package -DskipTests

跑

java -jar target/weather-mcp-server-0.0.1-SNAPSHOT.jar

服务器将于启动 http://localhost:8080

测试

# Initialize
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "initialize", "params": {}, "id": 1}'

# List tools
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "tools/list", "params": {}, "id": 2}'

# Call weather tool
curl -X POST http://localhost:8080/mcp \
  -H "Content-Type: application/json" \
  -d '{"jsonrpc": "2.0", "method": "tools/call", "params": {"name": "get_weather", "arguments": {"city": "Tokyo"}}, "id": 3}'

MCP配置

在您的MCP客户端(例如LM Studio、Claude Desktop)中使用此配置:

{
  "mcpServers": {
    "weather-server": {
      "url": "http://localhost:8080/mcp"
    }
  }
}

对于经过身份验证的访问:

{
  "mcpServers": {
    "weather-server": {
      "url": "http://localhost:8080/mcp",
      "headers": {
        "Authorization": "Bearer your-token-here"
      }
    }
  }
}

可用工具

get_weather

获取特定城市的天气信息。

输入架构:

{
  "type": "object",
  "properties": {
    "city": {
      "type": "string",
      "description": "The city to get weather for"
    }
  },
  "required": ["city"]
}

示例城市:

  • 伦敦→ 雨天,15°C
  • 纽约→ 阳光明媚,25°C
  • 东京→ 多云,20°C
  • 其他→ 阳光明媚,22°C

建筑

  • 框架:带有WebFlux的Spring Boot 3.4.0
  • 服务器:Netty(反应性)
  • 协议:MCP流式HTTP(2024-11-05)
  • 运输:单人 /mcp GET(SSE)和POST(JSON-RPC)的端点
  • 端口: 8080

重要说明

⚠️ 此服务器使用 WebFlux(Netty) 不是Spring MVC(Tomcat)。不要包括 spring-boot-starter-web 因为它与WebFlux冲突。

✅ 实施 可流式传输的HTTP 传输规范,它使用单端点而不是弃用的双端点SSE方法。