django-mcp

django-mcp

Bridges Django applications with AI assistants by implementing the Model Context Protocol, enabling AI to interact with Django data and functionality through simple decorators.

Category
访问服务器

README

✨ Django-MCP 🔮

<div align="center">

Python 3.10+ Django 4.0+ License Status Style Types

Supercharge your Django apps with AI assistant capabilities

</div>

Django-MCP bridges the gap between Django applications and AI assistants by implementing the Model Context Protocol (MCP). With a few simple decorators, you can expose your app's data and functionality to AI assistants, creating richer, more capable AI interactions.

Want AI to help users explore your data, perform admin tasks, or create content? Django-MCP makes it magical! ✨💫🧙‍♀️

🌟 Features

  • 🔮 Seamless Integration - Add MCP to your Django project with minimal configuration
  • 🧩 Decorator-Based API - Intuitive decorators to expose functions, models, and views
  • 📊 Django ORM Integration - First-class support for Django models and querysets
  • 🛡️ Admin Integration - Expose admin actions and panels to AI assistants
  • 🌐 DRF Compatibility - Works with Django REST Framework viewsets and serializers
  • 🔍 Auto-Discovery - Automatically finds and registers MCP components in your apps
  • 🚀 ASGI Support - Built-in ASGI server for SSE-based MCP communication
  • ⚙️ Flexible Configuration - Configure through familiar Django settings
  • 🔒 Security-Focused - Safe by default with clear permission controls
  • 📝 Contextual Awareness - MCP server understands Django request context

🚀 Installation

# Install with pip
pip install django-mcp

# Or with UV
uv add django-mcp

⚡ Quick Start

1. Add to INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    # ... your other apps ...
    'django_mcp',
]

# MCP Configuration (optional)
DJANGO_MCP_SERVER_NAME = "My Awesome Django App"
DJANGO_MCP_INSTRUCTIONS = "This server provides access to my Django app."

2. Update your ASGI configuration

# asgi.py
from django_mcp.asgi import get_asgi_application

application = get_asgi_application()

3. Include MCP URLs

# urls.py
from django.urls import path, include

urlpatterns = [
    # ... your other URLs ...
    path('', include('django_mcp.urls')),
]

4. Create your first MCP tool

# myapp/mcp_tools.py - automatically discovered!
from django_mcp.decorators import mcp_tool
from django_mcp.context import Context
from myapp.models import Product

@mcp_tool()
def search_products(context: Context, query: str) -> list:
    """Search for products by name"""
    products = Product.objects.filter(name__icontains=query)[:10]
    return [
        {
            "id": p.id,
            "name": p.name,
            "price": str(p.price),
            "description": p.description,
        }
        for p in products
    ]

5. Expose a Django model

# myapp/models.py
from django.db import models
from django_mcp.decorators import mcp_model_tool, mcp_model_resource

class Product(models.Model):
    name = models.CharField(max_length=100)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    description = models.TextField()

    def __str__(self):
        return self.name

# Expose CRUD operations for Product model
@mcp_model_tool(Product)
def product_tools():
    pass

# Expose Product instances as resources
@mcp_model_resource(Product)
def product_resource():
    pass

6. Connect your AI assistant

Point your MCP-compatible AI assistant (like Claude) to:

http://yourdomain.com/mcp/

Now your AI assistant can use your Django app's functionality!

🧙‍♀️ Using with AI Assistants

  1. Start your Django server with Django-MCP installed
  2. Connect your AI assistant using the MCP protocol URI
  3. The AI can now invoke your tools and access your resources!

Example conversation with an AI:

User: "How many products do we have in our database priced under $20?"

AI: *uses the search_products tool to find this information*
"I found 15 products under $20. The most popular ones are Product A ($15.99),
Product B ($19.50), and Product C ($12.75). Would you like to see more details
about any of these products?"

💫 Examples

Admin Integration

from django_mcp.admin_tools import register_admin_tools
from myapp.admin import ProductAdmin
from myapp.models import Product

# Register all admin actions for Product
register_admin_tools(ProductAdmin, Product)

DRF Integration

from django_mcp.drf_tools import register_drf_viewset
from myapp.api.viewsets import ProductViewSet

# Expose all viewset actions as tools
register_drf_viewset(ProductViewSet)

Creating Resources

from django_mcp.decorators import mcp_resource
from myapp.models import Category

@mcp_resource("category://{slug}")
def get_category(slug: str) -> str:
    """Get information about a product category"""
    category = Category.objects.get(slug=slug)
    products = category.product_set.all()

    return f"""# Category: {category.name}

{category.description}

## Products in this category

{', '.join(p.name for p in products[:10])}

Total products: {products.count()}
"""

🛠️ Advanced Configuration

Django-MCP offers advanced configuration options through Django settings:

# settings.py

# Core settings
DJANGO_MCP_SERVER_NAME = "My Django App"
DJANGO_MCP_URL_PREFIX = "mcp"  # URL prefix for MCP endpoints
DJANGO_MCP_INSTRUCTIONS = "This server provides access to my Django app."
DJANGO_MCP_DEPENDENCIES = []  # MCP dependencies

# Discovery settings
DJANGO_MCP_AUTO_DISCOVER = True  # Auto-discover MCP components
DJANGO_MCP_EXPOSE_MODELS = True  # Auto-expose Django models
DJANGO_MCP_EXPOSE_ADMIN = True  # Auto-expose Django admin
DJANGO_MCP_EXPOSE_DRF = True  # Auto-expose DRF ViewSets

# Security settings
DJANGO_MCP_ALLOWED_ORIGINS = []  # CORS allowed origins for SSE endpoint

📊 Dashboard

Django-MCP includes a built-in dashboard at /mcp/dashboard/ that shows all registered tools, resources, and prompts.

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests (pytest)
  5. Commit your changes (git commit -m 'Add some amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

📝 License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.


<div align="center">

Created by Stefanie Jane 🌠

If you find Django-MCP useful, buy me a Monster Ultra Violet ⚡️

</div>

推荐服务器

Baidu Map

Baidu Map

百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。

官方
精选
JavaScript
Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
Audiense Insights MCP Server

Audiense Insights MCP Server

通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。

官方
精选
本地
TypeScript
VeyraX

VeyraX

一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。

官方
精选
本地
graphlit-mcp-server

graphlit-mcp-server

模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。

官方
精选
TypeScript
Kagi MCP Server

Kagi MCP Server

一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。

官方
精选
Python
e2b-mcp-server

e2b-mcp-server

使用 MCP 通过 e2b 运行代码。

官方
精选
Neon MCP Server

Neon MCP Server

用于与 Neon 管理 API 和数据库交互的 MCP 服务器

官方
精选
Exa MCP Server

Exa MCP Server

模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。

官方
精选