fix(client): gate completion/complete on the completions capability (#846)

Gate `completion/complete` on the server's `completions` capability
instead of `prompts`.

closes #845

## Motivation and Context

A server that declares `completions` but not `prompts` is currently
rejected for completion. Details in #845.

## How Has This Been Tested?

New `ClientAssertCapabilityTest` cases; they fail on `main` and pass
with this change. `apiCheck` / `ktlintCheck` / `detekt` pass.

## Breaking Changes

None.

## Types of changes

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] 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

Co-authored-by: Pavel Gorgulov <devcrocod@gmail.com>
This commit is contained in:
Takuya Akamatsu 2026-06-30 00:18:53 +09:00 • committed by GitHub
commit 19db4b04e3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 26 additions and 2 deletions

View file

@ -242,13 +242,18 @@ public open class Client(private val clientInfo: Implementation, options: Client
Method.Defined.PromptsGet,
Method.Defined.PromptsList,
Method.Defined.CompletionComplete,
-> {
checkNotNull(serverCapabilities?.prompts) {
"Server does not support prompts (required for $method)"
}
}
Method.Defined.CompletionComplete -> {
checkNotNull(serverCapabilities?.completions) {
"Server does not support completions (required for $method)"
}
}
Method.Defined.ResourcesList,
Method.Defined.ResourcesTemplatesList,
Method.Defined.ResourcesRead,
@ -394,7 +399,7 @@ public open class Client(private val clientInfo: Implementation, options: Client
* @param params The completion request parameters.
* @param options Optional request options.
* @return The completion result returned by the server, or `null` if none.
* @throws IllegalStateException If the server does not support prompts or completion.
* @throws IllegalStateException If the server does not support completions.
*/
public suspend fun complete(params: CompleteRequest, options: RequestOptions? = null): CompleteResult =
request(params, options)

View file

@ -84,6 +84,25 @@ class ClientAssertCapabilityTest {
ex.message.orEmpty() shouldContain "Client does not support tasks"
}
@Test
fun `CompletionComplete does not throw when server declared completions`() = runTest {
val client = newTestClient(
serverCapabilities = ServerCapabilities(completions = ServerCapabilities.Completions),
)
client.exposedAssertCapabilityForMethod(Method.Defined.CompletionComplete)
}
@Test
fun `CompletionComplete throws when server has no completions capability`() = runTest {
val client = newTestClient(
serverCapabilities = ServerCapabilities(prompts = ServerCapabilities.Prompts()),
)
val ex = assertFailsWith<IllegalStateException> {
client.exposedAssertCapabilityForMethod(Method.Defined.CompletionComplete)
}
ex.message.orEmpty() shouldContain "Server does not support completions"
}
private suspend fun newTestClient(
serverCapabilities: ServerCapabilities = ServerCapabilities(),
clientCapabilities: ClientCapabilities = ClientCapabilities(),