一个基于FastAPI的强大实现,用于模型上下文协议(MCP),并增强了工具注册表的功能,利用了成熟的FastAPI生态系统。
Awesome MCP FastAPI 是一个生产就绪的MCP实现,通过与FastAPI强大的生态系统集成,增强了标准MCP功能。该项目提供了一个改进的工具注册系统,使得创建、管理和记录大型语言模型(LLMs)的AI工具变得更加容易。
虽然模型上下文协议为连接AI模型与工具和数据源提供了坚实的基础,但我们的实现提供了几个显著的优势:
我们的实现改进了标准MCP工具注册表:
# 克隆仓库
git clone https://github.com/yourusername/awesome-mcp-fastapi.git
cd awesome-mcp-fastapi
# 创建虚拟环境
python -m venv .venv
source .venv/bin/activate # 在Windows上:.venv\Scripts\activate
# 安装依赖
pip install -e .
uvicorn src.main:app --reload
访问 http://localhost:8000/docs 查看OpenAPI文档。
from fastapi import FastAPI
from src.utils.tools import auto_tool, bind_app_tools
app = FastAPI()
bind_app_tools(app)
@auto_tool(
name="计算器",
description="执行基本算术运算",
tags=["数学"]
)
@app.post("/api/calculator")
async def calculator(operation: str, a: float, b: float):
"""
执行基本算术运算。
参数:
- operation: 要执行的操作(加、减、乘、除)
- a: 第一个数字
- b: 第二个数字
返回:
操作的结果
"""
if operation == "加":
return {"结果": a + b}
elif operation == "减":
return {"结果": a - b}
elif operation == "乘":
1return {"结果": a * b}
elif operation == "除":
if b == 0:
return {"错误": "不能除以零"}
return {"结果": a / b}
else:
return {"错误": f"未知操作:{operation}"}
LLMs可以通过模型上下文协议发现并使用您的工具。例如使用Claude:
您可以使用计算器工具执行计算。尝试计算42 * 13。
Claude将自动找到并使用您的计算器工具来执行计算。
我们的应用程序遵循模块化架构:
src/
├── api/ # API端点
│ └── v1/ # API版本1
├── core/ # 核心功能
│ └── settings.py # 应用设置
├── db/ # 数据库连接
│ └── models/ # 数据库模型
├── main.py # 应用入口点
└── utils/ # 工具函数
└── tools.py # 增强的工具注册表
构建并运行Docker:
docker build -t awesome-mcp-fastapi .
docker run -p 8000:8000 --env-file .env awesome-m
欢迎贡献!请随时提交Pull Request。
本项目根据MIT许可证发布 - 详情见LICENSE文件。 </中文翻译>