天气服务

基于MCP协议的天气服务,提供天气预报和天气警报功能。

  • by harshjoeyit
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 harshjoeyit/mcp-server-weather

使用说明

MCP服务器原型

基于以下内容的快速原型- https://modelcontextprotocol.io/quickstart/server

MCP服务器日志

tail -n 20 -f ~/Library/Logs/Claude/mcp*.log

正在初始化服务器

2025-07-20T13:44:16.367Z [weather] [info] Initializing server... { metadata: undefined }
2025-07-20T13:44:16.378Z [weather] [info] Using MCP server command: /Users/harshitgangwar/.local/bin/uv with args and path: {
  metadata: {
    args: [
      '--directory',
      '/Users/harshitgangwar/Documents/MyProjects/prototypes/weather-for-mcp',
      'run',
      'weather.py',
      [length]: 4
    ],
    paths: [
      '/Users/harshitgangwar/.nvm/versions/node/v17.0.1/bin',
      '/Users/harshitgangwar/.nvm/versions/node/v18.18.2/bin',
      '/Users/harshitgangwar/.nvm/versions/node/v20.19.3/bin',
      '/Users/harshitgangwar/.nvm/versions/node/v21.6.2/bin',
      '/Users/harshitgangwar/.nvm/versions/node/v22.17.0/bin',
      '/usr/local/bin',
      '/opt/homebrew/bin',
      '/usr/bin',
      '/usr/bin',
      '/bin',
      '/usr/sbin',
      '/sbin',
      [length]: 12
    ]
  }
} %o

2025-07-20T13:44:16.380Z [weather] [info] Server started and connected successfully { metadata: undefined }

第一条消息(id=0)

2025-07-20T13:44:16.395Z [weather] [info] Message from client: {"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"claude-ai","version":"0.1.0"}},"jsonrpc":"2.0","id":0} { metadata: undefined }

2025-07-20T13:44:18.950Z [weather] [info] Message from server: {"jsonrpc":"2.0","id":0,"result":{"protocolVersion":"2025-06-18","capabilities":{"experimental":{},"prompts":{"listChanged":false},"resources":{"subscribe":false,"listChanged":false},"tools":{"listChanged":false}},"serverInfo":{"name":"weather","version":"1.12.0"}}} { metadata: undefined }

工具列表

2025-07-20T13:44:18.951Z [weather] [info] Message from client: {"method":"notifications/initialized","jsonrpc":"2.0"} { metadata: undefined }
2025-07-20T13:44:18.951Z [weather] [info] Message from client: {"method":"tools/list","params":{},"jsonrpc":"2.0","id":1} { metadata: undefined }
2025-07-20T13:44:18.952Z [weather] [info] Message from client: {"method":"tools/list","params":{},"jsonrpc":"2.0","id":2} { metadata: undefined }

[07/20/25 19:14:18] INFO     Processing request of type            server.py:625
ListToolsRequest                                   
INFO     Processing request of type            server.py:625
ListToolsRequest


2025-07-20T13:44:18.960Z [weather] [info] Message from server:  
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "tools": [
      {
        "name": "get_alerts",
        "description": "Get weather alerts for a US state.\n\nArgs:\n    state: Two-letter US state code (e.g. CA, NY)\n",
        "inputSchema": {
          "properties": { "state": { "title": "State", "type": "string" } },
          "required": ["state"],
          "title": "get_alertsArguments",
          "type": "object"
        },
        "outputSchema": {
          "properties": { "result": { "title": "Result", "type": "string" } },
          "required": ["result"],
          "title": "get_alertsOutput",
          "type": "object"
        }
      },
      {
        "name": "get_forecast",
        "description": "Get weather forecast for a location.\n\nArgs:\n    latitude: Latitude of the location\n    longitude: Longitude of the location\n",
        "inputSchema": {
          "properties": {
            "latitude": { "title": "Latitude", "type": "number" },
            "longitude": { "title": "Longitude", "type": "number" }
          },
          "required": ["latitude", "longitude"],
          "title": "get_forecastArguments",
          "type": "object"
        },
        "outputSchema": {
          "properties": { "result": { "title": "Result", "type": "string" } },
          "required": ["result"],
          "title": "get_forecastOutput",
          "type": "object"
        }
      }
    ]
  }
}
{ metadata: undefined }

资源列表

2025-07-20T13:44:18.952Z [weather] [info] Message from client: {"method":"resources/list","params":{},"jsonrpc":"2.0","id":3} { metadata: undefined }

INFO     Processing request of type            server.py:625
            ListResourcesRequest  
2025-07-20T13:44:18.962Z [weather] [info] Message from server: {"jsonrpc":"2.0","id":3,"result":{"resources":[]}} { metadata: undefined }

提示列表

2025-07-20T13:44:18.963Z [weather] [info] Message from client: {"method":"prompts/list","params":{},"jsonrpc":"2.0","id":4} { metadata: undefined }
INFO     Processing request of type            server.py:625
            ListPromptsRequest                                 
2025-07-20T13:44:18.964Z [weather] [info] Message from server: {"jsonrpc":"2.0","id":4,"result":{"prompts":[]}} { metadata: undefined }

每10分钟保持一次活力

2025-07-20T13:44:40.933Z [info] [weather] Message from client: {"method":"prompts/list","params":{},"jsonrpc":"2.0","id":8}
2025-07-20T13:44:40.936Z [info] [weather] Message from server: {"jsonrpc":"2.0","id":8,"result":{"prompts":[]}}
2025-07-20T13:54:40.902Z [info] [weather] Message from client: {"method":"resources/list","params":{},"jsonrpc":"2.0","id":9}
2025-07-20T13:54:40.921Z [info] [weather] Message from server: {"jsonrpc":"2.0","id":9,"result":{"resources":[]}}
2025-07-20T13:54:40.923Z [info] [weather] Message from client: {"method":"prompts/list","params":{},"jsonrpc":"2.0","id":10}
2025-07-20T13:54:40.925Z [info] [weather] Message from server: {"jsonrpc":"2.0","id":10,"result":{"prompts":[]}}
2025-07-20T14:04:40.884Z [info] [weather] Message from client: {"method":"resources/list","params":{},"jsonrpc":"2.0","id":11}
2025-07-20T14:04:40.920Z [info] [weather] Message from server: {"jsonrpc":"2.0","id":11,"result":{"resources":[]}}
2025-07-20T14:04:40.922Z [info] [weather] Message from client: {"method":"prompts/list","params":{},"jsonrpc":"2.0","id":12}
2025-07-20T14:04:40.924Z [info] [weather] Message from server: {"jsonrpc":"2.0","id":12,"result":{"prompts":[]}}

获得天气

==> /Users/harshitgangwar/Library/Logs/Claude/mcp.log <==
2025-07-20T14:09:08.507Z [info] [weather] Message from client: {"method":"tools/call","params":{"name":"get_forecast","arguments":{"latitude":38.5816,"longitude":-121.4944}},"jsonrpc":"2.0","id":13}


==> /Users/harshitgangwar/Library/Logs/Claude/mcp-server-weather.log <==
                    INFO     HTTP Request: GET                   _client.py:1740
                             https://api.weather.gov/points/38.5                
                             816,-121.4944 "HTTP/1.1 200 OK"                    
[07/20/25 19:39:09] INFO     HTTP Request: GET                   _client.py:1740
                             https://api.weather.gov/gridpoints/                
                             STO/41,68/forecast "HTTP/1.1 200                   
                             OK"  
2025-07-20T14:09:09.651Z [weather] [info] Message from server: {
{
  "jsonrpc": "2.0",
  "id": 13,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "\nToday:\nTemperature: 91°F\nWind: 5 to 10 mph SSW\nForecast: Sunny, with a high near 91. South southwest wind 5 to 10 mph, with gusts as high as 18 mph.\n\n---\n\nTonight:\nTemperature: 59°F\nWind: 10 mph S\nForecast: Mostly clear, with a low around 59. South wind around 10 mph, with gusts as high as 21 mph.\n\n---\n\nMonday:\nTemperature: 82°F\nWind: 9 to 13 mph S\nForecast: Sunny, with a high near 82. South wind 9 to 13 mph, with gusts as high as 22 mph.\n\n---\n\nMonday Night:\nTemperature: 58°F\nWind: 7 to 13 mph SSW\nForecast: Partly cloudy, with a low around 58. South southwest wind 7 to 13 mph, with gusts as high as 22 mph.\n\n---\n\nTuesday:\nTemperature: 80°F\nWind: 6 to 9 mph SSW\nForecast: Mostly sunny, with a high near 80. South southwest wind 6 to 9 mph.\n"
      }
    ],
    "structuredContent": {
      "result": "\nToday:\nTemperature: 91°F\nWind: 5 to 10 mph SSW\nForecast: Sunny, with a high near 91. South southwest wind 5 to 10 mph, with gusts as high as 18 mph.\n\n---\n\nTonight:\nTemperature: 59°F\nWind: 10 mph S\nForecast: Mostly clear, with a low around 59. South wind around 10 mph, with gusts as high as 21 mph.\n\n---\n\nMonday:\nTemperature: 82°F\nWind: 9 to 13 mph S\nForecast: Sunny, with a high near 82. South wind 9 to 13 mph, with gusts as high as 22 mph.\n\n---\n\nMonday Night:\nTemperature: 58°F\nWind: 7 to 13 mph SSW\nForecast: Partly cloudy, with a low around 58. South southwest wind 7 to 13 mph, with gusts as high as 22 mph.\n\n---\n\nTuesday:\nTemperature: 80°F\nWind: 6 to 9 mph SSW\nForecast: Mostly sunny, with a high near 80. South southwest wind 6 to 9 mph.\n"
    },
    "isError": false
  }
}

获取警报

==> /Users/harshitgangwar/Library/Logs/Claude/mcp-server-weather.log <==
2025-07-20T14:12:07.571Z [weather] [info] Message from client: {"method":"tools/call","params":{"name":"get_alerts","arguments":{"state":"TX"}},"jsonrpc":"2.0","id":18} { metadata: undefined }

==> /Users/harshitgangwar/Library/Logs/Claude/mcp-server-weather.log <==
[07/20/25 19:42:07] INFO     Processing request of type            server.py:625
                             CallToolRequest                                    
[07/20/25 19:42:08] INFO     HTTP Request: GET                   _client.py:1740
                             https://api.weather.gov/alerts/acti                
                             ve/area/TX "HTTP/1.1 200 OK"                       
2025-07-20T14:12:08.329Z [weather] [info] Message from server: {"jsonrpc":"2.0","id":18,"result":{"content":[{"type":"text","text":"\nEvent: Heat Advisory\nArea: Palo Duro Canyon\nSeverity: Moderate\nDescription: * WHAT...Heat index values up to 102 expected.\n\n* WHERE...Palo Duro Canyon County.\n\n* WHEN...From 1 PM this afternoon to 8 PM CDT this evening.\n\n* IMPACTS...Hot temperatures and high humidity may cause heat\nillnesses.\nInstructions: Drink plenty of fluids, stay in an air-conditioned room, stay out of\nthe sun, and check up on relatives and neighbors.\n\nTake extra precautions when outside. Wear lightweight and loose\nfitting clothing. Try to limit strenuous activities to early morning\nor evening. Take action when you see symptoms of heat exhaustion and\nheat stroke.\n\n---\n\nEvent: Heat Advisory\nArea: Harper; Woods; Alfalfa; Grant; Kay; Woodward; Major; Garfield; Noble; Blaine; Kingfisher; Logan; Payne; Caddo; Canadian; Oklahoma; Lincoln; Grady; McClain; Cleveland; Pottawatomie; Seminole; Hughes; Harmon; Greer; Kiowa; Jackson; Tillman; Comanche; Stephens; Garvin; Murray; Pontotoc; Coal; Cotton; Jefferson; Carter; Johnston;