Agents (MCP bridge)¶
Stdio MCP tools proxy Bearer HTTP to your DRF API. Enable DRF first (DRF API).
Two packages:
``djcrud_mcp`` (Django host, inside
djcrud) —McpProfileregistration``djcrud-client`` (agent subprocess) — FastMCP stdio server, OpenAPI tool generation, HTTP proxy (
mcp+httpxonly — no Django)
Install¶
Remote subprocesses (sandboxes, CI, remote agents):
pip install --pre djcrud-client
Django host:
pip install --pre "djcrud[drf,mcp]"
Remote agents install djcrud-client only and point at your API with
DJCRUD_BASE_URL / DJCRUD_TOKEN.
Example apps¶
drf_example— ViewSets at/api/(see DRF API)mcp_example— the project’s MCP profile (this chapter)
ViewSet registration lives in drf_example/djcrud.py. The publish
@action on ArticleViewSet becomes an MCP tool (article_publish) via
GET /api/schema/:
def can_publish(user, *, obj, **ctx):
if not user.is_authenticated:
return False
if obj is not None and (not is_owner(user, obj=obj, **ctx) or obj.published):
return False
return True
class ArticleViewSet(djcrud_drf.ModelViewSet):
model = Article
@action(detail=True, methods=["post"])
def publish(self, request, pk=None):
article = self.get_object()
article.publish()
return Response(self.get_serializer(article).data)
djcrud.permissions.add_perm(Article, "view,add,change,delete", check=authenticated)
djcrud.permissions.add_perm(Article, "publish", check=can_publish)
djcrud_drf.site.register(ArticleViewSet)
MCP profile¶
Register one McpProfile in djcrud.py — that is the MCP
surface for your project. List the ViewSets agents may call (same models as
DRF API):
import djcrud_mcp
from djcrud_example.drf_example.djcrud import ArticleViewSet, ProductViewSet
class ExampleMcp(djcrud_mcp.McpProfile):
viewsets = (ArticleViewSet, ProductViewSet)
djcrud_mcp.site.register(ExampleMcp)
Add djcrud_mcp and your MCP app to INSTALLED_APPS. The djcrud_mcp
package registers the profile HTTP API on djcrud_drf.site; your app’s
djcrud.py registers the profile itself:
INSTALLED_APPS = [
# ...
"djcrud_drf",
"djcrud_mcp",
"djcrud_example.mcp_example",
]
urlpatterns = (
djcrud.site.build().urlpatterns
+ djcrud_drf.site.build().urlpatterns
)
Run¶
export DJCRUD_TOKEN=<raw_key>
djcrud-client --call article_list --json '{}'
djcrud-client --call article_publish --json '{"pk": 1}'
djcrud-client -mcp
Reference: djcrud_mcp.