New MCP Schema types (#386)
Implement the last version of the mcp schema and complete restructuring of the mcp schema implementation from a monolithic `types.kt` file into separate files. - #158 - #185 - #353 - #298 - #248 ## Motivation and Context The previous implementation of the mcp schema was outdated and contained several issues. In addition, it stored all mcp types in a single large file, which made maintenance and further development more difficult ## How Has This Been Tested? All tests passed ## Breaking Changes Yes. The new types are now located in a separate package. Additionally, many types have new parameters and modified constructors ## Types of changes - [x] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [ ] 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 ## Additional context to adopt the new structure: **Before:** ```kotlin import io.modelcontextprotocol.kotlin.sdk.InitializeRequest import io.modelcontextprotocol.kotlin.sdk.Resource ``` **After:** ```kotlin import io.modelcontextprotocol.kotlin.sdk.types.InitializeRequest import io.modelcontextprotocol.kotlin.sdk.types.Resource ```
This commit is contained in:
parent
91d45b0f8d
commit
21393e7ce3
102 changed files with 13048 additions and 4230 deletions
|
|
@ -38,8 +38,11 @@ kover {
|
||||||
reports {
|
reports {
|
||||||
filters {
|
filters {
|
||||||
includes.classes("io.modelcontextprotocol.kotlin.sdk.*")
|
includes.classes("io.modelcontextprotocol.kotlin.sdk.*")
|
||||||
excludes.classes("io.modelcontextprotocol.kotlin.sdk.models.*") // temporary
|
excludes {
|
||||||
excludes.classes("io.modelcontextprotocol.kotlin.sdk.models.infrastructure.*") // generated
|
annotatedBy("kotlin.Deprecated")
|
||||||
|
classes("io.modelcontextprotocol.kotlin.sdk.models.*") // temporary
|
||||||
|
classes("io.modelcontextprotocol.kotlin.sdk.models.infrastructure.*") // generated
|
||||||
|
}
|
||||||
}
|
}
|
||||||
total {
|
total {
|
||||||
log {
|
log {
|
||||||
|
|
|
||||||
|
|
@ -1,53 +1,53 @@
|
||||||
public class io/modelcontextprotocol/kotlin/sdk/client/Client : io/modelcontextprotocol/kotlin/sdk/shared/Protocol {
|
public class io/modelcontextprotocol/kotlin/sdk/client/Client : io/modelcontextprotocol/kotlin/sdk/shared/Protocol {
|
||||||
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/client/ClientOptions;)V
|
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/types/Implementation;Lio/modelcontextprotocol/kotlin/sdk/client/ClientOptions;)V
|
||||||
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/client/ClientOptions;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/types/Implementation;Lio/modelcontextprotocol/kotlin/sdk/client/ClientOptions;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||||
public final fun addRoot (Ljava/lang/String;Ljava/lang/String;)V
|
public final fun addRoot (Ljava/lang/String;Ljava/lang/String;)V
|
||||||
public final fun addRoots (Ljava/util/List;)V
|
public final fun addRoots (Ljava/util/List;)V
|
||||||
protected final fun assertCapability (Ljava/lang/String;Ljava/lang/String;)V
|
protected final fun assertCapability (Ljava/lang/String;Ljava/lang/String;)V
|
||||||
protected fun assertCapabilityForMethod (Lio/modelcontextprotocol/kotlin/sdk/Method;)V
|
protected fun assertCapabilityForMethod (Lio/modelcontextprotocol/kotlin/sdk/types/Method;)V
|
||||||
protected fun assertNotificationCapability (Lio/modelcontextprotocol/kotlin/sdk/Method;)V
|
protected fun assertNotificationCapability (Lio/modelcontextprotocol/kotlin/sdk/types/Method;)V
|
||||||
public fun assertRequestHandlerCapability (Lio/modelcontextprotocol/kotlin/sdk/Method;)V
|
public fun assertRequestHandlerCapability (Lio/modelcontextprotocol/kotlin/sdk/types/Method;)V
|
||||||
public final fun callTool (Lio/modelcontextprotocol/kotlin/sdk/CallToolRequest;ZLio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun callTool (Lio/modelcontextprotocol/kotlin/sdk/types/CallToolRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public final fun callTool (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ZLio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun callTool (Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun callTool$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/CallToolRequest;ZLio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun callTool$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/CallToolRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public static synthetic fun callTool$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;ZLio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun callTool$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Ljava/lang/String;Ljava/util/Map;Ljava/util/Map;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun complete (Lio/modelcontextprotocol/kotlin/sdk/CompleteRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun complete (Lio/modelcontextprotocol/kotlin/sdk/types/CompleteRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun complete$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/CompleteRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun complete$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/CompleteRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public fun connect (Lio/modelcontextprotocol/kotlin/sdk/shared/Transport;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun connect (Lio/modelcontextprotocol/kotlin/sdk/shared/Transport;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public final fun getPrompt (Lio/modelcontextprotocol/kotlin/sdk/GetPromptRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun getPrompt (Lio/modelcontextprotocol/kotlin/sdk/types/GetPromptRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun getPrompt$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/GetPromptRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun getPrompt$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/GetPromptRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun getServerCapabilities ()Lio/modelcontextprotocol/kotlin/sdk/ServerCapabilities;
|
public final fun getServerCapabilities ()Lio/modelcontextprotocol/kotlin/sdk/types/ServerCapabilities;
|
||||||
public final fun getServerInstructions ()Ljava/lang/String;
|
public final fun getServerInstructions ()Ljava/lang/String;
|
||||||
public final fun getServerVersion ()Lio/modelcontextprotocol/kotlin/sdk/Implementation;
|
public final fun getServerVersion ()Lio/modelcontextprotocol/kotlin/sdk/types/Implementation;
|
||||||
public final fun listPrompts (Lio/modelcontextprotocol/kotlin/sdk/ListPromptsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun listPrompts (Lio/modelcontextprotocol/kotlin/sdk/types/ListPromptsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun listPrompts$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/ListPromptsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun listPrompts$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/ListPromptsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun listResourceTemplates (Lio/modelcontextprotocol/kotlin/sdk/ListResourceTemplatesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun listResourceTemplates (Lio/modelcontextprotocol/kotlin/sdk/types/ListResourceTemplatesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun listResourceTemplates$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/ListResourceTemplatesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun listResourceTemplates$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/ListResourceTemplatesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun listResources (Lio/modelcontextprotocol/kotlin/sdk/ListResourcesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun listResources (Lio/modelcontextprotocol/kotlin/sdk/types/ListResourcesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun listResources$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/ListResourcesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun listResources$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/ListResourcesRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun listTools (Lio/modelcontextprotocol/kotlin/sdk/ListToolsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun listTools (Lio/modelcontextprotocol/kotlin/sdk/types/ListToolsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun listTools$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/ListToolsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun listTools$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/ListToolsRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun ping (Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun ping (Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun ping$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun ping$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun readResource (Lio/modelcontextprotocol/kotlin/sdk/ReadResourceRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun readResource (Lio/modelcontextprotocol/kotlin/sdk/types/ReadResourceRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun readResource$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/ReadResourceRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun readResource$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/ReadResourceRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun removeRoot (Ljava/lang/String;)Z
|
public final fun removeRoot (Ljava/lang/String;)Z
|
||||||
public final fun removeRoots (Ljava/util/List;)I
|
public final fun removeRoots (Ljava/util/List;)I
|
||||||
public final fun sendRootsListChanged (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun sendRootsListChanged (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public final fun setElicitationHandler (Lkotlin/jvm/functions/Function1;)V
|
public final fun setElicitationHandler (Lkotlin/jvm/functions/Function1;)V
|
||||||
public final fun setLoggingLevel (Lio/modelcontextprotocol/kotlin/sdk/LoggingLevel;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun setLoggingLevel (Lio/modelcontextprotocol/kotlin/sdk/types/LoggingLevel;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun setLoggingLevel$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/LoggingLevel;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun setLoggingLevel$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/LoggingLevel;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun subscribeResource (Lio/modelcontextprotocol/kotlin/sdk/SubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun subscribeResource (Lio/modelcontextprotocol/kotlin/sdk/types/SubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun subscribeResource$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/SubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun subscribeResource$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/SubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun unsubscribeResource (Lio/modelcontextprotocol/kotlin/sdk/UnsubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun unsubscribeResource (Lio/modelcontextprotocol/kotlin/sdk/types/UnsubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun unsubscribeResource$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/UnsubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun unsubscribeResource$default (Lio/modelcontextprotocol/kotlin/sdk/client/Client;Lio/modelcontextprotocol/kotlin/sdk/types/UnsubscribeRequest;Lio/modelcontextprotocol/kotlin/sdk/shared/RequestOptions;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class io/modelcontextprotocol/kotlin/sdk/client/ClientOptions : io/modelcontextprotocol/kotlin/sdk/shared/ProtocolOptions {
|
public final class io/modelcontextprotocol/kotlin/sdk/client/ClientOptions : io/modelcontextprotocol/kotlin/sdk/shared/ProtocolOptions {
|
||||||
public fun <init> ()V
|
public fun <init> ()V
|
||||||
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/ClientCapabilities;Z)V
|
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/types/ClientCapabilities;Z)V
|
||||||
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/ClientCapabilities;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/types/ClientCapabilities;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||||
public final fun getCapabilities ()Lio/modelcontextprotocol/kotlin/sdk/ClientCapabilities;
|
public final fun getCapabilities ()Lio/modelcontextprotocol/kotlin/sdk/types/ClientCapabilities;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class io/modelcontextprotocol/kotlin/sdk/client/KtorClientKt {
|
public final class io/modelcontextprotocol/kotlin/sdk/client/KtorClientKt {
|
||||||
|
|
@ -61,14 +61,14 @@ public final class io/modelcontextprotocol/kotlin/sdk/client/SseClientTransport
|
||||||
public synthetic fun <init> (Lio/ktor/client/HttpClient;Ljava/lang/String;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
public synthetic fun <init> (Lio/ktor/client/HttpClient;Ljava/lang/String;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||||
public synthetic fun <init> (Lio/ktor/client/HttpClient;Ljava/lang/String;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
|
public synthetic fun <init> (Lio/ktor/client/HttpClient;Ljava/lang/String;Lkotlin/time/Duration;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||||
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public fun send (Lio/modelcontextprotocol/kotlin/sdk/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun send (Lio/modelcontextprotocol/kotlin/sdk/types/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class io/modelcontextprotocol/kotlin/sdk/client/StdioClientTransport : io/modelcontextprotocol/kotlin/sdk/shared/AbstractTransport {
|
public final class io/modelcontextprotocol/kotlin/sdk/client/StdioClientTransport : io/modelcontextprotocol/kotlin/sdk/shared/AbstractTransport {
|
||||||
public fun <init> (Lkotlinx/io/Source;Lkotlinx/io/Sink;)V
|
public fun <init> (Lkotlinx/io/Source;Lkotlinx/io/Sink;)V
|
||||||
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public fun send (Lio/modelcontextprotocol/kotlin/sdk/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun send (Lio/modelcontextprotocol/kotlin/sdk/types/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -78,9 +78,9 @@ public final class io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClien
|
||||||
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public final fun getProtocolVersion ()Ljava/lang/String;
|
public final fun getProtocolVersion ()Ljava/lang/String;
|
||||||
public final fun getSessionId ()Ljava/lang/String;
|
public final fun getSessionId ()Ljava/lang/String;
|
||||||
public final fun send (Lio/modelcontextprotocol/kotlin/sdk/JSONRPCMessage;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun send (Lio/modelcontextprotocol/kotlin/sdk/types/JSONRPCMessage;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public fun send (Lio/modelcontextprotocol/kotlin/sdk/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun send (Lio/modelcontextprotocol/kotlin/sdk/types/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public static synthetic fun send$default (Lio/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransport;Lio/modelcontextprotocol/kotlin/sdk/JSONRPCMessage;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
public static synthetic fun send$default (Lio/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransport;Lio/modelcontextprotocol/kotlin/sdk/types/JSONRPCMessage;Ljava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
|
||||||
public final fun setProtocolVersion (Ljava/lang/String;)V
|
public final fun setProtocolVersion (Ljava/lang/String;)V
|
||||||
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
public final fun terminateSession (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public final fun terminateSession (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
|
|
|
||||||
|
|
@ -1,63 +1,59 @@
|
||||||
package io.modelcontextprotocol.kotlin.sdk.client
|
package io.modelcontextprotocol.kotlin.sdk.client
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CallToolRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CallToolResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CallToolResultBase
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ClientCapabilities
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CompatibilityCallToolResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CompleteRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CompleteResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CreateElicitationRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CreateElicitationResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.EmptyRequestResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.GetPromptRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.GetPromptResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Implementation
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.InitializeRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.InitializeResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.InitializedNotification
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.LATEST_PROTOCOL_VERSION
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListPromptsRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListPromptsResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListResourceTemplatesRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListResourceTemplatesResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListResourcesRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListResourcesResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListRootsRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListRootsResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListToolsRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ListToolsResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.LoggingLevel
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.LoggingMessageNotification.SetLevelRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Method
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.PingRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ReadResourceRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ReadResourceResult
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Root
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.RootsListChangedNotification
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.SUPPORTED_PROTOCOL_VERSIONS
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.SubscribeRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.UnsubscribeRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.Protocol
|
import io.modelcontextprotocol.kotlin.sdk.shared.Protocol
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.ProtocolOptions
|
import io.modelcontextprotocol.kotlin.sdk.shared.ProtocolOptions
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.RequestOptions
|
import io.modelcontextprotocol.kotlin.sdk.shared.RequestOptions
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.Transport
|
import io.modelcontextprotocol.kotlin.sdk.shared.Transport
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.CallToolRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.CallToolRequestParams
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.CallToolResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ClientCapabilities
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.CompleteRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.CompleteResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ElicitRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ElicitResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.EmptyResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.GetPromptRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.GetPromptResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.Implementation
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.InitializeRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.InitializeRequestParams
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.InitializeResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.InitializedNotification
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.LATEST_PROTOCOL_VERSION
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListPromptsRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListPromptsResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListResourceTemplatesRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListResourceTemplatesResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListResourcesRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListResourcesResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListRootsRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListRootsResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListToolsRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ListToolsResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.LoggingLevel
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.Method
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.PingRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ReadResourceRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ReadResourceResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.RequestMeta
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.Root
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.RootsListChangedNotification
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.SUPPORTED_PROTOCOL_VERSIONS
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.SetLevelRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.SetLevelRequestParams
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.SubscribeRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.UnsubscribeRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.toJson
|
||||||
import kotlinx.atomicfu.atomic
|
import kotlinx.atomicfu.atomic
|
||||||
import kotlinx.atomicfu.getAndUpdate
|
import kotlinx.atomicfu.getAndUpdate
|
||||||
import kotlinx.atomicfu.update
|
import kotlinx.atomicfu.update
|
||||||
import kotlinx.collections.immutable.minus
|
import kotlinx.collections.immutable.minus
|
||||||
import kotlinx.collections.immutable.persistentMapOf
|
import kotlinx.collections.immutable.persistentMapOf
|
||||||
import kotlinx.collections.immutable.toPersistentSet
|
import kotlinx.collections.immutable.toPersistentSet
|
||||||
import kotlinx.serialization.ExperimentalSerializationApi
|
|
||||||
import kotlinx.serialization.json.JsonElement
|
|
||||||
import kotlinx.serialization.json.JsonNull
|
|
||||||
import kotlinx.serialization.json.JsonObject
|
import kotlinx.serialization.json.JsonObject
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
|
||||||
import kotlinx.serialization.json.add
|
|
||||||
import kotlinx.serialization.json.buildJsonArray
|
|
||||||
import kotlinx.serialization.json.buildJsonObject
|
|
||||||
import kotlin.coroutines.cancellation.CancellationException
|
import kotlin.coroutines.cancellation.CancellationException
|
||||||
|
|
||||||
private val logger = KotlinLogging.logger {}
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
@ -152,9 +148,11 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
|
|
||||||
try {
|
try {
|
||||||
val message = InitializeRequest(
|
val message = InitializeRequest(
|
||||||
|
InitializeRequestParams(
|
||||||
protocolVersion = LATEST_PROTOCOL_VERSION,
|
protocolVersion = LATEST_PROTOCOL_VERSION,
|
||||||
capabilities = capabilities,
|
capabilities = capabilities,
|
||||||
clientInfo = clientInfo,
|
clientInfo = clientInfo,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
val result = request<InitializeResult>(message)
|
val result = request<InitializeResult>(message)
|
||||||
|
|
||||||
|
|
@ -293,8 +291,7 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
* @param options Optional request options.
|
* @param options Optional request options.
|
||||||
* @throws IllegalStateException If the server does not support the ping method (unlikely).
|
* @throws IllegalStateException If the server does not support the ping method (unlikely).
|
||||||
*/
|
*/
|
||||||
public suspend fun ping(options: RequestOptions? = null): EmptyRequestResult =
|
public suspend fun ping(options: RequestOptions? = null): EmptyResult = request(PingRequest(), options)
|
||||||
request<EmptyRequestResult>(PingRequest(), options)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a completion request to the server, typically to generate or complete some content.
|
* Sends a completion request to the server, typically to generate or complete some content.
|
||||||
|
|
@ -314,8 +311,8 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
* @param options Optional request options.
|
* @param options Optional request options.
|
||||||
* @throws IllegalStateException If the server does not support logging.
|
* @throws IllegalStateException If the server does not support logging.
|
||||||
*/
|
*/
|
||||||
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyRequestResult =
|
public suspend fun setLoggingLevel(level: LoggingLevel, options: RequestOptions? = null): EmptyResult =
|
||||||
request(SetLevelRequest(level), options)
|
request(SetLevelRequest(SetLevelRequestParams(level)), options)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves a prompt by name from the server.
|
* Retrieves a prompt by name from the server.
|
||||||
|
|
@ -387,10 +384,8 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
* @param options Optional request options.
|
* @param options Optional request options.
|
||||||
* @throws IllegalStateException If the server does not support resource subscriptions.
|
* @throws IllegalStateException If the server does not support resource subscriptions.
|
||||||
*/
|
*/
|
||||||
public suspend fun subscribeResource(
|
public suspend fun subscribeResource(request: SubscribeRequest, options: RequestOptions? = null): EmptyResult =
|
||||||
request: SubscribeRequest,
|
request(request, options)
|
||||||
options: RequestOptions? = null,
|
|
||||||
): EmptyRequestResult = request(request, options)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unsubscribes from resource changes on the server.
|
* Unsubscribes from resource changes on the server.
|
||||||
|
|
@ -399,10 +394,8 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
* @param options Optional request options.
|
* @param options Optional request options.
|
||||||
* @throws IllegalStateException If the server does not support resource subscriptions.
|
* @throws IllegalStateException If the server does not support resource subscriptions.
|
||||||
*/
|
*/
|
||||||
public suspend fun unsubscribeResource(
|
public suspend fun unsubscribeResource(request: UnsubscribeRequest, options: RequestOptions? = null): EmptyResult =
|
||||||
request: UnsubscribeRequest,
|
request(request, options)
|
||||||
options: RequestOptions? = null,
|
|
||||||
): EmptyRequestResult = request(request, options)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a tool on the server by name, passing the specified arguments and metadata.
|
* Calls a tool on the server by name, passing the specified arguments and metadata.
|
||||||
|
|
@ -413,7 +406,6 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
* - Optional prefix: dot-separated labels followed by slash (e.g., "api.example.com/")
|
* - Optional prefix: dot-separated labels followed by slash (e.g., "api.example.com/")
|
||||||
* - Name: alphanumeric start/end, may contain hyphens, underscores, dots, alphanumerics
|
* - Name: alphanumeric start/end, may contain hyphens, underscores, dots, alphanumerics
|
||||||
* - Reserved prefixes starting with "mcp" or "modelcontextprotocol" are forbidden
|
* - Reserved prefixes starting with "mcp" or "modelcontextprotocol" are forbidden
|
||||||
* @param compatibility Whether to use compatibility mode for older protocol versions.
|
|
||||||
* @param options Optional request options.
|
* @param options Optional request options.
|
||||||
* @return The result of the tool call, or `null` if none.
|
* @return The result of the tool call, or `null` if none.
|
||||||
* @throws IllegalStateException If the server does not support tools.
|
* @throws IllegalStateException If the server does not support tools.
|
||||||
|
|
@ -422,40 +414,33 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
name: String,
|
name: String,
|
||||||
arguments: Map<String, Any?>,
|
arguments: Map<String, Any?>,
|
||||||
meta: Map<String, Any?> = emptyMap(),
|
meta: Map<String, Any?> = emptyMap(),
|
||||||
compatibility: Boolean = false,
|
|
||||||
options: RequestOptions? = null,
|
options: RequestOptions? = null,
|
||||||
): CallToolResultBase? {
|
): CallToolResult {
|
||||||
validateMetaKeys(meta.keys)
|
validateMetaKeys(meta.keys)
|
||||||
|
|
||||||
val jsonArguments = convertToJsonMap(arguments)
|
val jsonArguments = arguments.toJson()
|
||||||
val jsonMeta = convertToJsonMap(meta)
|
val jsonMeta = meta.toJson()
|
||||||
|
|
||||||
val request = CallToolRequest(
|
val request = CallToolRequest(
|
||||||
|
CallToolRequestParams(
|
||||||
name = name,
|
name = name,
|
||||||
arguments = JsonObject(jsonArguments),
|
arguments = JsonObject(jsonArguments),
|
||||||
_meta = JsonObject(jsonMeta),
|
meta = RequestMeta(JsonObject(jsonMeta)),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
return callTool(request, compatibility, options)
|
return callTool(request, options)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calls a tool on the server using a [CallToolRequest] object.
|
* Calls a tool on the server using a [CallToolRequest] object.
|
||||||
*
|
*
|
||||||
* @param request The request object containing the tool name and arguments.
|
* @param request The request object containing the tool name and arguments.
|
||||||
* @param compatibility Whether to use compatibility mode for older protocol versions.
|
|
||||||
* @param options Optional request options.
|
* @param options Optional request options.
|
||||||
* @return The result of the tool call, or `null` if none.
|
* @return The result of the tool call, or `null` if none.
|
||||||
* @throws IllegalStateException If the server does not support tools.
|
* @throws IllegalStateException If the server does not support tools.
|
||||||
*/
|
*/
|
||||||
public suspend fun callTool(
|
public suspend fun callTool(request: CallToolRequest, options: RequestOptions? = null): CallToolResult =
|
||||||
request: CallToolRequest,
|
request(request, options)
|
||||||
compatibility: Boolean = false,
|
|
||||||
options: RequestOptions? = null,
|
|
||||||
): CallToolResultBase? = if (compatibility) {
|
|
||||||
request<CompatibilityCallToolResult>(request, options)
|
|
||||||
} else {
|
|
||||||
request<CallToolResult>(request, options)
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists all available tools on the server.
|
* Lists all available tools on the server.
|
||||||
|
|
@ -570,14 +555,14 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
* @param handler The elicitation handler.
|
* @param handler The elicitation handler.
|
||||||
* @throws IllegalStateException if the client does not support elicitation.
|
* @throws IllegalStateException if the client does not support elicitation.
|
||||||
*/
|
*/
|
||||||
public fun setElicitationHandler(handler: (CreateElicitationRequest) -> CreateElicitationResult) {
|
public fun setElicitationHandler(handler: (ElicitRequest) -> ElicitResult) {
|
||||||
if (capabilities.elicitation == null) {
|
if (capabilities.elicitation == null) {
|
||||||
logger.error { "Failed to set elicitation handler: Client does not support elicitation" }
|
logger.error { "Failed to set elicitation handler: Client does not support elicitation" }
|
||||||
throw IllegalStateException("Client does not support elicitation.")
|
throw IllegalStateException("Client does not support elicitation.")
|
||||||
}
|
}
|
||||||
logger.info { "Setting the elicitation handler" }
|
logger.info { "Setting the elicitation handler" }
|
||||||
|
|
||||||
setRequestHandler<CreateElicitationRequest>(Method.Defined.ElicitationCreate) { request, _ ->
|
setRequestHandler<ElicitRequest>(Method.Defined.ElicitationCreate) { request, _ ->
|
||||||
handler(request)
|
handler(request)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -637,67 +622,4 @@ public open class Client(private val clientInfo: Implementation, options: Client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun convertToJsonMap(map: Map<String, Any?>): Map<String, JsonElement> = map.mapValues { (key, value) ->
|
|
||||||
try {
|
|
||||||
convertToJsonElement(value)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.warn { "Failed to convert value for key '$key': ${e.message}. Using string representation." }
|
|
||||||
JsonPrimitive(value.toString())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@OptIn(ExperimentalUnsignedTypes::class, ExperimentalSerializationApi::class)
|
|
||||||
private fun convertToJsonElement(value: Any?): JsonElement = when (value) {
|
|
||||||
null -> JsonNull
|
|
||||||
|
|
||||||
is JsonElement -> value
|
|
||||||
|
|
||||||
is String -> JsonPrimitive(value)
|
|
||||||
|
|
||||||
is Number -> JsonPrimitive(value)
|
|
||||||
|
|
||||||
is Boolean -> JsonPrimitive(value)
|
|
||||||
|
|
||||||
is Char -> JsonPrimitive(value.toString())
|
|
||||||
|
|
||||||
is Enum<*> -> JsonPrimitive(value.name)
|
|
||||||
|
|
||||||
is Map<*, *> -> buildJsonObject { value.forEach { (k, v) -> put(k.toString(), convertToJsonElement(v)) } }
|
|
||||||
|
|
||||||
is Collection<*> -> buildJsonArray { value.forEach { add(convertToJsonElement(it)) } }
|
|
||||||
|
|
||||||
is Array<*> -> buildJsonArray { value.forEach { add(convertToJsonElement(it)) } }
|
|
||||||
|
|
||||||
// Primitive arrays
|
|
||||||
is IntArray -> buildJsonArray { value.forEach { add(it) } }
|
|
||||||
|
|
||||||
is LongArray -> buildJsonArray { value.forEach { add(it) } }
|
|
||||||
|
|
||||||
is FloatArray -> buildJsonArray { value.forEach { add(it) } }
|
|
||||||
|
|
||||||
is DoubleArray -> buildJsonArray { value.forEach { add(it) } }
|
|
||||||
|
|
||||||
is BooleanArray -> buildJsonArray { value.forEach { add(it) } }
|
|
||||||
|
|
||||||
is ShortArray -> buildJsonArray { value.forEach { add(it) } }
|
|
||||||
|
|
||||||
is ByteArray -> buildJsonArray { value.forEach { add(it) } }
|
|
||||||
|
|
||||||
is CharArray -> buildJsonArray { value.forEach { add(it.toString()) } }
|
|
||||||
|
|
||||||
// Unsigned arrays
|
|
||||||
is UIntArray -> buildJsonArray { value.forEach { add(JsonPrimitive(it)) } }
|
|
||||||
|
|
||||||
is ULongArray -> buildJsonArray { value.forEach { add(JsonPrimitive(it)) } }
|
|
||||||
|
|
||||||
is UShortArray -> buildJsonArray { value.forEach { add(JsonPrimitive(it)) } }
|
|
||||||
|
|
||||||
is UByteArray -> buildJsonArray { value.forEach { add(JsonPrimitive(it)) } }
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
logger.debug { "Converting unknown type ${value::class} to string: $value" }
|
|
||||||
JsonPrimitive(value.toString())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ package io.modelcontextprotocol.kotlin.sdk.client
|
||||||
|
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.request.HttpRequestBuilder
|
import io.ktor.client.request.HttpRequestBuilder
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Implementation
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.LIB_VERSION
|
import io.modelcontextprotocol.kotlin.sdk.LIB_VERSION
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
|
import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.Implementation
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -14,9 +14,9 @@ import io.ktor.http.Url
|
||||||
import io.ktor.http.append
|
import io.ktor.http.append
|
||||||
import io.ktor.http.isSuccess
|
import io.ktor.http.isSuccess
|
||||||
import io.ktor.http.protocolWithAuthority
|
import io.ktor.http.protocolWithAuthority
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
|
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.McpJson
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
import kotlinx.coroutines.CoroutineName
|
import kotlinx.coroutines.CoroutineName
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
package io.modelcontextprotocol.kotlin.sdk.client
|
package io.modelcontextprotocol.kotlin.sdk.client
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.internal.IODispatcher
|
import io.modelcontextprotocol.kotlin.sdk.internal.IODispatcher
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
|
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.ReadBuffer
|
import io.modelcontextprotocol.kotlin.sdk.shared.ReadBuffer
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.serializeMessage
|
import io.modelcontextprotocol.kotlin.sdk.shared.serializeMessage
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
|
||||||
import kotlinx.coroutines.CoroutineName
|
import kotlinx.coroutines.CoroutineName
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
|
|
|
||||||
|
|
@ -21,13 +21,13 @@ import io.ktor.http.HttpStatusCode
|
||||||
import io.ktor.http.contentType
|
import io.ktor.http.contentType
|
||||||
import io.ktor.http.isSuccess
|
import io.ktor.http.isSuccess
|
||||||
import io.ktor.utils.io.readUTF8Line
|
import io.ktor.utils.io.readUTF8Line
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCNotification
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCResponse
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.RequestId
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
|
import io.modelcontextprotocol.kotlin.sdk.shared.AbstractTransport
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCNotification
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCResponse
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.McpJson
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.RequestId
|
||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.CoroutineName
|
import kotlinx.coroutines.CoroutineName
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
|
|
||||||
|
|
@ -2,9 +2,9 @@ package io.modelcontextprotocol.kotlin.sdk.client
|
||||||
|
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.request.HttpRequestBuilder
|
import io.ktor.client.request.HttpRequestBuilder
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Implementation
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.LIB_VERSION
|
import io.modelcontextprotocol.kotlin.sdk.LIB_VERSION
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
|
import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.Implementation
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,9 @@ package io.modelcontextprotocol.kotlin.sdk.client
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.request.HttpRequestBuilder
|
import io.ktor.client.request.HttpRequestBuilder
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Implementation
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.LIB_VERSION
|
import io.modelcontextprotocol.kotlin.sdk.LIB_VERSION
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
|
import io.modelcontextprotocol.kotlin.sdk.shared.IMPLEMENTATION_NAME
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.Implementation
|
||||||
|
|
||||||
private val logger = KotlinLogging.logger {}
|
private val logger = KotlinLogging.logger {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,238 @@
|
||||||
|
package io.modelcontextprotocol.kotlin.sdk.client
|
||||||
|
|
||||||
|
import dev.mokksy.mokksy.BuildingStep
|
||||||
|
import dev.mokksy.mokksy.Mokksy
|
||||||
|
import dev.mokksy.mokksy.StubConfiguration
|
||||||
|
import io.ktor.http.ContentType
|
||||||
|
import io.ktor.http.HttpMethod
|
||||||
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import io.ktor.sse.ServerSentEvent
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCRequest
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.RequestId
|
||||||
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
|
import kotlinx.serialization.json.JsonObject
|
||||||
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
|
import kotlinx.serialization.json.buildJsonObject
|
||||||
|
import kotlinx.serialization.json.contentOrNull
|
||||||
|
import kotlinx.serialization.json.jsonObject
|
||||||
|
import kotlinx.serialization.json.jsonPrimitive
|
||||||
|
import kotlinx.serialization.json.putJsonObject
|
||||||
|
|
||||||
|
const val MCP_SESSION_ID_HEADER = "Mcp-Session-Id"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* High-level helper for simulating an MCP server over Streaming HTTP transport with Server-Sent Events (SSE),
|
||||||
|
* built on top of an HTTP server using the [Mokksy](https://mokksy.dev) library.
|
||||||
|
*
|
||||||
|
* Provides test utilities to mock server behavior based on specific request conditions.
|
||||||
|
*
|
||||||
|
* @param verbose Whether to print detailed logs. Defaults to `false`.
|
||||||
|
* @author Konstantin Pavlov
|
||||||
|
*/
|
||||||
|
internal class MockMcp(verbose: Boolean = false) {
|
||||||
|
|
||||||
|
private val mokksy: Mokksy = Mokksy(verbose = verbose)
|
||||||
|
|
||||||
|
fun checkForUnmatchedRequests() {
|
||||||
|
mokksy.checkForUnmatchedRequests()
|
||||||
|
}
|
||||||
|
|
||||||
|
val url = "${mokksy.baseUrl()}/mcp"
|
||||||
|
|
||||||
|
@Suppress("LongParameterList")
|
||||||
|
fun onInitialize(
|
||||||
|
clientName: String? = null,
|
||||||
|
sessionId: String,
|
||||||
|
protocolVersion: String = "2025-03-26",
|
||||||
|
serverName: String = "Mock MCP Server",
|
||||||
|
serverVersion: String = "1.0.0",
|
||||||
|
capabilities: JsonObject = buildJsonObject {
|
||||||
|
putJsonObject("tools") {
|
||||||
|
put("listChanged", JsonPrimitive(false))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
val predicates = if (clientName != null) {
|
||||||
|
arrayOf<(JSONRPCRequest?) -> Boolean>({
|
||||||
|
it?.params?.jsonObject
|
||||||
|
?.get("clientInfo")?.jsonObject
|
||||||
|
?.get("name")?.jsonPrimitive
|
||||||
|
?.contentOrNull == clientName
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
emptyArray()
|
||||||
|
}
|
||||||
|
|
||||||
|
handleWithResult(
|
||||||
|
jsonRpcMethod = "initialize",
|
||||||
|
sessionId = sessionId,
|
||||||
|
bodyPredicates = predicates,
|
||||||
|
// language=json
|
||||||
|
result = """
|
||||||
|
{
|
||||||
|
"capabilities": $capabilities,
|
||||||
|
"protocolVersion": "$protocolVersion",
|
||||||
|
"serverInfo": {
|
||||||
|
"name": "$serverName",
|
||||||
|
"version": "$serverVersion"
|
||||||
|
},
|
||||||
|
"_meta": {
|
||||||
|
"foo": "bar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""".trimIndent(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onJSONRPCRequest(
|
||||||
|
httpMethod: HttpMethod = HttpMethod.Post,
|
||||||
|
jsonRpcMethod: String,
|
||||||
|
expectedSessionId: String? = null,
|
||||||
|
vararg bodyPredicates: (JSONRPCRequest) -> Boolean,
|
||||||
|
): BuildingStep<JSONRPCRequest> = mokksy.method(
|
||||||
|
configuration = StubConfiguration(removeAfterMatch = true),
|
||||||
|
httpMethod = httpMethod,
|
||||||
|
requestType = JSONRPCRequest::class,
|
||||||
|
) {
|
||||||
|
path("/mcp")
|
||||||
|
expectedSessionId?.let {
|
||||||
|
containsHeader(MCP_SESSION_ID_HEADER, it)
|
||||||
|
}
|
||||||
|
bodyMatchesPredicate(
|
||||||
|
description = "JSON-RPC version is '2.0'",
|
||||||
|
predicate =
|
||||||
|
{
|
||||||
|
it!!.jsonrpc == "2.0"
|
||||||
|
},
|
||||||
|
)
|
||||||
|
bodyMatchesPredicate(
|
||||||
|
description = "JSON-RPC Method should be '$jsonRpcMethod'",
|
||||||
|
predicate =
|
||||||
|
{
|
||||||
|
it!!.method == jsonRpcMethod
|
||||||
|
},
|
||||||
|
)
|
||||||
|
bodyPredicates.forEach { predicate ->
|
||||||
|
bodyMatchesPredicate(predicate = { predicate.invoke(it!!) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("LongParameterList")
|
||||||
|
fun handleWithResult(
|
||||||
|
httpMethod: HttpMethod = HttpMethod.Post,
|
||||||
|
jsonRpcMethod: String,
|
||||||
|
expectedSessionId: String? = null,
|
||||||
|
sessionId: String,
|
||||||
|
contentType: ContentType = ContentType.Application.Json,
|
||||||
|
statusCode: HttpStatusCode = HttpStatusCode.OK,
|
||||||
|
vararg bodyPredicates: (JSONRPCRequest) -> Boolean,
|
||||||
|
result: () -> JsonObject,
|
||||||
|
) {
|
||||||
|
onJSONRPCRequest(
|
||||||
|
httpMethod = httpMethod,
|
||||||
|
jsonRpcMethod = jsonRpcMethod,
|
||||||
|
expectedSessionId = expectedSessionId,
|
||||||
|
bodyPredicates = bodyPredicates,
|
||||||
|
) respondsWith {
|
||||||
|
val requestId = when (request.body.id) {
|
||||||
|
is RequestId.NumberId -> (request.body.id as RequestId.NumberId).value.toString()
|
||||||
|
is RequestId.StringId -> "\"${(request.body.id as RequestId.StringId).value}\""
|
||||||
|
}
|
||||||
|
val resultObject = result!!.invoke()
|
||||||
|
// language=json
|
||||||
|
body = """
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": $requestId,
|
||||||
|
"result": $resultObject
|
||||||
|
}
|
||||||
|
""".trimIndent()
|
||||||
|
this.contentType = contentType
|
||||||
|
headers += MCP_SESSION_ID_HEADER to sessionId
|
||||||
|
httpStatus = statusCode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("LongParameterList")
|
||||||
|
fun handleWithResult(
|
||||||
|
httpMethod: HttpMethod = HttpMethod.Post,
|
||||||
|
jsonRpcMethod: String,
|
||||||
|
expectedSessionId: String? = null,
|
||||||
|
sessionId: String,
|
||||||
|
contentType: ContentType = ContentType.Application.Json,
|
||||||
|
statusCode: HttpStatusCode = HttpStatusCode.OK,
|
||||||
|
vararg bodyPredicates: (JSONRPCRequest) -> Boolean,
|
||||||
|
result: String,
|
||||||
|
) {
|
||||||
|
handleWithResult(
|
||||||
|
httpMethod = httpMethod,
|
||||||
|
jsonRpcMethod = jsonRpcMethod,
|
||||||
|
expectedSessionId = expectedSessionId,
|
||||||
|
sessionId = sessionId,
|
||||||
|
contentType = contentType,
|
||||||
|
statusCode = statusCode,
|
||||||
|
bodyPredicates = bodyPredicates,
|
||||||
|
result = {
|
||||||
|
Json.parseToJsonElement(result).jsonObject
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Suppress("LongParameterList")
|
||||||
|
fun handleJSONRPCRequest(
|
||||||
|
httpMethod: HttpMethod = HttpMethod.Post,
|
||||||
|
jsonRpcMethod: String,
|
||||||
|
expectedSessionId: String? = null,
|
||||||
|
sessionId: String,
|
||||||
|
contentType: ContentType = ContentType.Application.Json,
|
||||||
|
statusCode: HttpStatusCode = HttpStatusCode.OK,
|
||||||
|
vararg bodyPredicates: (JSONRPCRequest?) -> Boolean,
|
||||||
|
bodyBuilder: () -> String = { "" },
|
||||||
|
) {
|
||||||
|
onJSONRPCRequest(
|
||||||
|
httpMethod = httpMethod,
|
||||||
|
jsonRpcMethod = jsonRpcMethod,
|
||||||
|
expectedSessionId = expectedSessionId,
|
||||||
|
bodyPredicates = bodyPredicates,
|
||||||
|
) respondsWith {
|
||||||
|
body = bodyBuilder.invoke()
|
||||||
|
this.contentType = contentType
|
||||||
|
headers += MCP_SESSION_ID_HEADER to sessionId
|
||||||
|
httpStatus = statusCode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun onSubscribe(httpMethod: HttpMethod = HttpMethod.Post, sessionId: String): BuildingStep<Any> = mokksy.method(
|
||||||
|
httpMethod = httpMethod,
|
||||||
|
name = "MCP GETs",
|
||||||
|
requestType = Any::class,
|
||||||
|
) {
|
||||||
|
path("/mcp")
|
||||||
|
containsHeader(MCP_SESSION_ID_HEADER, sessionId)
|
||||||
|
containsHeader("Accept", "application/json,text/event-stream")
|
||||||
|
containsHeader("Cache-Control", "no-store")
|
||||||
|
}
|
||||||
|
|
||||||
|
fun handleSubscribeWithGet(sessionId: String, block: () -> Flow<ServerSentEvent>) {
|
||||||
|
onSubscribe(
|
||||||
|
httpMethod = HttpMethod.Get,
|
||||||
|
sessionId = sessionId,
|
||||||
|
) respondsWithSseStream {
|
||||||
|
headers += MCP_SESSION_ID_HEADER to sessionId
|
||||||
|
this.flow = block.invoke()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun mockUnsubscribeRequest(sessionId: String) {
|
||||||
|
mokksy.delete(
|
||||||
|
configuration = StubConfiguration(removeAfterMatch = true),
|
||||||
|
requestType = JSONRPCRequest::class,
|
||||||
|
) {
|
||||||
|
path("/mcp")
|
||||||
|
containsHeader(MCP_SESSION_ID_HEADER, sessionId)
|
||||||
|
} respondsWith {
|
||||||
|
body = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,219 @@
|
||||||
|
package io.modelcontextprotocol.kotlin.sdk.client
|
||||||
|
|
||||||
|
import io.kotest.matchers.collections.shouldContain
|
||||||
|
import io.ktor.http.ContentType
|
||||||
|
import io.ktor.http.HttpMethod
|
||||||
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import io.ktor.sse.ServerSentEvent
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.ClientCapabilities
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.Implementation
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.Tool
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.flow
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.serialization.json.buildJsonObject
|
||||||
|
import kotlinx.serialization.json.put
|
||||||
|
import kotlinx.serialization.json.putJsonObject
|
||||||
|
import org.junit.jupiter.api.TestInstance
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
import kotlin.uuid.ExperimentalUuidApi
|
||||||
|
import kotlin.uuid.Uuid
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration tests for the `StreamableHttpClientTransport` implementation
|
||||||
|
* using the [Mokksy](https://mokksy.dev) library
|
||||||
|
* to simulate Streaming HTTP with server-sent events (SSE).
|
||||||
|
* @author Konstantin Pavlov
|
||||||
|
*/
|
||||||
|
@OptIn(ExperimentalUuidApi::class)
|
||||||
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
|
@Suppress("LongMethod")
|
||||||
|
internal class OldStreamableHttpClientTest : AbstractStreamableHttpClientTest() {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `test streamableHttpClient`() = runBlocking {
|
||||||
|
val client = Client(
|
||||||
|
clientInfo = Implementation(
|
||||||
|
name = "client1",
|
||||||
|
version = "1.0.0",
|
||||||
|
),
|
||||||
|
options = ClientOptions(
|
||||||
|
capabilities = ClientCapabilities(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
val sessionId = Uuid.random().toString()
|
||||||
|
|
||||||
|
mockMcp.onInitialize(
|
||||||
|
clientName = "client1",
|
||||||
|
sessionId = sessionId,
|
||||||
|
)
|
||||||
|
|
||||||
|
mockMcp.handleJSONRPCRequest(
|
||||||
|
jsonRpcMethod = "notifications/initialized",
|
||||||
|
expectedSessionId = sessionId,
|
||||||
|
sessionId = sessionId,
|
||||||
|
statusCode = HttpStatusCode.Accepted,
|
||||||
|
)
|
||||||
|
|
||||||
|
mockMcp.handleSubscribeWithGet(sessionId) {
|
||||||
|
flow {
|
||||||
|
delay(500.milliseconds)
|
||||||
|
emit(
|
||||||
|
ServerSentEvent(
|
||||||
|
event = "message",
|
||||||
|
id = "1",
|
||||||
|
data = @Suppress("MaxLineLength")
|
||||||
|
//language=json
|
||||||
|
"""{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"upload-123","progress":50,"total":100}}""",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
delay(200.milliseconds)
|
||||||
|
emit(
|
||||||
|
ServerSentEvent(
|
||||||
|
data = @Suppress("MaxLineLength")
|
||||||
|
//language=json
|
||||||
|
"""{"jsonrpc":"2.0","method":"notifications/progress","params":{"progressToken":"upload-123","progress":50,"total":100}}""",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: how to get notifications via Client API?
|
||||||
|
|
||||||
|
mockMcp.handleWithResult(
|
||||||
|
jsonRpcMethod = "tools/list",
|
||||||
|
sessionId = sessionId,
|
||||||
|
// language=json
|
||||||
|
result = """
|
||||||
|
{
|
||||||
|
"tools": [
|
||||||
|
{
|
||||||
|
"name": "get_weather",
|
||||||
|
"title": "Weather Information Provider",
|
||||||
|
"description": "Get current weather information for a location",
|
||||||
|
"inputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"location": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "City name or zip code"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["location"]
|
||||||
|
},
|
||||||
|
"outputSchema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"temperature": {
|
||||||
|
"type": "number",
|
||||||
|
"description": "Temperature, Celsius"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["temperature"]
|
||||||
|
},
|
||||||
|
"_meta": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
""".trimIndent(),
|
||||||
|
)
|
||||||
|
|
||||||
|
connect(client)
|
||||||
|
|
||||||
|
val listToolsResult = client.listTools()
|
||||||
|
|
||||||
|
listToolsResult.tools shouldContain Tool(
|
||||||
|
name = "get_weather",
|
||||||
|
title = "Weather Information Provider",
|
||||||
|
description = "Get current weather information for a location",
|
||||||
|
inputSchema = Tool.Input(
|
||||||
|
properties = buildJsonObject {
|
||||||
|
putJsonObject("location") {
|
||||||
|
put("type", "string")
|
||||||
|
put("description", "City name or zip code")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
required = listOf("location"),
|
||||||
|
),
|
||||||
|
outputSchema = Tool.Output(
|
||||||
|
properties = buildJsonObject {
|
||||||
|
putJsonObject("temperature") {
|
||||||
|
put("type", "number")
|
||||||
|
put("description", "Temperature, Celsius")
|
||||||
|
}
|
||||||
|
},
|
||||||
|
required = listOf("temperature"),
|
||||||
|
),
|
||||||
|
annotations = null,
|
||||||
|
_meta = EmptyJsonObject,
|
||||||
|
)
|
||||||
|
|
||||||
|
mockMcp.mockUnsubscribeRequest(sessionId = sessionId)
|
||||||
|
|
||||||
|
client.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `handle MethodNotAllowed`() = runBlocking {
|
||||||
|
checkSupportNonStreamingResponse(
|
||||||
|
ContentType.Text.EventStream,
|
||||||
|
HttpStatusCode.MethodNotAllowed,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `handle non-streaming response`() = runBlocking {
|
||||||
|
checkSupportNonStreamingResponse(
|
||||||
|
ContentType.Application.Json,
|
||||||
|
HttpStatusCode.OK,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun checkSupportNonStreamingResponse(contentType: ContentType, statusCode: HttpStatusCode) {
|
||||||
|
val sessionId = "SID_${Uuid.random().toHexString()}"
|
||||||
|
val clientName = "client-${Uuid.random().toHexString()}"
|
||||||
|
val client = Client(
|
||||||
|
clientInfo = Implementation(name = clientName, version = "1.0.0"),
|
||||||
|
options = ClientOptions(
|
||||||
|
capabilities = ClientCapabilities(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
mockMcp.onInitialize(clientName = clientName, sessionId = sessionId)
|
||||||
|
|
||||||
|
mockMcp.handleJSONRPCRequest(
|
||||||
|
jsonRpcMethod = "notifications/initialized",
|
||||||
|
expectedSessionId = sessionId,
|
||||||
|
sessionId = sessionId,
|
||||||
|
statusCode = HttpStatusCode.Accepted,
|
||||||
|
)
|
||||||
|
|
||||||
|
mockMcp.onSubscribe(
|
||||||
|
httpMethod = HttpMethod.Get,
|
||||||
|
sessionId = sessionId,
|
||||||
|
) respondsWith {
|
||||||
|
headers += MCP_SESSION_ID_HEADER to sessionId
|
||||||
|
body = null
|
||||||
|
httpStatus = statusCode
|
||||||
|
this.contentType = contentType
|
||||||
|
}
|
||||||
|
|
||||||
|
mockMcp.handleWithResult(jsonRpcMethod = "ping", sessionId = sessionId) {
|
||||||
|
buildJsonObject {}
|
||||||
|
}
|
||||||
|
|
||||||
|
mockMcp.mockUnsubscribeRequest(sessionId = sessionId)
|
||||||
|
|
||||||
|
connect(client)
|
||||||
|
|
||||||
|
delay(1.seconds)
|
||||||
|
|
||||||
|
client.ping() // connection is still alive
|
||||||
|
|
||||||
|
client.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,24 +1,27 @@
|
||||||
package io.modelcontextprotocol.kotlin.sdk.shared
|
package io.modelcontextprotocol.kotlin.sdk.shared
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.modelcontextprotocol.kotlin.sdk.CancelledNotification
|
import io.modelcontextprotocol.kotlin.sdk.types.CancelledNotification
|
||||||
import io.modelcontextprotocol.kotlin.sdk.EmptyRequestResult
|
import io.modelcontextprotocol.kotlin.sdk.types.CancelledNotificationParams
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ErrorCode
|
import io.modelcontextprotocol.kotlin.sdk.types.EmptyResult
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCError
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCError
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCNotification
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCNotification
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCRequest
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCResponse
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCResponse
|
||||||
import io.modelcontextprotocol.kotlin.sdk.McpError
|
import io.modelcontextprotocol.kotlin.sdk.types.McpException
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Method
|
import io.modelcontextprotocol.kotlin.sdk.types.McpJson
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Notification
|
import io.modelcontextprotocol.kotlin.sdk.types.Method
|
||||||
import io.modelcontextprotocol.kotlin.sdk.PingRequest
|
import io.modelcontextprotocol.kotlin.sdk.types.Notification
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Progress
|
import io.modelcontextprotocol.kotlin.sdk.types.PingRequest
|
||||||
import io.modelcontextprotocol.kotlin.sdk.ProgressNotification
|
import io.modelcontextprotocol.kotlin.sdk.types.Progress
|
||||||
import io.modelcontextprotocol.kotlin.sdk.Request
|
import io.modelcontextprotocol.kotlin.sdk.types.ProgressNotification
|
||||||
import io.modelcontextprotocol.kotlin.sdk.RequestId
|
import io.modelcontextprotocol.kotlin.sdk.types.ProgressToken
|
||||||
import io.modelcontextprotocol.kotlin.sdk.RequestResult
|
import io.modelcontextprotocol.kotlin.sdk.types.RPCError
|
||||||
import io.modelcontextprotocol.kotlin.sdk.fromJSON
|
import io.modelcontextprotocol.kotlin.sdk.types.Request
|
||||||
import io.modelcontextprotocol.kotlin.sdk.toJSON
|
import io.modelcontextprotocol.kotlin.sdk.types.RequestId
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.RequestResult
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.fromJSON
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.toJSON
|
||||||
import kotlinx.atomicfu.AtomicRef
|
import kotlinx.atomicfu.AtomicRef
|
||||||
import kotlinx.atomicfu.atomic
|
import kotlinx.atomicfu.atomic
|
||||||
import kotlinx.atomicfu.getAndUpdate
|
import kotlinx.atomicfu.getAndUpdate
|
||||||
|
|
@ -35,9 +38,6 @@ import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.JsonObject
|
import kotlinx.serialization.json.JsonObject
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
import kotlinx.serialization.json.encodeToJsonElement
|
import kotlinx.serialization.json.encodeToJsonElement
|
||||||
import kotlinx.serialization.serializer
|
|
||||||
import kotlin.reflect.KType
|
|
||||||
import kotlin.reflect.typeOf
|
|
||||||
import kotlin.time.Duration
|
import kotlin.time.Duration
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
|
|
@ -51,6 +51,11 @@ public const val IMPLEMENTATION_NAME: String = "mcp-ktor"
|
||||||
public typealias ProgressCallback = (Progress) -> Unit
|
public typealias ProgressCallback = (Progress) -> Unit
|
||||||
|
|
||||||
@OptIn(ExperimentalSerializationApi::class)
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
|
@Deprecated(
|
||||||
|
message = "Use McpJson instead",
|
||||||
|
replaceWith = ReplaceWith("McpJson", "io.modelcontextprotocol.kotlin.sdk.types.McpJson"),
|
||||||
|
level = DeprecationLevel.WARNING,
|
||||||
|
)
|
||||||
public val McpJson: Json by lazy {
|
public val McpJson: Json by lazy {
|
||||||
Json {
|
Json {
|
||||||
ignoreUnknownKeys = true
|
ignoreUnknownKeys = true
|
||||||
|
|
@ -143,9 +148,9 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
public val responseHandlers: Map<RequestId, (response: JSONRPCResponse?, error: Exception?) -> Unit>
|
public val responseHandlers: Map<RequestId, (response: JSONRPCResponse?, error: Exception?) -> Unit>
|
||||||
get() = _responseHandlers.value
|
get() = _responseHandlers.value
|
||||||
|
|
||||||
private val _progressHandlers: AtomicRef<PersistentMap<RequestId, ProgressCallback>> =
|
private val _progressHandlers: AtomicRef<PersistentMap<ProgressToken, ProgressCallback>> =
|
||||||
atomic(persistentMapOf())
|
atomic(persistentMapOf())
|
||||||
public val progressHandlers: Map<RequestId, ProgressCallback>
|
public val progressHandlers: Map<ProgressToken, ProgressCallback>
|
||||||
get() = _progressHandlers.value
|
get() = _progressHandlers.value
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -183,7 +188,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
}
|
}
|
||||||
|
|
||||||
setRequestHandler<PingRequest>(Method.Defined.Ping) { _, _ ->
|
setRequestHandler<PingRequest>(Method.Defined.Ping) { _, _ ->
|
||||||
EmptyRequestResult()
|
EmptyResult()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -222,7 +227,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
transport = null
|
transport = null
|
||||||
onClose()
|
onClose()
|
||||||
|
|
||||||
val error = McpError(ErrorCode.Defined.ConnectionClosed.code, "Connection closed")
|
val error = McpException(RPCError.ErrorCode.CONNECTION_CLOSED, "Connection closed")
|
||||||
for (handler in handlersToNotify) {
|
for (handler in handlersToNotify) {
|
||||||
handler(null, error)
|
handler(null, error)
|
||||||
}
|
}
|
||||||
|
|
@ -254,10 +259,10 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
logger.trace { "No handler found for request: ${request.method}" }
|
logger.trace { "No handler found for request: ${request.method}" }
|
||||||
try {
|
try {
|
||||||
transport?.send(
|
transport?.send(
|
||||||
JSONRPCResponse(
|
JSONRPCError(
|
||||||
id = request.id,
|
id = request.id,
|
||||||
error = JSONRPCError(
|
error = RPCError(
|
||||||
ErrorCode.Defined.MethodNotFound,
|
code = RPCError.ErrorCode.METHOD_NOT_FOUND,
|
||||||
message = "Server does not support ${request.method}",
|
message = "Server does not support ${request.method}",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
@ -276,7 +281,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
transport?.send(
|
transport?.send(
|
||||||
JSONRPCResponse(
|
JSONRPCResponse(
|
||||||
id = request.id,
|
id = request.id,
|
||||||
result = result,
|
result = result ?: EmptyResult(),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
} catch (cause: Throwable) {
|
} catch (cause: Throwable) {
|
||||||
|
|
@ -284,10 +289,10 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
|
|
||||||
try {
|
try {
|
||||||
transport?.send(
|
transport?.send(
|
||||||
JSONRPCResponse(
|
JSONRPCError(
|
||||||
id = request.id,
|
id = request.id,
|
||||||
error = JSONRPCError(
|
error = RPCError(
|
||||||
code = ErrorCode.Defined.InternalError,
|
code = RPCError.ErrorCode.INTERNAL_ERROR,
|
||||||
message = cause.message ?: "Internal error",
|
message = cause.message ?: "Internal error",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
@ -324,7 +329,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onResponse(response: JSONRPCResponse?, error: JSONRPCError?) {
|
private fun onResponse(response: JSONRPCResponse?, error: JSONRPCError?) {
|
||||||
val messageId = response?.id
|
val messageId = response?.id ?: error?.id
|
||||||
|
|
||||||
val oldResponseHandlers = _responseHandlers.getAndUpdate { current ->
|
val oldResponseHandlers = _responseHandlers.getAndUpdate { current ->
|
||||||
if (messageId != null && messageId in current) {
|
if (messageId != null && messageId in current) {
|
||||||
|
|
@ -347,10 +352,10 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
handler(response, null)
|
handler(response, null)
|
||||||
} else {
|
} else {
|
||||||
check(error != null)
|
check(error != null)
|
||||||
val error = McpError(
|
val error = McpException(
|
||||||
error.code.code,
|
code = error.error.code,
|
||||||
error.message,
|
message = error.error.message,
|
||||||
error.data,
|
data = error.error.data,
|
||||||
)
|
)
|
||||||
handler(null, error)
|
handler(null, error)
|
||||||
}
|
}
|
||||||
|
|
@ -415,11 +420,6 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
return@put
|
return@put
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response?.error != null) {
|
|
||||||
result.completeExceptionally(IllegalStateException(response.error.toString()))
|
|
||||||
return@put
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
result.complete(response!!.result as T)
|
result.complete(response!!.result as T)
|
||||||
|
|
@ -434,7 +434,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
_progressHandlers.update { current -> current.remove(messageId) }
|
_progressHandlers.update { current -> current.remove(messageId) }
|
||||||
|
|
||||||
val notification = CancelledNotification(
|
val notification = CancelledNotification(
|
||||||
params = CancelledNotification.Params(
|
params = CancelledNotificationParams(
|
||||||
requestId = messageId,
|
requestId = messageId,
|
||||||
reason = reason.message ?: "Unknown",
|
reason = reason.message ?: "Unknown",
|
||||||
),
|
),
|
||||||
|
|
@ -459,10 +459,10 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
} catch (cause: TimeoutCancellationException) {
|
} catch (cause: TimeoutCancellationException) {
|
||||||
logger.error { "Request timed out after ${timeout.inWholeMilliseconds}ms: ${request.method}" }
|
logger.error { "Request timed out after ${timeout.inWholeMilliseconds}ms: ${request.method}" }
|
||||||
cancel(
|
cancel(
|
||||||
McpError(
|
McpException(
|
||||||
ErrorCode.Defined.RequestTimeout.code,
|
code = RPCError.ErrorCode.REQUEST_TIMEOUT,
|
||||||
"Request timed out",
|
message = "Request timed out",
|
||||||
JsonObject(mutableMapOf("timeout" to JsonPrimitive(timeout.inWholeMilliseconds))),
|
data = JsonObject(mutableMapOf("timeout" to JsonPrimitive(timeout.inWholeMilliseconds))),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
result.cancel(cause)
|
result.cancel(cause)
|
||||||
|
|
@ -491,28 +491,21 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
|
||||||
method: Method,
|
method: Method,
|
||||||
noinline block: suspend (T, RequestHandlerExtra) -> RequestResult?,
|
noinline block: suspend (T, RequestHandlerExtra) -> RequestResult?,
|
||||||
) {
|
) {
|
||||||
setRequestHandler(typeOf<T>(), method, block)
|
setRequestHandlerInternal(method, block)
|
||||||
}
|
}
|
||||||
|
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal fun <T : Request> setRequestHandler(
|
@Suppress("UNCHECKED_CAST")
|
||||||
requestType: KType,
|
internal fun <T : Request> setRequestHandlerInternal(
|
||||||
method: Method,
|
method: Method,
|
||||||
block: suspend (T, RequestHandlerExtra) -> RequestResult?,
|
block: suspend (T, RequestHandlerExtra) -> RequestResult?,
|
||||||
) {
|
) {
|
||||||
assertRequestHandlerCapability(method)
|
assertRequestHandlerCapability(method)
|
||||||
|
|
||||||
val serializer = McpJson.serializersModule.serializer(requestType)
|
|
||||||
|
|
||||||
_requestHandlers.update { current ->
|
_requestHandlers.update { current ->
|
||||||
current.put(method.value) { request, extraHandler ->
|
current.put(method.value) { jSONRPCRequest, extraHandler ->
|
||||||
val result = McpJson.decodeFromJsonElement(serializer, request.params)
|
val request = jSONRPCRequest.fromJSON()
|
||||||
val response = if (result != null) {
|
val response = block(request as T, extraHandler)
|
||||||
@Suppress("UNCHECKED_CAST")
|
|
||||||
block(result as T, extraHandler)
|
|
||||||
} else {
|
|
||||||
EmptyRequestResult()
|
|
||||||
}
|
|
||||||
response
|
response
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
package io.modelcontextprotocol.kotlin.sdk.shared
|
package io.modelcontextprotocol.kotlin.sdk.shared
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.McpJson
|
||||||
import kotlinx.io.Buffer
|
import kotlinx.io.Buffer
|
||||||
import kotlinx.io.indexOf
|
import kotlinx.io.indexOf
|
||||||
import kotlinx.io.readString
|
import kotlinx.io.readString
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
package io.modelcontextprotocol.kotlin.sdk.shared
|
package io.modelcontextprotocol.kotlin.sdk.shared
|
||||||
|
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
|
||||||
import kotlinx.coroutines.CompletableDeferred
|
import kotlinx.coroutines.CompletableDeferred
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ import io.ktor.websocket.Frame
|
||||||
import io.ktor.websocket.WebSocketSession
|
import io.ktor.websocket.WebSocketSession
|
||||||
import io.ktor.websocket.close
|
import io.ktor.websocket.close
|
||||||
import io.ktor.websocket.readText
|
import io.ktor.websocket.readText
|
||||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
|
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
|
||||||
|
import io.modelcontextprotocol.kotlin.sdk.types.McpJson
|
||||||
import kotlinx.coroutines.CoroutineName
|
import kotlinx.coroutines.CoroutineName
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.InternalCoroutinesApi
|
import kotlinx.coroutines.InternalCoroutinesApi
|
||||||
|
|
@ -58,7 +59,7 @@ public abstract class WebSocketMcpTransport : AbstractTransport() {
|
||||||
while (true) {
|
while (true) {
|
||||||
val message = try {
|
val message = try {
|
||||||
session.incoming.receive()
|
session.incoming.receive()
|
||||||
} catch (e: ClosedReceiveChannelException) {
|
} catch (_: ClosedReceiveChannelException) {
|
||||||
logger.debug { "Closed receive channel, exiting" }
|
logger.debug { "Closed receive channel, exiting" }
|
||||||
return@launch
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -1,315 +1,40 @@
|
||||||
package io.modelcontextprotocol.kotlin.sdk
|
package io.modelcontextprotocol.kotlin.sdk
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
import io.modelcontextprotocol.kotlin.sdk.types.error
|
||||||
import io.modelcontextprotocol.kotlin.sdk.LoggingMessageNotification.SetLevelRequest
|
import io.modelcontextprotocol.kotlin.sdk.types.success
|
||||||
import kotlinx.serialization.DeserializationStrategy
|
|
||||||
import kotlinx.serialization.KSerializer
|
|
||||||
import kotlinx.serialization.descriptors.PrimitiveKind
|
|
||||||
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
|
|
||||||
import kotlinx.serialization.descriptors.SerialDescriptor
|
|
||||||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor
|
|
||||||
import kotlinx.serialization.encoding.Decoder
|
|
||||||
import kotlinx.serialization.encoding.Encoder
|
|
||||||
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
|
|
||||||
import kotlinx.serialization.json.JsonDecoder
|
|
||||||
import kotlinx.serialization.json.JsonElement
|
|
||||||
import kotlinx.serialization.json.JsonEncoder
|
|
||||||
import kotlinx.serialization.json.JsonObject
|
import kotlinx.serialization.json.JsonObject
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
|
||||||
import kotlinx.serialization.json.jsonObject
|
|
||||||
import kotlinx.serialization.json.jsonPrimitive
|
|
||||||
import kotlinx.serialization.json.long
|
|
||||||
import kotlinx.serialization.json.longOrNull
|
|
||||||
|
|
||||||
private val logger = KotlinLogging.logger {}
|
@Deprecated(
|
||||||
|
message = "Use `io.modelcontextprotocol.kotlin.sdk.types.EmptyJsonObject` instead",
|
||||||
internal object ErrorCodeSerializer : KSerializer<ErrorCode> {
|
replaceWith = ReplaceWith("EmptyJsonObject", "io.modelcontextprotocol.kotlin.sdk.types.EmptyJsonObject"),
|
||||||
override val descriptor: SerialDescriptor =
|
level = DeprecationLevel.WARNING,
|
||||||
PrimitiveSerialDescriptor("io.modelcontextprotocol.kotlin.sdk.ErrorCode", PrimitiveKind.INT)
|
)
|
||||||
|
public val EmptyJsonObject: JsonObject = io.modelcontextprotocol.kotlin.sdk.types.EmptyJsonObject
|
||||||
override fun serialize(encoder: Encoder, value: ErrorCode) {
|
|
||||||
encoder.encodeInt(value.code)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): ErrorCode {
|
|
||||||
val decodedInt = decoder.decodeInt()
|
|
||||||
return ErrorCode.Defined.entries.firstOrNull { it.code == decodedInt }
|
|
||||||
?: ErrorCode.Unknown(decodedInt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object RequestMethodSerializer : KSerializer<Method> {
|
|
||||||
override val descriptor: SerialDescriptor =
|
|
||||||
PrimitiveSerialDescriptor("io.modelcontextprotocol.kotlin.sdk.Method", PrimitiveKind.STRING)
|
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: Method) {
|
|
||||||
encoder.encodeString(value.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): Method {
|
|
||||||
val decodedString = decoder.decodeString()
|
|
||||||
return Method.Defined.entries.firstOrNull { it.value == decodedString }
|
|
||||||
?: Method.Custom(decodedString)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object StopReasonSerializer : KSerializer<StopReason> {
|
|
||||||
override val descriptor: SerialDescriptor =
|
|
||||||
PrimitiveSerialDescriptor("io.modelcontextprotocol.kotlin.sdk.StopReason", PrimitiveKind.STRING)
|
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: StopReason) {
|
|
||||||
encoder.encodeString(value.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): StopReason {
|
|
||||||
val decodedString = decoder.decodeString()
|
|
||||||
return when (decodedString) {
|
|
||||||
StopReason.StopSequence.value -> StopReason.StopSequence
|
|
||||||
StopReason.MaxTokens.value -> StopReason.MaxTokens
|
|
||||||
StopReason.EndTurn.value -> StopReason.EndTurn
|
|
||||||
else -> StopReason.Other(decodedString)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object ReferencePolymorphicSerializer : JsonContentPolymorphicSerializer<Reference>(Reference::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<Reference> =
|
|
||||||
when (element.jsonObject.getValue("type").jsonPrimitive.content) {
|
|
||||||
ResourceTemplateReference.TYPE -> ResourceTemplateReference.serializer()
|
|
||||||
PromptReference.TYPE -> PromptReference.serializer()
|
|
||||||
else -> UnknownReference.serializer()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object PromptMessageContentPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<PromptMessageContent>(PromptMessageContent::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<PromptMessageContent> =
|
|
||||||
when (element.jsonObject.getValue("type").jsonPrimitive.content) {
|
|
||||||
ImageContent.TYPE -> ImageContent.serializer()
|
|
||||||
TextContent.TYPE -> TextContent.serializer()
|
|
||||||
EmbeddedResource.TYPE -> EmbeddedResource.serializer()
|
|
||||||
AudioContent.TYPE -> AudioContent.serializer()
|
|
||||||
else -> UnknownContent.serializer()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object PromptMessageContentMultimodalPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<PromptMessageContentMultimodal>(PromptMessageContentMultimodal::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<PromptMessageContentMultimodal> =
|
|
||||||
when (element.jsonObject.getValue("type").jsonPrimitive.content) {
|
|
||||||
ImageContent.TYPE -> ImageContent.serializer()
|
|
||||||
TextContent.TYPE -> TextContent.serializer()
|
|
||||||
AudioContent.TYPE -> AudioContent.serializer()
|
|
||||||
else -> UnknownContent.serializer()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object ResourceContentsPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<ResourceContents>(ResourceContents::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<ResourceContents> {
|
|
||||||
val jsonObject = element.jsonObject
|
|
||||||
return when {
|
|
||||||
jsonObject.contains("text") -> TextResourceContents.serializer()
|
|
||||||
jsonObject.contains("blob") -> BlobResourceContents.serializer()
|
|
||||||
else -> UnknownResourceContents.serializer()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun selectRequestDeserializer(method: String): DeserializationStrategy<Request> {
|
|
||||||
selectClientRequestDeserializer(method)?.let { return it }
|
|
||||||
selectServerRequestDeserializer(method)?.let { return it }
|
|
||||||
return CustomRequest.serializer()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun selectClientRequestDeserializer(method: String): DeserializationStrategy<ClientRequest>? = when (method) {
|
|
||||||
Method.Defined.Ping.value -> PingRequest.serializer()
|
|
||||||
Method.Defined.Initialize.value -> InitializeRequest.serializer()
|
|
||||||
Method.Defined.CompletionComplete.value -> CompleteRequest.serializer()
|
|
||||||
Method.Defined.LoggingSetLevel.value -> SetLevelRequest.serializer()
|
|
||||||
Method.Defined.PromptsGet.value -> GetPromptRequest.serializer()
|
|
||||||
Method.Defined.PromptsList.value -> ListPromptsRequest.serializer()
|
|
||||||
Method.Defined.ResourcesList.value -> ListResourcesRequest.serializer()
|
|
||||||
Method.Defined.ResourcesTemplatesList.value -> ListResourceTemplatesRequest.serializer()
|
|
||||||
Method.Defined.ResourcesRead.value -> ReadResourceRequest.serializer()
|
|
||||||
Method.Defined.ResourcesSubscribe.value -> SubscribeRequest.serializer()
|
|
||||||
Method.Defined.ResourcesUnsubscribe.value -> UnsubscribeRequest.serializer()
|
|
||||||
Method.Defined.ToolsCall.value -> CallToolRequest.serializer()
|
|
||||||
Method.Defined.ToolsList.value -> ListToolsRequest.serializer()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun selectClientNotificationDeserializer(element: JsonElement): DeserializationStrategy<ClientNotification>? =
|
|
||||||
when (element.jsonObject.getValue("method").jsonPrimitive.content) {
|
|
||||||
Method.Defined.NotificationsCancelled.value -> CancelledNotification.serializer()
|
|
||||||
Method.Defined.NotificationsProgress.value -> ProgressNotification.serializer()
|
|
||||||
Method.Defined.NotificationsInitialized.value -> InitializedNotification.serializer()
|
|
||||||
Method.Defined.NotificationsRootsListChanged.value -> RootsListChangedNotification.serializer()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object ClientNotificationPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<ClientNotification>(ClientNotification::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<ClientNotification> =
|
|
||||||
selectClientNotificationDeserializer(element)
|
|
||||||
?: UnknownMethodRequestOrNotification.serializer()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun selectServerRequestDeserializer(method: String): DeserializationStrategy<ServerRequest>? = when (method) {
|
|
||||||
Method.Defined.Ping.value -> PingRequest.serializer()
|
|
||||||
Method.Defined.SamplingCreateMessage.value -> CreateMessageRequest.serializer()
|
|
||||||
Method.Defined.RootsList.value -> ListRootsRequest.serializer()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
internal fun selectServerNotificationDeserializer(element: JsonElement): DeserializationStrategy<ServerNotification>? =
|
|
||||||
when (element.jsonObject.getValue("method").jsonPrimitive.content) {
|
|
||||||
Method.Defined.NotificationsCancelled.value -> CancelledNotification.serializer()
|
|
||||||
Method.Defined.NotificationsProgress.value -> ProgressNotification.serializer()
|
|
||||||
Method.Defined.NotificationsMessage.value -> LoggingMessageNotification.serializer()
|
|
||||||
Method.Defined.NotificationsResourcesUpdated.value -> ResourceUpdatedNotification.serializer()
|
|
||||||
Method.Defined.NotificationsResourcesListChanged.value -> ResourceListChangedNotification.serializer()
|
|
||||||
Method.Defined.NotificationsToolsListChanged.value -> ToolListChangedNotification.serializer()
|
|
||||||
Method.Defined.NotificationsPromptsListChanged.value -> PromptListChangedNotification.serializer()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object ServerNotificationPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<ServerNotification>(ServerNotification::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<ServerNotification> =
|
|
||||||
selectServerNotificationDeserializer(element)
|
|
||||||
?: UnknownMethodRequestOrNotification.serializer()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object NotificationPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<Notification>(Notification::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<Notification> =
|
|
||||||
selectClientNotificationDeserializer(element)
|
|
||||||
?: selectServerNotificationDeserializer(element)
|
|
||||||
?: UnknownMethodRequestOrNotification.serializer()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object RequestPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<Request>(Request::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<Request> {
|
|
||||||
val method = element.jsonObject.getOrElse("method") { null }?.jsonPrimitive?.content ?: run {
|
|
||||||
logger.error { "No method in $element" }
|
|
||||||
error("No method in $element")
|
|
||||||
}
|
|
||||||
|
|
||||||
return selectClientRequestDeserializer(method)
|
|
||||||
?: selectServerRequestDeserializer(method)
|
|
||||||
?: UnknownMethodRequestOrNotification.serializer()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun selectServerResultDeserializer(element: JsonElement): DeserializationStrategy<ServerResult>? {
|
|
||||||
val jsonObject = element.jsonObject
|
|
||||||
return when {
|
|
||||||
jsonObject.contains("tools") -> ListToolsResult.serializer()
|
|
||||||
jsonObject.contains("resources") -> ListResourcesResult.serializer()
|
|
||||||
jsonObject.contains("resourceTemplates") -> ListResourceTemplatesResult.serializer()
|
|
||||||
jsonObject.contains("prompts") -> ListPromptsResult.serializer()
|
|
||||||
jsonObject.contains("capabilities") -> InitializeResult.serializer()
|
|
||||||
jsonObject.contains("description") -> GetPromptResult.serializer()
|
|
||||||
jsonObject.contains("completion") -> CompleteResult.serializer()
|
|
||||||
jsonObject.contains("toolResult") -> CompatibilityCallToolResult.serializer()
|
|
||||||
jsonObject.contains("contents") -> ReadResourceResult.serializer()
|
|
||||||
jsonObject.contains("content") -> CallToolResult.serializer()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun selectClientResultDeserializer(element: JsonElement): DeserializationStrategy<ClientResult>? {
|
|
||||||
val jsonObject = element.jsonObject
|
|
||||||
return when {
|
|
||||||
jsonObject.contains("model") -> CreateMessageResult.serializer()
|
|
||||||
jsonObject.contains("roots") -> ListRootsResult.serializer()
|
|
||||||
jsonObject.contains("action") -> CreateElicitationResult.serializer()
|
|
||||||
else -> null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object ServerResultPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<ServerResult>(ServerResult::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<ServerResult> =
|
|
||||||
selectServerResultDeserializer(element)
|
|
||||||
?: EmptyRequestResult.serializer()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object ClientResultPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<ClientResult>(ClientResult::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<ClientResult> =
|
|
||||||
selectClientResultDeserializer(element)
|
|
||||||
?: EmptyRequestResult.serializer()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object RequestResultPolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<RequestResult>(RequestResult::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<RequestResult> =
|
|
||||||
selectClientResultDeserializer(element)
|
|
||||||
?: selectServerResultDeserializer(element)
|
|
||||||
?: EmptyRequestResult.serializer()
|
|
||||||
}
|
|
||||||
|
|
||||||
internal object JSONRPCMessagePolymorphicSerializer :
|
|
||||||
JsonContentPolymorphicSerializer<JSONRPCMessage>(JSONRPCMessage::class) {
|
|
||||||
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<JSONRPCMessage> {
|
|
||||||
val jsonObject = element.jsonObject
|
|
||||||
return when {
|
|
||||||
jsonObject.contains("message") -> JSONRPCError.serializer()
|
|
||||||
!jsonObject.contains("method") -> JSONRPCResponse.serializer()
|
|
||||||
jsonObject.contains("id") -> JSONRPCRequest.serializer()
|
|
||||||
else -> JSONRPCNotification.serializer()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public val EmptyJsonObject: JsonObject = JsonObject(emptyMap())
|
|
||||||
|
|
||||||
public class RequestIdSerializer : KSerializer<RequestId> {
|
|
||||||
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("RequestId")
|
|
||||||
|
|
||||||
override fun deserialize(decoder: Decoder): RequestId {
|
|
||||||
val jsonDecoder = decoder as? JsonDecoder ?: error("Can only deserialize JSON")
|
|
||||||
val element = jsonDecoder.decodeJsonElement()
|
|
||||||
|
|
||||||
return when (element) {
|
|
||||||
is JsonPrimitive -> when {
|
|
||||||
element.isString -> RequestId.StringId(element.content)
|
|
||||||
element.longOrNull != null -> RequestId.NumberId(element.long)
|
|
||||||
else -> error("Invalid RequestId type")
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> error("Invalid RequestId format")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun serialize(encoder: Encoder, value: RequestId) {
|
|
||||||
val jsonEncoder = encoder as? JsonEncoder ?: error("Can only serialize JSON")
|
|
||||||
when (value) {
|
|
||||||
is RequestId.StringId -> jsonEncoder.encodeString(value.value)
|
|
||||||
is RequestId.NumberId -> jsonEncoder.encodeLong(value.value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a [CallToolResult] with single [TextContent] and [meta].
|
* Creates a [CallToolResult] with single [TextContent] and [meta].
|
||||||
*/
|
*/
|
||||||
public fun CallToolResult.Companion.ok(content: String, meta: JsonObject = EmptyJsonObject): CallToolResult =
|
@Deprecated(
|
||||||
CallToolResult(
|
message = "Use `CallToolResult.success` instead",
|
||||||
content = listOf(TextContent(content)),
|
replaceWith = ReplaceWith("CallToolResult.Companion.success"),
|
||||||
isError = false,
|
level = DeprecationLevel.WARNING,
|
||||||
_meta = meta,
|
)
|
||||||
)
|
public fun CallToolResult.ok(
|
||||||
|
content: String,
|
||||||
|
meta: JsonObject = EmptyJsonObject,
|
||||||
|
): io.modelcontextprotocol.kotlin.sdk.types.CallToolResult =
|
||||||
|
io.modelcontextprotocol.kotlin.sdk.types.CallToolResult.success(content, meta)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a [CallToolResult] with single [TextContent] and [meta], with `isError` being true.
|
* Creates a [CallToolResult] with single [TextContent] and [meta], with `isError` being true.
|
||||||
*/
|
*/
|
||||||
public fun CallToolResult.Companion.error(content: String, meta: JsonObject = EmptyJsonObject): CallToolResult =
|
@Deprecated(
|
||||||
CallToolResult(
|
message = "Use `CallToolResult.error` instead",
|
||||||
content = listOf(TextContent(content)),
|
replaceWith = ReplaceWith("CallToolResult.Companion.error"),
|
||||||
isError = true,
|
level = DeprecationLevel.WARNING,
|
||||||
_meta = meta,
|
)
|
||||||
)
|
public fun CallToolResult.error(
|
||||||
|
content: String,
|
||||||
|
meta: JsonObject = EmptyJsonObject,
|
||||||
|
): io.modelcontextprotocol.kotlin.sdk.types.CallToolResult =
|
||||||
|
io.modelcontextprotocol.kotlin.sdk.types.CallToolResult.error(content, meta)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package io.modelcontextprotocol.kotlin.sdk.types
|
||||||
|
|
||||||
|
import kotlinx.serialization.json.JsonElement
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an error specific to the MCP protocol.
|
||||||
|
*
|
||||||
|
* @property code The error code.
|
||||||
|
* @property message The error message.
|
||||||
|
* @property data Additional error data as a JSON object.
|
||||||
|
*/
|
||||||
|
public class McpException(public val code: Int, message: String, public val data: JsonElement? = null) : Exception() {
|
||||||
|
override val message: String = "MCP error $code: $message"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
package io.modelcontextprotocol.kotlin.sdk.types
|
||||||
|
|
||||||
|
import kotlinx.serialization.EncodeDefault
|
||||||
|
import kotlinx.serialization.ExperimentalSerializationApi
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
public data class PingRequest(override val params: BaseRequestParams? = null) :
|
||||||
|
ClientRequest,
|
||||||
|
ServerRequest {
|
||||||
|
@OptIn(ExperimentalSerializationApi::class)
|
||||||
|
@EncodeDefault
|
||||||
|
override val method: Method = Method.Defined.Ping
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue