Skip to content

Reject listRoots when client lacks roots capability - #1070

Open
nikita-kibitkin wants to merge 1 commit into
modelcontextprotocol:mainfrom
nikita-kibitkin:feature/1067-list-roots-capability-check
Open

Reject listRoots when client lacks roots capability#1070
nikita-kibitkin wants to merge 1 commit into
modelcontextprotocol:mainfrom
nikita-kibitkin:feature/1067-list-roots-capability-check

Conversation

@nikita-kibitkin

Copy link
Copy Markdown

Motivation and Context

Fixes #1067.

McpAsyncServerExchange.listRoots() sends a roots/list request even when the client did not declare the roots capability. Depending on the client, that ends in a confusing client-side error after a wire round-trip, or in a hang until requestTimeout if the client ignores unknown methods. createMessage() and createElicitation() in the same class already fail fast on a missing capability; listRoots() was the odd one out.

Description

  • listRoots(String cursor) now returns Mono.error(IllegalStateException) when the client is not initialized or did not declare the roots capability, without sending any request. The guard covers the no-arg listRoots() (a pagination aggregator over the cursor variant) and the sync exchange (delegates to async).
  • The error message reuses the wording already used for the same situation in McpAsyncClient: "Client must be configured with roots capabilities".

How Has This Been Tested?

Four new unit tests in McpAsyncServerExchangeTests and McpSyncServerExchangeTests (null capabilities and missing roots capability), each also verifying that sendRequest is never called. All four were verified to fail without the production change.

The existing testRootsWithoutCapability integration test asserted the old behavior (client-side "Roots not supported" error after the request crossed the wire); it now expects the server-side fail-fast error and asserts strictly that the call throws.

./mvnw clean test -pl mcp-core,mcp-test -am
BUILD SUCCESS, 0 failures (mcp-test: 936 tests)

Breaking Changes

None in the semver sense (bug fix aligning behavior with the sibling methods). Behavior change to be aware of: server-side callers of listRoots() against a capability-less client now get an immediate IllegalStateException instead of a client error or a timeout.

Types of changes

  • 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)

Fail fast with an IllegalStateException instead of sending a roots/list
request the client cannot handle, matching the existing guards in
createMessage and createElicitation.

Fixes modelcontextprotocol#1067
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reject listRoots if not supported by client, without sending any request

1 participant