Skip to main content
Glama
adri14gl

MCP Weather and Currency Assistant

by adri14gl

使用 MCP 的金融与天气助手

结合本地 MCP 服务器与 OpenAI Responses API 的命令行应用。该助手能够理解自然语言查询,发现服务器发布的工具,并执行它们以查询实时汇率和天气数据。

该项目集成了 FastMCP、ExchangeRate-API 和 Open-Meteo。它还支持链式调用:例如,将城市名称转换为坐标,然后查询其当前天气或预报。

功能

  • 通过 ISO 4217 代码进行货币间金额转换。

  • 查询基础货币的汇率。

  • 城市地理编码。

  • 查询当前天气和 1 至 7 天预报。

  • 客户端自动发现 MCP 工具。

  • 西班牙语交互式终端界面。

  • 调用 OpenAI 时临时错误的重试机制及网络错误处理。

Related MCP server: Weather MCP Server

架构

Usuario
  |
  v
CLI (main_client.py)
  |
  v
Cliente OpenAI Responses <----> Servidor MCP local (FastMCP)
                                      |
                                      +--> ExchangeRate-API
                                      +--> Open-Meteo

服务器默认通过 HTTP 传输暴露在 http://127.0.0.1:8000/mcp/。客户端连接到该 URL,加载五个可用工具,并将其结果传递给模型以生成最终响应。

MCP 工具

工具

描述

convert_currency

使用当前汇率在两种货币之间转换金额。

get_exchange_rates

返回基础货币相对于选定货币的汇率。

geocode_city

获取城市的坐标、国家和时区。

get_current_weather

查询当前温度、体感温度、湿度、风速和天气状况。

get_weather_forecast

返回未来几天的温度、降水概率和天气状况。

要求

  • Python 3.10 或更高版本。

  • 具有 Responses API 访问权限的 OpenAI API 密钥。

  • 互联网连接以查询 OpenAI 和外部 API。

  • ExchangeRate-API 密钥可选。没有密钥时使用其开放端点。

安装

克隆仓库并创建虚拟环境:

git clone <URL_DEL_REPOSITORIO>
cd <NOMBRE_DEL_REPOSITORIO>
python -m venv .venv

激活虚拟环境:

# Windows PowerShell
.venv\Scripts\Activate.ps1

# Windows (cmd)
.venv\Scripts\activate.bat

# macOS/Linux
source .venv/bin/activate

安装依赖:

python -m pip install -r requirements.txt

配置

复制示例配置文件并填写凭据。.env 文件是本地文件,不应包含在仓库中:

Copy-Item .env.example .env

在 macOS 或 Linux 上:

cp .env.example .env

生成的文件应包含以下变量:

OPENAI_API_KEY=tu_clave_de_openai

# Opcional. Si se deja vacío se usa el endpoint abierto de ExchangeRate-API.
API_KEY_EXCHANGE=

# Opcionales.
MCP_HOST=127.0.0.1
MCP_PORT=8000
OPENAI_MODEL=gpt-4o-mini

OPENAI_API_KEY 是使用客户端所必需的。Open-Meteo 不需要凭证。应用程序对外部 HTTP 调用使用 10 秒超时。

使用

服务器和客户端在独立的终端中运行。首先启动服务器:

python main_server.py

在另一个终端中,激活虚拟环境后启动客户端:

python main_client.py

测试

单元测试涵盖货币逻辑、天气预报、WMO 代码和错误处理,无需调用 OpenAI 或外部 API:

python -m pip install -r requirements-dev.txt
python -m pytest

之后你可以输入如下查询:

Convierte 100 USD a EUR
¿Cuál es el clima actual en Madrid?
Dame la previsión del tiempo para Nueva York durante 5 días
¿Qué coordenadas tiene Tokio?

CLI 中可用的命令:

命令

操作

/ayuda

显示工具和查询示例。

/monedas

显示常见货币代码。

/salir

关闭客户端。

项目结构

.
├── .env.example          # Plantilla de configuración local
├── .gitignore            # Archivos excluidos del repositorio
├── client/
│   ├── cli_interface.py    # Interfaz interactiva de terminal
│   └── openai_client.py    # Orquestación OpenAI + MCP
├── config/
│   └── settings.py         # Variables de entorno y endpoints
├── server/
│   ├── api_clients.py      # Clientes HTTP para las APIs externas
│   ├── currency_tools.py   # Herramientas de divisas
│   ├── geocoding_tools.py  # Herramienta de geocodificación
│   ├── mcp_server.py       # Servidor FastMCP y registro de herramientas
│   └── weather_tools.py    # Herramientas meteorológicas
├── main_client.py          # Punto de entrada del cliente
├── main_server.py          # Punto de entrada del servidor
├── requirements.txt        # Dependencias de Python
└── README.md

外部 API

结果取决于这些服务的可用性、限制和使用条款。使用 OpenAI 可能会根据账户和配置的模型产生费用。

Related MCP Connectors

Related MCP Servers

  • A
    license
    Not graded
    quality
    D
    maintenance
    A server that integrates the MCP library with OpenAI's API, allowing users to interact with various tools, such as the weather tool, through natural language queries.
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    Enables users to get current weather conditions and forecasts for any city through OpenWeatherMap API integration. Designed as a sample MCP server for the Puch AI Hackathon with basic weather querying capabilities.
    -
  • A
    license
    Not graded
    quality
    D
    maintenance
    A MCP server for querying real-time weather information for any city worldwide using the free Open-Meteo API, supporting CLI and integration with AI clients like Claude and Cursor.
    17 npm
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    A proof-of-concept MCP server that provides weather information using the Open-Meteo API, with tools for greeting, getting weather by coordinates, and by location name.
    -