Serve Django and MCP from one uvicorn process #144

Merged
CrowderSoup merged 2 commits from claude/asgi-mcp into main 2026-12-01 21:56:03 +00:00
Owner

Closes vTodo task #687 (Crowder Cloud workspace).

What changes

  • One process. uvicorn config.asgi:application replaces Gunicorn. config/asgi.py sends requests for the host of VTODO_MCP_PUBLIC_URL (mcp.vtodo.crowdersoup.com) to the MCP app: /mcp, /.well-known/oauth-protected-resource…, /up, plus the CORS headers and preflight the old server had. Every other request goes to Django, inside vJournal's ThreadSensitiveContext guard, which returns the DB connection on CancelledError or a disconnect.
  • Connectors keep working. The MCP hostname, the /mcp path and the OAuth resource (token audience) are all unchanged. kamal-proxy now routes both hosts to the web role.
  • No HTTP hop. The tools still call the API through VtodoClient, but mcp_server/inprocess.py is a requests adapter that hands each request to Django's WSGI handler on the tool's worker thread. So the API's checks still apply in one place: OAuth scope and audience, workspace roles, act-as, and the mcp source in the activity log. The whoami token check goes the same way. Sentry trace headers are passed along, so a tool call and its API requests are still one trace.
  • stdio only from the CLI. Over HTTP the server always uses OAuth. python -m mcp_server is now stdio only (one VTODO_API_TOKEN account against a remote vtodo). The standalone SSE/uvicorn mode is gone.
  • DB pool. Under ASGI each request gets its own thread, so Django can't keep CONN_MAX_AGE connections. This switches to psycopg 3 with a pool per web process, the same as vJournal: max 10 behind PgBouncer, with prepare_threshold=None. The Celery worker role sets DB_POOL=False and keeps its persistent connections.
  • Deploy. The mcp role is removed from config/deploy.yml. Because kamal-proxy won't give web a host that another service still holds, the deploy workflow now runs kamal build push, then removes the old vtodo-mcp proxy service and container, then runs kamal deploy --skip-push. MCP should only be down for the time it takes web to boot. Once the old service is gone, that step does nothing.

Testing

  • The full suite passes locally: 956 passed, browser tests included. makemigrations --check is clean.
  • New tests/test_asgi.py covers host dispatch, a real OAuth token through whoami, in-process tool calls (including source=mcp in the activity log), the 401 challenge, CORS preflight, and the metadata endpoint. The existing MCP HTTP tests now authenticate, because OAuth is always on over HTTP.
  • I also smoke-tested against Postgres 16 with uvicorn running 2 workers. Both hosts routed correctly, the metadata showed the right resource and issuer, and create_task and list_tasks worked over MCP. 60 concurrent tool calls left 6 pooled connections, with no errors. Stdio initialize still works.

Before merging

The deploy sets VTODO_API_URL=https://vtodo.crowdersoup.com in the shared env. That value is the OAuth issuer the MCP metadata advertises, so it has to match what django-oauth-toolkit reports. It's the same value the old mcp role used.

🤖 Generated with Claude Code

Closes vTodo task #687 (Crowder Cloud workspace). ## What changes - **One process.** `uvicorn config.asgi:application` replaces Gunicorn. `config/asgi.py` sends requests for the host of `VTODO_MCP_PUBLIC_URL` (`mcp.vtodo.crowdersoup.com`) to the MCP app: `/mcp`, `/.well-known/oauth-protected-resource…`, `/up`, plus the CORS headers and preflight the old server had. Every other request goes to Django, inside vJournal's `ThreadSensitiveContext` guard, which returns the DB connection on `CancelledError` or a disconnect. - **Connectors keep working.** The MCP hostname, the `/mcp` path and the OAuth resource (token audience) are all unchanged. kamal-proxy now routes both hosts to the web role. - **No HTTP hop.** The tools still call the API through `VtodoClient`, but `mcp_server/inprocess.py` is a `requests` adapter that hands each request to Django's WSGI handler on the tool's worker thread. So the API's checks still apply in one place: OAuth scope and audience, workspace roles, act-as, and the `mcp` source in the activity log. The whoami token check goes the same way. Sentry trace headers are passed along, so a tool call and its API requests are still one trace. - **stdio only from the CLI.** Over HTTP the server always uses OAuth. `python -m mcp_server` is now stdio only (one `VTODO_API_TOKEN` account against a remote vtodo). The standalone SSE/uvicorn mode is gone. - **DB pool.** Under ASGI each request gets its own thread, so Django can't keep `CONN_MAX_AGE` connections. This switches to psycopg 3 with a pool per web process, the same as vJournal: max 10 behind PgBouncer, with `prepare_threshold=None`. The Celery worker role sets `DB_POOL=False` and keeps its persistent connections. - **Deploy.** The `mcp` role is removed from `config/deploy.yml`. Because kamal-proxy won't give web a host that another service still holds, the deploy workflow now runs `kamal build push`, then removes the old `vtodo-mcp` proxy service and container, then runs `kamal deploy --skip-push`. MCP should only be down for the time it takes web to boot. Once the old service is gone, that step does nothing. ## Testing - The full suite passes locally: **956 passed**, browser tests included. `makemigrations --check` is clean. - New `tests/test_asgi.py` covers host dispatch, a real OAuth token through whoami, in-process tool calls (including `source=mcp` in the activity log), the 401 challenge, CORS preflight, and the metadata endpoint. The existing MCP HTTP tests now authenticate, because OAuth is always on over HTTP. - I also smoke-tested against Postgres 16 with uvicorn running 2 workers. Both hosts routed correctly, the metadata showed the right resource and issuer, and `create_task` and `list_tasks` worked over MCP. 60 concurrent tool calls left 6 pooled connections, with no errors. Stdio `initialize` still works. ## Before merging The deploy sets `VTODO_API_URL=https://vtodo.crowdersoup.com` in the shared env. That value is the OAuth issuer the MCP metadata advertises, so it has to match what django-oauth-toolkit reports. It's the same value the old mcp role used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Serve Django and MCP from one uvicorn process
All checks were successful
tests / pytest (push) Successful in 1m16s
9e54ea166f
vTodo was the last app on Gunicorn WSGI with MCP in a separate container
that called the API back over HTTPS. Now one ASGI app (config/asgi.py)
serves both, like vJournal and vPages:

- uvicorn replaces Gunicorn. Requests for the host of VTODO_MCP_PUBLIC_URL
  (mcp.vtodo.crowdersoup.com) go to the MCP app: /mcp, its RFC 9728
  metadata, /up, with the CORS handling the old server had. The hostname
  and the tokens' audience don't change, so signed-in connectors keep
  working. Other requests go to Django, inside vJournal's outer
  ThreadSensitiveContext guard that returns the DB connection on
  CancelledError or a disconnect.
- MCP tools still call the API, but in-process: mcp_server/inprocess.py is
  a requests adapter that hands each request to Django's WSGI handler on
  the tool's worker thread, carrying the trace. The API's auth, workspace,
  act-as and activity-source checks are unchanged. whoami goes the same way.
- Over HTTP the server always uses OAuth; python -m mcp_server is stdio
  only now, as one API-token account against a remote vtodo.
- Postgres moves to psycopg 3 with a pool per web process: under ASGI
  Django can't keep a persistent connection. The Celery worker sets
  DB_POOL=False and keeps CONN_MAX_AGE.
- deploy.yml drops the mcp role and gives the web role both hosts. The
  deploy workflow builds first, then removes the old vtodo-mcp proxy
  service and container (kamal-proxy won't hand its host to web while it
  holds it), then deploys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test the DB pool setting when it's on
All checks were successful
tests / pytest (push) Successful in 2m10s
cc235b9862
test_database_connections_are_kept_between_requests asserted
CONN_MAX_AGE > 0, which fails on Postgres now that the web process
uses psycopg's pool (Django requires CONN_MAX_AGE=0 with it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CrowderSoup deleted branch claude/asgi-mcp 2026-12-01 21:56:03 +00:00
Sign in to join this conversation.
No description provided.