Skip to content

fix: websocket connection closes immediately #175

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -7,6 +7,7 @@ import io.modelcontextprotocol.kotlin.sdk.Implementation
import io.modelcontextprotocol.kotlin.sdk.LIB_VERSION
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
import kotlinx.coroutines.job

/**
* Registers a WebSocket route that establishes an MCP (Model Context Protocol) server session.
Expand Down Expand Up @@ -98,7 +99,15 @@ private suspend fun Route.createMcpServer(

server.connect(transport)
handler(server)
server.close()

// Wait for the WebSocket session to complete instead of closing immediately
// This keeps the connection alive while the transport handles messages
try {
// The transport's message handling loop will keep running until the client disconnects
session.coroutineContext.job.join()
} finally {
server.close()
}
}

private fun createMcpTransport(
Expand Down