Skip to content

Commit 9f816bf

Browse files
committed
add ktlint and cleanup code
1 parent e5214fe commit 9f816bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+809
-756
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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
alias(libs.plugins.kotlin.atomicfu)
1313
alias(libs.plugins.dokka)
1414
alias(libs.plugins.jreleaser)
15+
alias(libs.plugins.ktlint)
1516
`maven-publish`
1617
signing
1718
alias(libs.plugins.kotlinx.binary.compatibility.validator)
@@ -66,9 +67,9 @@ jreleaser {
6667
if (publication is MavenPublication) {
6768
val pubName = publication.name
6869

69-
if (!pubName.contains("jvm", ignoreCase = true)
70-
&& !pubName.contains("metadata", ignoreCase = true)
71-
&& !pubName.contains("kotlinMultiplatform", ignoreCase = true)
70+
if (!pubName.contains("jvm", ignoreCase = true) &&
71+
!pubName.contains("metadata", ignoreCase = true) &&
72+
!pubName.contains("kotlinMultiplatform", ignoreCase = true)
7273
) {
7374

7475
artifactOverride {
@@ -188,7 +189,7 @@ abstract class GenerateLibVersionTask @Inject constructor(
188189
189190
public const val LIB_VERSION: String = "$libVersion"
190191
191-
""".trimIndent()
192+
""".trimIndent(),
192193
)
193194
}
194195
}
@@ -276,3 +277,8 @@ kotlin {
276277
}
277278
}
278279
}
280+
281+
ktlint {
282+
verbose = true
283+
outputToConsole = true
284+
}

gradle/libs.versions.toml

Lines changed: 3 additions & 1 deletion
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"
@@ -42,5 +43,6 @@ kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref
4243
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
4344
kotlin-atomicfu = { id = "org.jetbrains.kotlinx.atomicfu", version.ref = "atomicfu" }
4445
dokka = { id = "org.jetbrains.dokka", version.ref = "dokka" }
45-
jreleaser = { id = "org.jreleaser", version.ref = "jreleaser"}
46+
ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "ktlint" }
47+
jreleaser = { id = "org.jreleaser", version.ref = "jreleaser" }
4648
kotlinx-binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompatibilityValidatorPlugin" }

settings.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,3 @@ dependencyResolutionManagement {
1616
}
1717

1818
rootProject.name = "kotlin-sdk"
19-

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

Lines changed: 34 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,13 @@ 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 = request(PingRequest(), options)
293286

294287
/**
295288
* Sends a completion request to the server, typically to generate or complete some content.
@@ -299,9 +292,8 @@ public open class Client(
299292
* @return The completion result returned by the server, or `null` if none.
300293
* @throws IllegalStateException If the server does not support prompts or completion.
301294
*/
302-
public suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult? {
303-
return request<CompleteResult>(params, options)
304-
}
295+
public suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult =
296+
request(params, options)
305297

306298
/**
307299
* Sets the logging level on the server.
@@ -310,9 +302,8 @@ public open class Client(
310302
* @param options Optional request options.
311303
* @throws IllegalStateException If the server does not support logging.
312304
*/
313-
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult {
314-
return request<EmptyRequestResult>(SetLevelRequest(level), options)
315-
}
305+
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult =
306+
request(SetLevelRequest(level), options)
316307

317308
/**
318309
* Retrieves a prompt by name from the server.
@@ -322,9 +313,8 @@ public open class Client(
322313
* @return The requested prompt details, or `null` if not found.
323314
* @throws IllegalStateException If the server does not support prompts.
324315
*/
325-
public suspend fun getPrompt(request: GetPromptRequest, options: RequestOptions? = null): GetPromptResult? {
326-
return request<GetPromptResult>(request, options)
327-
}
316+
public suspend fun getPrompt(request: GetPromptRequest, options: RequestOptions? = null): GetPromptResult =
317+
request(request, options)
328318

329319
/**
330320
* Lists all available prompts from the server.
@@ -337,9 +327,7 @@ public open class Client(
337327
public suspend fun listPrompts(
338328
request: ListPromptsRequest = ListPromptsRequest(),
339329
options: RequestOptions? = null,
340-
): ListPromptsResult? {
341-
return request<ListPromptsResult>(request, options)
342-
}
330+
): ListPromptsResult = request(request, options)
343331

344332
/**
345333
* Lists all available resources from the server.
@@ -352,9 +340,7 @@ public open class Client(
352340
public suspend fun listResources(
353341
request: ListResourcesRequest = ListResourcesRequest(),
354342
options: RequestOptions? = null,
355-
): ListResourcesResult? {
356-
return request<ListResourcesResult>(request, options)
357-
}
343+
): ListResourcesResult = request(request, options)
358344

359345
/**
360346
* Lists resource templates available on the server.
@@ -367,9 +353,7 @@ public open class Client(
367353
public suspend fun listResourceTemplates(
368354
request: ListResourceTemplatesRequest,
369355
options: RequestOptions? = null,
370-
): ListResourceTemplatesResult? {
371-
return request<ListResourceTemplatesResult>(request, options)
372-
}
356+
): ListResourceTemplatesResult = request(request, options)
373357

374358
/**
375359
* Reads a resource from the server by its URI.
@@ -382,9 +366,7 @@ public open class Client(
382366
public suspend fun readResource(
383367
request: ReadResourceRequest,
384368
options: RequestOptions? = null,
385-
): ReadResourceResult? {
386-
return request<ReadResourceResult>(request, options)
387-
}
369+
): ReadResourceResult = request(request, options)
388370

389371
/**
390372
* Subscribes to resource changes on the server.
@@ -396,9 +378,7 @@ public open class Client(
396378
public suspend fun subscribeResource(
397379
request: SubscribeRequest,
398380
options: RequestOptions? = null,
399-
): EmptyRequestResult {
400-
return request<EmptyRequestResult>(request, options)
401-
}
381+
): EmptyRequestResult = request(request, options)
402382

403383
/**
404384
* Unsubscribes from resource changes on the server.
@@ -410,9 +390,7 @@ public open class Client(
410390
public suspend fun unsubscribeResource(
411391
request: UnsubscribeRequest,
412392
options: RequestOptions? = null,
413-
): EmptyRequestResult {
414-
return request<EmptyRequestResult>(request, options)
415-
}
393+
): EmptyRequestResult = request(request, options)
416394

417395
/**
418396
* Calls a tool on the server by name, passing the specified arguments.
@@ -443,7 +421,7 @@ public open class Client(
443421

444422
val request = CallToolRequest(
445423
name = name,
446-
arguments = JsonObject(jsonArguments)
424+
arguments = JsonObject(jsonArguments),
447425
)
448426
return callTool(request, compatibility, options)
449427
}
@@ -461,12 +439,10 @@ public open class Client(
461439
request: CallToolRequest,
462440
compatibility: Boolean = false,
463441
options: RequestOptions? = null,
464-
): CallToolResultBase? {
465-
return if (compatibility) {
466-
request<CompatibilityCallToolResult>(request, options)
467-
} else {
468-
request<CallToolResult>(request, options)
469-
}
442+
): CallToolResultBase? = if (compatibility) {
443+
request<CompatibilityCallToolResult>(request, options)
444+
} else {
445+
request<CallToolResult>(request, options)
470446
}
471447

472448
/**
@@ -480,9 +456,7 @@ public open class Client(
480456
public suspend fun listTools(
481457
request: ListToolsRequest = ListToolsRequest(),
482458
options: RequestOptions? = null,
483-
): ListToolsResult? {
484-
return request<ListToolsResult>(request, options)
485-
}
459+
): ListToolsResult = request(request, options)
486460

487461
/**
488462
* Registers a single root.
@@ -491,10 +465,7 @@ public open class Client(
491465
* @param name A human-readable name for the root.
492466
* @throws IllegalStateException If the client does not support roots.
493467
*/
494-
public fun addRoot(
495-
uri: String,
496-
name: String,
497-
) {
468+
public fun addRoot(uri: String, name: String) {
498469
if (capabilities.roots == null) {
499470
logger.error { "Failed to add root '$name': Client does not support roots capability" }
500471
throw IllegalStateException("Client does not support roots capability.")

0 commit comments

Comments
 (0)