Introduce server sessions (#198)
Introduce a `ServerSession` to manage multiple connections to the mcp server. `Protocall` logic of the `Server` was moved to `ServerSession`, while MCP resources logic remained in `Server` ## Motivation and Context 1. Fix the multiple connection issues reported for SSE transport 2. Avoid server duplications during configurations: currently every connection creates a session, but the server stays single ## How Has This Been Tested? Added `SseIntegrationTest` and `WebSocketIntegrationTest` integration tests to check connection with single and multiple clients ## Breaking Changes This change caused several depreciations: 1. The `Server` -> `Protocol` inheritance (which caused single connection issue) should be removed in future releases, now, all the methods inherited from `Protocall` marked as `Deprecated` with a `Warning` 2. Websocket Ktor server initialization changes in order not to create server instance per connection ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [x] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions --> --------- Co-authored-by: devcrocod <devcrocod@gmail.com>
This commit is contained in:
parent
0ed92f5b03
commit
d86813636e
2 changed files with 1191 additions and 602 deletions
|
|
@ -23,6 +23,7 @@ import io.modelcontextprotocol.kotlin.sdk.TextResourceContents
|
|||
import io.modelcontextprotocol.kotlin.sdk.Tool
|
||||
import io.modelcontextprotocol.kotlin.sdk.server.Server
|
||||
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
|
||||
import io.modelcontextprotocol.kotlin.sdk.server.ServerSession
|
||||
import io.modelcontextprotocol.kotlin.sdk.server.SseServerTransport
|
||||
import io.modelcontextprotocol.kotlin.sdk.server.mcp
|
||||
|
||||
|
|
@ -94,33 +95,33 @@ fun configureServer(): Server {
|
|||
}
|
||||
|
||||
suspend fun runSseMcpServerWithPlainConfiguration(port: Int) {
|
||||
val servers = ConcurrentMap<String, Server>()
|
||||
val serverSessions = ConcurrentMap<String, ServerSession>()
|
||||
println("Starting sse server on port $port. ")
|
||||
println("Use inspector to connect to the http://localhost:$port/sse")
|
||||
|
||||
val server = configureServer()
|
||||
|
||||
embeddedServer(CIO, host = "0.0.0.0", port = port) {
|
||||
install(SSE)
|
||||
routing {
|
||||
sse("/sse") {
|
||||
val transport = SseServerTransport("/message", this)
|
||||
val server = configureServer()
|
||||
|
||||
// For SSE, you can also add prompts/tools/resources if needed:
|
||||
// server.addTool(...), server.addPrompt(...), server.addResource(...)
|
||||
|
||||
servers[transport.sessionId] = server
|
||||
val serverSession = server.connect(transport)
|
||||
serverSessions[transport.sessionId] = server.connect(transport)
|
||||
|
||||
server.onClose {
|
||||
serverSession.onClose {
|
||||
println("Server closed")
|
||||
servers.remove(transport.sessionId)
|
||||
serverSessions.remove(transport.sessionId)
|
||||
}
|
||||
|
||||
server.connect(transport)
|
||||
}
|
||||
post("/message") {
|
||||
println("Received Message")
|
||||
val sessionId: String = call.request.queryParameters["sessionId"]!!
|
||||
val transport = servers[sessionId]?.transport as? SseServerTransport
|
||||
val transport = serverSessions[sessionId]?.transport as? SseServerTransport
|
||||
if (transport == null) {
|
||||
call.respond(HttpStatusCode.NotFound, "Session not found")
|
||||
return@post
|
||||
|
|
|
|||
|
|
@ -114,9 +114,9 @@ fun `run mcp server`() {
|
|||
)
|
||||
|
||||
runBlocking {
|
||||
server.connect(transport)
|
||||
val session = server.connect(transport)
|
||||
val done = Job()
|
||||
server.onClose {
|
||||
session.onClose {
|
||||
done.complete()
|
||||
}
|
||||
done.join()
|
||||
|
|
|
|||
Loading…
Reference in a new issue