Skip to content

Commit 67d1f77

Browse files
authored
Cleanup API (#29)
1 parent be6e473 commit 67d1f77

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public open class Server(
111111
* Called when the server connection is closing.
112112
* Invokes [onCloseCallback] if set.
113113
*/
114-
override fun onclose() {
114+
override fun onClose() {
115115
logger.info { "Server connection closing" }
116116
onCloseCallback?.invoke()
117117
}

src/main/kotlin/io/modelcontextprotocol/kotlin/sdk/shared/Protocol.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public open class ProtocolOptions(
4747
* Note that this DOES NOT affect checking of _local_ side capabilities, as it is
4848
* considered a logic error to mis-specify those.
4949
*
50-
* Currently this defaults to false, for backwards compatibility with SDK versions
50+
* Currently, this defaults to false, for backwards compatibility with SDK versions
5151
* that did not advertise capabilities correctly. In future, this will default to true.
5252
*/
5353
public var enforceStrictCapabilities: Boolean = false,
@@ -114,14 +114,14 @@ public abstract class Protocol(
114114
*
115115
* This is invoked when close() is called as well.
116116
*/
117-
public open fun onclose() {}
117+
public open fun onClose() {}
118118

119119
/**
120120
* Callback for when an error occurs.
121121
*
122122
* Note that errors are not necessarily fatal they are used for reporting any kind of exceptional condition out of band.
123123
*/
124-
public open fun onerror(error: Throwable) {}
124+
public open fun onError(error: Throwable) {}
125125

126126
/**
127127
* A handler to invoke for any request types that do not have their own handler installed.
@@ -136,7 +136,7 @@ public abstract class Protocol(
136136

137137
init {
138138
setNotificationHandler<ProgressNotification>(Method.Defined.NotificationsProgress) { notification ->
139-
this.onProgress(notification)
139+
onProgress(notification)
140140
COMPLETED
141141
}
142142

@@ -153,11 +153,11 @@ public abstract class Protocol(
153153
public open suspend fun connect(transport: Transport) {
154154
this.transport = transport
155155
transport.onClose = {
156-
this.onClose()
156+
doClose()
157157
}
158158

159159
transport.onError = {
160-
this.onError(it)
160+
onError(it)
161161
}
162162

163163
transport.onMessage = { message ->
@@ -172,22 +172,18 @@ public abstract class Protocol(
172172
return transport.start()
173173
}
174174

175-
private fun onClose() {
175+
private fun doClose() {
176176
responseHandlers.clear()
177177
progressHandlers.clear()
178178
transport = null
179-
onclose()
179+
onClose()
180180

181181
val error = McpError(ErrorCode.Defined.ConnectionClosed.code, "Connection closed")
182182
for (handler in responseHandlers.values) {
183183
handler(null, error)
184184
}
185185
}
186186

187-
private fun onError(error: Throwable) {
188-
onerror(error)
189-
}
190-
191187
private suspend fun onNotification(notification: JSONRPCNotification) {
192188
LOGGER.trace { "Received notification: ${notification.method}" }
193189
val function = notificationHandlers[notification.method]
@@ -208,7 +204,7 @@ public abstract class Protocol(
208204

209205
private suspend fun onRequest(request: JSONRPCRequest) {
210206
LOGGER.trace { "Received request: ${request.method} (id: ${request.id})" }
211-
val handler = requestHandlers[request.method] ?: this.fallbackRequestHandler
207+
val handler = requestHandlers[request.method] ?: fallbackRequestHandler
212208

213209
if (handler === null) {
214210
LOGGER.trace { "No handler found for request: ${request.method}" }
@@ -260,13 +256,13 @@ public abstract class Protocol(
260256
val total = notification.total
261257
val progressToken = notification.progressToken
262258

263-
val handler = this.progressHandlers[progressToken]
259+
val handler = progressHandlers[progressToken]
264260
if (handler == null) {
265261
val error = Error(
266262
"Received a progress notification for an unknown token: ${McpJson.encodeToString(notification)}",
267263
)
268264
LOGGER.error { error.message }
269-
this.onError(error)
265+
onError(error)
270266
return
271267
}
272268

@@ -275,14 +271,14 @@ public abstract class Protocol(
275271

276272
private fun onResponse(response: JSONRPCResponse?, error: JSONRPCError?) {
277273
val messageId = response?.id
278-
val handler = this.responseHandlers[messageId]
274+
val handler = responseHandlers[messageId]
279275
if (handler == null) {
280-
this.onError(Error("Received a response for an unknown message ID: ${McpJson.encodeToString(response)}"))
276+
onError(Error("Received a response for an unknown message ID: ${McpJson.encodeToString(response)}"))
281277
return
282278
}
283279

284-
this.responseHandlers.remove(messageId)
285-
this.progressHandlers.remove(messageId)
280+
responseHandlers.remove(messageId)
281+
progressHandlers.remove(messageId)
286282
if (response != null) {
287283
handler(response, null)
288284
} else {
@@ -469,7 +465,7 @@ public abstract class Protocol(
469465
* Note that this will replace any previous notification handler for the same method.
470466
*/
471467
public fun <T : Notification> setNotificationHandler(method: Method, handler: (notification: T) -> Deferred<Unit>) {
472-
this.notificationHandlers[method.value] = {
468+
notificationHandlers[method.value] = {
473469
@Suppress("UNCHECKED_CAST")
474470
handler(it.fromJSON() as T)
475471
}
@@ -479,6 +475,6 @@ public abstract class Protocol(
479475
* Removes the notification handler for the given method.
480476
*/
481477
public fun removeNotificationHandler(method: Method) {
482-
this.notificationHandlers.remove(method.value)
478+
notificationHandlers.remove(method.value)
483479
}
484480
}

0 commit comments

Comments
 (0)