Skip to content

Commit 8cba254

Browse files
f.bobinkpavlov
authored andcommitted
fix handling different sse endpoints (relative/absolute)
1 parent fb69538 commit 8cba254

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

kotlin-sdk-client/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/client/SSEClientTransport.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import io.ktor.client.request.setBody
1010
import io.ktor.client.statement.bodyAsText
1111
import io.ktor.http.ContentType
1212
import io.ktor.http.HttpHeaders
13-
import io.ktor.http.Url
13+
import io.ktor.http.URLBuilder
1414
import io.ktor.http.append
1515
import io.ktor.http.isSuccess
16-
import io.ktor.http.protocolWithAuthority
16+
import io.ktor.http.takeFrom
1717
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
1818
import io.modelcontextprotocol.kotlin.sdk.shared.TransportSendOptions
1919
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
@@ -58,14 +58,7 @@ public class SseClientTransport(
5858
private var job: Job? = null
5959

6060
private val baseUrl: String by lazy {
61-
session.call.request.url.let { url ->
62-
val path = url.encodedPath
63-
when {
64-
path.isEmpty() -> url.protocolWithAuthority
65-
path.endsWith("/") -> url.protocolWithAuthority + path.removeSuffix("/")
66-
else -> url.protocolWithAuthority + path.take(path.lastIndexOf("/"))
67-
}
68-
}
61+
session.call.request.url.toString()
6962
}
7063

7164
override suspend fun start() {
@@ -159,10 +152,9 @@ public class SseClientTransport(
159152
}
160153
}
161154

162-
private fun handleEndpoint(eventData: String) {
155+
private fun handleEndpoint(endpointData: String) {
163156
try {
164-
val path = if (eventData.startsWith("/")) eventData.substring(1) else eventData
165-
val endpointUrl = Url("$baseUrl/$path")
157+
val endpointUrl = URLBuilder(baseUrl).takeFrom(endpointData).build()
166158
endpoint.complete(endpointUrl.toString())
167159
logger.debug { "Client connected to endpoint: $endpointUrl" }
168160
} catch (e: Throwable) {

0 commit comments

Comments
 (0)