An implementation of the Model Context Protocol for Django applications.
Django MCP Server
Django MCP Server is an implementation of the Model Context Protocol (MCP) extension for Django. This module allows MCP Clients and AI agents to interact with any Django application seamlessly.
π Django-Style declarative style tools to allow AI Agents and MCP clients tool to interact with Django. π Expose Django models for AI Agents and MCP Tools to query in 2 lines of code in a safe way. π Convert Django Rest Framework APIs to MCP tools with one annotation. β Working on both WSGI and ASGI without infrastructure change. β Validated as a Remote Integration with Claude AI. π€ Any MCP Client or AI Agent supporting MCP , (Google Agent Developement Kit, Claude AI, Claude Desktop ...) can interact with your application.
Many thanks π to all the contributor community
Maintained β¨ with care by Smart GTS software engineering.
Licensed under the MIT License.
Features
- Expose Django models and logic as MCP tools.
- Serve an MCP endpoint inside your Django app.
- Easily integrate with AI agents, MCP Clients, or tools like Google ADK.
Quick Start
1οΈβ£ Install
pip install django-mcp-server
Or directly from GitHub:
pip install git+https://github.com/omarbenhamid/django-mcp-server.git
2οΈβ£ Configure Django
β
Add mcp_server to your INSTALLED_APPS:
INSTALLED_APPS = [
# your apps...
'mcp_server',
]
β
Add the MCP endpoint to your urls.py:
from django.urls import path, include
urlpatterns = [
# your urls...
path("", include('mcp_server.urls')),
]
By default, the MCP endpoint will be available at /mcp.
3οΈβ£ Define MCP Tools
In mcp.py create a subclass of ModelQueryToolset to give access to a model :
from mcp_server import ModelQueryToolset
from .models import *
class BirdQueryTool(ModelQueryToolset):
model = Bird
def get_queryset(self):
"""self.request can be used to filter the queryset"""
return super().get_queryset().filter(location__isnull=False)
class LocationTool(ModelQueryToolset):
model = Location
class CityTool(ModelQueryToolset):
model = City
Or create a sub class of MCPToolset to publish generic methods (private _ methods are not published)
Example:
from mcp_server import MCPToolset
from django.core.mail import send_mail
class MyAITools(MCPToolset):
def add(self, a: int, b: int) -> list[dict]:
"""A service to add two numbers together"""
return a+b
def send_email(self, to_email: str, subject: str, body: str):
""" A tool to send emails"""
send_mail(
subject=subject,
message=body,
from_email='your_email@example.com',
recipient_list=[to_email],
fail_silently=False,
)
Verify with MCP Inspect
Use the management commande mcp_inspect to ensure your tools are correctly declared :
python manage.py mcp_inspect
Use the MCP with any MCP Client
The mcp tool is now published on your Django App at /mcp endpoint.
IMPORTANT For production setup, on non-public data, consider enabling authorization through : DJANGO_MCP_AUTHENTICATION_CLASSES
Test with MCP Python SDK
You can test it with the python mcp SDK :
from mcp.client.streamable_http import streamablehttp_client
from mcp import ClientSession
async def main():
# Connect to a streamable HTTP server
async with streamablehttp_client("http://localhost:8000/mcp") as (
read_stream,
write_stream,
_,
):
# Create a session using the client streams
async with ClientSession(read_stream, write_stream) as session:
# Initialize the connection
await session.initialize()
# Call a tool
tool_result = await session.call_tool("get_alerts", {"state": "NY"})
print(tool_result)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
Replace http://localhost:8000/mcp by the acutal Django host and run this cript.
Use from Claude AI
As of June 2025 Claude AI support now MCPs through streamable HTTP protocol with preè-requisites :
*
- Setup OAuth2, for example :
Tools (2)
ModelQueryToolsetExposes Django models for AI agents to query data safely.MCPToolsetPublishes generic Python methods as tools for AI agents.Environment Variables
DJANGO_MCP_AUTHENTICATION_CLASSESAuthentication classes for securing the MCP endpoint in production.Configuration
{ "mcpServers": { "django": { "command": "python", "args": ["manage.py", "mcp_inspect"] } } }