Skip to content

Add async client type for modern MCP connections - #7392

Merged
tzolov merged 3 commits into
spring-projects:mainfrom
tzolov:modern-mcp-client-async
Nov 13, 2026
Merged

tzolov merged 3 commits into
spring-projects:mainfrom
tzolov:modern-mcp-client-async

Conversation

@tzolov

@tzolov tzolov commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Serves the connections that speak the 2026-07-28 ("modern") revision with spring.ai.mcp.client.type=ASYNC, on the asynchronous client of the MCP Java SDK.
It is the follow-up to #7385, which added the synchronous client type; until now a modern connection failed the startup with the asynchronous type.

Enable it

The connection properties are those of #7385, and the client type applies to legacy and modern connections alike:

spring.ai.mcp.client.type=ASYNC
spring.ai.mcp.client.streamable-http.connections.tickets.url=http://localhost:8080
spring.ai.mcp.client.streamable-http.connections.tickets.protocol=MODERN
# optional
spring.ai.mcp.client.streamable-http.connections.tickets.listen=true
spring.ai.mcp.client.streamable-http.connections.tickets.client-scope=PER_REQUEST

The tools of both kinds of connections are served by the existing AsyncMcpToolCallbackProvider bean, so a ChatClient sees a single provider.

Tool callbacks (spring-ai-mcp)

Each synchronous type of #7385 has an asynchronous counterpart:

Synchronous type Asynchronous type
SyncModernMcpConnection AsyncModernMcpConnection
SyncModernMcpToolCallbackProvider AsyncModernMcpToolCallbackProvider
SyncModernMcpToolCallback AsyncModernMcpToolCallback
SyncModernMcpClientScope AsyncModernMcpClientScope
SyncModernMcpToolCallCustomizer AsyncModernMcpToolCallCustomizer
transportContextProvider of the client AsyncModernMcpTransportContextProvider
SyncModernMcpChangeSubscriber AsyncModernMcpChangeSubscriber
SyncModernMcpChangeListener AsyncModernMcpChangeListener

AsyncMcpToolCallbackProvider takes the modern provider through modernToolCallbackProvider(...) and returns its callbacks next to the legacy ones, without caching them.

  • Identity - an asynchronous client holds no caller. An AsyncModernMcpTransportContextProvider bean supplies it each time the tools are resolved, on the thread that resolves them. The listing goes out for that caller, and the callbacks of that resolution keep it, so a tool that runs later, or on another thread, is called for the same caller.
  • The bean is the only source of the caller - a McpTransportContext in the Reactor context of the chat call is not used. Spring AI's MCP server transports put the inbound request context under the same key, and it must not travel to another server.
  • Blocking calls - the ToolCallbackProvider and ToolCallback contracts stay blocking, as they are for the legacy asynchronous clients. A tool listing that needs a blocking call on a Reactor non-blocking thread fails with an IllegalStateException that names the connection and the thread, instead of Reactor's message, for both client types. A listing that the provider keeps needs no blocking call, and with the synchronous type neither does one that a client which outlives the request keeps. A tool call of the asynchronous type on such a thread fails before anything is sent, and not as a ToolExecutionException, so the model is not invited to call the tool again.

Annotated handlers (spring-ai-mcp-annotations)

  • Reactive @McpElicitation and @McpProgress - methods that return a Mono answer the input requests and receive the progress of an asynchronous modern tool call. As with the synchronous type, they may take the schema types of either protocol and an optional ToolContext of the tool call.
  • Reactive list-changed methods - @McpToolListChanged, @McpPromptListChanged and @McpResourceListChanged methods that return Mono<Void> are called for an asynchronous modern connection that sets listen=true.
  • Classification - AbstractClientMcpHandlerRegistry gains hooks that decide which methods a modern client can call; ClientMcpAsyncHandlersRegistry takes only reactive methods, and a progress method only if it returns Mono<Void>. The asynchronous registry keeps one @McpElicitation method for each client.
  • Startup warnings - a method that names an asynchronous modern connection but is not called for it is reported, for example a synchronous one, as for the synchronous type.

Change notifications

  • AsyncModernMcpChangeSubscriber - keeps the listen streams open, with the reopen behaviour of the synchronous subscriber, and tells AsyncModernMcpChangeListener beans and the tool callback provider.
  • Ordering - the listeners are told one after the other, and the reports of one connection in the order they arrive: the next report is told when all listeners are done with the previous one, so a slow listing cannot deliver a stale list last. A listener must not block, and its Mono must complete.

Shared code

The listing, tool call and listen stream logic of the synchronous client moves into package-private classes that both types use: ModernToolListings (kept listings, invalidation, unreachable cool-down), ModernToolCalls (request and response mapping) and ModernChangeStreams (opening, reopening and telling the listeners).
The behaviour of the synchronous client does not change: its existing tests pass unchanged apart from the paths of private fields, and new tests cover the non-blocking-thread cases.

Auto-configuration

  • McpClientModernAutoConfiguration - with type=ASYNC it creates an AsyncModernMcpConnection for each modern connection, customized by McpClientCustomizer<McpClient.AsyncBuilder> beans, and the AsyncModernMcpChangeSubscriber bean. client-scope=CUSTOM requires an AsyncModernMcpClientScope bean. The startup guard that rejected modern connections with the asynchronous type is removed.
  • Warning - client-scope=SHARED together with an AsyncModernMcpTransportContextProvider bean is reported at startup, because a shared client keeps a private tool listing for all its callers.
  • McpToolCallbackAutoConfiguration - builds the asynchronous modern provider, with the annotated handlers and the AsyncModernMcpToolCallCustomizer beans applied to each tool call.
  • Tool-calling auto-configuration - AsyncModernMcpToolCallbackProvider and AsyncModernMcpToolCallback are treated like their legacy counterparts: their tools are not listed at startup, and an MCP server does not expose them as its own tools.

Changes for existing code

  • Registries - handlesModernFormElicitation, handlesModernUrlElicitation, handlesModernProgress and handlesModernListChanged move from ClientMcpSyncHandlersRegistry to the shared base class, so both registries offer them.
  • Legacy asynchronous clients - their reactive handler methods may now take the modern schema types and a ToolContext parameter, which is null there. A reactive @McpElicitation method that does not return a Mono still fails the startup.

Not included

  • Concurrent listing - the asynchronous provider lists its connections one after the other.
  • Per-caller cool-down - an unreachable server is left alone for all callers, as with the synchronous type.
  • STDIO - as in Support modern MCP client connections #7385, only Streamable HTTP connections are auto-configured.

Serve the connections that speak MCP 2026-07-28 with
`spring.ai.mcp.client.type=ASYNC`, on the asynchronous client of the
MCP Java SDK. Until now a modern connection failed the startup with
that type.

- Tool callbacks: `AsyncModernMcpConnection`,
  `AsyncModernMcpToolCallbackProvider` and `AsyncModernMcpToolCallback`
  list and call the tools, and `AsyncMcpToolCallbackProvider` returns
  them next to the legacy ones. An
  `AsyncModernMcpTransportContextProvider` bean supplies the caller of
  a request, and `AsyncModernMcpClientScope` and
  `AsyncModernMcpToolCallCustomizer` mirror the synchronous types.
- Handlers: reactive `@McpElicitation` and `@McpProgress` methods
  answer the tool calls with the `ToolContext` of the call, and
  reactive list-changed methods are called for a connection that sets
  `listen=true`.
- Change notifications: `AsyncModernMcpChangeSubscriber` keeps the
  listen streams open and tells `AsyncModernMcpChangeListener` beans
  and the provider, which drops a kept tool listing.
- The listing, tool call and listen stream logic of the synchronous
  client moves into package-private classes that both types share.
  Its behaviour does not change.
- Resolving the tools on a non-blocking thread fails with a clear
  error for both types.

The client docs gain the sections "Asynchronous Client Type" and
"Blocking Calls".

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
- Fail an async tool call on a non-blocking thread before the
  request is sent, and not as a tool error.
- Use the `AsyncModernMcpTransportContextProvider` bean as the only
  source of the caller. A transport context in the Reactor context
  of the chat call is no longer used.
- Count a reactive `@McpProgress` method only if it returns
  `Mono<Void>`, as its provider does.
- Fail the startup again for a reactive `@McpElicitation` method
  that does not return a `Mono`.
- Move the `handlesModern...` accessors to the registry base class.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
The check for a non-blocking thread rejected every listing, also one
that a synchronous client keeps and answers without a request, which
worked before. Fail up front only when a request to the server is
certain, and otherwise ask the client and turn Reactor's refusal into
the same message.

Also correct the Javadoc of
`AsyncModernMcpToolCallback.Builder.transportContext`, which still
described the Reactor context fallback.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
@tzolov tzolov added this to the 2.1.0 milestone Nov 13, 2026
@tzolov tzolov added mcp enhancement New feature or request labels Nov 13, 2026
@tzolov
tzolov merged commit 98cbc4f into spring-projects:main Nov 13, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request mcp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant