MCP FastAPI图表服务器
一个MCP(模型上下文协议)服务器,与FastAPI服务交互以生成matplotlib图表。
概述
此MCP服务器提供以下工具:
- 使用FastAPI图表生成服务生成图表
- 检查您的FastAPI服务器的状态
- 将图表图像直接返回给客户端
先决条件
- Python 3.8+
- 您的FastAPI服务器正在运行
localhost:8000随着/plot端点
安装
- 安装软件包:
pip install -e .配置
适用于克劳德桌面
将以下内容添加到您的Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json 窗户: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"fastapi-chart": {
"command": "python",
"args": ["-m", "mcp_fastapi_chart.server"]
}
}
}对于其他MCP客户端
配置您的MCP客户端以运行:
python -m mcp_fastapi_chart.server可用工具
1. generate_chart
使用FastAPI图表生成服务生成图表。
参数:
chart_type(字符串):要生成的图表类型-“直线”、“条形图”、“散点图”或“饼图”title(string):图表的主标题subtitle(string):图表的副标题x_label(string):X轴标签y_label(string):Y轴标签data(对象):带标签和值的图表数据
- labels (字符串数组):数据点的标签 - values (数字数组):数据点的值
示例用法:
{
"chart_type": "line",
"title": "Asylum Applications Trend",
"subtitle": "Monthly applications received (2023-2024)",
"x_label": "Month",
"y_label": "Applications Received",
"data": {
"labels": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
"values": [15400, 16800, 14200, 18900, 17600, 19500, 20300, 18700, 17400, 16200, 15800, 14900]
}
}2. check_fastapi_status
检查FastAPI服务器是否正在运行且可访问。
参数: 无
FastAPI服务器要求
您的FastAPI服务器应该在以下位置有一个端点 /plot 它接受具有以下JSON结构的POST请求:
{
"chart_type": "string",
"title": "string",
"subtitle": "string",
"data": {
"labels": ["string"],
"values": [number]
},
"x_label": "string",
"y_label": "string"
}并且应该返回一个PNG图像作为响应。
发展
要直接运行服务器进行测试,请执行以下操作:
python -m mcp_fastapi_chart.server云部署
当您将FastAPI服务器迁移到云端时,请更新 fastapi_base_url 在 mcp_fastapi_chart/server.py:
self.fastapi_base_url = "https://your-cloud-domain.com"故障排除
- 连接错误:确保您的FastAPI服务器正在运行
localhost:8000 - 图表生成错误:验证您的FastAPI
/plot端点工作正常 - MCP连接问题:检查您的Claude Desktop配置文件语法
许可证
麻省理工学院
