django-native-mcp
django-native-mcp
一个受 Celery 启发的小型 Django 原生 MCP 应用与工具注册框架。它将协议处理、模式、验证、序列化、stdio 和 Streamable HTTP 委托给官方的 mcp Python SDK。
这不是另一个 FastMCP 的封装,依赖项仅包含原生 Python SDK
安装
pip install django-native-mcp添加应用及其配置:
# settings.py
INSTALLED_APPS = [
# ...
"django_native_mcp",
]
DJANGO_NATIVE_MCP = {
"APP": "config.mcp:app",
}创建应用:
# config/mcp.py
from django_native_mcp import MCP
app = MCP("backend")
app.autodiscover_tools()在已安装的 Django 应用中显式声明工具:
# orders/mcp.py
from django_native_mcp import shared_tool
from .models import Order
@shared_tool
async def get_order(order_id: int) -> dict:
"""Get an order."""
order = await Order.objects.aget(pk=order_id)
return {"id": order.pk, "status": order.status}注册名称为 orders.get_order,使用 Django 应用标签。
python manage.py mcp_list
python manage.py mcp_inspect orders.get_order
python manage.py mcp_call orders.get_order '{"order_id": 1}'
python manage.py mcp_serve --transport stdio工具必须使用 async def。框架不会添加隐式线程或 sync_to_async。
Related MCP server: django-mcp
直接应用工具
from django_native_mcp import MCP
app = MCP("backend")
@app.tool(name="system.health")
async def health() -> dict:
return {"ok": True}@app.tool 会立即绑定到一个应用。@shared_tool 在自动发现绑定之前保持与应用无关。
在 Django 中使用 Streamable HTTP
官方 SDK ASGI 应用可以单独提供服务:
application = app.asgi_app()或者将 /mcp 路由到 MCP,其余请求路由到 Django:
# config/asgi.py
import os
from django.core.asgi import get_asgi_application
from django_native_mcp.asgi import MCPApplication
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
django_application = get_asgi_application()
# Import the MCP app only after Django's application registry is ready.
from config.mcp import app as mcp_app
application = MCPApplication(
django=django_application,
mcp=mcp_app,
mcp_path="/mcp",
)调度器将 ASGI 生命周期转发给官方 MCP 应用,因此其传输生命周期由外部 ASGI 服务器启动和停止。
测试
使用官方进程内客户端的轻量封装:
from django_native_mcp.testing import MCPTestClient
async with MCPTestClient(app) as client:
result = await client.call_tool("orders.get_order", {"order_id": 1})端到端示例
example/ 目录包含一个可运行的 Django 项目,使用内置的认证 User、一个 Streamable HTTP MCP 端点,以及一个独立的 OpenAI Responses API 客户端,该客户端通过 MCP 发现并调用 Django 工具。
架构
Django apps / mcp.py
↓
shared_tool → ToolDefinition → ToolRegistry → MCP
↓
official MCPServer
↙ ↘
stdio Streamable HTTP注册表是进程本地的,在其官方服务器创建后变为只读。每个工作进程在启动时从源码构建相同的注册表。
非目标
本包不是 MCP 协议实现、ORM 到 MCP 的生成器、REST/DRF 适配器、Celery 替代品、后台队列或 AI 代理框架。它不会自动暴露 Django 模型,也不会从中推断权限。
许可证
MIT
This server cannot be deployed
Maintenance
Related MCP Connectors
MCP server for progressive tool usage at any scale (see https://klavis.ai)
Build multi-tenant apps over MCP. Schemas, CRUD, deploys — access control enforced server-side.
An MCP server that let you interact with Cycloid.io Internal Development Portal and Platform
Build, deploy, and host full-stack web apps from any MCP client. DB, auth, storage, cron included.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceA Django MCP server that exposes tools and resources to AI agents using simple decorators, with auto-discovery, type safety, and custom authentication.MIT
- AlicenseNot gradedqualityFmaintenanceIntegrates MCP tool hosting into Django applications, enabling easy definition and serving of MCP tools, resources, and prompts via ASGI with support for URL path parameters and logging.771 PyPI72MIT
- AlicenseNot gradedqualityCmaintenanceA Model Context Protocol (MCP) server for developing Django applications. It exposes Django project information through MCP tools, enabling AI assistants to better understand and interact with Django codebases.113MIT
- AlicenseNot gradedqualityCmaintenanceExposes Django REST Framework APIs as MCP tools for AI agents via the Model Context Protocol, with automatic discovery and security.1MIT