mcpStreamableHttp
Configures the Ktor Application to handle Model Context Protocol (MCP) over Streamable HTTP Transport
Sets up SSE, HTTP POST, and DELETE endpoints at the specified path. Simple request/response pairs are returned as JSON (not SSE streams).
Automatically installs ContentNegotiation with McpJson and SSE. An already installed SSE plugin is reused.
A session expires after sessionIdleTimeout without requests (an open GET stream does not count), after which its id answers 404 Not Found. At most maxSessions sessions are open at once; beyond that, a new session is refused with 503 Service Unavailable.
Also installs HttpRequestLifecycle with cancelCallOnClose on the route, unless it is already installed.
Parameters
The base path for the MCP Streamable HTTP endpoint. Defaults to "/mcp".
Enables DNS rebinding attack protection for the endpoint. Defaults to true.
A list of hostnames allowed to access the endpoint. If null and DNS rebinding protection is enabled, defaults to localhost, 127.0.0.1, [::1].
A list of allowed Origin header values, compared by hostname only (scheme and port are ignored). Requests without an Origin header are allowed. When null while the localhost host defaults are in effect (no custom allowedHosts), the Origin header is validated against localhost, 127.0.0.1, [::1]. With custom allowedHosts, null skips origin validation.
An optional EventStore instance to enable resumable event stream functionality. Allows storing and replaying events.
The heartbeat configuration option for SSE connections. null means no heartbeat is sent.
How long a session may go without activity before it is closed. Defaults to 30 minutes. Expiry is checked every few seconds, so a session can outlive this by up to 5 seconds. Duration.INFINITE disables expiry, so abandoned sessions pile up until maxSessions is reached.
The maximum number of sessions open at once, counting those still initializing. Defaults to 10,000.
factory block with access to the RoutingContext (for reading request headers) that creates and returns the Server to handle the connection.
Throws
if sessionIdleTimeout or maxSessions is not positive.