Skip to content

feat(py/plugins/mcp): serve MCP over streamable HTTP as an ASGI app - #6883

Closed
cabljac wants to merge 2 commits into
feat/py-mcp-server-1-stdiofrom
feat/py-mcp-server-2-http
Closed

cabljac wants to merge 2 commits into
feat/py-mcp-server-1-stdiofrom
feat/py-mcp-server-2-http

Conversation

@cabljac

@cabljac cabljac commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #6882. Review the top commit only.

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.

  • Each call builds its own session manager and opens it in the app's lifespan, because a manager's run() can be entered only once.
  • The endpoint is a Route on an exact path, not a Mount, because a Mount redirects to a trailing slash and the client POSTs the path it was given.
  • 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. There is no switch to turn the check off.
  • To mount the app into an existing one, chain its lifespan into the host's. Starlette runs no mounted app's lifespan.
  • A single string for allowed_hosts or allowed_origins raises TypeError. A str type-checks as Sequence[str] and would otherwise refuse every request with 421.
  • starlette becomes a direct dependency. It is already a core genkit dependency, so there is no extra.
import uvicorn

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

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 to 127.0.0.1 can 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.
  • Under a real uvicorn, a hostile Host got 421 and a hostile Origin got 403.
  • py/packages/genkit-mcp: 85 passed. ruff, pyrefly, ty and pyright clean.

Not in this PR

  • Authentication: none. The README says to bind to localhost or put authentication in front.
  • Limits: sessions and request bodies are not limited here.
  • Raw session manager: stays private. asgi_app() covers serving and mounting.

@cabljac
cabljac added this pull request to stack #6884 October 23, 2026 16:40
@github-actions github-actions Bot added docs Improvements or additions to documentation python Python config labels Oct 23, 2026
@cabljac
cabljac removed this pull request from stack #6884 October 23, 2026 16:40
@cabljac
cabljac added this pull request to stack #6885 October 23, 2026 16:40

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +195 to +204
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),
),
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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.
@cabljac
cabljac force-pushed the feat/py-mcp-server-2-http branch from 04db97e to f3b351b Compare October 23, 2026 16:55
@hilariie

Copy link
Copy Markdown
Contributor

Moved to genkit-ai/genkit-python#95 along with the Python SDK.

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

Labels

config docs Improvements or additions to documentation python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants