Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds support for serving a Genkit MCP server over streamable HTTP using Starlette, introducing the asgi_app method to McpServer along with DNS rebinding protection and comprehensive tests. The review feedback points out a potential issue where passing a single string instead of a sequence of strings to allowed_hosts or allowed_origins would cause the string to be split into individual characters during list conversion, and suggests a defensive check to handle this case safely.
| manager = self._session_manager( | ||
| stateless=stateless, | ||
| json_response=json_response, | ||
| event_store=event_store, | ||
| security=TransportSecuritySettings( | ||
| enable_dns_rebinding_protection=True, | ||
| allowed_hosts=list(allowed_hosts), | ||
| allowed_origins=list(allowed_origins), | ||
| ), | ||
| ) |
There was a problem hiding this comment.
In Python, passing a single string (e.g., allowed_hosts='example.com') to a parameter expecting a Sequence[str] is a common mistake. Since str is itself a sequence, calling list(allowed_hosts) on a string will split it into individual characters (e.g., ['e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm']), which will cause host/origin validation to fail silently with 421 or 403 errors.
To prevent this, we should check if allowed_hosts or allowed_origins are strings and wrap them in a list automatically.
| manager = self._session_manager( | |
| stateless=stateless, | |
| json_response=json_response, | |
| event_store=event_store, | |
| security=TransportSecuritySettings( | |
| enable_dns_rebinding_protection=True, | |
| allowed_hosts=list(allowed_hosts), | |
| allowed_origins=list(allowed_origins), | |
| ), | |
| ) | |
| hosts = [allowed_hosts] if isinstance(allowed_hosts, str) else list(allowed_hosts) | |
| origins = [allowed_origins] if isinstance(allowed_origins, str) else list(allowed_origins) | |
| manager = self._session_manager( | |
| stateless=stateless, | |
| json_response=json_response, | |
| event_store=event_store, | |
| security=TransportSecuritySettings( | |
| enable_dns_rebinding_protection=True, | |
| allowed_hosts=hosts, | |
| allowed_origins=origins, | |
| ), | |
| ) |
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.
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.
04db97e to
f3b351b
Compare
|
Moved to genkit-ai/genkit-python#95 along with the Python SDK. |
What's new
server.asgi_app(path='/mcp', *, stateless=False, json_response=False, event_store=None, allowed_hosts=..., allowed_origins=...)returns a Starlette app that serves the server over streamable HTTP. It is the one public addition.run()can be entered only once.Routeon an exact path, not aMount, because aMountredirects to a trailing slash and the client POSTs the path it was given.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. There is no switch to turn the check off.allowed_hostsorallowed_originsraisesTypeError. Astrtype-checks asSequence[str]and would otherwise refuse every request with 421.starlettebecomes a direct dependency. It is already a core genkit dependency, so there is no extra.Part of genkit-ai/genkit-python#219.
Why
stdio ties a server to the one client that owns the process. Streamable HTTP is how several clients, or a client that did not start the server, reach it. The SDK 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 to127.0.0.1can call every tool on a local server.Tests
tests/mcp_http_test.py: a real client session over an httpx ASGI transport, with no port bound. Covers stateful and stateless list and call, session ids, JSON responses, a custom path, mounting with a chained lifespan, one lifespan per app, stdout left alone, host and origin checks and overrides, a single-string allow-list refused, the trailing-slash redirect, DELETE ending a session, and an app whose lifespan never ran failing every request.Hostgot 421 and a hostileOrigingot 403.py/packages/genkit-mcp: 85 passed. ruff, pyrefly, ty and pyright clean.Not in this PR
asgi_app()covers serving and mounting.