Skip to content
Merged
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
8 changes: 5 additions & 3 deletions kotlin-sdk-server/api/kotlin-sdk-server.api
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ public final class io/modelcontextprotocol/kotlin/sdk/server/RegisteredTool {
}

public class io/modelcontextprotocol/kotlin/sdk/server/Server {
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Ljava/lang/String;)V
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Lkotlin/jvm/functions/Function0;)V
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Lkotlin/jvm/functions/Function0;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun addPrompt (Lio/modelcontextprotocol/kotlin/sdk/Prompt;Lkotlin/jvm/functions/Function2;)V
public final fun addPrompt (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Lkotlin/jvm/functions/Function2;)V
public static synthetic fun addPrompt$default (Lio/modelcontextprotocol/kotlin/sdk/server/Server;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
Expand All @@ -61,6 +62,7 @@ public class io/modelcontextprotocol/kotlin/sdk/server/Server {
public final fun addTools (Ljava/util/List;)V
public final fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun connect (Lio/modelcontextprotocol/kotlin/sdk/shared/Transport;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun createSession (Lio/modelcontextprotocol/kotlin/sdk/shared/Transport;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
protected final fun getInstructionsProvider ()Lkotlin/jvm/functions/Function0;
protected final fun getOptions ()Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;
public final fun getPrompts ()Ljava/util/Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ internal suspend fun ServerSSESession.mcpSseEndpoint(
sseTransportManager.removeTransport(transport.sessionId)
}

server.connect(transport)
server.createSession(transport)

logger.debug { "Server connected to transport for sessionId: ${transport.sessionId}" }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,29 @@ public class ServerOptions(public val capabilities: ServerCapabilities, enforceS
* @param options Configuration options for the server.
* @param instructionsProvider Optional provider for instructions from the server to the client about how to use
* this server. The provider is called each time a new session is started to support dynamic instructions.
* @param block A block to configure the mcp server.
*/

public open class Server(
protected val serverInfo: Implementation,
protected val options: ServerOptions,
protected val instructionsProvider: (() -> String)? = null,
block: Server.() -> Unit = {},
) {
/**
* Alternative constructor that provides the instructions directly as a string.
*
* @param serverInfo Information about this server implementation (name, version).
* @param options Configuration options for the server.
* @param instructions Instructions from the server to the client about how to use this server.
* @param block A block to configure the mcp server.
*/
public constructor(
serverInfo: Implementation,
options: ServerOptions,
instructions: String,
) : this(serverInfo, options, { instructions })
block: Server.() -> Unit = {},
) : this(serverInfo, options, { instructions }, block)

private val sessions = atomic(persistentListOf<ServerSession>())

Expand All @@ -98,6 +102,10 @@ public open class Server(
public val resources: Map<String, RegisteredResource>
get() = _resources.value

init {
block(this)
}

public suspend fun close() {
logger.debug { "Closing MCP server" }
sessions.value.forEach { it.close() }
Expand All @@ -111,7 +119,21 @@ public open class Server(
* @param transport The transport layer to connect the session with.
* @return The initialized and connected server session.
*/
public suspend fun connect(transport: Transport): ServerSession {
@Deprecated(
"Use createSession(transport) instead.",
ReplaceWith("createSession(transport)"),
DeprecationLevel.WARNING,
)
public suspend fun connect(transport: Transport): ServerSession = createSession(transport)

/**
* Starts a new server session with the given transport and initializes
* internal request handlers based on the server's capabilities.
*
* @param transport The transport layer to connect the session with.
* @return The initialized and connected server session.
*/
public suspend fun createSession(transport: Transport): ServerSession {
val session = ServerSession(serverInfo, options, instructionsProvider?.invoke())

// Internal handlers for tools
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal suspend fun WebSocketServerSession.mcpWebSocketEndpoint(block: () -> Se
val server = block()
var session: ServerSession? = null
try {
session = server.connect(transport)
session = server.createSession(transport)
awaitCancellation()
} catch (e: CancellationException) {
session?.close()
Expand Down Expand Up @@ -103,7 +103,7 @@ public fun Route.mcpWebSocket(options: ServerOptions? = null, handler: suspend S
)
public fun Route.mcpWebSocket(block: () -> Server) {
webSocket {
block().connect(createMcpTransport(this))
block().createSession(createMcpTransport(this))
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ private suspend fun Route.createMcpServer(
),
)

server.connect(transport)
server.createSession(transport)
handler(server)
server.close()
}
1 change: 0 additions & 1 deletion kotlin-sdk-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ kotlin {
}
jvmTest {
dependencies {
implementation(kotlin("test-junit5"))
implementation(libs.awaitility)
runtimeOnly(libs.slf4j.simple)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class ClientTest {
client.connect(clientTransport)
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
},
).joinAll()

Expand Down Expand Up @@ -322,7 +322,7 @@ class ClientTest {
println("Client connected")
},
launch {
server.connect(serverTransport)
server.createSession(serverTransport)
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -379,7 +379,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -439,7 +439,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -502,7 +502,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -594,7 +594,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -680,7 +680,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -812,7 +812,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -859,7 +859,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down Expand Up @@ -939,7 +939,7 @@ class ClientTest {
println("Client connected")
},
launch {
serverSessionResult.complete(server.connect(serverTransport))
serverSessionResult.complete(server.createSession(serverTransport))
println("Server connected")
},
).joinAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SseTransportTest : BaseTransportTest() {

val actualPort = server.actualPort()

val client = HttpClient {
val transport = HttpClient {
install(ClientSSE)
}.mcpSseTransport {
url {
Expand All @@ -58,7 +58,7 @@ class SseTransportTest : BaseTransportTest() {
}

try {
testClientOpenClose(client)
testTransportRead(transport)
} finally {
server.stopSuspend()
}
Expand All @@ -76,7 +76,7 @@ class SseTransportTest : BaseTransportTest() {

val actualPort = server.actualPort()

val client = HttpClient {
val transport = HttpClient {
install(ClientSSE)
}.mcpSseTransport {
url {
Expand All @@ -86,7 +86,7 @@ class SseTransportTest : BaseTransportTest() {
}

try {
testClientRead(client)
testTransportRead(transport)
} finally {
server.stopSuspend()
}
Expand All @@ -104,7 +104,7 @@ class SseTransportTest : BaseTransportTest() {

val actualPort = server.actualPort()

val client = HttpClient {
val transport = HttpClient {
install(ClientSSE)
}.mcpSseTransport {
url {
Expand All @@ -115,7 +115,7 @@ class SseTransportTest : BaseTransportTest() {
}

try {
testClientRead(client)
testTransportRead(transport)
} finally {
server.stopSuspend()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WebSocketTransportTest : BaseTransportTest() {
install(io.ktor.client.plugins.websocket.WebSockets)
}.mcpWebSocketTransport()

testClientOpenClose(client)
testTransportOpenClose(client)
}

@Test
Expand All @@ -41,11 +41,11 @@ class WebSocketTransportTest : BaseTransportTest() {
}
}

val client = createClient {
val transport = createClient {
install(io.ktor.client.plugins.websocket.WebSockets)
}.mcpWebSocketTransport()

testClientRead(client)
testTransportRead(transport)

clientFinished.complete(Unit)
}
Expand Down
Loading
Loading