* Add test to reproduce #52 * Solve issue #52 by improving url handling in SseClientTransport * Remove unnecessary url processing --------- Co-authored-by: Abdullatif.Ghajar <abdullatif.ghajar@jetbrains.com>
This commit is contained in:
parent
b0532d9e9b
commit
be4f9c37e0
2 changed files with 54 additions and 3 deletions
|
|
@ -39,7 +39,7 @@ public class SseClientTransport(
|
||||||
private var job: Job? = null
|
private var job: Job? = null
|
||||||
|
|
||||||
private val baseUrl by lazy {
|
private val baseUrl by lazy {
|
||||||
session.call.request.url.toString().removeSuffix("/")
|
session.call.request.url.toString().removeSuffix("/sse")
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun start() {
|
override suspend fun start() {
|
||||||
|
|
@ -52,7 +52,7 @@ public class SseClientTransport(
|
||||||
|
|
||||||
session = urlString?.let {
|
session = urlString?.let {
|
||||||
client.sseSession(
|
client.sseSession(
|
||||||
urlString = it,
|
urlString = "$it/sse",
|
||||||
reconnectionTime = reconnectionTime,
|
reconnectionTime = reconnectionTime,
|
||||||
block = requestBuilder,
|
block = requestBuilder,
|
||||||
)
|
)
|
||||||
|
|
@ -79,7 +79,7 @@ public class SseClientTransport(
|
||||||
val eventData = event.data ?: ""
|
val eventData = event.data ?: ""
|
||||||
|
|
||||||
// check url correctness
|
// check url correctness
|
||||||
val maybeEndpoint = Url("$baseUrl/$eventData")
|
val maybeEndpoint = Url(baseUrl + eventData)
|
||||||
|
|
||||||
endpoint.complete(maybeEndpoint.toString())
|
endpoint.complete(maybeEndpoint.toString())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|
|
||||||
51
src/jvmTest/kotlin/integration/SseIntegrationTest.kt
Normal file
51
src/jvmTest/kotlin/integration/SseIntegrationTest.kt
Normal file
|
|
@ -0,0 +1,51 @@
|
||||||
|
package integration
|
||||||
|
|
||||||
|
import io.ktor.client.*
|
||||||
|
import io.ktor.client.plugins.sse.*
|
||||||
|
import io.ktor.server.cio.*
|
||||||
|
import io.ktor.server.engine.*
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.Implementation
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.client.Client
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.client.mcpSse
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.server.Server
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.server.mcp
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.junit.jupiter.api.assertDoesNotThrow
|
||||||
|
import io.ktor.client.engine.cio.CIO as ClientCIO
|
||||||
|
import io.ktor.server.cio.CIO as ServerCIO
|
||||||
|
|
||||||
|
class SseIntegrationTest {
|
||||||
|
@Test
|
||||||
|
fun `client should be able to connect to sse server`() { // runTest will cause network timeout issues
|
||||||
|
runBlocking {
|
||||||
|
val serverEngine = initServer()
|
||||||
|
try {
|
||||||
|
assertDoesNotThrow { initClient() }
|
||||||
|
} finally {
|
||||||
|
// Make sure to stop the server
|
||||||
|
serverEngine.stop(1000, 2000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun initClient(): Client {
|
||||||
|
return HttpClient(ClientCIO) { install(SSE) }.mcpSse("http://$URL:$PORT")
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initServer(): EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration> {
|
||||||
|
val server = Server(
|
||||||
|
Implementation(name = "sse-e2e-test", version = "1.0.0"),
|
||||||
|
ServerOptions(capabilities = ServerCapabilities()),
|
||||||
|
)
|
||||||
|
|
||||||
|
return embeddedServer(ServerCIO, host = URL, port = PORT) { mcp { server } }.start(wait = false)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val PORT = 3001
|
||||||
|
private const val URL = "localhost"
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue