|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.types |
| 2 | + |
| 3 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 4 | +import kotlinx.serialization.SerialName |
| 5 | +import kotlinx.serialization.Serializable |
| 6 | +import kotlinx.serialization.json.JsonClassDiscriminator |
| 7 | +import kotlinx.serialization.json.JsonObject |
| 8 | +import kotlin.jvm.JvmInline |
| 9 | + |
| 10 | +// ============================================================================ |
| 11 | +// Protocol Version Constants |
| 12 | +// ============================================================================ |
| 13 | + |
| 14 | +public const val LATEST_PROTOCOL_VERSION: String = "2025-03-26" |
| 15 | + |
| 16 | +public val SUPPORTED_PROTOCOL_VERSIONS: Array<String> = arrayOf( |
| 17 | + LATEST_PROTOCOL_VERSION, |
| 18 | + "2024-11-05", |
| 19 | +) |
| 20 | + |
| 21 | +// ============================================================================ |
| 22 | +// Base Interfaces |
| 23 | +// ============================================================================ |
| 24 | + |
| 25 | +/** |
| 26 | + * Represents an entity that includes additional metadata in its responses. |
| 27 | + */ |
| 28 | +// TODO: реализовать meta |
| 29 | +// The _meta property/parameter is reserved by MCP to allow clients and servers to attach additional metadata to their interactions. |
| 30 | +// Certain key names are reserved by MCP for protocol-level metadata, as specified below; implementations MUST NOT make assumptions about values at these keys. |
| 31 | +// Additionally, definitions in the schema may reserve particular names for purpose-specific metadata, as declared in those definitions. |
| 32 | +// Key name format: valid _meta key names have two segments: an optional prefix, and a name. |
| 33 | +// Prefix: |
| 34 | +// If specified, MUST be a series of labels separated by dots (.), followed by a slash (/). |
| 35 | +// Labels MUST start with a letter and end with a letter or digit; interior characters can be letters, digits, or hyphens (-). |
| 36 | +// Any prefix beginning with zero or more valid labels, followed by modelcontextprotocol or mcp, followed by any valid label, is reserved for MCP use. |
| 37 | +// For example: modelcontextprotocol.io/, mcp.dev/, api.modelcontextprotocol.org/, and tools.mcp.com/ are all reserved. |
| 38 | +// Name: |
| 39 | +// Unless empty, MUST begin and end with an alphanumeric character ([a-z0-9A-Z]). |
| 40 | +// MAY contain hyphens (-), underscores (_), dots (.), and alphanumerics in between. |
| 41 | +@Serializable |
| 42 | +public sealed interface WithMeta { |
| 43 | + @SerialName("_meta") |
| 44 | + public val meta: JsonObject? |
| 45 | +} |
| 46 | + |
| 47 | +// ============================================================================ |
| 48 | +// Tokens |
| 49 | +// ============================================================================ |
| 50 | + |
| 51 | +/** |
| 52 | + * A progress token, used to associate progress notifications with the original request. |
| 53 | + */ |
| 54 | +@Serializable // TODO custom serializer |
| 55 | +public sealed interface ProgressToken { |
| 56 | + @JvmInline |
| 57 | + @Serializable |
| 58 | + public value class ProgressTokenString(public val value: String) : ProgressToken |
| 59 | + |
| 60 | + @JvmInline |
| 61 | + @Serializable |
| 62 | + public value class ProgressTokenInt(public val value: Long) : ProgressToken |
| 63 | +} |
| 64 | + |
| 65 | +// ============================================================================ |
| 66 | +// Visual Elements |
| 67 | +// ============================================================================ |
| 68 | + |
| 69 | +/** |
| 70 | + * An optionally sized icon that can be displayed in a user interface. |
| 71 | + * |
| 72 | + * Icons help clients provide visual branding and identification for MCP implementations. |
| 73 | + * |
| 74 | + * **Security considerations:** |
| 75 | + * - Consumers SHOULD ensure URLs serving icons are from the same domain as the client/server |
| 76 | + * or a trusted domain to prevent malicious content. |
| 77 | + * - Consumers SHOULD take appropriate precautions when rendering SVGs as they can contain |
| 78 | + * executable JavaScript. Consider sanitizing SVG content or rendering in isolated contexts. |
| 79 | + * |
| 80 | + * @property src A standard URI pointing to an icon resource. |
| 81 | + * Maybe an HTTP/HTTPS URL or a data: URI with Base64-encoded image data. |
| 82 | + * Example: "https://example.com/icon.png" or "data:image/png;base64,iVBORw0KG..." |
| 83 | + * @property mimeType Optional MIME type override if the source MIME type is missing or generic. |
| 84 | + * For example, "image/png", "image/jpeg", or "image/svg+xml". |
| 85 | + * Useful when the URL doesn't include a file extension or uses a generic MIME type. |
| 86 | + * @property sizes Optional array of strings that specify sizes at which the icon can be used. |
| 87 | + * Each string should be in WxH format (e.g., "48x48", "96x96") or "any" for |
| 88 | + * scalable formats like SVG. If not provided, the client should assume that |
| 89 | + * the icon can be used at any size. |
| 90 | + * @property theme Optional specifier for the theme this icon is designed for. |
| 91 | + * [Theme.Light] indicates the icon is designed for a light background, |
| 92 | + * [Theme.Dark] indicates the icon is designed for a dark background. |
| 93 | + * If not provided, the client should assume the icon can be used with any theme. |
| 94 | + */ |
| 95 | +@Serializable |
| 96 | +public data class Icon( |
| 97 | + val src: String, |
| 98 | + val mimeType: String? = null, |
| 99 | + val sizes: List<String>? = null, |
| 100 | + val theme: Theme? = null, |
| 101 | +) { |
| 102 | + /** |
| 103 | + * The theme context for which an icon is designed. |
| 104 | + * |
| 105 | + * @property Light Icon designed for use with a light background (typically darker icon). |
| 106 | + * @property Dark Icon designed for use with a dark background (typically lighter icon). |
| 107 | + */ |
| 108 | + @Serializable |
| 109 | + public enum class Theme { |
| 110 | + /** Icon designed for use with a light background */ |
| 111 | + @SerialName("light") |
| 112 | + Light, |
| 113 | + |
| 114 | + /** Icon designed for use with a dark background */ |
| 115 | + @SerialName("dark") |
| 116 | + Dark, |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// ============================================================================ |
| 121 | +// Roles and References |
| 122 | +// ============================================================================ |
| 123 | + |
| 124 | +/** |
| 125 | + * The sender or recipient of messages and data in a conversation. |
| 126 | + */ |
| 127 | +@Serializable |
| 128 | +public enum class Role { |
| 129 | + @SerialName("user") |
| 130 | + User, |
| 131 | + |
| 132 | + @SerialName("assistant") |
| 133 | + Assistant, |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Base interface for reference types in the protocol. |
| 138 | + * |
| 139 | + * References are used to point to other entities (prompts, resources, etc.) |
| 140 | + * without including their full definitions. |
| 141 | + */ |
| 142 | +@OptIn(ExperimentalSerializationApi::class) |
| 143 | +@Serializable |
| 144 | +@JsonClassDiscriminator("type") |
| 145 | +public sealed interface Reference |
| 146 | + |
| 147 | +// ============================================================================ |
| 148 | +// Annotations and Metadata |
| 149 | +// ============================================================================ |
| 150 | + |
| 151 | +/** |
| 152 | + * Optional annotations for the client. |
| 153 | + * |
| 154 | + * The client can use annotations to inform how objects are used or displayed. |
| 155 | + * |
| 156 | + * @property audience Describes who the intended customer of this object or data is. |
| 157 | + * Can include multiple entries to indicate content useful for multiple audiences |
| 158 | + * (e.g., [Role.user, Role.assistant]). |
| 159 | + * @property priority Describes how important this data is for operating the server. |
| 160 | + * A value of 1.0 means "most important" and indicates that the data is effectively required, |
| 161 | + * while 0.0 means "least important" and indicates that the data is entirely optional. |
| 162 | + * Should be a value between 0.0 and 1.0. |
| 163 | + * @property lastModified The moment the resource was last modified, as an ISO 8601 formatted string |
| 164 | + * (e.g., "2025-01-12T15:00:58Z"). |
| 165 | + * Examples: last activity timestamp in an open file, timestamp when the resource was attached, etc. |
| 166 | + */ |
| 167 | +@Serializable |
| 168 | +public data class Annotations( |
| 169 | + val audience: List<Role>? = null, |
| 170 | + val priority: Double? = null, |
| 171 | + val lastModified: String? = null, |
| 172 | +) { |
| 173 | + init { |
| 174 | + require(priority == null || priority in 0.0..1.0) { "Priority must be between 0.0 and 1.0" } |
| 175 | + } |
| 176 | +} |
0 commit comments