djangorestframework-mcp-server
Expose djangorestframework-services
services and selectors as a Model Context Protocol
(MCP) server, conforming to MCP 2026-07-28 and 2025-11-25 (Streamable HTTP).
Idea
Register ServiceSpec instances directly — no DRF router or viewset
involvement. The unit of registration is the ServiceSpec, not a view.
from django.urls import path
from rest_framework_services.types.selector_kind import SelectorKind
from rest_framework_services.types.selector_spec import SelectorSpec
from rest_framework_services.types.service_spec import ServiceSpec
from rest_framework_mcp import MCPServer
server = MCPServer(name="my-app")
server.register_service_tool(
name="invoices.create",
spec=ServiceSpec(
service=create_invoice,
input_serializer=InvoiceInputSerializer,
output_selector_spec=SelectorSpec(
kind=SelectorKind.RETRIEVE,
output_serializer=InvoiceOutputSerializer,
),
),
)
server.register_resource(
name="invoice",
uri_template="invoices://{pk}",
selector=SelectorSpec(
kind=SelectorKind.RETRIEVE,
selector=get_invoice,
output_serializer=InvoiceOutputSerializer,
),
)
urlpatterns = [path("mcp/", server.urls)]
A decorator form is also supported (@server.service_tool(...) / @server.resource(...)).
See the quickstart for the full end-to-end recipe.
- Services (mutations) → MCP tools.
- Selectors (reads) → MCP resources.
- A single
/mcpendpoint speaks Streamable HTTP. The/.well-known/oauth-protected-resourceendpoint comes mounted alongside.
What ships
- Tools —
tools/list,tools/callforregister_service_tool(mutations) andregister_selector_tool(reads, with optionalFilterSet+ ordering + pagination). - Resources —
resources/list,resources/templates/list,resources/readagainstSelectorSpec-backed callables; RFC 6570 templated URIs. - Prompts —
prompts/list,prompts/getagainst render callables returning strings,PromptMessages, or async coroutines. - In-process transport surface — call tools without an HTTP round-trip:
MCPServer.call_tool/acall_toolandlist_tools/alist_toolsdrive the same dispatch and permission checks as the wire path, for embedding in agent bridges, toolsets, or management commands. - Tool annotations — pass
annotations=at registration (or rely on the read/mutation default) to advertise MCP hints likereadOnlyHint/destructiveHintontools/list. - Generic
_meta— passmeta=at registration to populate the base protocol's free-form_metaobject on a tool, resource, or prompt's listing entry (and on thecontentsofresources/read). Passed through verbatim, so protocol extensions have somewhere to live. - Interactive views (MCP Apps) —
register_ui_resource(...)declares an HTML view with typed CSP / permission metadata;ui=UIToolMeta(...)on a tool links its result to that view, and a host renders it inline in the chat. The render payload is thestructuredContentyou already emit, and a view's owntools/calls inherit your auth, permissions and rate limits. An extension over base MCP, so no protocol bump. We declare; the host sandboxes and renders. - Pluggable auth —
DjangoOAuthToolkitBackend(default) andAllowAnyBackend(dev only). Per-bindingMCPPermissionclasses (ScopeRequired,DjangoPermRequired) plus your own. - RFC 8707 audience binding when
RESOURCE_URLis configured; RFC 9728 PRM served from the configured backend. - Per-binding rate limits —
MCPRateLimitProtocol withFixedWindowRateLimit,SlidingWindowRateLimit, andTokenBucketRateLimitimplementations shipped. - Output formats — JSON (default) and TOON (token-oriented; optional extra with safe JSON fallback).
- Async POST/DELETE + GET-side SSE push — sync
urlsfor WSGI,async_urlsfor ASGI;MCPServer.notify(session_id, payload)pushes JSON-RPC frames on the session's SSE stream. Per-workerInMemorySSEBrokeror cross-workerRedisSSEBroker(behind[redis]);Last-Event-IDresume viaInMemorySSEReplayBuffer/RedisSSEReplayBuffer. - OpenTelemetry instrumentation —
mcp.tools.call,mcp.resources.read,mcp.prompts.getspans (no-op without the[otel]extra installed). - Origin allowlist + protocol-version validation + session
lifecycle per the transport rules of both revisions.
2026-07-28and later declare version, identity and capabilities on every request and hold no session;2025-11-25and earlier negotiate once throughinitialize. Both are served concurrently on one endpoint, because a legacy client has no fall-forward mechanism.
Install
pip install djangorestframework-mcp-server # JSON only
pip install "djangorestframework-mcp-server[toon]" # +TOON encoder
pip install "djangorestframework-mcp-server[oauth]" # +django-oauth-toolkit backend
pip install "djangorestframework-mcp-server[redis]" # +Redis SSE broker for multi-worker ASGI
pip install "djangorestframework-mcp-server[otel]" # +OpenTelemetry instrumentation
pip install "djangorestframework-mcp-server[filter]" # +django-filter for selector-tool FilterSets
pip install "djangorestframework-mcp-server[spectacular]" # +drf-spectacular schema overrides
pip install "djangorestframework-mcp-server[jwt]" # +SimpleJWTCookieAdapter (djangorestframework-simplejwt)
pip install "djangorestframework-mcp-server[toon,oauth,redis,otel,filter,spectacular,jwt]" # everything
…or with uv:
uv add djangorestframework-mcp-server # JSON only
uv add "djangorestframework-mcp-server[toon]" # +TOON encoder
uv add "djangorestframework-mcp-server[oauth]" # +django-oauth-toolkit backend
uv add "djangorestframework-mcp-server[redis]" # +Redis SSE broker for multi-worker ASGI
uv add "djangorestframework-mcp-server[otel]" # +OpenTelemetry instrumentation
uv add "djangorestframework-mcp-server[filter]" # +django-filter for selector-tool FilterSets
uv add "djangorestframework-mcp-server[spectacular]" # +drf-spectacular schema overrides
uv add "djangorestframework-mcp-server[jwt]" # +SimpleJWTCookieAdapter (djangorestframework-simplejwt)
uv add "djangorestframework-mcp-server[toon,oauth,redis,otel,filter,spectacular,jwt]" # everything
Optional extras degrade gracefully: TOON falls back to JSON with a runtime
warning if python-toon is not installed, and the OAuth backend module imports
cleanly without oauth2_provider, so a server used in process needs no DOT.
Mounting one over HTTP refuses without it, while the URLConf is imported.
Try it
Install mcp-inspector and point it at your dev server:
npx @modelcontextprotocol/inspector --url http://localhost:8000/mcp/
Inspector lists tools, fills in arguments from the generated JSON Schema, and walks the OAuth auth flow against your configured Authorization Server.
Documentation
- Quickstart — copy-pasteable end-to-end.
- Concepts — tools vs resources, sessions, output formats.
- Authentication — backends, permissions, audience binding, bring-your-own AS recipe.
- Recipes — focused cookbook entries.
- Reference — autodocs for every public symbol.
License
MIT.
Metadata
Release files for djangorestframework-mcp-server 0.43.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| djangorestframework_mcp_server-0.43.0.tar.gz | 902.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| djangorestframework_mcp_server-0.43.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.3 MB
Release files / djangorestframework_mcp_server-0.43.0.tar.gz
| Download URL | djangorestframework_mcp_server-0.43.0.tar.gz |
|---|---|
| Size | 902.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4a2d0d4aea788f693b178137712ea3e641550fc82690cc38a5fe46b452a05dbc
|
|
BLAKE2b-256 checksum How to use checksums |
d31024ca38130d30545dc1a910be885a2feb64aea0fb70bf6e9cc2be1e379c49
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 16, 2026.
Transparency logRelease files / djangorestframework_mcp_server-0.43.0-py3-none-any.whl
| Download URL | djangorestframework_mcp_server-0.43.0-py3-none-any.whl |
|---|---|
| Size | 419.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
18f4070b6770c0f4e6f197a61d352f85ac3f022271d1c06edb4b23cc95072080
|
|
BLAKE2b-256 checksum How to use checksums |
9c15c03b47368e078b0fbda3d9b929ca3ea6b5bfffa908b1af9379428c3695b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 16, 2026.
Transparency log