-
Notifications
You must be signed in to change notification settings - Fork 186
test(integration): Add Streamable HTTP transport integration tests #486
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
Open
jskjw157
wants to merge
1
commit into
modelcontextprotocol:main
Choose a base branch
from
jskjw157:test/streamable-http-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,166
−47
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,27 @@ package io.modelcontextprotocol.kotlin.sdk.integration.kotlin | |
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.engine.cio.CIO | ||
| import io.ktor.client.plugins.sse.SSE | ||
| import io.ktor.serialization.kotlinx.json.json | ||
| import io.ktor.server.application.install | ||
| import io.ktor.server.engine.EmbeddedServer | ||
| import io.ktor.server.engine.embeddedServer | ||
| import io.ktor.server.plugins.contentnegotiation.ContentNegotiation | ||
| import io.ktor.server.routing.delete | ||
| import io.ktor.server.routing.get | ||
| import io.ktor.server.routing.post | ||
| import io.ktor.server.routing.route | ||
| import io.ktor.server.routing.routing | ||
| import io.modelcontextprotocol.kotlin.sdk.client.Client | ||
| import io.modelcontextprotocol.kotlin.sdk.client.SseClientTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.client.StdioClientTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.client.StreamableHttpClientTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.server.Server | ||
| import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions | ||
| import io.modelcontextprotocol.kotlin.sdk.server.StdioServerTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.server.StreamableHttpServerTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.server.mcp | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Implementation | ||
| import io.modelcontextprotocol.kotlin.sdk.types.McpJson | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities | ||
| import io.modelcontextprotocol.kotlin.test.utils.Retry | ||
| import kotlinx.coroutines.runBlocking | ||
|
|
@@ -44,21 +53,24 @@ abstract class KotlinTestBase { | |
| protected lateinit var serverEngine: EmbeddedServer<*, *> | ||
|
|
||
| // Transport selection | ||
| protected enum class TransportKind { SSE, STDIO } | ||
| protected enum class TransportKind { SSE, STDIO, STREAMABLE_HTTP } | ||
| protected open val transportKind: TransportKind = TransportKind.STDIO | ||
|
|
||
| // STDIO-specific fields | ||
| private var stdioServerTransport: StdioServerTransport? = null | ||
| private var stdioClientInput: Source? = null | ||
| private var stdioClientOutput: Sink? = null | ||
|
|
||
| // StreamableHTTP-specific fields | ||
| private var streamableHttpServerTransport: StreamableHttpServerTransport? = null | ||
|
Contributor
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. Just a heads-up: this should probably be in the streamable test subclass, but since we already have transport-specific fields here, let’s tackle it separately. |
||
|
|
||
| protected abstract fun configureServerCapabilities(): ServerCapabilities | ||
| protected abstract fun configureServer() | ||
|
|
||
| @BeforeEach | ||
| fun setUp() { | ||
| setupServer() | ||
| if (transportKind == TransportKind.SSE) { | ||
| if (transportKind == TransportKind.SSE || transportKind == TransportKind.STREAMABLE_HTTP) { | ||
| await | ||
| .ignoreExceptions() | ||
| .until { | ||
|
|
@@ -98,6 +110,19 @@ abstract class KotlinTestBase { | |
| ) | ||
| client.connect(transport) | ||
| } | ||
|
|
||
| TransportKind.STREAMABLE_HTTP -> { | ||
| val transport = StreamableHttpClientTransport( | ||
| client = HttpClient(CIO) { | ||
| install(SSE) | ||
| }, | ||
| url = "http://$host:$port/mcp", | ||
| ) | ||
| client = Client( | ||
| Implementation("test", "1.0"), | ||
| ) | ||
| client.connect(transport) | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -111,35 +136,77 @@ abstract class KotlinTestBase { | |
|
|
||
| configureServer() | ||
|
|
||
| if (transportKind == TransportKind.SSE) { | ||
| serverEngine = embeddedServer(ServerCIO, host = host, port = port) { | ||
| install(ServerSSE) | ||
| routing { | ||
| mcp { server } | ||
| when (transportKind) { | ||
| TransportKind.SSE -> { | ||
| serverEngine = embeddedServer(ServerCIO, host = host, port = port) { | ||
| install(ServerSSE) | ||
| routing { | ||
| mcp { server } | ||
| } | ||
| }.start(wait = false) | ||
| } | ||
|
|
||
| TransportKind.STREAMABLE_HTTP -> { | ||
| // Create StreamableHTTP server transport | ||
| // Using JSON response mode for simpler testing (no SSE session required) | ||
| val transport = StreamableHttpServerTransport( | ||
| enableJsonResponse = true, // Use JSON response mode for testing | ||
| ) | ||
| // Use stateless mode to skip session validation for simpler testing | ||
| transport.setSessionIdGenerator(null) | ||
| streamableHttpServerTransport = transport | ||
|
|
||
| // IMPORTANT: Create server session BEFORE starting the HTTP server | ||
| // This ensures message handlers are set up before any requests come in | ||
| runBlocking { | ||
| server.createSession(transport) | ||
| } | ||
|
|
||
| // Start embedded server with routing for StreamableHTTP | ||
| serverEngine = embeddedServer(ServerCIO, host = host, port = port) { | ||
| // Install ContentNegotiation for JSON serialization | ||
| install(ContentNegotiation) { | ||
| json(McpJson) | ||
| } | ||
| routing { | ||
| route("/mcp") { | ||
| post { | ||
| transport.handlePostRequest(null, call) | ||
| } | ||
| get { | ||
| transport.handleGetRequest(null, call) | ||
| } | ||
| delete { | ||
| transport.handleDeleteRequest(call) | ||
| } | ||
| } | ||
| } | ||
| }.start(wait = false) | ||
| } | ||
|
|
||
| TransportKind.STDIO -> { | ||
| // Create in-memory stdio pipes: client->server and server->client | ||
| val clientToServerOut = PipedOutputStream() | ||
| val clientToServerIn = PipedInputStream(clientToServerOut) | ||
|
|
||
| val serverToClientOut = PipedOutputStream() | ||
| val serverToClientIn = PipedInputStream(serverToClientOut) | ||
|
|
||
| // Server transport reads from client and writes to client | ||
| val serverTransport = StdioServerTransport( | ||
| inputStream = clientToServerIn.asSource().buffered(), | ||
| outputStream = serverToClientOut.asSink().buffered(), | ||
| ) | ||
| stdioServerTransport = serverTransport | ||
|
|
||
| // Prepare client-side streams for later client initialization | ||
| stdioClientInput = serverToClientIn.asSource().buffered() | ||
| stdioClientOutput = clientToServerOut.asSink().buffered() | ||
|
|
||
| // Start server transport by connecting the server | ||
| runBlocking { | ||
| server.createSession(serverTransport) | ||
| } | ||
| }.start(wait = false) | ||
| } else { | ||
| // Create in-memory stdio pipes: client->server and server->client | ||
| val clientToServerOut = PipedOutputStream() | ||
| val clientToServerIn = PipedInputStream(clientToServerOut) | ||
|
|
||
| val serverToClientOut = PipedOutputStream() | ||
| val serverToClientIn = PipedInputStream(serverToClientOut) | ||
|
|
||
| // Server transport reads from client and writes to client | ||
| val serverTransport = StdioServerTransport( | ||
| inputStream = clientToServerIn.asSource().buffered(), | ||
| outputStream = serverToClientOut.asSink().buffered(), | ||
| ) | ||
| stdioServerTransport = serverTransport | ||
|
|
||
| // Prepare client-side streams for later client initialization | ||
| stdioClientInput = serverToClientIn.asSource().buffered() | ||
| stdioClientOutput = clientToServerOut.asSink().buffered() | ||
|
|
||
| // Start server transport by connecting the server | ||
| runBlocking { | ||
| server.createSession(serverTransport) | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -160,24 +227,39 @@ abstract class KotlinTestBase { | |
| } | ||
|
|
||
| // stop server | ||
| if (transportKind == TransportKind.SSE) { | ||
| if (::serverEngine.isInitialized) { | ||
| try { | ||
| serverEngine.stop(500, 1000) | ||
| } catch (e: Exception) { | ||
| println("Warning: Error during server stop: ${e.message}") | ||
| when (transportKind) { | ||
| TransportKind.SSE, TransportKind.STREAMABLE_HTTP -> { | ||
| if (::serverEngine.isInitialized) { | ||
| try { | ||
| serverEngine.stop(500, 1000) | ||
| } catch (e: Exception) { | ||
| println("Warning: Error during server stop: ${e.message}") | ||
| } | ||
| } | ||
| if (transportKind == TransportKind.STREAMABLE_HTTP) { | ||
| streamableHttpServerTransport?.let { | ||
| try { | ||
| runBlocking { it.close() } | ||
| } catch (e: Exception) { | ||
| println("Warning: Error during streamable http server stop: ${e.message}") | ||
| } finally { | ||
| streamableHttpServerTransport = null | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| stdioServerTransport?.let { | ||
| try { | ||
| runBlocking { it.close() } | ||
| } catch (e: Exception) { | ||
| println("Warning: Error during stdio server stop: ${e.message}") | ||
| } finally { | ||
| stdioServerTransport = null | ||
| stdioClientInput = null | ||
| stdioClientOutput = null | ||
|
|
||
| TransportKind.STDIO -> { | ||
| stdioServerTransport?.let { | ||
| try { | ||
| runBlocking { it.close() } | ||
| } catch (e: Exception) { | ||
| println("Warning: Error during stdio server stop: ${e.message}") | ||
| } finally { | ||
| stdioServerTransport = null | ||
| stdioClientInput = null | ||
| stdioClientOutput = null | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.