Django MCP集成工具

Django MCP集成工具是一个用于在Django框架中透明集成MCP(Model Context Protocol)的开发工具,支持通过FastMCP进行高效集成。

  • by mosco23
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 mosco23/django-mcp-integration

使用说明

Django MCP集成

通过FastMCP将模型上下文协议(MCP)无缝集成到Django中。

安装

pip install django-mcp-integration

配置Django

  • 修改文件 settings.py
# settings.py

INSTALLED_APPS = [
    # ...
    'django_mcp_integration',
]


# Optionnel

MCP_SERVER_NAME = "Mon Serveur MCP"
MCP_HOST = "localhost"
MCP_PORT = 8000
MCP_HTTP_PATH = "/mcp/"
MCP_ENABLED  = True
MCP_SERVER_INSTRUCTIONS = None
MCP_SERVER_VERSION = "1.0.0"

  • 修改文件 asgi.py
# asgi.py


import os

# from django.core.asgi import get_asgi_application
from django_mcp_integration.handlers.asgi import get_mcp_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_project.settings')

application = get_mcp_asgi_application()

在工具中使用

# blog/tools.py or blog/mcp_tools.py
from django_mcp_integration import mcp_tool


@mcp_tool(
    name="add",
    description="make a + b",
)
async def add(a: int, b: int) -> int:
    return a + b


@mcp_tool(
    name="create_post",
    description="create post by IA"
)
class CreatePostTool:

    # optional
    def check_permission(self, obj):
        pass

    # required method
    async def execute(self, title: str, content: str):
        pass

启动服务器


uvicorn django_mcp_test.asgi:application