基于 MCP (Model Context Protocol) 的地震数据服务,提供实时地震数据查询和统计分析功能。
# 使用pip安装
pip install uv
# 或者使用其他方式安装,参考uv官方文档:
# https://github.com/astral-sh/uv
git clone [项目地址]
cd earthquake
# 使用uv安装依赖
uv pip install -r requirements.txt
# 或者使用传统pip安装
pip install -r requirements.txt
项目使用FastMCP框架,需要在你的MCP配置文件中添加以下设置:
{
"mcpServers": {
"cnb.cool/cnb/tools/earthquake-mcp": {
"command": "uv",
"args": [
"--directory",
"C:/my/PyProject/dizhen/earthquake",
"run",
"main.py"
]
}
}
}
command: 使用 uv 作为Python包管理器和运行器args:
--directory: 指定项目目录路径run: 运行Python脚本main.py: 主程序文件注意:
获取指定条件的地震信息数据。
参数说明:
start_date (str): 开始日期,格式:YYYY-MM-DDend_date (str, 可选): 结束日期,格式:YYYY-MM-DDmin_magnitude (float, 可选): 最小震级location (str, 可选): 地点关键词返回值格式: 返回一个包含地震信息的列表,每条信息包含以下字段:
服务使用标准的HTTP状态码进行错误处理:
错误响应格式:
{
"detail": "错误描述信息"
}
from mcp.client import Client
async def get_latest_earthquakes():
client = Client()
# 获取最近一天的地震信息
result = await client.call_tool("cnb.cool/cnb/tools/earthquake-mcp", "earthquake_info", {
"start_date": "2024-01-31"
})
print(result)
async def get_filtered_earthquakes():
client = Client()
# 获取指定日期范围内,震级大于4.0的四川地区地震
results = await client.call_tool("cnb.cool/cnb/tools/earthquake-mcp", "earthquake_info", {
"start_date": "2024-01-01",
"end_date": "2024-01-31",
"min_magnitude": 4.0,
"location": "四川"
})
for quake in results:
print(f"时间: {quake['time']}, 地点: {quake['location']}, 震级: {quake['magnitude']}")
async def handle_errors():
client = Client()
try:
result = await client.call_tool("cnb.cool/cnb/tools/earthquake-mcp", "earthquake_info", {
"start_date": "2024-01-31"
})
except HTTPException as e:
print(f"错误: {e.detail} (状态码: {e.status_code})")
except Exception as e:
print(f"未知错误: {str(e)}")
async def use_server_variable():
client = Client()
# 将服务器名称存储为变量,便于维护
server_name = "cnb.cool/cnb/tools/earthquake-mcp"
# 获取不同条件的地震数据
recent_quakes = await client.call_tool(server_name, "earthquake_info", {
"start_date": "2024-01-31"
})
significant_quakes = await client.call_tool(server_name, "earthquake_info", {
"start_date": "2024-01-01",
"min_magnitude": 5.0
})
print("最近地震:", recent_quakes)
print("重要地震:", significant_quakes)
如果遇到网络请求失败:
如果返回空结果:
如果遇到API调用错误:
如果遇到uv相关问题: