### Start FastMCP Server (`runmcp`) Source: https://context7.com/orco82/django-drf-mcp/llms.txt Use `runmcp` to start a standalone FastMCP server. Configure the transport method (stdio, streamable-http, sse) and network options like host and port. For remote Django servers, specify the `--base-url`. ```bash python manage.py runmcp --transport stdio ``` ```bash python manage.py runmcp --transport streamable-http --host 0.0.0.0 --port 8001 ``` ```bash python manage.py runmcp --transport sse --host 0.0.0.0 --port 8001 ``` ```bash python manage.py runmcp --transport stdio --base-url https://my-app.example.com ``` -------------------------------- ### Standalone SSE Server Configuration Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure a standalone FastMCP server using SSE. This example shows basic setup and how to include authentication headers. ```json { "mcpServers": { "my-app": { "type": "sse", "url": "http://localhost:8001/sse" } } } ``` ```json { "mcpServers": { "my-app": { "type": "sse", "url": "http://localhost:8001/sse", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### Install django-drf-mcp Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Install the package using pip. Dependencies are installed automatically. ```bash pip install django-drf-mcp ``` -------------------------------- ### Include Specific Resources Source: https://context7.com/orco82/django-drf-mcp/llms.txt This configuration selectively exposes only specific resources, such as GET requests for products and POST requests for orders. ```python DJANGO_MCP = { "INCLUDE": [ "GET:/api/products/*", "POST:/api/orders/", ], } ``` -------------------------------- ### Configure ProductViewSet Permissions Source: https://context7.com/orco82/django-drf-mcp/llms.txt This example sets permissions for a DRF ModelViewSet. It requires users to be authenticated for browser access and allows bypass via an internal token for MCP proxy calls. ```python # myapp/views.py from rest_framework import viewsets from rest_framework.permissions import IsAuthenticated from django_drf_mcp.permissions import IsMCPInternalRequest from .models import Product from .serializers import ProductSerializer class ProductViewSet(viewsets.ModelViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer # Browser: must be authenticated; MCP proxy: passes via internal token permission_classes = [IsAuthenticated | IsMCPInternalRequest] ``` -------------------------------- ### Full django-drf-mcp Configuration Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Use this for a comprehensive setup, enabling documentation features and defining API details. ```python DJANGO_MCP = { "NAME": "products-api", "BASE_URL": "http://localhost:8000", "MCP_PATH": "/mcp/", "HEADERS": { "Authorization": "Token abc123...", }, "MCP_DOCS_ENABLED": True, "MCP_DOCS_TITLE": "Products MCP Tools", "MCP_DOCS_DESCRIPTION": "MCP tools for the Products API", "MCP_DOCS_EMOJI": "\U0001f6d2", "MCP_DOCS_LINKS": [ {"text": "Admin", "url": "/admin/"}, {"text": "GitHub", "url": "https://github.com/myorg/myrepo"}, ], "MCP_DOCS_ENABLE_CORS": True, "MCP_DOCS_VERBOSE": False, "SWAGGER_ENABLED": True, } ``` -------------------------------- ### Run MCP with STDIO transport Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Start the MCP server using the 'runmcp' management command with the 'stdio' transport. ```bash python manage.py runmcp --transport stdio ``` -------------------------------- ### Run MCP Management Command Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Use the `runmcp` management command to start the MCP server with various transport and binding options. ```bash python manage.py runmcp [options] ``` -------------------------------- ### Run MCP with Streamable HTTP transport Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Start the MCP server using the 'runmcp' management command with the 'streamable-http' transport, specifying host and port. ```bash python manage.py runmcp --transport streamable-http --host 0.0.0.0 --port 8001 ``` -------------------------------- ### Standalone Server Configuration Source: https://context7.com/orco82/django-drf-mcp/llms.txt Examples of running the django-drf-mcp server in standalone mode with different transports and ports. ```APIDOC ## Run standalone Streamable HTTP on port 8001 server.run(transport="streamable-http", host="0.0.0.0", port=8001) ## Run standalone SSE on port 8001 server.run(transport="sse", host="0.0.0.0", port=8001) ``` -------------------------------- ### Filter Endpoints with INCLUDE Source: https://context7.com/orco82/django-drf-mcp/llms.txt Configure which DRF endpoints are exposed as MCP tools using glob patterns. This example only exposes read-only (GET) endpoints under the /api/ path. ```python DJANGO_MCP = { "INCLUDE": ["GET:/api/*"], } ``` -------------------------------- ### Run MCP with SSE transport Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Start the MCP server using the 'runmcp' management command with the 'sse' transport, specifying host and port. ```bash python manage.py runmcp --transport sse --host 0.0.0.0 --port 8001 ``` -------------------------------- ### Include Specific Endpoints Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Use the INCLUDE pattern to expose only GET endpoints under /api/. ```python # Only expose GET endpoints under /api/ DJANGO_MCP = { "INCLUDE": ["GET:/api/*"], } ``` -------------------------------- ### Django Settings Configuration (DJANGO_MCP) Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure the django-drf-mcp package by adding the DJANGO_MCP dictionary to your settings.py. This example shows all available settings. ```python DJANGO_MCP = { # --- Server --- "NAME": "django-drf-mcp", # MCP server name "BASE_URL": "http://localhost:8000", # Django server URL "MCP_PATH": "/mcp/", # MCP endpoint path # --- Endpoint Filtering --- "INCLUDE": [], # Only expose matching METHOD:PATH patterns "EXCLUDE": [], # Remove matching METHOD:PATH patterns # --- Authentication --- "HEADERS": {}, # HTTP headers sent with every MCP tool call "INTERNAL_TOKEN": "", # Shared secret for MCP internal requests (optional) # --- MCP Docs UI --- "MCP_DOCS_ENABLED": True, # Enable/disable /mcp/docs "MCP_DOCS_TITLE": None, # Docs page title (defaults to NAME) "MCP_DOCS_DESCRIPTION": None, # Docs description (defaults to OpenAPI description) "MCP_DOCS_VERSION": None, # Docs version (defaults to OpenAPI version) "MCP_DOCS_EMOJI": None, # Emoji shown before the title "MCP_DOCS_LINKS": [], # Extra links: [{"text": "...", "url": "..."}] "MCP_DOCS_ENABLE_CORS": True, # Enable CORS on docs routes "MCP_DOCS_VERBOSE": True, # Show tools load during server startup # --- DRF Swagger / Schema --- "SWAGGER_ENABLED": False, # Enable Swagger UI + OpenAPI schema endpoints "SCHEMA_PATH": "/api/schema/", # Schema endpoint path "SWAGGER_PATH": "/api/docs/", # Swagger UI path } ``` -------------------------------- ### Filter Endpoints with EXCLUDE Source: https://context7.com/orco82/django-drf-mcp/llms.txt Configure which DRF endpoints are exposed as MCP tools using glob patterns. This example exposes everything except DELETE operations. ```python DJANGO_MCP = { "EXCLUDE": ["DELETE:*"], } ``` -------------------------------- ### Retrieve Active Internal Token Source: https://context7.com/orco82/django-drf-mcp/llms.txt This function retrieves the current MCP internal token. It can be used to verify requests manually, for example, in tests. ```python from django_drf_mcp.tokens import get_token, HEADER_NAME token = get_token() print(HEADER_NAME) # "X-MCP-Internal-Token" print(token[:8]) # e.g. "a3f9c1b2..." (256-bit hex) # Verify a request manually (e.g. in tests) import hmac incoming = request.META.get("HTTP_X_MCP_INTERNAL_TOKEN", "") is_valid = hmac.compare_digest(incoming, get_token()) ``` -------------------------------- ### Enable MCP Docs UI with ASGI Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Replace your asgi.py to enable the interactive MCP Docs UI. Requires running with an ASGI server like uvicorn. ```python # asgi.py import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") django.setup() from django_drf_mcp.asgi import get_asgi_application application = get_asgi_application() ``` -------------------------------- ### Configure INSTALLED_APPS and include URLs Source: https://context7.com/orco82/django-drf-mcp/llms.txt Add 'django_drf_mcp' to INSTALLED_APPS and include its URLs in your project's urls.py. drf-spectacular is automatically injected. ```python INSTALLED_APPS = [ "django.contrib.admin", "django.contrib.auth", "rest_framework", "django_drf_mcp", # drf_spectacular is injected automatically "myapp", ] ``` ```python from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), path("api/", include("myapp.urls")), path("", include("django_drf_mcp.urls")), # mounts /mcp/ ] ``` -------------------------------- ### get_asgi_application() - ASGI Entry Point Source: https://context7.com/orco82/django-drf-mcp/llms.txt Mounts Django and the FastMCP Docs Starlette app under a single ASGI entry point, routing specific paths to the MCP app. ```APIDOC ## `get_asgi_application()` — ASGI Entry Point with MCP Docs Mounts Django and the FastMCP Docs Starlette app under a single ASGI entry point. Routes `/mcp/docs`, `/mcp/api/*`, `/mcp/openapi.json`, and `/mcp/favicon` to the FastMCP Starlette sub-app; everything else goes to Django. ```python # asgi.py import os import django os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings") django.setup() from django_drf_mcp.asgi import get_asgi_application application = get_asgi_application() ``` Run with: ```bash uvicorn myproject.asgi:application --host 0.0.0.0 --port 8000 ``` Available routes after startup: | Path | Description | |---|---| | `/mcp/` | MCP JSON-RPC endpoint (POST) / health check (GET) | | `/mcp/docs` | Interactive MCP Docs UI | | `/mcp/openapi.json` | MCP OpenAPI schema | | `/mcp/api/tools` | MCP tools list (JSON) | | `/mcp/api/tools/{tool_name}` | MCP tool detail (JSON) | | `/api/schema/` | DRF OpenAPI schema (when `SWAGGER_ENABLED=True`) | | `/api/docs/` | DRF Swagger UI (when `SWAGGER_ENABLED=True`) | ``` -------------------------------- ### Call an MCP Tool Source: https://context7.com/orco82/django-drf-mcp/llms.txt This curl command demonstrates how to call an MCP tool, specifically 'get_api_products_list', with empty arguments. ```bash # Call an MCP tool (e.g. list products) curl -X POST http://localhost:8000/mcp/ \ -H "Content-Type: application/json" \ -H "Authorization: Token abc123..." \ -d '{ "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "get_api_products_list", "arguments": {} } }' ``` -------------------------------- ### Run Standalone Streamable HTTP Server Source: https://context7.com/orco82/django-drf-mcp/llms.txt Use this to run the server with streamable HTTP transport on a specified port. ```python server.run(transport="streamable-http", host="0.0.0.0", port=8001) ``` -------------------------------- ### Run Uvicorn with Custom ASGI Application Source: https://context7.com/orco82/django-drf-mcp/llms.txt This command runs the Uvicorn server using the custom ASGI application defined in your project's asgi.py file. ```bash uvicorn myproject.asgi:application --host 0.0.0.0 --port 8000 ``` -------------------------------- ### Run Standalone SSE Server Source: https://context7.com/orco82/django-drf-mcp/llms.txt Use this to run the server with Server-Sent Events (SSE) transport on a specified port. ```python server.run(transport="sse", host="0.0.0.0", port=8001) ``` -------------------------------- ### Build FastMCP Server with create_mcp_server() Source: https://context7.com/orco82/django-drf-mcp/llms.txt Instantiate a FastMCP server instance connected to the DRF OpenAPI schema. The server can be run with various transports like STDIO. ```python from django_drf_mcp.server import create_mcp_server # Use settings defaults server = create_mcp_server() # Override base URL (e.g. for runmcp --base-url) server = create_mcp_server( base_url="https://my-app.example.com", name="my-api-prod", ) # Run over STDIO (for Claude Desktop / Claude Code) server.run(transport="stdio") ``` -------------------------------- ### Configure DJANGO_MCP Settings Source: https://context7.com/orco82/django-drf-mcp/llms.txt Customize server behavior, endpoint filtering, authentication, and MCP Docs UI with the DJANGO_MCP dictionary in settings.py. ```python DJANGO_MCP = { # --- Server --- "NAME": "products-api", # shown in health check and docs "BASE_URL": "http://localhost:8000", # Django server URL for proxying "MCP_PATH": "/mcp/", # MCP endpoint mount path # --- Endpoint Filtering --- "INCLUDE": ["GET:/api/*"], # only expose matching METHOD:PATH globs "EXCLUDE": ["DELETE:*"], # remove matching METHOD:PATH globs # --- Authentication --- "HEADERS": { "Authorization": "Token abc123...", # sent with every tool call }, "INTERNAL_TOKEN": "strong-random-secret", # required for multi-worker deployments # --- MCP Docs UI (requires ASGI) --- "MCP_DOCS_ENABLED": True, "MCP_DOCS_TITLE": "Products MCP Tools", "MCP_DOCS_DESCRIPTION": "MCP tools for the Products API", "MCP_DOCS_VERSION": "1.2.0", "MCP_DOCS_EMOJI": "🛒", "MCP_DOCS_LINKS": [ {"text": "Admin", "url": "/admin/"}, {"text": "GitHub", "url": "https://github.com/myorg/myrepo"}, ], "MCP_DOCS_ENABLE_CORS": True, "MCP_DOCS_VERBOSE": False, # --- DRF Swagger (optional) --- "SWAGGER_ENABLED": True, "SCHEMA_PATH": "/api/schema/", "SWAGGER_PATH": "/api/docs/", } ``` -------------------------------- ### List Available MCP Tools Source: https://context7.com/orco82/django-drf-mcp/llms.txt This curl command requests a list of available MCP tools by sending a 'tools/list' method to the MCP endpoint. ```bash # List available MCP tools curl -X POST http://localhost:8000/mcp/ \ -H "Content-Type: application/json" \ -H "Authorization: Token abc123..." \ -d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}' ``` -------------------------------- ### Configure MCP Client Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Define the MCP server configuration for the client, including the server URL and authorization headers. ```json // .mcp.json (MCP client) { "mcpServers": { "my-api": { "type": "streamable-http", "url": "http://localhost:8000/mcp/", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### Read Resolved Configuration with get_config() Source: https://context7.com/orco82/django-drf-mcp/llms.txt Retrieve the merged configuration dictionary, combining user overrides with default settings. Useful for inspecting effective settings at runtime. ```python from django_drf_mcp.server import get_config config = get_config() print(config["NAME"]) print(config["BASE_URL"]) print(config["INCLUDE"]) print(config["HEADERS"]) ``` -------------------------------- ### Client-side Authentication Configuration Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure client-side authentication headers for MCP clients accessing the /mcp/ endpoint. ```json { "mcpServers": { "my-app": { "type": "streamable-http", "url": "http://localhost:8000/mcp/", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### MCP Client Configuration - STDIO Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure the MCP client to use the STDIO transport, either locally or with a remote Django server. ```json { "mcpServers": { "my-app": { "command": "python", "args": ["manage.py", "runmcp", "--transport", "stdio"] } } } ``` ```json { "mcpServers": { "my-app": { "command": "python", "args": ["manage.py", "runmcp", "--transport", "stdio", "--base-url", "https://my-app.example.com"] } } } ``` -------------------------------- ### MCP Tools List API Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md JSON API to list available MCP tools. ```APIDOC ## GET /mcp/api/tools ### Description MCP tools JSON API (list). ### Method GET ### Endpoint /mcp/api/tools ``` -------------------------------- ### Add django-drf-mcp to INSTALLED_APPS Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Add 'django_drf_mcp' to your project's INSTALLED_APPS in settings.py. drf-spectacular is auto-injected. ```python # settings.py INSTALLED_APPS = [ ... "rest_framework", "django_drf_mcp", ] ``` -------------------------------- ### Configure DRF for Token Authentication Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Set up Django REST Framework to use Token Authentication and ensure authenticated users have IsAuthenticated permission. ```python # settings.py REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": [ "rest_framework.authentication.TokenAuthentication", ], "DEFAULT_PERMISSION_CLASSES": [ "rest_framework.permissions.IsAuthenticated", ], } DJANGO_MCP = { "NAME": "my-api", "HEADERS": { "Authorization": "Token abc123...", }, } ``` -------------------------------- ### MCP Docs UI Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Provides access to the interactive documentation UI for MCP. ```APIDOC ## GET /mcp/docs ### Description MCP Docs UI. ### Method GET ### Endpoint /mcp/docs ``` -------------------------------- ### Server-side Authentication: Basic Auth Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure Basic Authentication headers for MCP tool calls to the Django API. ```python # Basic Auth DJANGO_MCP = { "HEADERS": { "Authorization": "Basic dXNlcjpwYXNz", }, } ``` -------------------------------- ### MCP Client Configuration: SSE Source: https://context7.com/orco82/django-drf-mcp/llms.txt Configure the MCP client for Server-Sent Events (SSE) transport. Provide the URL and any required headers. ```json { "mcpServers": { "my-api": { "type": "sse", "url": "http://localhost:8001/sse", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### Combined INCLUDE and EXCLUDE for Endpoint Filtering Source: https://context7.com/orco82/django-drf-mcp/llms.txt This configuration exposes all /api/ endpoints but specifically blocks user and admin deletion operations. ```python DJANGO_MCP = { "INCLUDE": ["*:/api/*"], "EXCLUDE": ["DELETE:/api/users/*", "DELETE:/api/admin/*"], } ``` -------------------------------- ### MCP Initialize Request Source: https://context7.com/orco82/django-drf-mcp/llms.txt This curl command sends an 'initialize' request to the MCP endpoint. It includes necessary headers and a JSON payload for initialization. ```bash # MCP initialize curl -X POST http://localhost:8000/mcp/ \ -H "Content-Type: application/json" \ -H "Authorization: Token abc123..." \ -d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}' # Response: # {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{"listChanged":false}},"serverInfo":{"name":"django-drf-mcp","version":"1.0.0"}}} ``` -------------------------------- ### Generate OpenAPI Schema with generate_openapi_schema() Source: https://context7.com/orco82/django-drf-mcp/llms.txt Create a Python dictionary representing the OpenAPI schema from all registered DRF views using drf-spectacular. Useful for inspection or custom tool building. ```python from django_drf_mcp.server import generate_openapi_schema schema = generate_openapi_schema() # Inspect available paths for path, methods in schema["paths"].items(): print(path, list(methods.keys())) ``` -------------------------------- ### Minimal django-drf-mcp Configuration Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md A basic configuration to disable MCP documentation features. ```python DJANGO_MCP = { "NAME": "my-api", "BASE_URL": "https://my-api.example.com", "MCP_DOCS_ENABLED": False, } ``` -------------------------------- ### Adding IsMCPInternalRequest Permission Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Combine IsAuthenticated with IsMCPInternalRequest for views accessible by both browser users and MCP tools. ```python from rest_framework.permissions import IsAuthenticated from django_drf_mcp.permissions import IsMCPInternalRequest class MyViewSet(viewsets.ModelViewSet): permission_classes = [IsAuthenticated | IsMCPInternalRequest] ``` -------------------------------- ### Endpoint Filtering with INCLUDE/EXCLUDE Source: https://context7.com/orco82/django-drf-mcp/llms.txt Demonstrates how to control which DRF endpoints are exposed as MCP tools using glob patterns. ```APIDOC ## Endpoint Filtering — `INCLUDE` / `EXCLUDE` Control which DRF endpoints are exposed as MCP tools using glob patterns with the format "METHOD:PATH". ```python # Only expose read-only (GET) endpoints under /api/ DJANGO_MCP = { "INCLUDE": ["GET:/api/*"], } # Expose everything except DELETE operations DJANGO_MCP = { "EXCLUDE": ["DELETE:*"], } # Expose /api/ endpoints but block user deletion DJANGO_MCP = { "INCLUDE": ["*: /api/*"], "EXCLUDE": ["DELETE:/api/users/*", "DELETE:/api/admin/*"], } # Only expose specific resources DJANGO_MCP = { "INCLUDE": [ "GET:/api/products/*", "POST:/api/orders/", ], } ``` ``` -------------------------------- ### MCP Client Configuration: Streamable HTTP Source: https://context7.com/orco82/django-drf-mcp/llms.txt Configure the MCP client for Streamable HTTP transport. Specify the URL and any necessary headers, such as Authorization. ```json { "mcpServers": { "my-api": { "type": "streamable-http", "url": "http://localhost:8000/mcp/", "headers": { "Authorization": "Token abc123..." } } } } ``` ```json { "mcpServers": { "my-api": { "type": "streamable-http", "url": "http://localhost:8001/mcp", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### Include django-drf-mcp URLs Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Include the django-drf-mcp URLs in your project's urls.py to make the MCP endpoint live at /mcp/. ```python # urls.py from django.urls import path, include urlpatterns = [ ... path("", include("django_drf_mcp.urls")), ] ``` -------------------------------- ### Django Settings for Authentication Source: https://context7.com/orco82/django-drf-mcp/llms.txt Configure Django REST Framework for Token Authentication and ensure IsAuthenticated permission. Set DJANGO_MCP settings, including `INTERNAL_TOKEN` for multi-worker environments. ```python # settings.py REST_FRAMEWORK = { "DEFAULT_AUTHENTICATION_CLASSES": [ "rest_framework.authentication.TokenAuthentication", ], "DEFAULT_PERMISSION_CLASSES": [ "rest_framework.permissions.IsAuthenticated", ], } DJANGO_MCP = { "NAME": "my-api", "BASE_URL": "http://localhost:8000", # Sent by the MCP proxy when calling DRF endpoints "HEADERS": { "Authorization": "Token abc123...", }, # Required when running multiple workers or standalone runmcp "INTERNAL_TOKEN": "Xk9mP2vQwRtLn8cBjYeH5sA0dFuG3iZ1", } ``` -------------------------------- ### DRF Swagger UI Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Provides access to the Swagger UI for DRF API documentation. ```APIDOC ## GET /api/docs/ ### Description DRF Swagger UI. ### Method GET ### Endpoint /api/docs/ ``` -------------------------------- ### MCP Client Configuration - Streamable HTTP (Standalone) Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure the MCP client for Streamable HTTP transport when using a standalone FastMCP server on a separate port. ```json { "mcpServers": { "my-app": { "type": "streamable-http", "url": "http://localhost:8001/mcp" } } } ``` ```json { "mcpServers": { "my-app": { "type": "streamable-http", "url": "http://localhost:8001/mcp", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### Server-side Authentication: Custom API Key Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure a custom API key header for MCP tool calls to the Django API. ```python # Custom API key header DJANGO_MCP = { "HEADERS": { "X-API-Key": "my-secret-key", }, } ``` -------------------------------- ### MCP Docs Favicon Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Provides the favicon for the MCP documentation UI. ```APIDOC ## GET /mcp/favicon ### Description MCP Docs favicon. ### Method GET ### Endpoint /mcp/favicon ``` -------------------------------- ### MCP Tool Detail API Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md JSON API to retrieve details for a specific MCP tool. ```APIDOC ## GET /mcp/api/tools/{tool_name} ### Description MCP tool detail JSON API. ### Method GET ### Endpoint /mcp/api/tools/{tool_name} ### Parameters #### Path Parameters - **tool_name** (string) - Required - The name of the tool to retrieve details for. ``` -------------------------------- ### get_token() - Retrieve Internal Token Source: https://context7.com/orco82/django-drf-mcp/llms.txt How to retrieve the active MCP internal token using the `get_token()` function and verify requests manually. ```APIDOC ## `get_token()` — Retrieve the Active Internal Token Returns the current MCP internal token — either from `DJANGO_MCP["INTERNAL_TOKEN"]` or the auto-generated per-process 256-bit hex token. ```python from django_drf_mcp.tokens import get_token, HEADER_NAME token = get_token() print(HEADER_NAME) # "X-MCP-Internal-Token" print(token[:8]) # e.g. "a3f9c1b2..." (256-bit hex) # Verify a request manually (e.g. in tests) import hmac incoming = request.META.get("HTTP_X_MCP_INTERNAL_TOKEN", "") is_valid = hmac.compare_digest(incoming, get_token()) ``` ``` -------------------------------- ### MCP OpenAPI Schema Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Serves the OpenAPI schema for the MCP API in JSON format. ```APIDOC ## GET /mcp/openapi.json ### Description MCP OpenAPI schema. ### Method GET ### Endpoint /mcp/openapi.json ``` -------------------------------- ### MCP Client Configuration - Streamable HTTP (Embedded) Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure the MCP client for Streamable HTTP transport when embedded within the same Django application port. ```json { "mcpServers": { "my-app": { "type": "streamable-http", "url": "http://localhost:8000/mcp/" } } } ``` ```json { "mcpServers": { "my-app": { "type": "streamable-http", "url": "http://localhost:8000/mcp/", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### Server-side Authentication: JWT Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure JWT (e.g., SimpleJWT) headers for MCP tool calls to the Django API. ```python # JWT (e.g. SimpleJWT) DJANGO_MCP = { "HEADERS": { "Authorization": "Bearer eyJ...", }, } ``` -------------------------------- ### Combined Include and Exclude Endpoint Filtering Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Filter endpoints by including /api/* and then excluding DELETE operations on /api/users/*. ```python # Expose /api/ endpoints, but exclude DELETE on users DJANGO_MCP = { "INCLUDE": ["*:/api/*"], "EXCLUDE": ["DELETE:/api/users/*"], } ``` -------------------------------- ### Shared Internal Token for Multi-worker Deployments Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Set a shared INTERNAL_TOKEN for multi-worker or standalone runmcp deployments. ```python DJANGO_MCP = { "INTERNAL_TOKEN": "your-shared-secret-here", # Use a strong random value } ``` -------------------------------- ### MCP Client Authentication for DRF Endpoint Source: https://context7.com/orco82/django-drf-mcp/llms.txt Configure the MCP client's `.mcp.json` file to send authentication headers when connecting to the `/mcp/` DRF endpoint. ```json // .mcp.json — MCP client sends auth to /mcp/ endpoint { "mcpServers": { "my-api": { "type": "streamable-http", "url": "http://localhost:8000/mcp/", "headers": { "Authorization": "Token abc123..." } } } } ``` -------------------------------- ### IsMCPInternalRequest Permission Class Source: https://context7.com/orco82/django-drf-mcp/llms.txt Details on using the `IsMCPInternalRequest` permission class for session auth bypass via `X-MCP-Internal-Token` header. ```APIDOC ## `IsMCPInternalRequest` — Permission Class for Session Auth Bypass A DRF `BasePermission` that grants access when a valid `X-MCP-Internal-Token` header is present. Combine with `|` (OR) so browser users still authenticate normally while MCP tool calls bypass session auth. ```python # myapp/views.py from rest_framework import viewsets from rest_framework.permissions import IsAuthenticated from django_drf_mcp.permissions import IsMCPInternalRequest from .models import Product from .serializers import ProductSerializer class ProductViewSet(viewsets.ModelViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer # Browser: must be authenticated; MCP proxy: passes via internal token permission_classes = [IsAuthenticated | IsMCPInternalRequest] ``` For multi-worker or standalone `runmcp` deployments, set a shared `INTERNAL_TOKEN`: ```python # settings.py — generate with: python -c "import secrets; print(secrets.token_urlsafe(32))" DJANGO_MCP = { "INTERNAL_TOKEN": "Xk9mP2vQwRtLn8cBjYeH5sA0dFuG3iZ1", } ``` ``` -------------------------------- ### DRF OpenAPI Schema Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Serves the OpenAPI schema for the DRF API in YAML format. ```APIDOC ## GET /api/schema/ ### Description DRF OpenAPI schema (YAML). ### Method GET ### Endpoint /api/schema/ ``` -------------------------------- ### Server-side Authentication: DRF Token Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Configure DRF TokenAuthentication headers for MCP tool calls to the Django API. ```python # DRF TokenAuthentication DJANGO_MCP = { "HEADERS": { "Authorization": "Token abc123...", }, } ``` -------------------------------- ### Set Django MCP Internal Token Source: https://context7.com/orco82/django-drf-mcp/llms.txt Configure a shared internal token for multi-worker or standalone runmcp deployments. Generate a secure token using the provided Python command. ```python # settings.py — generate with: python -c "import secrets; print(secrets.token_urlsafe(32))" DJANGO_MCP = { "INTERNAL_TOKEN": "Xk9mP2vQwRtLn8cBjYeH5sA0dFuG3iZ1", } ``` -------------------------------- ### Exclude Specific HTTP Methods Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md Use the EXCLUDE pattern to exclude all DELETE operations. ```python # Expose everything except DELETE operations DJANGO_MCP = { "EXCLUDE": ["DELETE:*"], } ``` -------------------------------- ### Django View Permissions for MCP Source: https://context7.com/orco82/django-drf-mcp/llms.txt Apply permissions to Django views to allow both standard authentication and internal MCP requests. `IsMCPInternalRequest` is used for requests proxied by the MCP client. ```python # myapp/views.py from rest_framework import viewsets from rest_framework.permissions import IsAuthenticated from django_drf_mcp.permissions import IsMCPInternalRequest class OrderViewSet(viewsets.ModelViewSet): permission_classes = [IsAuthenticated | IsMCPInternalRequest] # Browser: session/token auth required # MCP proxy: passes via X-MCP-Internal-Token ``` -------------------------------- ### McpView - Embedded DRF MCP View Source: https://context7.com/orco82/django-drf-mcp/llms.txt Details on the `McpView` APIView for handling MCP JSON-RPC over HTTP, including health checks and tool calls. ```APIDOC ## `McpView` — Embedded DRF MCP View A stateless DRF `APIView` that handles MCP JSON-RPC over HTTP. Inherits `DEFAULT_AUTHENTICATION_CLASSES` and `DEFAULT_PERMISSION_CLASSES` from `REST_FRAMEWORK` settings. ```bash # Health check curl http://localhost:8000/mcp/ # Response: # {"name": "django-drf-mcp", "protocol": "mcp", "transport": "streamable-http", "status": "ok"} # MCP initialize curl -X POST http://localhost:8000/mcp/ \ -H "Content-Type: application/json" \ -H "Authorization: Token abc123..." \ -d '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}' # Response: # {"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2024-11-05","capabilities":{"tools":{"listChanged":false}},"serverInfo":{"name":"django-drf-mcp","version":"1.0.0"}}} # List available MCP tools curl -X POST http://localhost:8000/mcp/ \ -H "Content-Type: application/json" \ -H "Authorization: Token abc123..." \ -d '{"jsonrpc": "2.0", "id": 2, "method": "tools/list", "params": {}}' # Call an MCP tool (e.g. list products) curl -X POST http://localhost:8000/mcp/ \ -H "Content-Type: application/json" \ -H "Authorization: Token abc123..." \ -d '{ "jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": { "name": "get_api_products_list", "arguments": {} } }' ``` ``` -------------------------------- ### MCP Protocol Endpoint Source: https://github.com/orco82/django-drf-mcp/blob/main/README.md The main endpoint for the MCP protocol, typically used for JSON-RPC communication. ```APIDOC ## GET /mcp/ ### Description MCP protocol endpoint (JSON-RPC). ### Method GET ### Endpoint /mcp/ ``` -------------------------------- ### Health Check with McpView Source: https://context7.com/orco82/django-drf-mcp/llms.txt This curl command performs a health check on the MCP endpoint. It expects a JSON response indicating the server status. ```bash # Health check curl http://localhost:8000/mcp/ # Response: # {"name": "django-drf-mcp", "protocol": "mcp", "transport": "streamable-http", "status": "ok"} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.