Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,22 @@ public class StreamableHttpClientTransport(
}
logger.debug { "Client SSE session started successfully." }
} catch (e: SSEClientException) {
if (e.response?.status == HttpStatusCode.MethodNotAllowed) {
val responseStatus = e.response?.status
val responseContentType = e.response?.contentType()
Comment on lines +238 to +239
Copy link

Copilot AI Oct 9, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider extracting the response object to a local variable to avoid repeated null-safe calls and improve readability: val response = e.response

Suggested change
val responseStatus = e.response?.status
val responseContentType = e.response?.contentType()
val response = e.response
val responseStatus = response?.status
val responseContentType = response?.contentType()

Copilot uses AI. Check for mistakes.

// 405 means server doesn't support SSE at GET endpoint - this is expected and valid
if (responseStatus == HttpStatusCode.MethodNotAllowed) {
logger.info { "Server returned 405 for GET/SSE, stream disabled." }
return
}

// If server returns application/json, it means it doesn't support SSE for this session
// This is valid per spec - server can choose to only use JSON responses
if (responseContentType?.match(ContentType.Application.Json) == true) {
logger.info { "Server returned application/json for GET/SSE, using JSON-only mode." }
return
}

_onError(e)
throw e
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.modelcontextprotocol.kotlin.sdk.client

import io.kotest.matchers.collections.shouldContain
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.sse.ServerSentEvent
import io.modelcontextprotocol.kotlin.sdk.ClientCapabilities
Expand Down Expand Up @@ -148,4 +149,40 @@ internal class StreamableHttpClientTest : AbstractStreamableHttpClientTest() {

client.close()
}

@Test
fun `handle streaming not supported`() = runBlocking {
val client = Client(
clientInfo = Implementation(name = "client2", version = "1.0.0"),
options = ClientOptions(
capabilities = ClientCapabilities(),
),
)

val sessionId = UUID.randomUUID().toString()

mockMcp.onInitialize(clientName = "client2", sessionId = sessionId)

mockMcp.handleJSONRPCRequest(
jsonRpcMethod = "notifications/initialized",
expectedSessionId = sessionId,
sessionId = sessionId,
statusCode = HttpStatusCode.Accepted,
)

mockMcp.onSubscribe(
httpMethod = HttpMethod.Get,
sessionId = sessionId,
) respondsWith {
headers += MCP_SESSION_ID_HEADER to sessionId
body = null
httpStatus = HttpStatusCode.UnsupportedMediaType
}

mockMcp.mockUnsubscribeRequest(sessionId = sessionId)

connect(client)

client.close()
}
}
Loading