drf-mcp-docs
API documentation via MCP for AI coding agents
______________________________________________________________________
drf mcp文档 公开Django REST框架API 文档 通过 模型上下文协议(MCP) 因此,AI编码代理可以读取、理解并帮助您编写正确的前端集成代码。
这与其他Django+MCP包有什么不同? 套餐如django-mcp-server和django-rest-framework-mcp暴露DRF 行动 作为MCP工具,AI代理直接调用您的端点。 drf mcp文档 根本不同:它公开了API 文档 因此,AI代理可以帮助开发人员 编写前端代码 (React、Vue、Angular等)。 把它想象成: *drf survival在浏览器中为人类生成文档* → *drf-mcp-docs通过mcp为AI代理生成文档*.
特性
- MCP资源 -浏览API结构:概述、端点、模式、身份验证方法
- MCP工具 --搜索端点,获取详细文档,生成请求/响应示例
- 代码生成 --生成具有实际类型和文档的集成代码(JS/TS:fetch、axios、ky-Python:requests、httpx--cURL)
- 多适配器 --适用于drf壮观、drf-yasg或drf的内置模式生成
- 零风险 --只读文档,不可能发生数据突变
- 两次运输 --stdio用于本地AI工具,可流式传输http用于远程/网络访问
快速开始
1.安装
pip install drf-mcp-docs使用特定的模式生成器:
pip install drf-mcp-docs[spectacular] # recommended
pip install drf-mcp-docs[yasg]2.配置
添加到Django设置中:
INSTALLED_APPS = [
# ...
'rest_framework',
'drf_mcp_docs',
]这是基本用法。drf-mcp-docs会自动检测您的模式生成器。
3.跑步
stdio传输 (适用于本地AI工具,如Claude Code、Cursor等):
python manage.py runmcpserver --transport stdio可流式HTTP传输 (用于网络访问):
python manage.py runmcpserver --transport streamable-http --host 0.0.0.0 --port 8100检查配置 (验证设置、适配器和架构):
python manage.py checkmcpconfig4.连接您的AI工具
克劳德代码 (~/.claude.json):
{
"mcpServers": {
"my-api-docs": {
"command": "python",
"args": ["manage.py", "runmcpserver", "--transport", "stdio"],
"cwd": "/path/to/your/django/project"
}
}
}光标 (.cursor/mcp.json):
{
"mcpServers": {
"my-api-docs": {
"command": "python",
"args": ["manage.py", "runmcpserver", "--transport", "stdio"],
"cwd": "/path/to/your/django/project"
}
}
}AI代理可以做什么
一旦连接,您的AI编码代理可以:
You: "Show me all the product endpoints"
Agent: [reads api://endpoints resource, filters by tag]
You: "Generate a React hook to create a new product"
Agent: [calls get_endpoint_detail for POST /api/products/]
[calls get_request_example for the request body]
[calls generate_code_snippet with typescript + fetch]
→ Generates a complete, typed React hook with correct fields可用资源
| 资源URI | 描述 |
|---|---|
api://overview | API标题、版本、基本URL、身份验证摘要、标记、终结点计数 |
api://endpoints | 所有端点:路径、方法、摘要、标签(紧凑列表) |
api://endpoints/{method}/{path} | 一个端点的完整细节 |
api://schemas | 所有模式/模型定义(名称+字段摘要) |
api://schemas/{name} | 具有属性、类型和约束的完整架构 |
api://auth | 包含所有身份验证方法的身份验证指南 |
可用工具
| 工具 | 参数 | 说明 |
|---|---|---|
search_endpoints | query, method?, tag? | 按关键字搜索端点 |
get_endpoint_detail | path, method | 完整的端点文档 |
get_request_example | path, method | 请求正文和参数示例 |
get_response_example | path, method, status_code? | 示例响应 |
generate_code_snippet | path, method, language?, client? | 支持分页的集成代码(JS/TS、Python、cURL) |
list_schemas | -- | 所有数据模型名称和描述 |
get_schema_detail | name | 包含所有字段和类型的完整架构 |
配置
所有设置都是可选的。添加一个 DRF_MCP_DOCS dict到你的Django设置:
DRF_MCP_DOCS = {
# Server
'SERVER_NAME': 'my-api', # MCP server name (default: 'drf-mcp-docs')
'SERVER_INSTRUCTIONS': 'Custom prompt...', # Instructions shown to AI agents
# Schema
'SCHEMA_ADAPTER': None, # Auto-detect, or full dotted path
'SCHEMA_PATH_PREFIX': '/api/', # Only include endpoints under this prefix
'EXCLUDE_PATHS': ['/api/internal/'], # Paths to exclude
'CACHE_SCHEMA': not DEBUG, # Cache in production, refresh in dev
'CACHE_TTL': None, # Schema cache TTL in seconds (None = no expiry)
# Transport
'TRANSPORT': 'streamable-http', # Default transport: 'streamable-http' or 'stdio'
'MCP_ENDPOINT': '/mcp/', # URL path for HTTP transport
# Code generation
'DEFAULT_CODE_LANGUAGE': 'javascript', # 'javascript', 'typescript', or 'python'
'DEFAULT_HTTP_CLIENT': 'fetch', # 'fetch', 'axios', 'ky', 'requests', or 'httpx'
}架构适配器选择
drf-mcp-docs会按照以下优先级顺序自动检测您的模式生成器:
- drf壮观 (推荐)--最完整的OpenAPI 3.x输出
- yasg博士 --Swagger 2.0自动转换为OpenAPI 3.0
- DRF内置 --具有有限模式细节的基本回退
要强制使用特定适配器,请执行以下操作:
DRF_MCP_DOCS = {
'SCHEMA_ADAPTER': 'drf_mcp_docs.adapters.spectacular.SpectacularAdapter',
}ASGI集成
对于使用ASGI的项目,在Django应用程序旁边挂载MCP端点:
# asgi.py
from django.core.asgi import get_asgi_application
from drf_mcp_docs.urls import mount_mcp
django_app = get_asgi_application()
application = mount_mcp(django_app) # Mounts at /mcp/ by default使用自定义路径:
application = mount_mcp(django_app, path="/api-docs-mcp/")发展
git clone https://github.com/Abdulkhalek-1/drf-mcp-docs.git
cd drf-mcp-docs
pip install -e ".[dev]"
pytest运作原理
┌─────────────┐ ┌──────────────┐ ┌─────────────────┐ ┌───────────┐
│ AI Agent │────>│ MCP Server │────>│ Schema Processor │────>│ Adapter │
│ (Claude, │<────│ (FastMCP) │<────│ (OpenAPI dict │<────│ (spectac- │
│ Cursor...) │ │ │ │ → structured) │ │ ular, │
└─────────────┘ └──────────────┘ └─────────────────┘ │ yasg, │
Resources + Tools Dataclasses + Search │ DRF) │
└───────────┘- 适配器 从所选生成器中提取OpenAPI模式
- 处理器 将原始字典转换为结构化、人工智能友好的数据类
- MCP服务器 公开资源(可浏览文档)和工具(搜索、示例、代码生成)
- AI 代理 通过stdio或HTTP连接,读取API文档,帮助编写前端代码
许可证
MIT许可证。看 许可证 了解详情。
