-
Couldn't load subscription status.
- Fork 169
Add Server Streamable Http Transport #235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import io.github.oshai.kotlinlogging.KotlinLogging | |
| import io.ktor.http.HttpStatusCode | ||
| import io.ktor.server.application.Application | ||
| import io.ktor.server.application.install | ||
| import io.ktor.server.request.header | ||
| import io.ktor.server.response.respond | ||
| import io.ktor.server.routing.Routing | ||
| import io.ktor.server.routing.RoutingContext | ||
|
|
@@ -15,6 +16,7 @@ import io.ktor.server.sse.ServerSSESession | |
| import io.ktor.server.sse.sse | ||
| import io.ktor.util.collections.ConcurrentMap | ||
| import io.ktor.utils.io.KtorDsl | ||
| import io.modelcontextprotocol.kotlin.sdk.ErrorCode | ||
|
|
||
| private val logger = KotlinLogging.logger {} | ||
|
|
||
|
|
@@ -64,6 +66,51 @@ public fun Application.mcp(block: ServerSSESession.() -> Server) { | |
| } | ||
| } | ||
|
|
||
| @KtorDsl | ||
| public fun Application.mcpStreamableHttp( | ||
| enableDnsRebindingProtection: Boolean = false, | ||
| allowedHosts: List<String>? = null, | ||
| allowedOrigins: List<String>? = null, | ||
| eventStore: EventStore? = null, | ||
| block: RoutingContext.() -> Server, | ||
| ) { | ||
| val transports = ConcurrentMap<String, StreamableHttpServerTransport>() | ||
|
|
||
| routing { | ||
| post("/mcp") { | ||
| mcpStreamableHttpEndpoint( | ||
| transports, | ||
| enableDnsRebindingProtection, | ||
| allowedHosts, | ||
| allowedOrigins, | ||
| eventStore, | ||
| block, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @KtorDsl | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing KDoc |
||
| public fun Application.mcpStatelessStreamableHttp( | ||
| enableDnsRebindingProtection: Boolean = false, | ||
| allowedHosts: List<String>? = null, | ||
| allowedOrigins: List<String>? = null, | ||
| eventStore: EventStore? = null, | ||
| block: RoutingContext.() -> Server, | ||
| ) { | ||
| routing { | ||
| post("/mcp") { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| mcpStatelessStreamableHttpEndpoint( | ||
| enableDnsRebindingProtection, | ||
| allowedHosts, | ||
| allowedOrigins, | ||
| eventStore, | ||
| block, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private suspend fun ServerSSESession.mcpSseEndpoint( | ||
| postEndpoint: String, | ||
| transports: ConcurrentMap<String, SseServerTransport>, | ||
|
|
@@ -94,6 +141,88 @@ internal fun ServerSSESession.mcpSseTransport( | |
| return transport | ||
| } | ||
|
|
||
| private suspend fun RoutingContext.mcpStreamableHttpEndpoint( | ||
| transports: ConcurrentMap<String, StreamableHttpServerTransport>, | ||
| enableDnsRebindingProtection: Boolean = false, | ||
| allowedHosts: List<String>? = null, | ||
| allowedOrigins: List<String>? = null, | ||
| eventStore: EventStore? = null, | ||
| block: RoutingContext.() -> Server, | ||
| ) { | ||
| val sessionId = this.call.request.header(MCP_SESSION_ID_HEADER) | ||
| val transport = if (sessionId != null && transports.containsKey(sessionId)) { | ||
| transports[sessionId]!! | ||
| } else if (sessionId == null) { | ||
| val transport = StreamableHttpServerTransport( | ||
| enableDnsRebindingProtection = enableDnsRebindingProtection, | ||
| allowedHosts = allowedHosts, | ||
| allowedOrigins = allowedOrigins, | ||
| eventStore = eventStore, | ||
| enableJsonResponse = true, | ||
| ) | ||
|
|
||
| transport.setOnSessionInitialized { sessionId -> | ||
| transports[sessionId] = transport | ||
|
|
||
| logger.info { "New StreamableHttp connection established and stored with sessionId: $sessionId" } | ||
| } | ||
|
|
||
| val server = block() | ||
| server.onClose { | ||
| logger.info { "Server connection closed for sessionId: ${transport.sessionId}" } | ||
| } | ||
|
|
||
| server.connect(transport) | ||
|
|
||
| transport | ||
| } else { | ||
| null | ||
| } | ||
|
|
||
| if (transport == null) { | ||
| this.call.reject( | ||
| HttpStatusCode.BadRequest, | ||
| ErrorCode.Unknown(-32000), | ||
| "Bad Request: No valid session ID provided", | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| transport.handleRequest(null, this.call) | ||
| logger.debug { "Server connected to transport for sessionId: ${transport.sessionId}" } | ||
| } | ||
|
|
||
| private suspend fun RoutingContext.mcpStatelessStreamableHttpEndpoint( | ||
| enableDnsRebindingProtection: Boolean = false, | ||
| allowedHosts: List<String>? = null, | ||
| allowedOrigins: List<String>? = null, | ||
| eventStore: EventStore? = null, | ||
| block: RoutingContext.() -> Server, | ||
| ) { | ||
| val transport = StreamableHttpServerTransport( | ||
| enableDnsRebindingProtection = enableDnsRebindingProtection, | ||
| allowedHosts = allowedHosts, | ||
| allowedOrigins = allowedOrigins, | ||
| eventStore = eventStore, | ||
| enableJsonResponse = true, | ||
| ) | ||
| transport.setSessionIdGenerator(null) | ||
|
|
||
| logger.info { "New stateless StreamableHttp connection established without sessionId" } | ||
|
|
||
| val server = block() | ||
|
|
||
| server.onClose { | ||
| logger.info { "Server connection closed without sessionId" } | ||
| } | ||
|
|
||
| server.connect(transport) | ||
|
|
||
| transport.handleRequest(null, this.call) | ||
|
|
||
| logger.debug { "Server connected to transport without sessionId" } | ||
| } | ||
|
|
||
| internal suspend fun RoutingContext.mcpPostEndpoint(transports: ConcurrentMap<String, SseServerTransport>) { | ||
| val sessionId: String = call.request.queryParameters["sessionId"] | ||
| ?: run { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the spec, the
idis required field