Skip to content

Commit 91cf120

Browse files
authored
Add ktlint (#213)
* Add ktlint * Simplify `initClient` function and clean imports in SseIntegrationTest.
1 parent b0f0759 commit 91cf120

File tree

40 files changed

+800
-758
lines changed

40 files changed

+800
-758
lines changed

.editorconfig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
max_line_length = 120
10+
11+
[*.json]
12+
indent_size = 2
13+
14+
[{*.yaml,*.yml}]
15+
indent_size = 2
16+
17+
[*.{kt,kts}]
18+
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL
19+
20+
# Disable wildcard imports entirely
21+
ij_kotlin_name_count_to_use_star_import = 2147483647
22+
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
23+
ij_kotlin_packages_to_use_import_on_demand = unset
24+
25+
ktlint_code_style = intellij_idea
26+
ktlint_experimental = enabled
27+
ktlint_standard_filename = disabled
28+
ktlint_standard_no-empty-first-line-in-class-body = disabled
29+
ktlint_class_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 4
30+
ktlint_function_signature_rule_force_multiline_when_parameter_count_greater_or_equal_than = 4
31+
ktlint_standard_chain-method-continuation = disabled
32+
ktlint_ignore_back_ticked_identifier = true
33+
ktlint_standard_multiline-expression-wrapping = disabled
34+
ktlint_standard_when-entry-bracing = disabled
35+
36+
[*/build/**/*]
37+
ktlint = disabled

build.gradle.kts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
plugins {
2+
alias(libs.plugins.ktlint)
3+
}
4+
15
allprojects {
26
group = "io.modelcontextprotocol"
37
version = "0.6.0"
4-
}
8+
}
9+
10+
subprojects {
11+
apply(plugin = "org.jlleitschuh.gradle.ktlint")
12+
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
kotlin = "2.2.0"
44
dokka = "2.0.0"
55
atomicfu = "0.29.0"
6+
ktlint = "13.0.0"
67

78
# libraries version
89
serialization = "1.9.0"
@@ -57,6 +58,7 @@ ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serializatio
5758

5859
[plugins]
5960
kotlinx-binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibilityValidatorPlugin" }
61+
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
6062

6163
# Samples
6264
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

kotlin-sdk-client/build.gradle.kts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@ plugins {
1111
}
1212

1313
kotlin {
14-
iosArm64(); iosX64(); iosSimulatorArm64()
15-
watchosX64(); watchosArm64(); watchosSimulatorArm64()
16-
tvosX64(); tvosArm64(); tvosSimulatorArm64()
14+
iosArm64()
15+
iosX64()
16+
iosSimulatorArm64()
17+
watchosX64()
18+
watchosArm64()
19+
watchosSimulatorArm64()
20+
tvosX64()
21+
tvosArm64()
22+
tvosSimulatorArm64()
1723
js {
1824
browser()
1925
nodejs()
@@ -40,4 +46,4 @@ kotlin {
4046
}
4147
}
4248
}
43-
}
49+
}

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

Lines changed: 35 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ public class ClientOptions(
8080
* @param clientInfo Information about the client implementation (name, version).
8181
* @param options Configuration options for this client.
8282
*/
83-
public open class Client(
84-
private val clientInfo: Implementation,
85-
options: ClientOptions = ClientOptions(),
86-
) : Protocol(options) {
83+
public open class Client(private val clientInfo: Implementation, options: ClientOptions = ClientOptions()) :
84+
Protocol(options) {
8785

8886
/**
8987
* Retrieves the server's reported capabilities after the initialization process completes.
@@ -144,13 +142,13 @@ public open class Client(
144142
val message = InitializeRequest(
145143
protocolVersion = LATEST_PROTOCOL_VERSION,
146144
capabilities = capabilities,
147-
clientInfo = clientInfo
145+
clientInfo = clientInfo,
148146
)
149147
val result = request<InitializeResult>(message)
150148

151149
if (!SUPPORTED_PROTOCOL_VERSIONS.contains(result.protocolVersion)) {
152150
throw IllegalStateException(
153-
"Server's protocol version is not supported: ${result.protocolVersion}"
151+
"Server's protocol version is not supported: ${result.protocolVersion}",
154152
)
155153
}
156154

@@ -165,11 +163,9 @@ public open class Client(
165163
}
166164

167165
throw error
168-
169166
}
170167
}
171168

172-
173169
override fun assertCapabilityForMethod(method: Method) {
174170
when (method) {
175171
Method.Defined.LoggingSetLevel -> {
@@ -181,7 +177,7 @@ public open class Client(
181177
Method.Defined.PromptsGet,
182178
Method.Defined.PromptsList,
183179
Method.Defined.CompletionComplete,
184-
-> {
180+
-> {
185181
if (serverCapabilities?.prompts == null) {
186182
throw IllegalStateException("Server does not support prompts (required for $method)")
187183
}
@@ -192,28 +188,28 @@ public open class Client(
192188
Method.Defined.ResourcesRead,
193189
Method.Defined.ResourcesSubscribe,
194190
Method.Defined.ResourcesUnsubscribe,
195-
-> {
191+
-> {
196192
val resCaps = serverCapabilities?.resources
197193
?: error("Server does not support resources (required for $method)")
198194

199195
if (method == Method.Defined.ResourcesSubscribe && resCaps.subscribe != true) {
200196
throw IllegalStateException(
201-
"Server does not support resource subscriptions (required for $method)"
197+
"Server does not support resource subscriptions (required for $method)",
202198
)
203199
}
204200
}
205201

206202
Method.Defined.ToolsCall,
207203
Method.Defined.ToolsList,
208-
-> {
204+
-> {
209205
if (serverCapabilities?.tools == null) {
210206
throw IllegalStateException("Server does not support tools (required for $method)")
211207
}
212208
}
213209

214210
Method.Defined.Initialize,
215211
Method.Defined.Ping,
216-
-> {
212+
-> {
217213
// No specific capability required
218214
}
219215

@@ -228,15 +224,15 @@ public open class Client(
228224
Method.Defined.NotificationsRootsListChanged -> {
229225
if (capabilities.roots?.listChanged != true) {
230226
throw IllegalStateException(
231-
"Client does not support roots list changed notifications (required for $method)"
227+
"Client does not support roots list changed notifications (required for $method)",
232228
)
233229
}
234230
}
235231

236232
Method.Defined.NotificationsInitialized,
237233
Method.Defined.NotificationsCancelled,
238234
Method.Defined.NotificationsProgress,
239-
-> {
235+
-> {
240236
// Always allowed
241237
}
242238

@@ -251,23 +247,23 @@ public open class Client(
251247
Method.Defined.SamplingCreateMessage -> {
252248
if (capabilities.sampling == null) {
253249
throw IllegalStateException(
254-
"Client does not support sampling capability (required for $method)"
250+
"Client does not support sampling capability (required for $method)",
255251
)
256252
}
257253
}
258254

259255
Method.Defined.RootsList -> {
260256
if (capabilities.roots == null) {
261257
throw IllegalStateException(
262-
"Client does not support roots capability (required for $method)"
258+
"Client does not support roots capability (required for $method)",
263259
)
264260
}
265261
}
266262

267263
Method.Defined.ElicitationCreate -> {
268264
if (capabilities.elicitation == null) {
269265
throw IllegalStateException(
270-
"Client does not support elicitation capability (required for $method)"
266+
"Client does not support elicitation capability (required for $method)",
271267
)
272268
}
273269
}
@@ -280,16 +276,14 @@ public open class Client(
280276
}
281277
}
282278

283-
284279
/**
285280
* Sends a ping request to the server to check connectivity.
286281
*
287282
* @param options Optional request options.
288283
* @throws IllegalStateException If the server does not support the ping method (unlikely).
289284
*/
290-
public suspend fun ping(options: RequestOptions? = null): EmptyRequestResult {
291-
return request<EmptyRequestResult>(PingRequest(), options)
292-
}
285+
public suspend fun ping(options: RequestOptions? = null): EmptyRequestResult =
286+
request<EmptyRequestResult>(PingRequest(), options)
293287

294288
/**
295289
* Sends a completion request to the server, typically to generate or complete some content.
@@ -299,9 +293,8 @@ public open class Client(
299293
* @return The completion result returned by the server, or `null` if none.
300294
* @throws IllegalStateException If the server does not support prompts or completion.
301295
*/
302-
public suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult {
303-
return request(params, options)
304-
}
296+
public suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult =
297+
request(params, options)
305298

306299
/**
307300
* Sets the logging level on the server.
@@ -310,9 +303,8 @@ public open class Client(
310303
* @param options Optional request options.
311304
* @throws IllegalStateException If the server does not support logging.
312305
*/
313-
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult {
314-
return request<EmptyRequestResult>(SetLevelRequest(level), options)
315-
}
306+
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult =
307+
request<EmptyRequestResult>(SetLevelRequest(level), options)
316308

317309
/**
318310
* Retrieves a prompt by name from the server.
@@ -322,9 +314,8 @@ public open class Client(
322314
* @return The requested prompt details, or `null` if not found.
323315
* @throws IllegalStateException If the server does not support prompts.
324316
*/
325-
public suspend fun getPrompt(request: GetPromptRequest, options: RequestOptions? = null): GetPromptResult {
326-
return request(request, options)
327-
}
317+
public suspend fun getPrompt(request: GetPromptRequest, options: RequestOptions? = null): GetPromptResult =
318+
request(request, options)
328319

329320
/**
330321
* Lists all available prompts from the server.
@@ -337,9 +328,7 @@ public open class Client(
337328
public suspend fun listPrompts(
338329
request: ListPromptsRequest = ListPromptsRequest(),
339330
options: RequestOptions? = null,
340-
): ListPromptsResult {
341-
return request(request, options)
342-
}
331+
): ListPromptsResult = request(request, options)
343332

344333
/**
345334
* Lists all available resources from the server.
@@ -352,9 +341,7 @@ public open class Client(
352341
public suspend fun listResources(
353342
request: ListResourcesRequest = ListResourcesRequest(),
354343
options: RequestOptions? = null,
355-
): ListResourcesResult {
356-
return request(request, options)
357-
}
344+
): ListResourcesResult = request(request, options)
358345

359346
/**
360347
* Lists resource templates available on the server.
@@ -367,9 +354,7 @@ public open class Client(
367354
public suspend fun listResourceTemplates(
368355
request: ListResourceTemplatesRequest,
369356
options: RequestOptions? = null,
370-
): ListResourceTemplatesResult {
371-
return request(request, options)
372-
}
357+
): ListResourceTemplatesResult = request(request, options)
373358

374359
/**
375360
* Reads a resource from the server by its URI.
@@ -382,9 +367,7 @@ public open class Client(
382367
public suspend fun readResource(
383368
request: ReadResourceRequest,
384369
options: RequestOptions? = null,
385-
): ReadResourceResult {
386-
return request(request, options)
387-
}
370+
): ReadResourceResult = request(request, options)
388371

389372
/**
390373
* Subscribes to resource changes on the server.
@@ -396,9 +379,7 @@ public open class Client(
396379
public suspend fun subscribeResource(
397380
request: SubscribeRequest,
398381
options: RequestOptions? = null,
399-
): EmptyRequestResult {
400-
return request(request, options)
401-
}
382+
): EmptyRequestResult = request(request, options)
402383

403384
/**
404385
* Unsubscribes from resource changes on the server.
@@ -410,9 +391,7 @@ public open class Client(
410391
public suspend fun unsubscribeResource(
411392
request: UnsubscribeRequest,
412393
options: RequestOptions? = null,
413-
): EmptyRequestResult {
414-
return request(request, options)
415-
}
394+
): EmptyRequestResult = request(request, options)
416395

417396
/**
418397
* Calls a tool on the server by name, passing the specified arguments.
@@ -443,7 +422,7 @@ public open class Client(
443422

444423
val request = CallToolRequest(
445424
name = name,
446-
arguments = JsonObject(jsonArguments)
425+
arguments = JsonObject(jsonArguments),
447426
)
448427
return callTool(request, compatibility, options)
449428
}
@@ -461,12 +440,10 @@ public open class Client(
461440
request: CallToolRequest,
462441
compatibility: Boolean = false,
463442
options: RequestOptions? = null,
464-
): CallToolResultBase? {
465-
return if (compatibility) {
466-
request<CompatibilityCallToolResult>(request, options)
467-
} else {
468-
request<CallToolResult>(request, options)
469-
}
443+
): CallToolResultBase? = if (compatibility) {
444+
request<CompatibilityCallToolResult>(request, options)
445+
} else {
446+
request<CallToolResult>(request, options)
470447
}
471448

472449
/**
@@ -480,9 +457,7 @@ public open class Client(
480457
public suspend fun listTools(
481458
request: ListToolsRequest = ListToolsRequest(),
482459
options: RequestOptions? = null,
483-
): ListToolsResult {
484-
return request(request, options)
485-
}
460+
): ListToolsResult = request(request, options)
486461

487462
/**
488463
* Registers a single root.
@@ -491,10 +466,7 @@ public open class Client(
491466
* @param name A human-readable name for the root.
492467
* @throws IllegalStateException If the client does not support roots.
493468
*/
494-
public fun addRoot(
495-
uri: String,
496-
name: String,
497-
) {
469+
public fun addRoot(uri: String, name: String) {
498470
if (capabilities.roots == null) {
499471
logger.error { "Failed to add root '$name': Client does not support roots capability" }
500472
throw IllegalStateException("Client does not support roots capability.")

0 commit comments

Comments
 (0)