← Back to Pull requests

Resolve the Django user in tools; permission-lock and hide tools per user #24

Closed
unknown closed this pull request without merging on Jul 29, 2026 12:26 UTC
View on GitHub
unknown unknown commented on Jul 29, 2026 12:12 UTC
Author

Completes the auth story from ADR-0011 and answers two needs directly: access the user in tools to permission-lock them, and hide tools a user can't use from tools/list. Recorded in ADR-0014, with a full django-oauth-toolkit recipe.

user_resolver — the user in tools

mcp_view(server, token_verifier=..., user_resolver=async_token_to_user)

After a token verifies, the resolver sets request.user, so tools gate the ordinary Django way:

if not django_request(ctx).user.has_perm("app.delete_thing"):
    raise PermissionError(...)

A separate hook from the SDK token_verifier on purpose: the SDK's AccessToken is token-shaped (client_id, scopes) with no slot for a Django user, and the verifier is the only place that knows the user.

PermittedToolsFilter — hide tools per user

MCPServer(middleware=[PermittedToolsFilter(lambda user, name: user.has_perm(...))])

Filters tools/list so a client is never even shown a tool it can't use. Runs the predicate in a worker thread so has_perm (which queries) doesn't raise SynchronousOnlyOperation on the event loop.

The load-bearing decision: hiding ≠ gating

Visibility filtering is not a security boundary, and the design refuses to pretend it is. Nothing stops a client calling a hidden tool by name. A test encodes the model: a hidden tool called by name is still refused, because the tool gates its own execution. Gate to be safe; hide to be tidy. Docs and ADR say so explicitly.

Two things worth reading

  • Package code must not import Django models at module top level. permissions.py imported AnonymousUser at import time, forcing the app registry to load on import django_stateless_mcp and crashing the django-stubs mypy plugin with AppRegistryNotReady. Now lazy. Rule recorded in the ADR.

  • The tool-list result is a dict at the middleware tier, not a ListToolsResult — the same two-shapes quirk ADR-0013 hit. The filter handles both.
  • The recipe

    docs/recipes/oauth-and-permissions.md shows the whole picture with django-oauth-toolkit + django-oauth-toolkit-dcr — DOTTokenVerifier, a DOT-backed user_resolver, execution gating, and visibility filtering — mirroring how ic_cookie layers OAuth today. DOT is documented, not depended on.

    Test plan

  • 32 tests on Django 5.2.16 and 6.0.7: user reachable in tool, permission-lock allows/denies, tool hidden without perm / shown with perm, and hidden-tool-still-execution-gated

  • Conformance suite → baseline unchanged, exit 0

  • mypy strict (incl. the AppRegistryNotReady fix), pre-commit, mkdocs --strict clean

  • CI green
  • Diff: 335 lines (+251 / -84) across 4 files Reviewer: BenA-SA Review duration: 14m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.65) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.65 · 335 lines diff across 4 files
    0.65
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.348
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.065
    Real PR Conversation Timeline
    1 comments
    "Review active on #24"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 65.4% (HIGH) · Higher = larger blast radius
    65.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.8% · Safe to merge
    25.8%