MCP Weather Forecast Server

MCP Weather Forecast Server

Enables LLMs to retrieve weather forecasts for Israeli cities via browser automation and USA weather via API, maintaining state across tool calls for sequential operations.

Category
访问服务器

README

MCP Weather Forecast Server

A Model Context Protocol (MCP) server that enables LLMs to retrieve weather forecasts from Israeli websites using browser automation with Playwright. This project demonstrates how to build MCP tools that maintain state across sequential tool calls, allowing an LLM to control a browser interactively.

Project Overview

This project implements an MCP server using the official Anthropic MCP SDK in Python. It provides tools for:

  • Opening a browser and navigating to an Israeli weather forecast website
  • Entering city names into search fields
  • Selecting cities from autocomplete dropdowns
  • Extracting weather data from the loaded page

The key feature is the global state mechanism that persists browser and page instances across different tool calls, enabling sequential operations on the same browser page.

Architecture

Components

  • host.py: Main chat host that manages MCP clients and orchestrates tool calls with Anthropic's Claude API
  • weather_Israel.py: MCP server implementing browser automation tools for Israeli weather forecasts
  • weather_USA.py: MCP server implementing API-based weather tools for USA weather data
  • client.py: MCP client implementation for connecting to MCP servers via stdio transport

Global State Management

The weather_Israel.py module uses a BrowserState class to maintain global browser and page instances:

  • playwright: The Playwright async context
  • browser: The Chromium browser instance (headless=False for visibility)
  • page: The browser page for navigation and interaction

This state persists across tool calls, allowing sequential operations like:

  1. Open browser → 2. Enter city → 3. Select city → 4. Extract data

Prerequisites

  • Python 3.13 or higher
  • uv package manager
  • Anthropic API key (set in .env file)

Installation

  1. Clone the repository and navigate to the project directory:
cd project-template
  1. Install dependencies using uv:
uv sync
  1. Install Playwright Chromium browser:
uv run playwright install chromium
  1. Set up your environment variables:
# Edit .env and add your ANTHROPIC_API_KEY

Usage

Running the MCP Server

Start the interactive chat host:

uv run host.py

This will:

  • Connect to the configured MCP servers (weather_Israel.py and weather_USA.py)
  • Start an interactive chat loop
  • Allow you to ask questions that trigger tool calls

Example Questions

Once the host is running, you can ask questions like:

Israeli Weather:

  • "What is the weather in Jerusalem today?"
  • "Tell me the forecast for Tel Aviv"
  • "What's the current temperature in Haifa?"

USA Weather:

  • "Are there any weather alerts for California?"
  • "What's the forecast for New York City?"
  • "Get the weather forecast for latitude 37.7749, longitude -122.4194"

How It Works

When you ask a question, the system:

  1. Sends your query to Claude via the Anthropic API
  2. Claude determines which tools to call based on your question
  3. The host executes the tools in sequence (e.g., open browser → enter city → select → extract)
  4. Results are returned to Claude for final response generation
  5. The answer is displayed in the chat

MCP Tools

weather_Israel.py Tools

open_weather_forecast_israel()

Launches a Chromium browser (headless=False) and navigates to https://www.weather2day.co.il/forecast.

enter_weather_forecast_city_israel(city: str)

Locates the search input field and types the given city name into it.

  • Parameter: city - The name of the Israeli city (e.g., "Jerusalem", "Tel Aviv")

select_weather_forecast_city_israel()

Waits for the autocomplete dropdown to appear and clicks the first city in the list.

extract_weather_forecast_israel()

Extracts weather data from the loaded page, cleans up HTML and whitespace, and returns the text for LLM processing.

weather_USA.py Tools

get_alerts_in_USA(state: str)

Gets weather alerts for a USA state.

  • Parameter: state - Two-letter USA state code (e.g., CA, NY)

get_forecast_in_USA(latitude: float, longitude: float)

Gets weather forecast for a location in USA.

  • Parameters:
    • latitude - Latitude of the location
    • longitude - Longitude of the location

Development

Adding New MCP Servers

To add a new MCP server:

  1. Create a new Python file (e.g., weather_Europe.py)
  2. Import FastMCP and create an instance:
from mcp.server.fastmcp import FastMCP

mcp = FastMCP("weather-Europe")
  1. Define tools using the @mcp.tool() decorator:
@mcp.tool()
async def your_tool_name(param: str) -> str:
    """Tool description"""
    # Your implementation
    return result
  1. Add the main entry point:
def main():
    mcp.run(transport="stdio")

if __name__ == "__main__":
    main()
  1. Register the server in host.py by adding it to the mcp_clients list:
self.mcp_clients: list[MCPClient] = [
    MCPClient("./weather_USA.py"),
    MCPClient("./weather_Israel.py"),
    MCPClient("./weather_Europe.py")  # Add your new server
]

Debugging

  • The browser runs in headless mode by default. Set headless=False in BrowserState.ensure_browser() to see the browser actions.
  • Check the console output for tool execution logs and error messages.
  • Use the @mcp.tool() decorator's docstring to provide clear descriptions for the LLM.

Troubleshooting

Browser not launching: Ensure Playwright Chromium is installed with uv run playwright install chromium

Timeout errors: The website might be slow or loading differently. Adjust timeout values in the tool functions.

Selector not found: The website structure may have changed. Update the CSS selectors in the tool functions.

API errors: Verify your Anthropic API key is correctly set in the .env file.

License

This project is part of an academic project for learning MCP (Model Context Protocol) implementation.

Acknowledgments

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选