Skip to content

Commit 6d5c725

Browse files
committed
Cleanup API
1 parent d8a8f89 commit 6d5c725

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
@@ -112,7 +112,7 @@ public open class Server(
112112
* Called when the server connection is closing.
113113
* Invokes [onCloseCallback] if set.
114114
*/
115-
override fun onclose() {
115+
override fun onClose() {
116116
logger.info { "Server connection closing" }
117117
onCloseCallback?.invoke()
118118
}

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

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public open class ProtocolOptions(
6464
* Note that this DOES NOT affect checking of _local_ side capabilities, as it is
6565
* considered a logic error to mis-specify those.
6666
*
67-
* Currently this defaults to false, for backwards compatibility with SDK versions
67+
* Currently, this defaults to false, for backwards compatibility with SDK versions
6868
* that did not advertise capabilities correctly. In future, this will default to true.
6969
*/
7070
public var enforceStrictCapabilities: Boolean = false,
@@ -131,14 +131,14 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
131131
*
132132
* This is invoked when close() is called as well.
133133
*/
134-
public open fun onclose() {}
134+
public open fun onClose() {}
135135

136136
/**
137137
* Callback for when an error occurs.
138138
*
139139
* Note that errors are not necessarily fatal they are used for reporting any kind of exceptional condition out of band.
140140
*/
141-
public open fun onerror(error: Throwable) {}
141+
public open fun onError(error: Throwable) {}
142142

143143
/**
144144
* A handler to invoke for any request types that do not have their own handler installed.
@@ -153,7 +153,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
153153

154154
init {
155155
setNotificationHandler<ProgressNotification>(Method.Defined.NotificationsProgress) { notification ->
156-
this.onProgress(notification)
156+
onProgress(notification)
157157
COMPLETED
158158
}
159159

@@ -171,11 +171,11 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
171171
public open suspend fun connect(transport: Transport) {
172172
this.transport = transport
173173
transport.onClose = {
174-
this.onClose()
174+
doClose()
175175
}
176176

177177
transport.onError = {
178-
this.onError(it)
178+
onError(it)
179179
}
180180

181181
transport.onMessage = { message ->
@@ -190,7 +190,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
190190
return transport.start()
191191
}
192192

193-
private fun onClose() {
193+
private fun doClose() {
194194
val error = McpError(ErrorCode.Defined.ConnectionClosed.code, "Connection closed")
195195
for (handler in responseHandlers.values) {
196196
handler(null, error)
@@ -199,11 +199,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
199199
responseHandlers.clear()
200200
progressHandlers.clear()
201201
transport = null
202-
onclose()
203-
}
204-
205-
private fun onError(error: Throwable) {
206-
onerror(error)
202+
onClose()
207203
}
208204

209205
private suspend fun onNotification(notification: JSONRPCNotification) {
@@ -226,7 +222,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
226222

227223
private suspend fun onRequest(request: JSONRPCRequest) {
228224
LOGGER.trace { "Received request: ${request.method} (id: ${request.id})" }
229-
val handler = requestHandlers[request.method] ?: this.fallbackRequestHandler
225+
val handler = requestHandlers[request.method] ?: fallbackRequestHandler
230226

231227
if (handler === null) {
232228
LOGGER.trace { "No handler found for request: ${request.method}" }
@@ -278,13 +274,13 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
278274
val total = notification.total
279275
val progressToken = notification.progressToken
280276

281-
val handler = this.progressHandlers[progressToken]
277+
val handler = progressHandlers[progressToken]
282278
if (handler == null) {
283279
val error = Error(
284280
"Received a progress notification for an unknown token: ${McpJson.encodeToString(notification)}",
285281
)
286282
LOGGER.error { error.message }
287-
this.onError(error)
283+
onError(error)
288284
return
289285
}
290286

@@ -293,14 +289,14 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
293289

294290
private fun onResponse(response: JSONRPCResponse?, error: JSONRPCError?) {
295291
val messageId = response?.id
296-
val handler = this.responseHandlers[messageId]
292+
val handler = responseHandlers[messageId]
297293
if (handler == null) {
298-
this.onError(Error("Received a response for an unknown message ID: ${McpJson.encodeToString(response)}"))
294+
onError(Error("Received a response for an unknown message ID: ${McpJson.encodeToString(response)}"))
299295
return
300296
}
301297

302-
this.responseHandlers.remove(messageId)
303-
this.progressHandlers.remove(messageId)
298+
responseHandlers.remove(messageId)
299+
progressHandlers.remove(messageId)
304300
if (response != null) {
305301
handler(response, null)
306302
} else {
@@ -488,7 +484,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
488484
* Note that this will replace any previous notification handler for the same method.
489485
*/
490486
public fun <T : Notification> setNotificationHandler(method: Method, handler: (notification: T) -> Deferred<Unit>) {
491-
this.notificationHandlers[method.value] = {
487+
notificationHandlers[method.value] = {
492488
@Suppress("UNCHECKED_CAST")
493489
handler(it.fromJSON() as T)
494490
}
@@ -498,6 +494,6 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
498494
* Removes the notification handler for the given method.
499495
*/
500496
public fun removeNotificationHandler(method: Method) {
501-
this.notificationHandlers.remove(method.value)
497+
notificationHandlers.remove(method.value)
502498
}
503499
}

0 commit comments

Comments
 (0)