MCP工具服务器
使用FastAPI构建的生产就绪的MCP服务器,提供了一个增强的工具注册表,用于为大型语言模型(LLMs)创建、管理和记录AI工具。
服务介绍
Awesome MCP FastAPI
这是一个基于 FastAPI 的强大实现,它增强了 Model Context Protocol (MCP) 的工具注册表功能,并利用了成熟的 FastAPI 生态系统。
概览
Awesome MCP FastAPI 是一个生产就绪的 Model Context Protocol 实现,通过将其与 FastAPI 强大的生态系统集成,增强了和扩展了标准 MCP 功能。该项目提供了一个改进的工具注册表系统,使得为大型语言模型 (LLMs) 创建、管理和记录 AI 工具变得更加容易。
为什么这比标准 MCP 更好
虽然 Model Context Protocol 为连接 AI 模型与工具和数据源提供了坚实的基础,但我们的实现提供了几个显著的优势:
FastAPI 成熟的生态系统
- 生产就绪的 Web 框架:基于 FastAPI 构建,这是一个高性能的现代 Web 框架,具有自动 OpenAPI 文档生成。
- 依赖注入:利用 FastAPI 强大的依赖注入系统来编写更易于维护和测试的代码。
- 中间件支持:轻松集成认证、监控和其他中间件组件。
- 内置验证:Pydantic 集成用于强大的请求/响应验证和数据建模。
- 异步支持:对高并发应用的一流 async/await 模式支持。
增强的工具注册表
我们的实现通过以下方式改进了标准 MCP 工具注册表:
- 自动生成文档:工具在 MCP 格式和 OpenAPI 规范中自动生成文档。
- 改进的类型提示:增强的类型信息提取以获得更好的工具和 IDE 支持。
- 更丰富的模式定义:更具表达力的 JSON Schema 定义用于工具输入和输出。
- 更好的错误处理:结构化的错误响应,包含详细信息。
- 增强的 docstring 支持:更好地从 Python docstrings 中提取文档。
其他特性
- 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 访问工具
LLMs 可以通过 Model Context Protocol 发现并使用您的工具。例如使用 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 registry
Docker 支持
使用 Docker 构建和运行:
docker build -t awesome-mcp-fastapi .
docker run -p 8000:8000 --env-file .env awesome-mcp-fastapi
贡献
希望您喜欢这个翻译版本!如果还有其他需要帮助的地方,请告诉我。
欢迎贡献!请随时提交拉取请求。
许可证
本项目采用 MIT 许可证进行授权 - 详情请参阅 LICENSE 文件。