refactor: explicit locked detekt config (#729)
Migrate detekt to a convention plugin in `buildSrc`, switch the project to a fully explicit YAML config (`buildUponDefaultConfig = false`), fix the real findings that this stricter setup revealed, and remove `@Suppress` annotations that became unnecessary after the rule changes. ## Motivation and Context - Explicit visibility into the rule set - Lock against detekt version upgrades ## Breaking Changes None ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [x] Documentation update ## Checklist - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [x] I have added or updated documentation as needed
This commit is contained in:
parent
a238fc0a2e
commit
64804a05c2
30 changed files with 1106 additions and 507 deletions
|
|
@ -99,8 +99,10 @@ public abstract class AbstractClientTransport : AbstractTransport() {
|
|||
* proceeding with further operations. It is often used to enforce state invariants within the transport's
|
||||
* lifecycle mechanics, enabling robust error handling and debugging in case of unexpected state transitions.
|
||||
*
|
||||
* @param expected The expected transport state. An exception will be thrown if the actual state does not match this value.
|
||||
* @throws IllegalStateException If the current state does not match the expected state.
|
||||
* @param expected The expected transport state.
|
||||
* An exception will be thrown if the actual state does not match this value.
|
||||
* @param lazyMessage Function that builds the exception message from the actual state when the check fails.
|
||||
* Defaults to a message that compares the expected state against the observed one.
|
||||
*/
|
||||
protected fun checkState(
|
||||
expected: ClientTransportState,
|
||||
|
|
|
|||
|
|
@ -6,11 +6,13 @@ import kotlin.concurrent.atomics.AtomicBoolean
|
|||
import kotlin.concurrent.atomics.ExperimentalAtomicApi
|
||||
|
||||
/**
|
||||
* Implements [onClose], [onError] and [onMessage] functions of [Transport] providing
|
||||
* corresponding [_onClose], [_onError] and [_onMessage] properties to use for an implementation.
|
||||
* Base class for [Transport] implementations that handles the bookkeeping for [onClose], [onError]
|
||||
* and [onMessage] callback registration, exposing the composed callbacks to subclasses for invocation.
|
||||
*
|
||||
* The callbacks are composed: each call to [onClose], [onError], or [onMessage] chains the new block
|
||||
* after any previously registered ones.
|
||||
*/
|
||||
@OptIn(ExperimentalAtomicApi::class)
|
||||
@Suppress("PropertyName")
|
||||
public abstract class AbstractTransport : Transport {
|
||||
private val onCloseCalled = AtomicBoolean(false)
|
||||
private var _onClose: (() -> Unit) = {}
|
||||
|
|
|
|||
|
|
@ -53,22 +53,16 @@ public typealias ProgressCallback = (Progress) -> Unit
|
|||
/**
|
||||
* Additional initialization options.
|
||||
*
|
||||
* @property enforceStrictCapabilities whether to restrict emitted requests to only those that the
|
||||
* remote side has indicated it can handle through its advertised capabilities.
|
||||
* This does NOT affect checking of _local_ side capabilities, as it is considered a logic error
|
||||
* to mis-specify those.
|
||||
* Currently defaults to `false` for backwards compatibility with SDK versions that did not advertise
|
||||
* capabilities correctly; in the future, this will default to `true`.
|
||||
* @property timeout default timeout for outgoing requests
|
||||
*/
|
||||
public open class ProtocolOptions(
|
||||
/**
|
||||
* Whether to restrict emitted requests to only those that the remote side has indicated
|
||||
* that they can handle, through their advertised capabilities.
|
||||
*
|
||||
* Note that this DOES NOT affect checking of _local_ side capabilities, as it is
|
||||
* considered a logic error to mis-specify those.
|
||||
*
|
||||
* Currently, this defaults to false, for backwards compatibility with SDK versions
|
||||
* that did not advertise capabilities correctly.
|
||||
* In the future, this will default to true.
|
||||
*/
|
||||
public var enforceStrictCapabilities: Boolean = false,
|
||||
|
||||
public var timeout: Duration = DEFAULT_REQUEST_TIMEOUT,
|
||||
)
|
||||
|
||||
|
|
@ -80,18 +74,18 @@ public val DEFAULT_REQUEST_TIMEOUT: Duration = 60.seconds
|
|||
/**
|
||||
* Options that can be given per request.
|
||||
*
|
||||
* @property relatedRequestId if present,
|
||||
* `relatedRequestId` is used to indicate to the transport which incoming request to associate this outgoing message with.
|
||||
* @property resumptionToken the resumption token used to continue long-running requests that were interrupted.
|
||||
* This allows clients to reconnect and continue from where they left off, if supported by the transport.
|
||||
* @property onResumptionToken a callback that is invoked when the resumption token changes, if supported by the transport.
|
||||
* This allows clients to persist the latest token for potential reconnection.
|
||||
* @param relatedRequestId if present, used to indicate to the transport which incoming request to
|
||||
* associate this outgoing message with.
|
||||
* @param resumptionToken the resumption token used to continue long-running requests that were interrupted.
|
||||
* Allows clients to reconnect and continue from where they left off, if supported by the transport.
|
||||
* @param onResumptionToken callback invoked when the resumption token changes, if supported by the transport.
|
||||
* Allows clients to persist the latest token for potential reconnection.
|
||||
* @property onProgress callback for progress notifications.
|
||||
* If set, requests progress notifications from the remote end (if supported).
|
||||
* When progress notifications are received, this callback will be invoked.
|
||||
* If set, requests progress notifications from the remote end (if supported);
|
||||
* when progress notifications are received, this callback is invoked.
|
||||
* @property timeout a timeout for this request.
|
||||
* If exceeded, a McpException with code `RequestTimeout` will be raised from request().
|
||||
* If not specified, `DEFAULT_REQUEST_TIMEOUT` will be used as the timeout.
|
||||
* If exceeded, a [McpException] with code `RequestTimeout` is raised from [Protocol.request].
|
||||
* If not specified, [DEFAULT_REQUEST_TIMEOUT] is used.
|
||||
*/
|
||||
public class RequestOptions(
|
||||
relatedRequestId: RequestId? = null,
|
||||
|
|
@ -147,19 +141,18 @@ internal val COMPLETED = CompletableDeferred(Unit).also { it.complete(Unit) }
|
|||
* Implements MCP protocol framing on top of a pluggable transport, including
|
||||
* features like request/response linking, notifications, and progress.
|
||||
*
|
||||
* @property transport the active transport, or `null` if not connected
|
||||
* @property requestHandlers registered request handlers keyed by method name
|
||||
* @property notificationHandlers registered notification handlers keyed by method name
|
||||
* @property responseHandlers pending response handlers keyed by request ID
|
||||
* @property progressHandlers registered progress callbacks keyed by progress token
|
||||
* @property options protocol-level configuration; `null` falls back to defaults
|
||||
*/
|
||||
public abstract class Protocol(@PublishedApi internal val options: ProtocolOptions?) {
|
||||
/** The active transport, or `null` if not connected. */
|
||||
public var transport: Transport? = null
|
||||
private set
|
||||
|
||||
private val _requestHandlers:
|
||||
AtomicRef<PersistentMap<String, suspend (JSONRPCRequest, RequestHandlerExtra) -> RequestResult?>> =
|
||||
atomic(persistentMapOf())
|
||||
|
||||
/** Registered request handlers keyed by method name. */
|
||||
public val requestHandlers: Map<
|
||||
String,
|
||||
suspend (
|
||||
|
|
@ -171,17 +164,23 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
|||
|
||||
private val _notificationHandlers =
|
||||
atomic(persistentMapOf<String, suspend (notification: JSONRPCNotification) -> Unit>())
|
||||
|
||||
/** Registered notification handlers keyed by method name. */
|
||||
public val notificationHandlers: Map<String, suspend (notification: JSONRPCNotification) -> Unit>
|
||||
get() = _notificationHandlers.value
|
||||
|
||||
private val _responseHandlers:
|
||||
AtomicRef<PersistentMap<RequestId, (response: JSONRPCResponse?, error: Exception?) -> Unit>> =
|
||||
atomic(persistentMapOf())
|
||||
|
||||
/** Pending response handlers keyed by request ID. */
|
||||
public val responseHandlers: Map<RequestId, (response: JSONRPCResponse?, error: Exception?) -> Unit>
|
||||
get() = _responseHandlers.value
|
||||
|
||||
private val _progressHandlers: AtomicRef<PersistentMap<ProgressToken, ProgressCallback>> =
|
||||
atomic(persistentMapOf())
|
||||
|
||||
/** Registered progress callbacks keyed by progress token. */
|
||||
public val progressHandlers: Map<ProgressToken, ProgressCallback>
|
||||
get() = _progressHandlers.value
|
||||
|
||||
|
|
@ -285,7 +284,6 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("ThrowsCount")
|
||||
private suspend fun onRequest(request: JSONRPCRequest) {
|
||||
logger.trace { "Received request: ${request.method} (id: ${request.id})" }
|
||||
|
||||
|
|
@ -312,7 +310,6 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
|||
return
|
||||
}
|
||||
|
||||
@Suppress("TooGenericExceptionCaught", "InstanceOfCheckForException")
|
||||
try {
|
||||
val result = handler(request, RequestHandlerExtra())
|
||||
logger.trace { "Request handled successfully: ${request.method} (id: ${request.id})" }
|
||||
|
|
@ -395,7 +392,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
|||
if (response != null) {
|
||||
handler(response, null)
|
||||
} else {
|
||||
check(error != null)
|
||||
checkNotNull(error)
|
||||
val mcpException = McpException(
|
||||
code = error.error.code,
|
||||
message = error.error.message,
|
||||
|
|
@ -479,8 +476,8 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
|||
try {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
result.complete(response!!.result as T)
|
||||
} catch (error: Throwable) {
|
||||
result.completeExceptionally(error)
|
||||
} catch (e: Throwable) {
|
||||
result.completeExceptionally(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,7 +39,6 @@ public class ReadBuffer {
|
|||
val line = readNextLine() ?: return null
|
||||
if (line.isBlank()) continue
|
||||
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
val message = try {
|
||||
deserializeMessage(line)
|
||||
} catch (e: Exception) {
|
||||
|
|
@ -78,7 +77,7 @@ public class ReadBuffer {
|
|||
if (braceIndex == -1) return null
|
||||
|
||||
val trimmed = line.substring(braceIndex)
|
||||
@Suppress("TooGenericExceptionCaught")
|
||||
|
||||
return try {
|
||||
deserializeMessage(trimmed)
|
||||
} catch (ignored: Exception) {
|
||||
|
|
|
|||
|
|
@ -21,9 +21,9 @@ public interface Transport {
|
|||
/**
|
||||
* Sends a JSON-RPC message (request or response).
|
||||
*
|
||||
* @property message The JSON-RPC message to send, either a request or a response.
|
||||
* @property options Optional transport-specific options that control sending behavior.
|
||||
* Different transport implementations may support different options.
|
||||
* @param message the JSON-RPC message to send, either a request or a response
|
||||
* @param options optional transport-specific options that control sending behavior;
|
||||
* different transport implementations may support different options
|
||||
*/
|
||||
public suspend fun send(message: JSONRPCMessage, options: TransportSendOptions? = null)
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ public abstract class WebSocketMcpTransport : AbstractTransport() {
|
|||
*/
|
||||
protected abstract suspend fun initializeSession()
|
||||
|
||||
@Suppress("ThrowsCount")
|
||||
override suspend fun start() {
|
||||
logger.debug { "Starting websocket transport" }
|
||||
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@ import kotlin.jvm.JvmOverloads
|
|||
/**
|
||||
* Represents an error specific to the MCP protocol.
|
||||
*
|
||||
* @property code The MCP/JSON‑RPC error code.
|
||||
* @property data Optional additional error payload as a JSON element; `null` when not provided.
|
||||
* @param message The error message. Used verbatim as [Exception.message] — no error code prefix is prepended.
|
||||
* Defaults to `"MCP error $code"` when not provided.
|
||||
* @param cause The original cause.
|
||||
* @property code the MCP/JSON-RPC error code
|
||||
* @param message the error message; used verbatim as [Exception.message] without an error-code prefix.
|
||||
* Defaults to `"MCP error $code"` when not provided.
|
||||
* @property data optional additional error payload as a JSON element
|
||||
* @param cause the original cause
|
||||
*/
|
||||
public class McpException @JvmOverloads public constructor(
|
||||
public val code: Int,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
|
|||
/**
|
||||
* Represents a ping request used to check if the connection is alive.
|
||||
*
|
||||
* @property meta optional request metadata
|
||||
* @property params optional request parameters carrying metadata
|
||||
*/
|
||||
@Serializable
|
||||
public data class PingRequest(override val params: BaseRequestParams? = null) :
|
||||
|
|
@ -17,6 +17,7 @@ public data class PingRequest(override val params: BaseRequestParams? = null) :
|
|||
@EncodeDefault
|
||||
override val method: Method = Method.Defined.Ping
|
||||
|
||||
/** Convenience accessor for [params]'s metadata. */
|
||||
public val meta: RequestMeta?
|
||||
get() = params?.meta
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,11 +28,10 @@ public val SUPPORTED_PROTOCOL_VERSIONS: List<String> = listOf(
|
|||
|
||||
/**
|
||||
* Represents an entity that includes additional metadata in its responses.
|
||||
*
|
||||
* @property meta optional metadata attached to this entity
|
||||
*/
|
||||
@Serializable
|
||||
public sealed interface WithMeta {
|
||||
/** Optional metadata attached to this entity. */
|
||||
@SerialName("_meta")
|
||||
public val meta: JsonObject?
|
||||
}
|
||||
|
|
@ -91,9 +90,6 @@ public data class Icon(
|
|||
) {
|
||||
/**
|
||||
* The theme context for which an icon is designed.
|
||||
*
|
||||
* @property Light Icon designed for use with a light background (typically darker icon).
|
||||
* @property Dark Icon designed for use with a dark background (typically lighter icon).
|
||||
*/
|
||||
@Serializable
|
||||
public enum class Theme {
|
||||
|
|
@ -128,11 +124,10 @@ public enum class Role {
|
|||
*
|
||||
* References are used to point to other entities (prompts, resources, etc.)
|
||||
* without including their full definitions.
|
||||
*
|
||||
* @property type discriminator identifying the reference subtype
|
||||
*/
|
||||
@Serializable(with = ReferencePolymorphicSerializer::class)
|
||||
public sealed interface Reference {
|
||||
/** Discriminator identifying the reference subtype. */
|
||||
public val type: ReferenceType
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ import kotlinx.serialization.json.JsonObject
|
|||
* A request from the client to the server to ask for completion options.
|
||||
*
|
||||
* @property params The request parameters containing the argument to complete and its context.
|
||||
* @property meta optional request metadata
|
||||
*/
|
||||
@Serializable
|
||||
public data class CompleteRequest(override val params: CompleteRequestParams) : ClientRequest {
|
||||
|
|
@ -38,6 +37,7 @@ public data class CompleteRequest(override val params: CompleteRequestParams) :
|
|||
public val context: Context?
|
||||
get() = params.context
|
||||
|
||||
/** Convenience accessor for [params]'s metadata. */
|
||||
public val meta: RequestMeta?
|
||||
get() = params.meta
|
||||
}
|
||||
|
|
@ -48,6 +48,8 @@ public data class CompleteRequest(override val params: CompleteRequestParams) :
|
|||
* @property argument The argument's information for which completion options are requested.
|
||||
* @property ref A reference to either a prompt or resource template to complete within.
|
||||
* @property context Additional, optional context for generating completions.
|
||||
* @property meta Optional metadata for this request. May include a progressToken for
|
||||
* out-of-band progress notifications.
|
||||
*/
|
||||
@Serializable
|
||||
public data class CompleteRequestParams(
|
||||
|
|
|
|||
|
|
@ -39,11 +39,10 @@ public enum class ContentTypes(public val value: String) {
|
|||
|
||||
/**
|
||||
* Base interface for all content blocks in the protocol.
|
||||
*
|
||||
* @property type discriminator identifying the content block subtype
|
||||
*/
|
||||
@Serializable(with = ContentBlockPolymorphicSerializer::class)
|
||||
public sealed interface ContentBlock : WithMeta {
|
||||
/** Discriminator identifying the content block subtype. */
|
||||
public val type: ContentTypes
|
||||
}
|
||||
|
||||
|
|
@ -63,11 +62,10 @@ public sealed interface MediaContent :
|
|||
*
|
||||
* Implemented by [TextContent], [ImageContent], [AudioContent], [ToolUseContent],
|
||||
* and [ToolResultContent].
|
||||
*
|
||||
* @property type discriminator identifying the content block subtype
|
||||
*/
|
||||
@Serializable(with = SamplingMessageContentPolymorphicSerializer::class)
|
||||
public sealed interface SamplingMessageContent : WithMeta {
|
||||
/** discriminator identifying the content block subtype */
|
||||
public val type: ContentTypes
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -46,11 +46,10 @@ public data class ElicitRequest(override val params: ElicitRequestParams) : Serv
|
|||
* Represents the parameters for an `elicitation/create` request.
|
||||
*
|
||||
* Implementations: [ElicitRequestFormParams], [ElicitRequestURLParams].
|
||||
*
|
||||
* @property message The message to present to the user describing what information is being requested.
|
||||
*/
|
||||
@Serializable(with = ElicitRequestParamsSerializer::class)
|
||||
public sealed interface ElicitRequestParams : RequestParams {
|
||||
/** The message to present to the user describing what information is being requested. */
|
||||
public val message: String
|
||||
|
||||
/**
|
||||
|
|
@ -64,7 +63,6 @@ public sealed interface ElicitRequestParams : RequestParams {
|
|||
* Each property must be a primitive type (string, number, boolean).
|
||||
* @property required Optional list of property names that must be provided by the user.
|
||||
* If omitted, all fields are considered optional.
|
||||
* @property type Always "object" for elicitation schemas.
|
||||
*/
|
||||
@Serializable
|
||||
public data class RequestedSchema(
|
||||
|
|
@ -73,6 +71,7 @@ public sealed interface ElicitRequestParams : RequestParams {
|
|||
val properties: Map<String, PrimitiveSchemaDefinition>,
|
||||
val required: List<String>? = null,
|
||||
) {
|
||||
/** Always "object" for elicitation schemas. */
|
||||
@EncodeDefault
|
||||
val type: String = "object"
|
||||
}
|
||||
|
|
@ -91,7 +90,7 @@ public sealed interface ElicitRequestParams : RequestParams {
|
|||
ReplaceWith("ElicitRequestFormParams(message, requestedSchema = requestedSchema, meta = meta)"),
|
||||
DeprecationLevel.WARNING,
|
||||
)
|
||||
@Suppress("FunctionNaming", "FunctionName")
|
||||
@Suppress("FunctionName")
|
||||
public fun ElicitRequestParams(
|
||||
message: String,
|
||||
requestedSchema: ElicitRequestParams.RequestedSchema,
|
||||
|
|
@ -113,7 +112,6 @@ public fun ElicitRequestParams(
|
|||
* later via `tasks/result`.
|
||||
* @property requestedSchema A restricted subset of JSON Schema. Only top-level properties
|
||||
* are allowed, without nesting.
|
||||
* @property mode The elicitation mode discriminator, always `"form"`.
|
||||
* @property meta Optional metadata. May include a progressToken for out-of-band progress notifications.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -124,6 +122,7 @@ public data class ElicitRequestFormParams(
|
|||
@SerialName("_meta")
|
||||
override val meta: RequestMeta? = null,
|
||||
) : ElicitRequestParams {
|
||||
/** The elicitation mode discriminator, always `"form"`. */
|
||||
@EncodeDefault
|
||||
public val mode: String = "form"
|
||||
}
|
||||
|
|
@ -141,7 +140,6 @@ public data class ElicitRequestFormParams(
|
|||
* @property task If specified, the caller is requesting task-augmented execution. The request
|
||||
* will return a [CreateTaskResult] immediately, and the actual result can be retrieved
|
||||
* later via `tasks/result`.
|
||||
* @property mode The elicitation mode discriminator, always `"url"`.
|
||||
* @property meta Optional metadata. May include a progressToken for out-of-band progress notifications.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -153,6 +151,7 @@ public data class ElicitRequestURLParams(
|
|||
@SerialName("_meta")
|
||||
override val meta: RequestMeta? = null,
|
||||
) : ElicitRequestParams {
|
||||
/** The elicitation mode discriminator, always `"url"`. */
|
||||
@EncodeDefault
|
||||
public val mode: String = "url"
|
||||
}
|
||||
|
|
@ -182,10 +181,6 @@ public data class ElicitResult(
|
|||
|
||||
/**
|
||||
* The user's response action to an elicitation request.
|
||||
*
|
||||
* @property Accept User submitted the form/confirmed the action. Content will be provided.
|
||||
* @property Decline User explicitly declined the action. No content provided.
|
||||
* @property Cancel User dismissed the dialog without making an explicit choice. No content provided.
|
||||
*/
|
||||
@Serializable
|
||||
public enum class Action {
|
||||
|
|
|
|||
|
|
@ -104,11 +104,10 @@ internal fun JSONRPCNotification.fromJSON(): Notification =
|
|||
* Base interface for all JSON-RPC 2.0 messages.
|
||||
*
|
||||
* All messages in the MCP protocol follow the JSON-RPC 2.0 specification.
|
||||
*
|
||||
* @property jsonrpc the JSON-RPC protocol version, always `"2.0"`
|
||||
*/
|
||||
@Serializable(with = JSONRPCMessagePolymorphicSerializer::class)
|
||||
public sealed interface JSONRPCMessage {
|
||||
/** The JSON-RPC protocol version, always `"2.0"`. */
|
||||
public val jsonrpc: String
|
||||
}
|
||||
|
||||
|
|
@ -131,7 +130,6 @@ public data object JSONRPCEmptyMessage : JSONRPCMessage {
|
|||
* The server or client (depending on a direction) must respond with either a
|
||||
* [JSONRPCResponse] or [JSONRPCError] that has the same [id].
|
||||
*
|
||||
* @property jsonrpc Always "2.0" to indicate JSON-RPC 2.0 protocol.
|
||||
* @property id A unique identifier for this request. The response will include the same ID.
|
||||
* Can be a string or number. UUID string is used by default.
|
||||
* @property method The name of the method to invoke (e.g., "tools/list", "resources/read").
|
||||
|
|
@ -144,6 +142,7 @@ public data class JSONRPCRequest @JvmOverloads public constructor(
|
|||
val method: String,
|
||||
val params: JsonElement? = null,
|
||||
) : JSONRPCMessage {
|
||||
/** Always `"2.0"` to indicate JSON-RPC 2.0 protocol. */
|
||||
@EncodeDefault
|
||||
override val jsonrpc: String = JSONRPC_VERSION
|
||||
|
||||
|
|
@ -186,13 +185,13 @@ public data class JSONRPCRequest @JvmOverloads public constructor(
|
|||
*
|
||||
* Examples: progress updates, resource change notifications, log messages.
|
||||
*
|
||||
* @property jsonrpc Always "2.0" to indicate JSON-RPC 2.0 protocol.
|
||||
* @property method The name of the notification method (e.g., "notifications/progress",
|
||||
* "notifications/resources/updated").
|
||||
* @property params Optional parameters for the notification. Structure depends on the specific method.
|
||||
*/
|
||||
@Serializable
|
||||
public data class JSONRPCNotification(val method: String, val params: JsonElement? = null) : JSONRPCMessage {
|
||||
/** Always `"2.0"` to indicate JSON-RPC 2.0 protocol. */
|
||||
@EncodeDefault
|
||||
override val jsonrpc: String = JSONRPC_VERSION
|
||||
}
|
||||
|
|
@ -207,12 +206,12 @@ public data class JSONRPCNotification(val method: String, val params: JsonElemen
|
|||
* Sent in response to a [JSONRPCRequest] when the method execution succeeds.
|
||||
* The [id] must match the [id] of the original request.
|
||||
*
|
||||
* @property jsonrpc Always "2.0" to indicate JSON-RPC 2.0 protocol.
|
||||
* @property id The identifier from the original request. Used to match responses to requests.
|
||||
* @property result The result of the method execution. Structure depends on the method that was called.
|
||||
*/
|
||||
@Serializable
|
||||
public data class JSONRPCResponse(val id: RequestId, val result: RequestResult = EmptyResult()) : JSONRPCMessage {
|
||||
/** Always `"2.0"` to indicate JSON-RPC 2.0 protocol. */
|
||||
@EncodeDefault
|
||||
override val jsonrpc: String = JSONRPC_VERSION
|
||||
}
|
||||
|
|
@ -227,12 +226,12 @@ public data class JSONRPCResponse(val id: RequestId, val result: RequestResult =
|
|||
* Sent in response to a [JSONRPCRequest] when the method execution fails.
|
||||
* The [id] must match the [id] of the original request.
|
||||
*
|
||||
* @property jsonrpc Always "2.0" to indicate JSON-RPC 2.0 protocol.
|
||||
* @property id The identifier from the original request. Used to match error responses to requests.
|
||||
* @property error Details about the error that occurred, including error code and message.
|
||||
*/
|
||||
@Serializable
|
||||
public data class JSONRPCError(val id: RequestId?, val error: RPCError) : JSONRPCMessage {
|
||||
/** Always `"2.0"` to indicate JSON-RPC 2.0 protocol. */
|
||||
@EncodeDefault
|
||||
override val jsonrpc: String = JSONRPC_VERSION
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,11 +4,10 @@ import kotlinx.serialization.Serializable
|
|||
|
||||
/**
|
||||
* Represents a method in the protocol, which can be predefined or custom.
|
||||
*
|
||||
* @property value the string representation of this method name
|
||||
*/
|
||||
@Serializable(with = MethodSerializer::class)
|
||||
public sealed interface Method {
|
||||
/** The string representation of this method name. */
|
||||
public val value: String
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -11,13 +11,13 @@ import kotlinx.serialization.json.JsonObject
|
|||
|
||||
/**
|
||||
* Represents a notification in the protocol.
|
||||
*
|
||||
* @property method the notification method identifier
|
||||
* @property params optional notification parameters
|
||||
*/
|
||||
@Serializable(with = NotificationPolymorphicSerializer::class)
|
||||
public sealed interface Notification {
|
||||
/** The notification method identifier. */
|
||||
public val method: Method
|
||||
|
||||
/** Optional notification parameters. */
|
||||
public val params: NotificationParams?
|
||||
}
|
||||
|
||||
|
|
@ -34,9 +34,7 @@ public sealed interface ClientNotification : Notification
|
|||
public sealed interface ServerNotification : Notification
|
||||
|
||||
/**
|
||||
* Interface for notification parameter types.
|
||||
*
|
||||
* @property meta Optional metadata for the notification.
|
||||
* Interface for notification parameter types. Inherits the optional `_meta` field from [WithMeta].
|
||||
*/
|
||||
@Serializable
|
||||
public sealed interface NotificationParams : WithMeta
|
||||
|
|
@ -76,13 +74,13 @@ public class Progress(
|
|||
* @property method The custom method name. By convention, custom methods often contain
|
||||
* organization-specific prefixes (e.g., "mycompany/custom_event").
|
||||
* @property params Raw JSON parameters for the custom notification, if present.
|
||||
* @property meta optional metadata for this notification
|
||||
*/
|
||||
@Serializable
|
||||
public data class CustomNotification(override val method: Method, override val params: BaseNotificationParams? = null) :
|
||||
ClientNotification,
|
||||
ServerNotification {
|
||||
|
||||
/** Convenience accessor for [params]'s metadata. */
|
||||
public val meta: JsonObject?
|
||||
get() = params?.meta
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,11 +15,11 @@ import kotlin.jvm.JvmInline
|
|||
* Metadata attached to a request's `_meta` field.
|
||||
*
|
||||
* @property json the raw JSON object containing the metadata
|
||||
* @property progressToken optional progress token for tracking request progress
|
||||
*/
|
||||
@JvmInline
|
||||
@Serializable
|
||||
public value class RequestMeta(public val json: JsonObject) {
|
||||
/** Optional progress token for tracking request progress. */
|
||||
public val progressToken: ProgressToken?
|
||||
get() = json["progressToken"]?.let { element ->
|
||||
when {
|
||||
|
|
@ -72,14 +72,14 @@ public data class BaseRequestParams(@SerialName("_meta") override val meta: Requ
|
|||
|
||||
/**
|
||||
* Represents a request in the protocol.
|
||||
*
|
||||
* @property method the request method identifier
|
||||
* @property params optional request parameters
|
||||
*/
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@Serializable(with = RequestPolymorphicSerializer::class)
|
||||
public sealed interface Request {
|
||||
/** The request method identifier. */
|
||||
public val method: Method
|
||||
|
||||
/** Optional request parameters. */
|
||||
public val params: RequestParams?
|
||||
}
|
||||
|
||||
|
|
@ -142,7 +142,7 @@ public sealed interface ServerResult : RequestResult
|
|||
/**
|
||||
* An empty result for a request containing optional metadata.
|
||||
*
|
||||
* @param meta Additional metadata for the response. Defaults to an empty JSON object.
|
||||
* @property meta Additional metadata for the response. Defaults to an empty JSON object.
|
||||
*/
|
||||
@Serializable
|
||||
public data class EmptyResult(@SerialName("_meta") override val meta: JsonObject? = null) :
|
||||
|
|
@ -151,11 +151,10 @@ public data class EmptyResult(@SerialName("_meta") override val meta: JsonObject
|
|||
|
||||
/**
|
||||
* Represents a paginated result.
|
||||
*
|
||||
* @property nextCursor opaque token for retrieving the next page of results, or `null` if no more results
|
||||
*/
|
||||
@Serializable
|
||||
public sealed interface PaginatedResult : RequestResult {
|
||||
/** Opaque token for retrieving the next page of results, or `null` if no more results. */
|
||||
public val nextCursor: String?
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -117,15 +117,14 @@ public data class ResourceTemplateReference(val uri: String) : Reference {
|
|||
}
|
||||
|
||||
/**
|
||||
* The contents of a specific resource or sub-resource.
|
||||
*
|
||||
* @property uri The URI of this resource.
|
||||
* @property mimeType The MIME type of this resource, if known.
|
||||
* @property meta Optional metadata for this response.
|
||||
* The contents of a specific resource or sub-resource. Carries the optional `_meta` field via [WithMeta].
|
||||
*/
|
||||
@Serializable(with = ResourceContentsPolymorphicSerializer::class)
|
||||
public sealed interface ResourceContents : WithMeta {
|
||||
/** The URI of this resource. */
|
||||
public val uri: String
|
||||
|
||||
/** The MIME type of this resource, if known. */
|
||||
public val mimeType: String?
|
||||
}
|
||||
|
||||
|
|
@ -136,6 +135,7 @@ public sealed interface ResourceContents : WithMeta {
|
|||
* This must only be set if the item can actually be represented as text (not binary data).
|
||||
* @property uri The URI of this resource.
|
||||
* @property mimeType The MIME type of this resource, if known.
|
||||
* @property meta Optional metadata for these contents.
|
||||
*/
|
||||
@Serializable
|
||||
public data class TextResourceContents(
|
||||
|
|
@ -152,6 +152,7 @@ public data class TextResourceContents(
|
|||
* @property blob A base64-encoded string representing the binary data of the item.
|
||||
* @property uri The URI of this resource.
|
||||
* @property mimeType The MIME type of this resource, if known.
|
||||
* @property meta Optional metadata for these contents.
|
||||
*/
|
||||
@Serializable
|
||||
public data class BlobResourceContents(
|
||||
|
|
@ -167,6 +168,7 @@ public data class BlobResourceContents(
|
|||
*
|
||||
* @property uri The URI of this resource.
|
||||
* @property mimeType The MIME type of this resource, if known.
|
||||
* @property meta Optional metadata for these contents.
|
||||
*/
|
||||
@Serializable
|
||||
public data class UnknownResourceContents(
|
||||
|
|
|
|||
|
|
@ -53,7 +53,6 @@ public data class Root(
|
|||
* `listChanged = true` to receive these requests.
|
||||
*
|
||||
* @property params Optional request parameters containing metadata.
|
||||
* @property meta optional request metadata
|
||||
*/
|
||||
@Serializable
|
||||
public data class ListRootsRequest(override val params: BaseRequestParams? = null) : ServerRequest {
|
||||
|
|
@ -61,6 +60,7 @@ public data class ListRootsRequest(override val params: BaseRequestParams? = nul
|
|||
@EncodeDefault
|
||||
override val method: Method = Method.Defined.RootsList
|
||||
|
||||
/** Convenience accessor for [params]'s metadata. */
|
||||
public val meta: RequestMeta?
|
||||
get() = params?.meta
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ public sealed interface PrimitiveSchemaDefinition
|
|||
* @property minLength Minimum string length.
|
||||
* @property maxLength Maximum string length.
|
||||
* @property format Optional format constraint (e.g., email, URI, date).
|
||||
* @property type JSON Schema type discriminator, always `"string"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -36,6 +35,7 @@ public data class StringSchema(
|
|||
val format: StringSchemaFormat? = null,
|
||||
val default: String? = null,
|
||||
) : PrimitiveSchemaDefinition {
|
||||
/** JSON Schema type discriminator, always `"string"`. */
|
||||
@EncodeDefault
|
||||
public val type: String = "string"
|
||||
}
|
||||
|
|
@ -73,7 +73,6 @@ public sealed interface NumberSchemaDefinition : PrimitiveSchemaDefinition
|
|||
* @property description Optional description for the field.
|
||||
* @property minimum Minimum allowed value.
|
||||
* @property maximum Maximum allowed value.
|
||||
* @property type JSON Schema type discriminator, always `"integer"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -84,6 +83,7 @@ public data class IntegerSchema(
|
|||
val maximum: Int? = null,
|
||||
val default: Int? = null,
|
||||
) : NumberSchemaDefinition {
|
||||
/** JSON Schema type discriminator, always `"integer"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "integer"
|
||||
}
|
||||
|
|
@ -95,7 +95,6 @@ public data class IntegerSchema(
|
|||
* @property description Optional description for the field.
|
||||
* @property minimum Minimum allowed value.
|
||||
* @property maximum Maximum allowed value.
|
||||
* @property type JSON Schema type discriminator, always `"number"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -106,6 +105,7 @@ public data class DoubleSchema(
|
|||
val maximum: Double? = null,
|
||||
val default: Double? = null,
|
||||
) : NumberSchemaDefinition {
|
||||
/** JSON Schema type discriminator, always `"number"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "number"
|
||||
}
|
||||
|
|
@ -115,7 +115,6 @@ public data class DoubleSchema(
|
|||
*
|
||||
* @property title Optional display title for the field.
|
||||
* @property description Optional description for the field.
|
||||
* @property type JSON Schema type discriminator, always `"boolean"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -124,6 +123,7 @@ public data class BooleanSchema(
|
|||
val description: String? = null,
|
||||
val default: Boolean? = null,
|
||||
) : PrimitiveSchemaDefinition {
|
||||
/** JSON Schema type discriminator, always `"boolean"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "boolean"
|
||||
}
|
||||
|
|
@ -161,7 +161,6 @@ public sealed interface SingleSelectEnumSchema : EnumSchemaDefinition
|
|||
* @property title Optional display title for the field.
|
||||
* @property description Optional description for the field.
|
||||
* @property enumValues Array of enum values to choose from.
|
||||
* @property type JSON Schema type discriminator, always `"string"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -172,6 +171,7 @@ public data class UntitledSingleSelectEnumSchema(
|
|||
val enumValues: List<String>,
|
||||
val default: String? = null,
|
||||
) : SingleSelectEnumSchema {
|
||||
/** JSON Schema type discriminator, always `"string"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "string"
|
||||
}
|
||||
|
|
@ -182,7 +182,6 @@ public data class UntitledSingleSelectEnumSchema(
|
|||
* @property title Optional display title for the field.
|
||||
* @property description Optional description for the field.
|
||||
* @property oneOf Array of enum options with values and display labels.
|
||||
* @property type JSON Schema type discriminator, always `"string"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -192,6 +191,7 @@ public data class TitledSingleSelectEnumSchema(
|
|||
val oneOf: List<EnumOption>,
|
||||
val default: String? = null,
|
||||
) : SingleSelectEnumSchema {
|
||||
/** JSON Schema type discriminator, always `"string"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "string"
|
||||
}
|
||||
|
|
@ -205,7 +205,6 @@ public data class TitledSingleSelectEnumSchema(
|
|||
* @property description Optional description for the field.
|
||||
* @property enumValues Array of enum values to choose from.
|
||||
* @property enumNames Display names for enum values. Non-standard according to JSON Schema 2020-12.
|
||||
* @property type JSON Schema type discriminator, always `"string"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Deprecated("Use TitledSingleSelectEnumSchema instead")
|
||||
|
|
@ -218,6 +217,7 @@ public data class LegacyTitledEnumSchema(
|
|||
val enumNames: List<String>? = null,
|
||||
val default: String? = null,
|
||||
) : EnumSchemaDefinition {
|
||||
/** JSON Schema type discriminator, always `"string"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "string"
|
||||
}
|
||||
|
|
@ -238,7 +238,6 @@ public sealed interface MultiSelectEnumSchema : EnumSchemaDefinition
|
|||
* @property minItems Minimum number of items to select.
|
||||
* @property maxItems Maximum number of items to select.
|
||||
* @property items Schema for the array items.
|
||||
* @property type JSON Schema type discriminator, always `"array"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -250,13 +249,13 @@ public data class UntitledMultiSelectEnumSchema(
|
|||
val items: Items,
|
||||
val default: List<String>? = null,
|
||||
) : MultiSelectEnumSchema {
|
||||
/** JSON Schema type discriminator, always `"array"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "array"
|
||||
|
||||
/**
|
||||
* Schema for the array items with plain enum values.
|
||||
*
|
||||
* @property type JSON Schema type discriminator, always `"string"`.
|
||||
* @property enumValues Array of enum values to choose from.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -264,6 +263,7 @@ public data class UntitledMultiSelectEnumSchema(
|
|||
@SerialName("enum")
|
||||
val enumValues: List<String>,
|
||||
) {
|
||||
/** JSON Schema type discriminator, always `"string"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "string"
|
||||
}
|
||||
|
|
@ -277,7 +277,6 @@ public data class UntitledMultiSelectEnumSchema(
|
|||
* @property minItems Minimum number of items to select.
|
||||
* @property maxItems Maximum number of items to select.
|
||||
* @property items Schema for array items with enum options and display labels.
|
||||
* @property type JSON Schema type discriminator, always `"array"`.
|
||||
* @property default Optional default value.
|
||||
*/
|
||||
@Serializable
|
||||
|
|
@ -289,6 +288,7 @@ public data class TitledMultiSelectEnumSchema(
|
|||
val items: Items,
|
||||
val default: List<String>? = null,
|
||||
) : MultiSelectEnumSchema {
|
||||
/** JSON Schema type discriminator, always `"array"`. */
|
||||
@EncodeDefault
|
||||
val type: String = "array"
|
||||
|
||||
|
|
|
|||
|
|
@ -424,7 +424,6 @@ private fun selectClientResultDeserializer(element: JsonElement): Deserializatio
|
|||
* Selects the appropriate deserializer for server results based on JSON content.
|
||||
* Returns null if the structure doesn't match any known server result type.
|
||||
*/
|
||||
@Suppress("CyclomaticComplexMethod")
|
||||
private fun selectServerResultDeserializer(element: JsonElement): DeserializationStrategy<ServerResult>? {
|
||||
val jsonObject = element.jsonObject
|
||||
return when {
|
||||
|
|
@ -530,7 +529,6 @@ internal object RequestIdPolymorphicSerializer : JsonContentPolymorphicSerialize
|
|||
internal object ElicitRequestParamsSerializer : JsonContentPolymorphicSerializer<ElicitRequestParams>(
|
||||
ElicitRequestParams::class,
|
||||
) {
|
||||
@Suppress("ThrowsCount")
|
||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<ElicitRequestParams> {
|
||||
val mode = when (val modeElement = element.jsonObject["mode"]) {
|
||||
null -> null
|
||||
|
|
@ -559,7 +557,6 @@ internal object ElicitRequestParamsSerializer : JsonContentPolymorphicSerializer
|
|||
internal object PrimitiveSchemaDefinitionSerializer : JsonContentPolymorphicSerializer<PrimitiveSchemaDefinition>(
|
||||
PrimitiveSchemaDefinition::class,
|
||||
) {
|
||||
@Suppress("ThrowsCount")
|
||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<PrimitiveSchemaDefinition> {
|
||||
val obj = element.jsonObject
|
||||
val typeElement = obj["type"]
|
||||
|
|
@ -589,7 +586,6 @@ internal object PrimitiveSchemaDefinitionSerializer : JsonContentPolymorphicSeri
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun selectStringTypeDeserializer(
|
||||
obj: Map<String, JsonElement>,
|
||||
): DeserializationStrategy<PrimitiveSchemaDefinition> = when {
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue