-
Notifications
You must be signed in to change notification settings - Fork 213
feat!: capabilities for tasks, elicitation #732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devcrocod
wants to merge
1
commit into
main
Choose a base branch
from
feat/issue-544-capabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/ClientAssertCapabilityTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package io.modelcontextprotocol.kotlin.sdk.client | ||
|
|
||
| import io.kotest.matchers.string.shouldContain | ||
| import io.modelcontextprotocol.kotlin.sdk.shared.Transport | ||
| import io.modelcontextprotocol.kotlin.sdk.shared.TransportSendOptions | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ClientCapabilities | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Implementation | ||
| import io.modelcontextprotocol.kotlin.sdk.types.InitializeResult | ||
| import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage | ||
| import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCResponse | ||
| import io.modelcontextprotocol.kotlin.sdk.types.LATEST_PROTOCOL_VERSION | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Method | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertFailsWith | ||
|
|
||
| /** | ||
| * Tests for the protected helper [Client.assertCapability]. | ||
| * | ||
| * `assertCapability(capability, method)` is `protected`, so this suite uses | ||
| * a small subclass [TestClient] that re-exports it via [TestClient.exposedAssertCapability]. | ||
| * Server capabilities are seeded by completing the initialization handshake through | ||
| * [CapabilitiesTransport], which replays a configurable [ServerCapabilities] in its | ||
| * `initialize` response. | ||
| */ | ||
| class ClientAssertCapabilityTest { | ||
|
|
||
| @Test | ||
| fun `assertCapability tasks throws when server has no tasks capability`() = runTest { | ||
| val client = newTestClient(serverCapabilities = ServerCapabilities()) | ||
| val ex = assertFailsWith<IllegalStateException> { | ||
| client.exposedAssertCapability("tasks", "tasks/list") | ||
| } | ||
| ex.message.orEmpty() shouldContain "Server does not support tasks" | ||
| } | ||
|
|
||
| @Test | ||
| fun `TasksGet throws when server has no tasks capability`() = runTest { | ||
| val client = newTestClient(serverCapabilities = ServerCapabilities()) | ||
| val ex = assertFailsWith<IllegalStateException> { | ||
| client.exposedAssertCapabilityForMethod(Method.Defined.TasksGet) | ||
| } | ||
| ex.message.orEmpty() shouldContain "Server does not support tasks" | ||
| } | ||
|
|
||
| @Test | ||
| fun `TasksGet does not throw when server declared tasks`() = runTest { | ||
| val client = newTestClient( | ||
| serverCapabilities = ServerCapabilities(tasks = ServerCapabilities.Tasks()), | ||
| ) | ||
| client.exposedAssertCapabilityForMethod(Method.Defined.TasksGet) | ||
| } | ||
|
|
||
| @Test | ||
| fun `TasksList throws when server tasks list is null`() = runTest { | ||
| val client = newTestClient( | ||
| serverCapabilities = ServerCapabilities(tasks = ServerCapabilities.Tasks()), | ||
| ) | ||
| val ex = assertFailsWith<IllegalStateException> { | ||
| client.exposedAssertCapabilityForMethod(Method.Defined.TasksList) | ||
| } | ||
| ex.message.orEmpty() shouldContain "Server does not support listing tasks" | ||
| } | ||
|
|
||
| @Test | ||
| fun `TasksCancel throws when server tasks cancel is null`() = runTest { | ||
| val client = newTestClient( | ||
| serverCapabilities = ServerCapabilities(tasks = ServerCapabilities.Tasks()), | ||
| ) | ||
| val ex = assertFailsWith<IllegalStateException> { | ||
| client.exposedAssertCapabilityForMethod(Method.Defined.TasksCancel) | ||
| } | ||
| ex.message.orEmpty() shouldContain "Server does not support cancelling tasks" | ||
| } | ||
|
|
||
| @Test | ||
| fun `NotificationsTasksStatus throws when client has no tasks capability`() = runTest { | ||
| val client = newTestClient(clientCapabilities = ClientCapabilities()) | ||
| val ex = assertFailsWith<IllegalStateException> { | ||
| client.exposedAssertNotificationCapability(Method.Defined.NotificationsTasksStatus) | ||
| } | ||
| ex.message.orEmpty() shouldContain "Client does not support tasks" | ||
| } | ||
|
|
||
| private suspend fun newTestClient( | ||
| serverCapabilities: ServerCapabilities = ServerCapabilities(), | ||
| clientCapabilities: ClientCapabilities = ClientCapabilities(), | ||
| ): TestClient { | ||
| val client = TestClient( | ||
| Implementation("test-client", "1.0.0"), | ||
| ClientOptions(capabilities = clientCapabilities), | ||
| ) | ||
| val transport = CapabilitiesTransport(serverCapabilities) | ||
| client.connect(transport) | ||
| return client | ||
| } | ||
|
|
||
| /** | ||
| * Test-only [Client] subclass that exposes the protected | ||
| * [Client.assertCapability], [Client.assertCapabilityForMethod], and | ||
| * [Client.assertNotificationCapability] helpers to the test code. | ||
| */ | ||
| private class TestClient(clientInfo: Implementation, options: ClientOptions = ClientOptions()) : | ||
| Client(clientInfo, options) { | ||
| fun exposedAssertCapability(capability: String, method: String): Unit = assertCapability(capability, method) | ||
| fun exposedAssertCapabilityForMethod(method: Method): Unit = assertCapabilityForMethod(method) | ||
| fun exposedAssertNotificationCapability(method: Method): Unit = assertNotificationCapability(method) | ||
| } | ||
|
|
||
| /** | ||
| * Minimal in-memory [Transport] that responds to `initialize` with a configurable | ||
| * [ServerCapabilities]. Other JSON-RPC traffic (e.g. the `notifications/initialized` | ||
| * sent by [Client.connect]) is silently consumed. | ||
| */ | ||
| private class CapabilitiesTransport(private val serverCapabilities: ServerCapabilities) : Transport { | ||
| private var onMessageBlock: (suspend (JSONRPCMessage) -> Unit)? = null | ||
| private var onCloseBlock: (() -> Unit)? = null | ||
|
|
||
| override suspend fun start() = Unit | ||
|
|
||
| override suspend fun send(message: JSONRPCMessage, options: TransportSendOptions?) { | ||
| if (message is JSONRPCRequest && message.method == "initialize") { | ||
| onMessageBlock?.invoke( | ||
| JSONRPCResponse( | ||
| id = message.id, | ||
| result = InitializeResult( | ||
| protocolVersion = LATEST_PROTOCOL_VERSION, | ||
| capabilities = serverCapabilities, | ||
| serverInfo = Implementation("mock-server", "1.0.0"), | ||
| ), | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| override suspend fun close() { | ||
| onCloseBlock?.invoke() | ||
| } | ||
|
|
||
| override fun onMessage(block: suspend (JSONRPCMessage) -> Unit) { | ||
| onMessageBlock = block | ||
| } | ||
|
|
||
| override fun onClose(block: () -> Unit) { | ||
| onCloseBlock = block | ||
| } | ||
|
|
||
| override fun onError(block: (Throwable) -> Unit) = Unit | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tasks methods were added to
assertCapabilityForMethod, butassertRequestHandlerCapabilitystill has no gating fortasks/*handlers. Since tasks requests can be received by either side, this allows registering handlers fortasks/get|result|list|cancelwithout advertisingClientCapabilities.tasks(and without checkinglist/cancelsub-capabilities). Consider adding correspondingtasks/*cases inassertRequestHandlerCapabilitythat validatecapabilities.tasksand, for list/cancel,capabilities.tasks.list/capabilities.tasks.cancel.