Skip to content

Commit d25c358

Browse files
committed
Fix types and add test for common, completion, content, resources
1 parent bbfae7f commit d25c358

File tree

12 files changed

+1778
-74
lines changed

12 files changed

+1778
-74
lines changed

kotlin-sdk-core/api/kotlin-sdk-core.api

Lines changed: 65 additions & 29 deletions
Large diffs are not rendered by default.

kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.util.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ internal object ErrorCodeSerializer : KSerializer<ErrorCode> {
3838
}
3939
}
4040

41+
@Deprecated(
42+
message = "Use `MethodSerializer` instead",
43+
replaceWith = ReplaceWith("MethodSerializer"),
44+
level = DeprecationLevel.WARNING,
45+
)
4146
internal object RequestMethodSerializer : KSerializer<Method> {
4247
override val descriptor: SerialDescriptor =
4348
PrimitiveSerialDescriptor("io.modelcontextprotocol.kotlin.sdk.Method", PrimitiveKind.STRING)

kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types/completion.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.modelcontextprotocol.kotlin.sdk.types
22

33
import kotlinx.serialization.EncodeDefault
44
import kotlinx.serialization.ExperimentalSerializationApi
5+
import kotlinx.serialization.SerialName
56
import kotlinx.serialization.Serializable
67
import kotlinx.serialization.json.JsonObject
78

@@ -29,6 +30,7 @@ public data class CompleteRequestParams(
2930
val argument: Argument,
3031
val ref: Reference,
3132
val context: Context? = null,
33+
@SerialName("_meta")
3234
override val meta: RequestMeta? = null,
3335
) : RequestParams {
3436
/**
@@ -62,8 +64,11 @@ public data class CompleteRequestParams(
6264
* @property meta Optional metadata for this response. See MCP specification for details on _meta usage.
6365
*/
6466
@Serializable
65-
public data class CompleteResult(public val completion: Completion, override val meta: JsonObject? = null) :
66-
ServerResult {
67+
public data class CompleteResult(
68+
public val completion: Completion,
69+
@SerialName("_meta")
70+
override val meta: JsonObject? = null,
71+
) : ServerResult {
6772

6873
/**
6974
* Completion options and pagination information.

kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types/content.kt

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,34 @@ package io.modelcontextprotocol.kotlin.sdk.types
44

55
import kotlinx.serialization.EncodeDefault
66
import kotlinx.serialization.ExperimentalSerializationApi
7+
import kotlinx.serialization.SerialName
78
import kotlinx.serialization.Serializable
89
import kotlinx.serialization.json.JsonObject
910

1011
@Serializable
12+
public enum class ContentTypes(public val value: String) {
13+
@SerialName("text")
14+
TEXT("text"),
15+
16+
@SerialName("image")
17+
IMAGE("image"),
18+
19+
@SerialName("audio")
20+
AUDIO("audio"),
21+
22+
@SerialName("resource_link")
23+
RESOURCE_LINK("resource_link"),
24+
25+
@SerialName("resource")
26+
EMBEDDED_RESOURCE("resource"),
27+
}
28+
29+
@Serializable(with = ContentBlockPolymorphicSerializer::class)
1130
public sealed interface ContentBlock : WithMeta {
12-
public val type: String
31+
public val type: ContentTypes
1332
}
1433

15-
@Serializable
34+
@Serializable(with = MediaContentPolymorphicSerializer::class)
1635
public sealed interface MediaContent : ContentBlock
1736

1837
/**
@@ -27,10 +46,11 @@ public sealed interface MediaContent : ContentBlock
2746
public data class TextContent(
2847
val text: String,
2948
val annotations: Annotations? = null,
49+
@SerialName("_meta")
3050
override val meta: JsonObject? = null,
3151
) : MediaContent {
3252
@EncodeDefault
33-
public override val type: String = "text"
53+
public override val type: ContentTypes = ContentTypes.TEXT
3454
}
3555

3656
/**
@@ -47,10 +67,11 @@ public data class ImageContent(
4767
val data: String,
4868
val mimeType: String,
4969
val annotations: Annotations? = null,
70+
@SerialName("_meta")
5071
override val meta: JsonObject? = null,
5172
) : MediaContent {
5273
@EncodeDefault
53-
public override val type: String = "image"
74+
public override val type: ContentTypes = ContentTypes.IMAGE
5475
}
5576

5677
/**
@@ -67,10 +88,11 @@ public data class AudioContent(
6788
val data: String,
6889
val mimeType: String,
6990
val annotations: Annotations? = null,
91+
@SerialName("_meta")
7092
override val meta: JsonObject? = null,
7193
) : MediaContent {
7294
@EncodeDefault
73-
public override val type: String = "audio"
95+
public override val type: ContentTypes = ContentTypes.AUDIO
7496
}
7597

7698
/**
@@ -111,11 +133,12 @@ public data class ResourceLink(
111133
val icons: List<Icon>? = null,
112134
val description: String? = null,
113135
val annotations: Annotations? = null,
136+
@SerialName("_meta")
114137
override val meta: JsonObject? = null,
115138
) : ContentBlock,
116139
ResourceLike {
117140
@EncodeDefault
118-
public override val type: String = "resource_link"
141+
public override val type: ContentTypes = ContentTypes.RESOURCE_LINK
119142
}
120143

121144
/**
@@ -132,8 +155,9 @@ public data class ResourceLink(
132155
public data class EmbeddedResource(
133156
val resource: ResourceContents,
134157
val annotations: Annotations? = null,
158+
@SerialName("_meta")
135159
override val meta: JsonObject? = null,
136160
) : ContentBlock {
137161
@EncodeDefault
138-
public override val type: String = "resource"
162+
public override val type: ContentTypes = ContentTypes.EMBEDDED_RESOURCE
139163
}

kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types/methods.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import kotlinx.serialization.Serializable
55
/**
66
* Represents a method in the protocol, which can be predefined or custom.
77
*/
8-
@Serializable // TODO: Custom serializer or use SerialName
8+
@Serializable(with = MethodSerializer::class)
99
public sealed interface Method {
1010
public val value: String
1111

kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types/resources.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ public data class ResourceTemplateReference(val uri: String) : Reference {
118118
* @property mimeType The MIME type of this resource, if known.
119119
* @property meta Optional metadata for this response.
120120
*/
121-
@Serializable
122-
public sealed interface ResourceContents : WithMeta { // TODO: need serializer
121+
@Serializable(with = ResourceContentsPolymorphicSerializer::class)
122+
public sealed interface ResourceContents : WithMeta {
123123
public val uri: String
124124
public val mimeType: String?
125125
}
@@ -137,6 +137,7 @@ public data class TextResourceContents(
137137
val text: String,
138138
override val uri: String,
139139
override val mimeType: String? = null,
140+
@SerialName("_meta")
140141
override val meta: JsonObject? = null,
141142
) : ResourceContents
142143

@@ -152,6 +153,7 @@ public data class BlobResourceContents(
152153
val blob: String,
153154
override val uri: String,
154155
override val mimeType: String? = null,
156+
@SerialName("_meta")
155157
override val meta: JsonObject? = null,
156158
) : ResourceContents
157159

@@ -165,6 +167,7 @@ public data class BlobResourceContents(
165167
public data class UnknownResourceContents(
166168
override val uri: String,
167169
override val mimeType: String? = null,
170+
@SerialName("_meta")
168171
override val meta: JsonObject? = null,
169172
) : ResourceContents
170173

0 commit comments

Comments
 (0)