超棒的 MCP FastAPI
基于 FastAPI 的模型上下文协议 (MCP) 的强大实现,具有增强的工具注册功能,利用成熟的 FastAPI 生态系统。
概述
Awesome MCP FastAPI 是模型上下文协议 (MCP) 的生产级实现,它通过与 FastAPI 强大的生态系统集成,增强并扩展了标准 MCP 功能。该项目提供了一个改进的工具注册系统,使创建、管理和记录大型语言模型 (LLM) 的 AI 工具变得更加容易。
Related MCP server: Fastn Server
为什么这比标准 MCP 更好
虽然模型上下文协议为连接 AI 模型与工具和数据源提供了坚实的基础,但我们的实现还具有几个显著的优势:
FastAPI 的成熟生态系统
生产就绪 Web 框架:基于 FastAPI 构建,这是一个高性能、现代的 Web 框架,具有自动 OpenAPI 文档生成功能。
依赖注入:利用 FastAPI 强大的依赖注入系统获得更易于维护和测试的代码。
中间件支持:轻松与身份验证、监控和其他中间件组件集成。
内置验证:Pydantic 集成,用于强大的请求/响应验证和数据建模。
异步支持:对高并发应用程序的异步/等待模式提供一流的支持。
增强工具注册表
我们的实施通过以下方式改进了标准 MCP 工具注册表:
自动文档生成:工具自动以 MCP 格式和 OpenAPI 规范记录。
改进的类型提示:增强类型信息提取,以获得更好的工具和 IDE 支持。
更丰富的模式定义:为工具输入和输出提供更具表现力的 JSON 模式定义。
更好的错误处理:带有详细信息的结构化错误响应。
增强的文档字符串支持:更好地从 Python 文档字符串中提取文档。
附加功能
CORS 支持:支持跨域请求,可轻松与 Web 应用程序集成。
生命周期管理:通过 FastAPI 的生命周期 API 进行适当的资源初始化和清理。
入门
先决条件
Python 3.10+
安装
# Clone the repository
git clone https://github.com/yourusername/awesome-mcp-fastapi.git
cd awesome-mcp-fastapi
# Create a virtual environment
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
# Install dependencies
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="calculator",
description="Perform basic arithmetic operations",
tags=["math"]
)
@app.post("/api/calculator")
async def calculator(operation: str, a: float, b: float):
"""
Perform basic arithmetic operations.
Parameters:
- operation: The operation to perform (add, subtract, multiply, divide)
- a: First number
- b: Second number
Returns:
The result of the operation
"""
if operation == "add":
return {"result": a + b}
elif operation == "subtract":
return {"result": a - b}
elif operation == "multiply":
return {"result": a * b}
elif operation == "divide":
if b == 0:
return {"error": "Cannot divide by zero"}
return {"result": a / b}
else:
return {"error": f"Unknown operation: {operation}"}通过 MCP 访问工具
LLM 可以通过模型上下文协议 (MCP) 发现并使用你的工具。例如,使用 Claude 的示例:
You can perform calculations using the calculator tool. Try calculating 42 * 13.Claude 将自动查找并使用您的计算器工具进行计算。
建筑学
我们的应用程序遵循模块化架构:
src/
├── api/ # API endpoints
│ └── v1/ # API version 1
├── core/ # Core functionality
│ └── settings.py # Application settings
├── db/ # Database connections
│ └── models/ # Database models
├── main.py # Application entry point
└── utils/ # Utility functions
└── tools.py # Enhanced tool registryDocker 支持
使用 Docker 构建并运行:
docker build -t awesome-mcp-fastapi .
docker run -p 8000:8000 --env-file .env awesome-mcp-fastapi贡献
欢迎贡献代码!欢迎提交 Pull 请求。
执照
该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE 文件。
Appeared in Searches
- Open-Source MCP Server in Python with PostgreSQL and OAuth 2.0
- Servers for Cloud Architecture (AWS) and Platform Engineering with Oauth Integration for Documentation Access
- Information about development or programming
- A search for development-related topics or tools
- Enhancing Claude-based Code Generation