Repository navigation
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Changes
Routeon the exact path, so/mcpis served rather than redirected to/mcp/.Hostmust be inallowed_hosts(else 421) and a browser'sOrigininallowed_origins(else 403). Both default to localhost,127.0.0.1and[::1]on any port, over http or https. A single string for either raisesTypeError.mcpnow 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_storewithstateless=TrueraisesValueError, since a stateless app has no session to replay.starlettebecomes a direct dependency. It is already a core genkit dependency.TestClient, CORS for pages on another origin, and theHosta 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.pyruns 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.Part of #91.