MCP Weather Tools

MCP Weather Tools

An MCP server that enables AI assistants to call weather tools, read resources, and use prompt templates for live weather data integration.

Category
访问服务器

README

MCP Weather Tools — AI Tool Integration System

A production-style Model Context Protocol (MCP) server that enables AI assistants to call structured tools, read external resources, and use prompt templates — demonstrated through a live weather data integration with a React frontend.

TypeScript MCP React Node.js


Problem Statement

Large Language Models are powerful at reasoning and generating text, but they cannot access live data or perform real-world actions on their own. When a user asks "What's the weather in Tokyo?", the LLM has no built-in mechanism to query a weather API and return current conditions.

Model Context Protocol (MCP) solves this by providing a standardized interface between AI assistants and external tools. This project implements a complete MCP server that:

  • Registers callable tools that the LLM invokes during a conversation
  • Exposes read-only resources the LLM can query for context
  • Provides reusable prompt templates that pre-fill structured queries
  • Returns structured JSON responses the LLM uses to generate accurate answers

Architecture Overview

flowchart LR
    User([User]) --> Client[AI Client\nCursor / React App]
    Client --> LLM[LLM\nClaude / GPT]
    LLM -->|tool_call| Client
    Client -->|JSON-RPC\nstdio| MCP1[Custom MCP\nweather-data-fetcher]
    Client -->|JSON-RPC\nstdio| MCP2[Filesystem MCP]
    Client -->|JSON-RPC\nstdio| MCP3[Memory MCP]
    MCP1 --> Tool[getWeatherDataByCity]
    MCP1 --> Resource["weather://cities\nweather://help"]
    MCP1 --> Prompt[weather-inquiry]
    Tool -->|HTTP| API[Open-Meteo API]
    API --> Tool
    MCP1 --> Client
    MCP2 --> Client
    MCP3 --> Client
    Client --> LLM
    LLM --> Client
    Client --> User

Flow: User asks a question → LLM determines which tool to use → MCP client sends JSON-RPC to the appropriate server (custom weather, filesystem, or memory) → server executes → structured response flows back → LLM composes a natural language answer.


Features

Capability Description
Custom + Official MCP Local MCP server plus Anthropic’s official servers (filesystem, memory); showcases big-company MCP integration
Tool Registration Declarative tool definitions with Zod schema validation on inputs
Structured Responses Tools return typed JSON that the LLM can reliably parse
Modular Tool Design Shared business logic (weather.ts) consumed by both MCP server and REST API
Resource Endpoints Read-only data exposed via weather:// URI scheme
Prompt Templates Pre-built prompt structures with argument interpolation
Input Validation Zod schemas enforce type safety at the protocol boundary
REST API Bridge Express server exposes MCP capabilities as HTTP endpoints for browser clients
React Frontend Interactive UI demonstrating all three MCP primitives (tools, resources, prompts)

Tech Stack

Layer Technology Purpose
MCP Server @modelcontextprotocol/sdk, TypeScript Tool registration, JSON-RPC handling, stdio transport
Validation Zod Input schema enforcement at protocol boundary
External API Open-Meteo (free, no key) Geocoding + weather forecast data
REST Bridge Express, CORS HTTP API for browser-based clients
Frontend React 19, TypeScript, Vite Interactive demo of MCP capabilities
Dev Tools tsx, concurrently Development server, parallel process management
Protocol JSON-RPC 2.0 over stdio MCP transport layer

Installation & Running

# Clone the repository
git clone https://github.com/selva/mcp-weather-tools.git
cd mcp-weather-tools

# Install server dependencies
npm install

# Install client dependencies
cd client
npm install
cd ..

Running

npm run demo

Then open http://localhost:5173


Example Tool Call

JSON-RPC Request (MCP Client → Server)

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "getWeatherDataByCity",
    "arguments": {
      "city": "Tokyo"
    }
  }
}

JSON-RPC Response (Server → Client)

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "{\"temp\":\"22°C\",\"humidity\":\"65%\",\"weather\":\"Partly cloudy\",\"wind\":\"12 km/h\",\"city\":\"Tokyo\",\"country\":\"Japan\"}"
      }
    ]
  }
}

REST API Equivalent

curl http://localhost:3001/api/weather?city=Tokyo
{
  "temp": "22°C",
  "humidity": "65%",
  "weather": "Partly cloudy",
  "wind": "12 km/h",
  "city": "Tokyo",
  "country": "Japan"
}

Project Structure

mcp-weather-tools/
├── server.ts              # MCP server — tool, resource, prompt registration
├── weather.ts             # Shared business logic (Open-Meteo API client)
├── api/
│   └── index.ts           # Express REST API — HTTP bridge for browser clients
├── client/                # React frontend (Vite + TypeScript)
│   ├── src/
│   │   ├── App.tsx        # Main UI — weather, cities, prompt, about tabs
│   │   ├── App.css        # Dark theme styling
│   │   └── api.ts         # Typed fetch wrappers for REST endpoints
│   └── vite.config.ts     # Dev proxy /api → localhost:3001
├── docs/
│   ├── images/            # Screenshots (MCP Inspector, etc.)
│   ├── architecture.md   # Detailed MCP architecture explanation
│   ├── third-party-mcp.md # Using official MCP servers (filesystem, memory)
│   ├── adding-tools.md   # Guide: how to add new tools to this server
│   ├── request-flow.md   # Step-by-step MCP request lifecycle
│   ├── demo.md           # Example conversation walkthrough
│   └── demo-video-script.md
├── SECURITY.md            # AI tool system security considerations
├── package.json
├── tsconfig.json
└── README.md

MCP Capabilities

Tools (Actions)

Tool Input Output Description
getWeatherDataByCity { city: string } Weather JSON Geocodes city, fetches live forecast from Open-Meteo

Resources (Read-only Data)

URI MIME Type Description
weather://cities text/plain Newline-separated list of example cities
weather://help text/plain Usage instructions for the weather server

Prompts (Templates)

Prompt Arguments Description
weather-inquiry { city: string } Pre-fills: "What's the current weather in {city}?"

Cursor IDE Integration

Add to .cursor/mcp.json:

{
  "mcpServers": {
    "weather-data-fetcher": {
      "command": "npx",
      "args": ["tsx", "server.ts"],
      "cwd": "/path/to/mcp-weather-tools"
    },
    "filesystem": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/your/project"]
    },
    "memory": {
      "command": "npx",
      "args": ["-y", "@modelcontextprotocol/server-memory"]
    }
  }
}

This config runs both:

  • Custom server (weather-data-fetcher) — our local MCP with getWeatherDataByCity, resources, prompts
  • Official servers (filesystem, memory) — Anthropic’s @modelcontextprotocol servers for file operations and persistent memory

Then ask in Cursor chat: "What's the weather in London?" or "Read docs/architecture.md" — the LLM can call tools from any server.


MCP Inspector

Use the MCP Inspector to debug and test the server — call tools, read resources, and try prompts without Cursor.

npm run inspector

This opens a web UI where you can list and invoke tools, read resources (weather://cities, weather://help), and test the weather-inquiry prompt with any city.

MCP Inspector — weather-inquiry prompt


Security Considerations

See SECURITY.md for a detailed analysis. Key points:

  • Input validation — All tool inputs validated through Zod schemas before execution
  • No arbitrary code execution — Tools perform specific, scoped operations only
  • External API isolation — Weather logic is the only outbound network call; no user-controlled URLs
  • Prompt injection awareness — Tool responses are structured JSON, not raw user input passed to system prompts
  • No secrets in transport — Open-Meteo requires no API keys; no credentials cross the stdio boundary

Future Improvements

Area Enhancement
Authentication API key or OAuth for REST endpoints
Rate Limiting Token bucket per client to prevent tool abuse
Sandboxed Execution Run tools in isolated containers or V8 isolates
Logging & Monitoring Structured logging with correlation IDs per request
Tool Registry Dynamic tool loading from a plugin directory
Caching TTL-based response cache for repeated city lookups
Error Classification Distinguish retriable vs. permanent failures in tool responses
Multi-tool Orchestration Chain tools (e.g., get cities → get weather for each)

Documentation

Document Description
Architecture MCP protocol deep-dive, component interaction, transport layer
Third-Party MCP Integration Using external MCP servers alongside the custom server
Adding Tools Developer guide for registering new MCP tools
Request Flow Step-by-step lifecycle of an MCP request
Demo Walkthrough Example conversations showing tool calls in action
Security Threat model and mitigation strategies for AI tool systems

License

MIT

推荐服务器

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 模型以安全和受控的方式获取实时的网络信息。

官方
精选