Skip to content

Commit e7ed845

Browse files
tiginamariadevcrocod
authored andcommitted
Fix linter, add enums instead of strings in capabilities assertion
1 parent 5113fe0 commit e7ed845

File tree

12 files changed

+129
-134
lines changed

12 files changed

+129
-134
lines changed

kotlin-sdk-client/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/client/Client.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public open class Client(private val clientInfo: Implementation, options: Client
161161
close()
162162

163163
if (error !is CancellationException) {
164-
throw IllegalStateException("Error connecting to transport: ${error.message}")
164+
throw IllegalStateException("Error connecting to transport: ${error.message}", error)
165165
}
166166

167167
throw error

kotlin-sdk-client/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/client/WebSocketMcpKtorClientExtensions.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
99

1010
private val logger = KotlinLogging.logger {}
1111

12-
1312
/**
1413
* Returns a new WebSocket transport for the Model Context Protocol using the provided HttpClient.
1514
*

kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/shared/WebSocketMcpTransport.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public const val MCP_SUBPROTOCOL: String = "mcp"
2020

2121
private val logger = KotlinLogging.logger {}
2222

23-
2423
/**
2524
* Abstract class representing a WebSocket transport for the Model Context Protocol (MCP).
2625
* Handles communication over a WebSocket session.

kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/KtorServer.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,11 @@ internal fun ServerSSESession.mcpSseTransport(
104104
return transport
105105
}
106106

107-
internal suspend fun RoutingContext.mcpPostEndpoint(
108-
sseTransportManager: SseTransportManager,
109-
) {
110-
val sessionId: String = call.request.queryParameters["sessionId"]
111-
?: run {
112-
call.respond(HttpStatusCode.BadRequest, "sessionId query parameter is not provided")
113-
return
114-
}
107+
internal suspend fun RoutingContext.mcpPostEndpoint(sseTransportManager: SseTransportManager) {
108+
val sessionId: String = call.request.queryParameters["sessionId"] ?: run {
109+
call.respond(HttpStatusCode.BadRequest, "sessionId query parameter is not provided")
110+
return
111+
}
115112

116113
logger.debug { "Received message for sessionId: $sessionId" }
117114

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public class ServerOptions(public val capabilities: ServerCapabilities, enforceS
5454
* @param serverInfo Information about this server implementation (name, version).
5555
* @param options Configuration options for the server.
5656
*/
57-
public open class Server(
58-
private val serverInfo: Implementation,
59-
private val options: ServerOptions,
60-
) {
57+
public open class Server(private val serverInfo: Implementation, private val options: ServerOptions) {
6158
private val sessions = atomic(persistentListOf<ServerSession>())
6259

6360
@Suppress("ktlint:standard:backing-property-naming")
@@ -154,7 +151,7 @@ public open class Server(
154151
@Deprecated(
155152
"Initialization moved to ServerSession, use ServerSession.onInitialized instead.",
156153
ReplaceWith("ServerSession.onInitialized"),
157-
DeprecationLevel.WARNING
154+
DeprecationLevel.WARNING,
158155
)
159156
public fun onInitialized(block: () -> Unit) {
160157
val old = _onInitialized

kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerSession.kt

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import io.modelcontextprotocol.kotlin.sdk.ListRootsRequest
1818
import io.modelcontextprotocol.kotlin.sdk.ListRootsResult
1919
import io.modelcontextprotocol.kotlin.sdk.LoggingMessageNotification
2020
import io.modelcontextprotocol.kotlin.sdk.Method
21+
import io.modelcontextprotocol.kotlin.sdk.Method.Defined
2122
import io.modelcontextprotocol.kotlin.sdk.PingRequest
2223
import io.modelcontextprotocol.kotlin.sdk.PromptListChangedNotification
2324
import io.modelcontextprotocol.kotlin.sdk.ResourceListChangedNotification
@@ -31,11 +32,11 @@ import kotlinx.serialization.json.JsonObject
3132

3233
private val logger = KotlinLogging.logger {}
3334

34-
public open class ServerSession(
35-
private val serverInfo: Implementation,
36-
options: ServerOptions,
37-
) : Protocol(options) {
35+
public open class ServerSession(private val serverInfo: Implementation, options: ServerOptions) : Protocol(options) {
36+
@Suppress("ktlint:standard:backing-property-naming")
3837
private var _onInitialized: (() -> Unit) = {}
38+
39+
@Suppress("ktlint:standard:backing-property-naming")
3940
private var _onClose: () -> Unit = {}
4041

4142
init {
@@ -102,9 +103,7 @@ public open class ServerSession(
102103
* @return The result of the ping request.
103104
* @throws IllegalStateException If for some reason the method is not supported or the connection is closed.
104105
*/
105-
public suspend fun ping(): EmptyRequestResult {
106-
return request<EmptyRequestResult>(PingRequest())
107-
}
106+
public suspend fun ping(): EmptyRequestResult = request<EmptyRequestResult>(PingRequest())
108107

109108
/**
110109
* Creates a message using the server's sampling capability.
@@ -116,7 +115,7 @@ public open class ServerSession(
116115
*/
117116
public suspend fun createMessage(
118117
params: CreateMessageRequest,
119-
options: RequestOptions? = null
118+
options: RequestOptions? = null,
120119
): CreateMessageResult {
121120
logger.debug { "Creating message with params: $params" }
122121
return request<CreateMessageResult>(params, options)
@@ -132,7 +131,7 @@ public open class ServerSession(
132131
*/
133132
public suspend fun listRoots(
134133
params: JsonObject = EmptyJsonObject,
135-
options: RequestOptions? = null
134+
options: RequestOptions? = null,
136135
): ListRootsResult {
137136
logger.debug { "Listing roots with params: $params" }
138137
return request<ListRootsResult>(ListRootsRequest(params), options)
@@ -141,7 +140,7 @@ public open class ServerSession(
141140
public suspend fun createElicitation(
142141
message: String,
143142
requestedSchema: RequestedSchema,
144-
options: RequestOptions? = null
143+
options: RequestOptions? = null,
145144
): CreateElicitationResult {
146145
logger.debug { "Creating elicitation with message: $message" }
147146
return request(CreateElicitationRequest(message, requestedSchema), options)
@@ -201,29 +200,33 @@ public open class ServerSession(
201200
*/
202201
override fun assertCapabilityForMethod(method: Method) {
203202
logger.trace { "Asserting capability for method: ${method.value}" }
204-
when (method.value) {
205-
"sampling/createMessage" -> {
203+
when (method) {
204+
Defined.SamplingCreateMessage -> {
206205
if (clientCapabilities?.sampling == null) {
207206
logger.error { "Client capability assertion failed: sampling not supported" }
208207
throw IllegalStateException("Client does not support sampling (required for ${method.value})")
209208
}
210209
}
211210

212-
"roots/list" -> {
211+
Defined.RootsList -> {
213212
if (clientCapabilities?.roots == null) {
214213
throw IllegalStateException("Client does not support listing roots (required for ${method.value})")
215214
}
216215
}
217216

218-
"elicitation/create" -> {
217+
Defined.ElicitationCreate -> {
219218
if (clientCapabilities?.elicitation == null) {
220219
throw IllegalStateException("Client does not support elicitation (required for ${method.value})")
221220
}
222221
}
223222

224-
"ping" -> {
223+
Defined.Ping -> {
225224
// No specific capability required
226225
}
226+
227+
else -> {
228+
throw IllegalStateException("Server does not support $method")
229+
}
227230
}
228231
}
229232

@@ -236,37 +239,49 @@ public open class ServerSession(
236239
*/
237240
override fun assertNotificationCapability(method: Method) {
238241
logger.trace { "Asserting notification capability for method: ${method.value}" }
239-
when (method.value) {
240-
"notifications/message" -> {
242+
when (method) {
243+
Defined.NotificationsMessage -> {
241244
if (serverCapabilities.logging == null) {
242245
logger.error { "Server capability assertion failed: logging not supported" }
243246
throw IllegalStateException("Server does not support logging (required for ${method.value})")
244247
}
245248
}
246249

247-
"notifications/resources/updated",
248-
"notifications/resources/list_changed" -> {
250+
Defined.NotificationsResourcesUpdated,
251+
Defined.NotificationsResourcesListChanged,
252+
-> {
249253
if (serverCapabilities.resources == null) {
250-
throw IllegalStateException("Server does not support notifying about resources (required for ${method.value})")
254+
throw IllegalStateException(
255+
"Server does not support notifying about resources (required for ${method.value})",
256+
)
251257
}
252258
}
253259

254-
"notifications/tools/list_changed" -> {
260+
Defined.NotificationsResourcesListChanged -> {
255261
if (serverCapabilities.tools == null) {
256-
throw IllegalStateException("Server does not support notifying of tool list changes (required for ${method.value})")
262+
throw IllegalStateException(
263+
"Server does not support notifying of tool list changes (required for ${method.value})",
264+
)
257265
}
258266
}
259267

260-
"notifications/prompts/list_changed" -> {
268+
Defined.NotificationsPromptsListChanged -> {
261269
if (serverCapabilities.prompts == null) {
262-
throw IllegalStateException("Server does not support notifying of prompt list changes (required for ${method.value})")
270+
throw IllegalStateException(
271+
"Server does not support notifying of prompt list changes (required for ${method.value})",
272+
)
263273
}
264274
}
265275

266-
"notifications/cancelled",
267-
"notifications/progress" -> {
276+
Defined.NotificationsCancelled,
277+
Defined.NotificationsProgress,
278+
-> {
268279
// Always allowed
269280
}
281+
282+
else -> {
283+
throw IllegalStateException("Server does not support $method or it's not a notification method")
284+
}
270285
}
271286
}
272287

@@ -279,45 +294,54 @@ public open class ServerSession(
279294
*/
280295
override fun assertRequestHandlerCapability(method: Method) {
281296
logger.trace { "Asserting request handler capability for method: ${method.value}" }
282-
when (method.value) {
283-
"sampling/createMessage" -> {
297+
when (method) {
298+
Defined.SamplingCreateMessage -> {
284299
if (serverCapabilities.sampling == null) {
285300
logger.error { "Server capability assertion failed: sampling not supported" }
286301
throw IllegalStateException("Server does not support sampling (required for $method)")
287302
}
288303
}
289304

290-
"logging/setLevel" -> {
305+
Defined.LoggingSetLevel -> {
291306
if (serverCapabilities.logging == null) {
292307
throw IllegalStateException("Server does not support logging (required for $method)")
293308
}
294309
}
295310

296-
"prompts/get",
297-
"prompts/list" -> {
311+
Defined.PromptsGet,
312+
Defined.PromptsList,
313+
-> {
298314
if (serverCapabilities.prompts == null) {
299315
throw IllegalStateException("Server does not support prompts (required for $method)")
300316
}
301317
}
302318

303-
"resources/list",
304-
"resources/templates/list",
305-
"resources/read" -> {
319+
Defined.ResourcesList,
320+
Defined.ResourcesTemplatesList,
321+
Defined.ResourcesRead,
322+
Defined.ResourcesSubscribe,
323+
Defined.ResourcesUnsubscribe,
324+
-> {
306325
if (serverCapabilities.resources == null) {
307326
throw IllegalStateException("Server does not support resources (required for $method)")
308327
}
309328
}
310329

311-
"tools/call",
312-
"tools/list" -> {
330+
Defined.ToolsCall,
331+
Defined.ToolsList,
332+
-> {
313333
if (serverCapabilities.tools == null) {
314334
throw IllegalStateException("Server does not support tools (required for $method)")
315335
}
316336
}
317337

318-
"ping", "initialize" -> {
338+
Defined.Ping, Defined.Initialize -> {
319339
// No capability required
320340
}
341+
342+
else -> {
343+
throw IllegalStateException("Server does not support $method or it's not a request handler method")
344+
}
321345
}
322346
}
323347

@@ -330,14 +354,16 @@ public open class ServerSession(
330354
val protocolVersion = if (SUPPORTED_PROTOCOL_VERSIONS.contains(requestedVersion)) {
331355
requestedVersion
332356
} else {
333-
logger.warn { "Client requested unsupported protocol version $requestedVersion, falling back to $LATEST_PROTOCOL_VERSION" }
357+
logger.warn {
358+
"Client requested unsupported protocol version $requestedVersion, falling back to $LATEST_PROTOCOL_VERSION"
359+
}
334360
LATEST_PROTOCOL_VERSION
335361
}
336362

337363
return InitializeResult(
338364
protocolVersion = protocolVersion,
339365
capabilities = serverCapabilities,
340-
serverInfo = serverInfo
366+
serverInfo = serverInfo,
341367
)
342368
}
343369
}

0 commit comments

Comments
 (0)