<a href="https://www.statcan.gc.ca/en/start" target="_blank"><img src="assets/StatCan-Header.png" alt="加拿大统计局MCP服务器"></a>
<a href="https://www.python.org/downloads/" target="_blank"><img src="https://img.shields.io/badge/python-3.10+-blue.svg" alt="Python 3.10+"></a> <a href="https://opensource.org/licenses/MIT" target="_blank"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="MIT许可证"></a> <a href="https://modelcontextprotocol.io/" target="_blank"><img src="https://img.shields.io/badge/MCP-ModelContextProtocol-green.svg" alt="MCP"></a> <a href="https://github.com/Aryan-Jhaveri" target="_blank"><img src="https://img.shields.io/badge/GitHub-Aryan--Jhaveri-lightgrey?logo=github" alt="GitHub"></a>
本项目实现了一个<a href="https://modelcontextprotocol.io/" target="_blank">模型上下文协议(MCP)</a>服务器,提供与加拿大统计局(StatCan)数据API交互的工具。它允许大型语言模型(LLMs)或其他MCP客户端以结构化方式访问和检索加拿大统计数据。
该服务器使用<a href="https://github.com/jlowin/fastmcp" target="_blank">FastMCP</a>库构建,并通过httpx与StatCan Web数据服务进行交互。
| 数据集 | 查询示例 | 演示 | 数据源 |
|---|---|---|---|
| 加拿大温室气体排放量 (2018-2022) | "嘿Claude!你能为过去四年的加拿大整体温室气体排放创建一个简单的可视化吗?" | <a href="https://claude.ai/share/7de892a1-e1d9-410f-96f7-90cd140e5dd9" target="_blank">查看演示</a> | <a href="https://www150.statcan.gc.ca/t1/tbl1/en/cv.action?pid=3810009701" target="_blank">StatCan表格</a> |
| 加拿大的国际服务贸易 | "嘿Claude,你能为过去六个月的国际服务贸易快速分析一下吗?请生成一个包含关键数字的可视化。" | <a href="https://claude.ai/share/c00eba2d-4e86-4405-878a-7ea4110cb7d3" target="_blank">查看演示</a> | <a href="https://www150.statcan.gc.ca/t1/tbl1/en/cv.action?pid=1210014401" target="_blank">StatCan表格</a> |
| 安大略省建筑价格指数 | "嘿Claude!你能生成从2023年第四季度到2024年第四季度的安大略省建筑价格指数的可视化吗?谢谢!" | <a href="https://claude.ai/share/12ce906f-5a26-4e74-86d9-10451ab5bc4b" target="_blank">查看演示</a> | <a href="https://www150.statcan.gc.ca/t1/tbl1/en/cv.action?pid=1811028901" target="_blank">StatCan表格</a> |
为了从使用此加拿大统计局MCP服务器的Claude获得最准确的结果:
⚠️ 警告:像Claude这样的大型语言模型偶尔可能会创建模拟可视化或编造数据,当无法检索实际信息时。它们也可能生成含有不在加拿大统计局中的数据的响应来满足查询。始终将结果与官方来源进行核对。
此服务器公开了StatCan API功能作为MCP工具,包括:
服务器自动创建一个SQLite数据库(temp_statcan_data.db),用于:
这允许持久存储检索的数据并通过SQL进行更复杂的操作。
(参见src/api/中的具体工具函数以获取详细参数和返回类型。)
src/:包含MCP服务器的主要源代码。api/:定义了封装StatCan API调用的MCP工具(cube_tools.py,vector_tools.py,metadata_tools.py)。db/:处理数据库交互,包括连接、模式和查询。models/:包含Pydantic模型,用于API请求/响应验证和数据库表示。util/:实用函数(例如,坐标填充)。config.py:配置加载(例如,数据库凭证,API基础URL)。server.py:主要的FastMCP服务器定义和工具注册。__init__.py:src包初始化。pyproject.toml:项目依赖和构建配置。.env:(假设)用于存储敏感配置如数据库凭证,由src/config.py加载。如果你是Python或编程新手,请按照以下简单步骤开始:
# 打开你的终端(Mac/Linux)或命令提示符(Windows)并运行:
curl -fsSL https://astral.sh/uv/install.sh | bash
# 或在Windows上:
# curl.exe -fsSL https://astral.sh/uv/install.ps1 -o install.ps1; powershell -ExecutionPolicy Bypass -File install.ps1
uv pip install fastmcp httpx pydantic
git clone https://github.com/Aryan-Jhaveri/mcp-statcan.git
cd mcp-statcan
提示:如果遇到任何“模块未找到”错误,请使用以下命令安装缺失的包:
uv pip install package_name
要与Claude Desktop集成:
claude_desktop_config.json中:导航至:Claude Desktop应用 → 设置(⌘ + ,)→ 开发者 → 编辑配置
{
"mcpServers": {
"StatCanAPI_DB_Server": {
"command": "uv",
"args": [
"run",
"--with", "fastmcp",
"--with", "httpx",
"sh",
"-c",
"cd /path/to/mcp-statcan && python -m src.server"
]
}
}
}
将/path/to/mcp-statcan替换为你项目的绝对路径。手动编辑是必要的,以确保服务器在正确的目录下运行,以便正确解析模块。
# 搜索关于就业的数据表
tables = await search_cubes_by_title("employment")
# 获取特定向量ID的最近数据点
data = await get_data_from_vectors_and_latest_n_periods(VectorLatestNInput(vectorId=12345, latestN=5))
# 获取特定时间段的数据
range_data = await get_data_from_vector_by_reference_period_range(
VectorPeriodRangeInput(vectorId=12345, startDate="2020-01-01", endDate="2020-12-31")
)
# 将API结果存储在SQLite数据库中
create_table_from_data(TableDataInput(table_name="employment_data", data=data))
# 查询数据库
result = query_database(QueryInput(sql_query="SELECT * FROM employment_data LIMIT 10"))
# 列出可用表
tables = list_tables()
# 获取表模式
schema = get_table_schema(TableSchemaInput(table_name="employment_data"))