djangorestframework-mcp is an extension library based on Django REST Framework, designed to help developers quickly build MCP (Model Context Protocol) servers that expose existing DRF APIs as MCP tools, enabling LLM and agent applications to directly call these APIs to complete operations.
Purpose
The core purpose of this library is to simplify the process of setting up an MCP server. Developers only need to add a decorator to a ViewSet to automatically register DRF's CRUD operations and custom actions as MCP tools. Typical applications include: managing backend data through natural language instructions (such as deactivating accounts, extending trial periods, querying statistics), and transforming traditional SaaS interfaces into conversational interactions (such as renaming article titles).
Main capabilities
- Automatic tool generation: After applying the @mcp_viewset decorator to a ViewSet that inherits from GenericViewSet, the defined list, retrieve, create, update, partial_update, and destroy methods are automatically exposed as MCP tools, with tool names generated based on the ViewSet's model name (such as customers_list).
- Automatic schema derivation: Automatically generates input/output schemas for tools based on DRF serializers, including required/optional fields, constraints (length, numeric range, regex, allow_null, allow_blank), formats (url, email, etc.), help_text/label as parameter titles and descriptions, and support for primitive types, composite fields, enums, related fields, nested serializers, and ListSerializer.
- Permission and authentication preservation: MCP requests go through the ViewSet's authentication and permission checks (perform_authentication, check_permissions), supporting DRF's authentication framework. Additional control can be applied at the MCP endpoint level by inheriting MCPView to set authentication_classes and implement the has_mcp_permission method.
- Custom action support: Custom actions created with the @action decorator must be paired with the @mcp_tool decorator and specify input_serializer (which may be None) to be exposed as tools. The input_serializer for CRUD actions is optional and is inferred from serializer_class by default.
- Selective registration: The actions parameter of @mcp_viewset can be used to whitelist the actions to expose.
- Tool name and description customization: The @mcp_tool decorator supports custom name, title, and description.
- MCP-specific overrides: Behavior can be made conditional within a ViewSet via the request.is_mcp_request attribute, or a dedicated ViewSet can be created that inherits from an existing ViewSet.
- Testing tools: Provides the MCPClient testing tool, which inherits from Django's test client and supports authentication helper methods such as login() and force_login().
- Error handling: On authentication failure, it returns 401/403 status codes and a WWW-Authenticate header by default, and the JSON-RPC response body contains a human-readable error message. The RETURN_200_FOR_ERRORS setting can be enabled to return HTTP 200 for compatibility with some clients.
Usage
- Install the package and add it to INSTALLED_APPS.
- Add an MCP endpoint in urls.py (default path /mcp/).
- Apply the @mcp_viewset decorator to ViewSets and the @mcp_tool decorator to custom actions.
- After starting the Django service, MCP clients can connect http://localhost:8000/mcp/ Perform tool discovery and invocation.
- For STDIO transport, use the udo bridge (e.g., to connect to Claude Desktop), and pass credentials via environment variables.
Applicable scenarios
- Enhance backend admin workflows by performing data operations through natural language instructions.
- Turn traditional SaaS interfaces into conversational interactions, reducing frontend work.
- Provide LLM and intelligent applications with direct access to backend APIs.
Limitations
- Currently only HTTP transport is supported; SSE and stdio are not yet natively supported (a bridge is required).
- MCP requests do not go through the full DRF request lifecycle; dispatch, initial, perform_content_negotiation, finalize_response, and other methods are not called, and the request lacks method, path, and similar attributes.
- Discovery of resources, prompts, and notifications is not yet supported.
- Some DRF features are not yet supported, including filtering (DjangoFilterBackend), search, pagination, throttling, and file uploads.
- Custom actions must explicitly specify an input_serializer.
- Some MCP clients handle 401/401 status codes poorly, so enabling RETURN_200 may be necessary.