NBP MCP服务器
模型上下文协议(MCP)服务器,用于访问波兰国家银行(NBP)数据,包括货币汇率和黄金价格。
特性
此服务器提供对NBP公共API的访问,并具有以下功能:
汇率
- 获取任何货币的当前汇率
- 检索完整的汇率表(A、B或C)
- 查询特定日期范围的历史汇率
- 获取最后N个汇率以进行趋势分析
黄金价格
- 获取当前黄金价格(PLN/克)
- 检索历史黄金价格
- 查询最近N个黄金报价
表类型
- A表:外币平均汇率(每日更新)
- 表B:其他货币的平均汇率(每周更新)
- 表C:货币的买入价和卖出价(每日更新)
安装
创建新项目
uv init nbp-mcp-server
cd nbp-mcp-server
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate添加依赖项
uv add "mcp[cli]" httpx配置
克劳德桌面
将此添加到您的Claude Desktop配置文件中:
MacOS: ~/Library/Application Support/Claude/claude_desktop_config.json 视窗: %APPDATA%/Claude/claude_desktop_config.json
{
"mcpServers": {
"nbp": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/nbp-mcp-server",
"run",
"main.py"
]
}
}
}其他MCP客户端
对于支持stdio传输的其他MCP客户端,请将其配置为运行:
uv --directory /path/to/nbp-mcp-server run main.pyn8n(HTTP传输)
对于n8n集成,请使用Docker的HTTP Streamable传输:
Docker编写配置
nbp-mcp-server:
image: quay.io/migi/nbp-mcp-server:1.0.3
hostname: nbp
container_name: nbp-mcp-server
networks: ['mynetwork']
ports:
- 8000:8000
environment:
- PYTHONUNBUFFERED=1
restart: unless-stoppedn8n MCP客户端工具节点设置
在n8n中配置MCP客户端工具节点:
- 统一资源定位符:
http://nbp:8000/mcp - 服务器传输:HTTP流式传输
n8n的AI提示示例
Ile kosztowało EURO 15 lutego 2022 roku.
# Rules
- The date which is going in the tool needs to be in a format YYYY-MM-DD
- Always use available MCP NBP tool to query for exchange rate.
- When asking for week of the year you need to use some external knowledge to get the dates
- If no data is returned you need to query the range at least 7 days prior to the given date and choose the closest one. This is because public holidays do not contain data.
# Output
- Always respond in pure JSON without any additional text or formatting.
- Always give ONLY one rate as a number without anything else
- Example output {'convertion': '4.2566'}
- If there is some problem simply give back {'conversion': 'n/a'}可用工具
1.get_currency_rate
获取特定货币的当前汇率或特定日期的汇率。
参数:
code(必填):三个字母的货币代码(如美元、欧元、英镑)-ISO 4217date(可选):YYYY-MM-DD格式的特定日期(ISO 8601)。如果没有提供,则获取当前费率。table(可选):表类型-“a”、“b”或“c”(默认值:“a”)
例子:
Get current USD exchange rate from table A
Get EUR bid/ask rates for 2024-01-15 (table C)
What was the GBP rate on 2024-03-20?2.get_exchange表
获取包含所有货币的完整汇率表,无论是当前货币还是特定日期的货币。
参数:
date(可选):YYYY-MM-DD格式的特定日期(ISO 8601)。如果没有提供,则获取当前表。table(可选):表类型-“a”、“b”或“c”(默认值:“a”)
例子:
Get the current table A with all exchange rates
Get table C with all bid/ask rates for 2024-02-15
What were all the exchange rates on 2024-01-01?3.get_current_rate_history
获取日期范围内货币的历史汇率。
参数:
code(必填):三个字母的货币代码(ISO 4217)start_date(必填):开始日期,格式为YYYY-MM-DDend_date(必填):YYYY-MM-DD格式的结束日期table(可选):表类型-“a”、“b”或“c”(默认值:“a”)
注: 日期范围不能超过93天。
例子:
Get USD rates from 2024-01-01 to 2024-01-31
Get EUR historical data for the last month4.get_currency_rate_last_n
获取一种货币的最后N个汇率。
参数:
code(必填):三个字母的货币代码(ISO 4217)count(必填):要检索的费率数量(1-255)table(可选):表类型-“a”、“b”或“c”(默认值:“a”)
例子:
Get last 10 USD exchange rates
Get last 30 GBP rates from table C5.获取价格
获取当前或特定日期的每克PLN黄金价格。
参数:
date(可选):YYYY-MM-DD格式的特定日期(ISO 8601)。如果没有提供,则获取当前价格。
注: 黄金价格数据自2013年1月2日起公布。
例子:
What is the current gold price?
What was the gold price on 2024-01-15?
Get gold price for 2023-12-256.获取价格历史
获取日期范围内的历史金价。
参数:
start_date(必填):开始日期,格式为YYYY-MM-DDend_date(必填):YYYY-MM-DD格式的结束日期
注: 日期范围不能超过93天。黄金价格数据自2013年1月2日起公布。
例子:
Get gold prices from 2024-01-01 to 2024-01-31
Show me gold price history for last month7.获取价格
获取最后N个黄金报价。
参数:
count(必填):要检索的价格数量(1-255)
例子:
Get last 10 gold prices
Show me the last 30 gold quotations使用示例
在Claude Desktop或其他MCP客户端中配置后,您可以询问:
- “目前美元对波兰兹罗提的汇率是多少?”
- “2024-02-15年的欧元汇率是多少?”
- “显示最近30天的欧元汇率”
- “获取包含所有当前汇率的完整表A”
- “2024年1月1日的汇率是多少?”
- “2024年1月15日的金价是多少?”
- “比较表A和表C之间的美元汇率”
- “显示过去10天英镑汇率趋势”
数据源
所有数据来源于官方NBP(Narodowy Bank Polski)API:
- API文件:https://api.nbp.pl/
- 汇率数据可从2002年1月2日获得
- 黄金价格数据可从:2013年1月2日
发展
本地运行
# Activate virtual environment
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Run the server
python main.py测试
该项目包括对所有功能的全面测试。
安装测试依赖项
# Install all dependencies including dev extras (pytest, pytest-asyncio, pytest-cov)
uv sync --all-extras运行测试
重要提示: 始终使用 uv run pytest 以确保测试在具有所有依赖关系的正确虚拟环境中运行。
# Run all tests
uv run pytest
# Run tests with verbose output
uv run pytest -v
# Run tests with coverage report (default configuration)
uv run pytest
# Coverage reports are generated automatically per pyproject.toml config
# Run tests with HTML coverage report
uv run pytest --cov-report=html
# Then open htmlcov/index.html in your browser
# Run specific test file
uv run pytest tests/test_currency_rates.py
# Run specific test function
uv run pytest tests/test_currency_rates.py::test_get_currency_rate_success
# Run tests matching a pattern
uv run pytest -k "currency"测试结构
测试套件组织如下:
tests/
├── __init__.py
├── conftest.py # Pytest configuration and fixtures
├── test_currency_rates.py # Tests for currency exchange rate tools
├── test_gold_prices.py # Tests for gold price tools
└── test_helpers.py # Tests for helper functions测试覆盖率
测试包括:
货币汇率工具:
- 获取当前汇率(表A、B、C)
- 正在检索完整的汇率表
- 按日期范围查询历史汇率
- 获取最后N个汇率
- 输入验证(无效的表类型、计数)
- 错误处理(API错误、网络问题)
- 不区分大小写的货币代码
黄金价格工具:
- 获取当前黄金价格
- 检索历史黄金价格
- 获取最后N个黄金价格
- 输入验证
- 错误处理
辅助功能:
- 使用正确标头处理API请求
- HTTP错误处理(404、超时、网络错误)
- 汇率、表格和金价的数据格式
- 边缘情况(缺少字段、空响应)
连续测试
对于开发,您可以使用pytestwatch在文件更改时自动运行测试:
# Install pytest-watch
uv pip install pytest-watch
# Run tests in watch mode with uv
uv run ptwMCP检验员测试
选项1:使用stdio传输(推荐)
npx @modelcontextprotocol/inspector uv --directory /FULL/PATH/to/nbp-mcp-server run main.py这使用默认的stdio传输,检查器可以直接与之通信。
选项2:使用流式http传输
如果你想专门测试HTTP传输,你需要分别运行服务器和检查器:
端子1 -启动服务器:
cd /path/to/nbp-mcp-server
uv run main.py --transport streamable-http2号航站楼 -将检查器连接到正在运行的服务器:
npx @modelcontextprotocol/inspector http://localhost:8000/mcp重要提示: MCP端点位于 /mcp,而不是根路径。
默认情况下,服务器会监听 0.0.0.0:8000。您可以对此进行自定义:
uv run main.py --transport streamable-http --host 127.0.0.1 --port 3000然后连接到 http://localhost:3000/mcp
许可证
该项目使用Apache许可证2.0版,但使用api调用从NBP公共api收集数据。 有关数据使用指南,请参阅NBP的服务条款。
贡献
欢迎投稿!请随时提交问题或拉取请求。
