Skip to content

Commit 8c0a6aa

Browse files
committed
Refactor SseTransportTest to use route for better clarity and update server handling with suspend functions.
1 parent 9a153e3 commit 8c0a6aa

File tree

2 files changed

+17
-41
lines changed

2 files changed

+17
-41
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import io.ktor.http.HttpHeaders
1212
import io.ktor.http.Url
1313
import io.ktor.http.append
1414
import io.ktor.http.isSuccess
15+
import io.ktor.http.protocolWithAuthority
1516
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
1617
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
1718
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
@@ -24,7 +25,6 @@ import kotlinx.coroutines.SupervisorJob
2425
import kotlinx.coroutines.cancel
2526
import kotlinx.coroutines.cancelAndJoin
2627
import kotlinx.coroutines.launch
27-
import kotlinx.serialization.encodeToString
2828
import kotlin.concurrent.atomics.AtomicBoolean
2929
import kotlin.concurrent.atomics.ExperimentalAtomicApi
3030
import kotlin.properties.Delegates

src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/SseTransportTest.kt

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.ktor.server.application.install
66
import io.ktor.server.cio.CIO
77
import io.ktor.server.engine.embeddedServer
88
import io.ktor.server.routing.post
9+
import io.ktor.server.routing.route
910
import io.ktor.server.routing.routing
1011
import io.ktor.server.sse.sse
1112
import io.ktor.util.collections.ConcurrentMap
@@ -85,65 +86,40 @@ class SseTransportTest : BaseTransportTest() {
8586

8687
@Test
8788
fun `test sse path not root path`() = runTest {
88-
val server = embeddedServer(CIO, port = PORT) {
89+
val port = 3007
90+
val server = embeddedServer(CIO, port = port) {
8991
install(io.ktor.server.sse.SSE)
9092
val transports = ConcurrentMap<String, SseServerTransport>()
9193
routing {
92-
sse("/sse") {
93-
mcpSseTransport("/messages", transports).apply {
94-
onMessage {
95-
send(it)
94+
route("/sse") {
95+
sse {
96+
mcpSseTransport("", transports).apply {
97+
onMessage {
98+
send(it)
99+
}
100+
101+
start()
96102
}
97-
98-
start()
99103
}
100-
}
101-
102-
post("/messages") {
103-
104-
mcpPostEndpoint(transports)
105-
}
106-
}
107-
}.start(wait = false)
108-
109-
val client = HttpClient {
110-
install(SSE)
111-
}.mcpSseTransport {
112-
url {
113-
host = "localhost"
114-
port = PORT
115-
pathSegments = listOf("sse")
116-
}
117-
}
118-
119-
testClientRead(client)
120-
server.stop()
121-
}
122104

123-
@Test
124-
fun `test sse path not root path`() = runTest {
125-
val server = embeddedServer(CIO, port = PORT) {
126-
install(io.ktor.server.sse.SSE)
127-
routing {
128-
mcpSseTransport(path = "/sse", incomingPath = "/messages") {
129-
onMessage = {
130-
send(it)
105+
post {
106+
mcpPostEndpoint(transports)
131107
}
132108
}
133109
}
134-
}.start(wait = false)
110+
}.startSuspend(wait = false)
135111

136112
val client = HttpClient {
137113
install(SSE)
138114
}.mcpSseTransport {
139115
url {
140116
host = "localhost"
141-
port = PORT
117+
this.port = port
142118
pathSegments = listOf("sse")
143119
}
144120
}
145121

146122
testClientRead(client)
147-
server.stop()
123+
server.stopSuspend()
148124
}
149125
}

0 commit comments

Comments
 (0)