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 は即座に 1 つのアプリケーションにバインドされます。@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/ ディレクトリには、組み込みの認証 User、Streamable HTTP の MCP エンドポイント、および MCP を通じて Django ツールを検出して呼び出すスタンドアロンの OpenAI Responses API クライアントを使用した、実行可能な 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