Skip to content
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

sse server does not process endpoint correctly when sse path is not a directory #43

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -39,7 +39,18 @@ public class SseClientTransport(
private var job: Job? = null

private val baseUrl by lazy {
session.call.request.url.toString().removeSuffix("/")
val requestUrl = session.call.request.url.toString()
val url = Url(requestUrl)
var path = url.encodedPath
if (path.isEmpty()) {
url.protocolWithAuthority
} else if (path.endsWith("/")) {
url.protocolWithAuthority + path.removeSuffix("/")
} else {
// the last item is not a directory, so will not be taken into account
path = path.substring(0, path.lastIndexOf("/"))
url.protocolWithAuthority + path
}
}

override suspend fun start() {
Expand Down Expand Up @@ -79,8 +90,7 @@ public class SseClientTransport(
val eventData = event.data ?: ""

// check url correctness
val maybeEndpoint = Url("$baseUrl/$eventData")

val maybeEndpoint = Url("$baseUrl/${if (eventData.startsWith("/")) eventData.substring(1) else eventData}")
endpoint.complete(maybeEndpoint.toString())
} catch (e: Exception) {
_onError(e)
Expand Down
64 changes: 64 additions & 0 deletions src/jvmTest/kotlin/client/SseTransportTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,68 @@ class SseTransportTest : BaseTransportTest() {
testClientRead(client)
server.stop()
}

@Test
fun `test sse path not root path`() = runTest {
val server = embeddedServer(CIO, port = PORT) {
install(io.ktor.server.sse.SSE)
val transports = ConcurrentMap<String, SseServerTransport>()
routing {
sse("/sse") {
mcpSseTransport("/messages", transports).apply {
onMessage {
send(it)
}

start()
}
}

post("/messages") {

mcpPostEndpoint(transports)
}
}
}.start(wait = false)

val client = HttpClient {
install(SSE)
}.mcpSseTransport {
url {
host = "localhost"
port = PORT
pathSegments = listOf("sse")
}
}

testClientRead(client)
server.stop()
}

@Test
fun `test sse path not root path`() = runTest {
val server = embeddedServer(CIO, port = PORT) {
install(io.ktor.server.sse.SSE)
routing {
mcpSseTransport(path = "/sse", incomingPath = "/messages") {
onMessage = {
send(it)
}
}
}
}.start(wait = false)

val client = HttpClient {
install(SSE)
}.mcpSseTransport {
url {
host = "localhost"
port = PORT
pathSegments = listOf("sse")
}
}

testClientRead(client)
server.stop()
}
}
Loading