Skip to content

Commit 8426228

Browse files
authored
Migrate to kotlin style callbacks. Close #14 (#36)
1 parent c2c0df5 commit 8426228

File tree

4 files changed

+64
-22
lines changed

4 files changed

+64
-22
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Add the dependency:
2929

3030
```kotlin
3131
dependencies {
32-
implementation("io.modelcontextprotocol:kotlin-sdk:0.2.0")
32+
implementation("io.modelcontextprotocol:kotlin-sdk:0.3.0")
3333
}
3434
```
3535

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

+33-9
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public class SSEClientTransport(
3333
private var session: ClientSSESession by Delegates.notNull()
3434
private val endpoint = CompletableDeferred<String>()
3535

36-
override var onClose: (() -> Unit)? = null
37-
override var onError: ((Throwable) -> Unit)? = null
38-
override var onMessage: (suspend ((JSONRPCMessage) -> Unit))? = null
36+
private var _onClose: (() -> Unit) = {}
37+
private var _onError: ((Throwable) -> Unit) = {}
38+
private var _onMessage: (suspend ((JSONRPCMessage) -> Unit)) = {}
3939

4040
private var job: Job? = null
4141

@@ -67,7 +67,7 @@ public class SSEClientTransport(
6767
when (event.event) {
6868
"error" -> {
6969
val e = IllegalStateException("SSE error: ${event.data}")
70-
onError?.invoke(e)
70+
_onError(e)
7171
throw e
7272
}
7373

@@ -84,7 +84,7 @@ public class SSEClientTransport(
8484

8585
endpoint.complete(maybeEndpoint.toString())
8686
} catch (e: Exception) {
87-
onError?.invoke(e)
87+
_onError(e)
8888
close()
8989
error(e)
9090
}
@@ -93,9 +93,9 @@ public class SSEClientTransport(
9393
else -> {
9494
try {
9595
val message = McpJson.decodeFromString<JSONRPCMessage>(event.data ?: "")
96-
onMessage?.invoke(message)
96+
_onMessage(message)
9797
} catch (e: Exception) {
98-
onError?.invoke(e)
98+
_onError(e)
9999
}
100100
}
101101
}
@@ -122,7 +122,7 @@ public class SSEClientTransport(
122122
error("Error POSTing to endpoint (HTTP ${response.status}): $text")
123123
}
124124
} catch (e: Exception) {
125-
onError?.invoke(e)
125+
_onError(e)
126126
throw e
127127
}
128128
}
@@ -133,7 +133,31 @@ public class SSEClientTransport(
133133
}
134134

135135
session.cancel()
136-
onClose?.invoke()
136+
_onClose()
137137
job?.cancelAndJoin()
138138
}
139+
140+
override fun onClose(block: () -> Unit) {
141+
val old = _onClose
142+
_onClose = {
143+
old()
144+
block()
145+
}
146+
}
147+
148+
override fun onError(block: (Throwable) -> Unit) {
149+
val old = _onError
150+
_onError = { e ->
151+
old(e)
152+
block(e)
153+
}
154+
}
155+
156+
override fun onMessage(block: suspend (JSONRPCMessage) -> Unit) {
157+
val old = _onMessage
158+
_onMessage = { message ->
159+
old(message)
160+
block(message)
161+
}
162+
}
139163
}

src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/Server.kt

+27-9
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ public class ServerOptions(
3535
public open class Server(
3636
private val serverInfo: Implementation,
3737
options: ServerOptions,
38-
public var onCloseCallback: (() -> Unit)? = null
3938
) : Protocol(options) {
39+
private var _onInitialized: (() -> Unit) = {}
40+
private var _onClose: () -> Unit = {}
4041

4142
/**
4243
* The client's reported capabilities after initialization.
@@ -49,18 +50,13 @@ public open class Server(
4950
*/
5051
public var clientVersion: Implementation? = null
5152
private set
53+
5254
private val capabilities: ServerCapabilities = options.capabilities
5355

5456
private val tools = mutableMapOf<String, RegisteredTool>()
5557
private val prompts = mutableMapOf<String, RegisteredPrompt>()
5658
private val resources = mutableMapOf<String, RegisteredResource>()
5759

58-
/**
59-
* A callback invoked when the server has completed the initialization sequence.
60-
* After initialization, the server is ready to handle requests.
61-
*/
62-
public var onInitialized: (() -> Unit)? = null
63-
6460
init {
6561
logger.debug { "Initializing MCP server with capabilities: $capabilities" }
6662

@@ -69,7 +65,7 @@ public open class Server(
6965
handleInitialize(request)
7066
}
7167
setNotificationHandler<InitializedNotification>(Method.Defined.NotificationsInitialized) {
72-
onInitialized?.invoke()
68+
_onInitialized()
7369
CompletableDeferred(Unit)
7470
}
7571

@@ -107,13 +103,35 @@ public open class Server(
107103
}
108104
}
109105

106+
/**
107+
* Registers a callback to be invoked when the server has completed initialization.
108+
*/
109+
public fun onInitalized(block: () -> Unit) {
110+
val old = _onInitialized
111+
_onInitialized = {
112+
old()
113+
block()
114+
}
115+
}
116+
117+
/**
118+
* Registers a callback to be invoked when the server connection is closing.
119+
*/
120+
public fun onClose(block: () -> Unit) {
121+
val old = _onClose
122+
_onClose = {
123+
old()
124+
block()
125+
}
126+
}
127+
110128
/**
111129
* Called when the server connection is closing.
112130
* Invokes [onCloseCallback] if set.
113131
*/
114132
override fun onClose() {
115133
logger.info { "Server connection closing" }
116-
onCloseCallback?.invoke()
134+
_onClose()
117135
}
118136

119137
/**

src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/shared/Transport.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ public interface Transport {
3131
*
3232
* This should be invoked when close() is called as well.
3333
*/
34-
public var onClose: (() -> Unit)?
34+
public fun onClose(block: () -> Unit)
3535

3636
/**
3737
* Callback for when an error occurs.
3838
*
3939
* Note that errors are not necessarily fatal; they are used for reporting any kind of
4040
* exceptional condition out of band.
4141
*/
42-
public var onError: ((Throwable) -> Unit)?
42+
public fun onError(block: (Throwable) -> Unit)
4343

4444
/**
4545
* Callback for when a message (request or response) is received over the connection.
4646
*/
47-
public var onMessage: (suspend ((JSONRPCMessage) -> Unit))?
47+
public fun onMessage(block: suspend (JSONRPCMessage) -> Unit)
4848
}

0 commit comments

Comments
 (0)