Skip to content

feat(mcp): serve MCP over streamable HTTP as an ASGI app - #95

Open
hilariie wants to merge 10 commits into
feat/mcp-server-stdiofrom
feat/mcp-server-http
Open

hilariie wants to merge 10 commits into
feat/mcp-server-stdiofrom
feat/mcp-server-http

Conversation

@hilariie

Copy link
Copy Markdown
Contributor

Stacked on #93 (MCP stdio server), which merges first.

server.asgi_app(path='/mcp', *, stateless=False, json_response=False, event_store=None, allowed_hosts=..., allowed_origins=...) returns a Starlette app that serves an MCP server over streamable HTTP.

import uvicorn

app = server.asgi_app()
uvicorn.run(app, host='127.0.0.1', port=8000)  # POST http://127.0.0.1:8000/mcp

Changes

  • Each call builds its own session manager and opens it in the app's lifespan, so the returned app starts once. Mounting it into another app means chaining its lifespan into the host's.
  • The endpoint is a Route on the exact path, so /mcp is served rather than redirected to /mcp/.
  • DNS rebinding protection is always on. Host must be in allowed_hosts (else 421) and a browser's Origin in allowed_origins (else 403). Both default to localhost, 127.0.0.1 and [::1] on any port, over http or https. A single string for either raises TypeError.
  • mcp now needs 1.30. Earlier releases opened a session for every request without a session id, refused or not, and never closed it. 1.30 also brings a 30 minute idle timeout, a 10,000 session cap and a 4 MiB body limit.
  • event_store with stateless=True raises ValueError, since a stateless app has no session to replay.
  • starlette becomes a direct dependency. It is already a core genkit dependency.
  • The README covers serving, mounting, testing with TestClient, CORS for pages on another origin, and the Host a proxy forwards.

Not in this PR: authentication, a public session manager, and settings for the SDK's session and body limits.

Breaking changes

None.

Verification

  • mcp_http_test.py runs a real client session over an httpx ASGI transport with no port bound: stateful and stateless list and call, session ids, JSON responses, custom path, mounting with a chained lifespan, host and origin checks and overrides, refused requests leaving no session, event store use, trailing-slash redirect, DELETE, and an app whose lifespan never ran.
  • The refused-session test fails on mcp 1.26 and passes on 1.30.

Part of #91.

cabljac and others added 10 commits November 11, 2026 14:02
stdio ties a server to one client that owns the process. Streamable HTTP
is how a server is reached by several clients, or by any client that did
not start it, and the SDK already ships the transport. starlette is
already a core dependency, so this is an ASGI app with no extra.
genkit-mcp now imports starlette itself, so it declares it directly.

asgi_app() is the one public addition. It returns a Starlette app that
owns a session manager and opens it in its own lifespan. Each call
builds a new manager, because run() can be entered once per instance and
cannot be re-entered, so a shared one would make the second app fail at
startup. The raw session manager and the endpoint wrapping it stay
private: asgi_app() covers serving and mounting, and nothing else needs
them yet.

The endpoint is a Route on an exact path, not a Mount: a Mount redirects
the path it is mounted on to one with a trailing slash, and the client
POSTs the path it was given. The endpoint is a class rather than a
function because Starlette reads a function endpoint as a request
handler, which never sees the GET and DELETE the transport needs.

The endpoint has no authentication, so it checks where a request comes
from. The SDK's transport turns DNS rebinding protection off unless it
is given settings, and the MCP spec says a server must validate Origin.
Without the check, a web page that rebinds its own name to 127.0.0.1
reaches a local server and can call every tool. asgi_app() always turns
the check on: Host must be in allowed_hosts (else 421) and a browser's
Origin in allowed_origins (else 403). Both default to localhost,
127.0.0.1 and [::1] on any port, which is what the SDK's FastMCP allows
when bound to localhost; bare names are allowed too, since a client on
port 80 sends no port. A deployment on a public name lists its own.
There is no switch to turn the check off. The README says to bind to
localhost or put authentication in front, and that sessions and request
bodies are not limited here.

Mounting the app into an existing one needs its lifespan chained into
the host's, because Starlette runs no mounted app's lifespan and the
manager's task group is created there. The README says so and a test
pins it. An app whose lifespan never ran fails each request with the
SDK's "Task group is not initialized" rather than hanging, and a test
pins that as well.

start() moves this process's stdout because stdout is the stdio wire.
Nothing on this path does, and a test pins that too.

The tests drive the app through an httpx ASGI transport, so a real
client session runs against it with no port bound and nothing polled.
They cover the host and origin checks, the redirect from a trailing
slash, and a DELETE ending a session. Under a real uvicorn, a request
with a hostile Host got 421 and one with a hostile Origin got 403.

Upstream-Commit: genkit-ai/genkit@591621485
Upstream-PR: genkit-ai/genkit#6883
A str is a Sequence[str] of its characters, so allowed_hosts='example.com'
type-checks and then refuses every request with 421, because no Host is
one character long. asgi_app() raises TypeError for a str in either
allow-list, rather than wrapping it, so the call reads the same way it
is typed.

Upstream-Commit: genkit-ai/genkit@f3b351bbb
Upstream-PR: genkit-ai/genkit#6883
mcp 1.26 opened a session for every request without a session id, refused or not, and never closed it.
The SDK drops the store in stateless mode without a word.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants