Skip to content

Support modern MCP client connections - #7385

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

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

Conversation

@tzolov

@tzolov tzolov commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Adds client support for MCP servers that speak the 2026-07-28 ("modern") revision, on top of the modern client of the MCP Java SDK (modelcontextprotocol/java-sdk#1325).
It is the client counterpart of the modern server introduced in #7284 and #7349.

Enable a modern connection

The protocol is set for each Streamable HTTP connection, so an application can use legacy and modern servers at the same time:

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

A connection without the protocol property stays on the legacy protocol, and the legacy transport auto-configurations skip modern connections.
The tools of both kinds of connections are served by the existing SyncMcpToolCallbackProvider bean, so a ChatClient sees a single provider.

Tool callbacks (spring-ai-mcp)

  • SyncModernMcpConnection - hands out the client that serves the current request: a new client for each request, one shared client, or the client that a SyncModernMcpClientScope selects.
  • SyncModernMcpToolCallbackProvider - lists the tools of every connection on each call and binds the callbacks to the client of that call. SyncMcpToolCallbackProvider takes it through modernToolCallbackProvider(...) and returns its callbacks next to the legacy ones, without caching them.
  • SyncModernMcpToolCallback - calls a tool through that client. Its failures follow the legacy callbacks: a protocol error of the server is rethrown, and every other failure, a timeout included, becomes a ToolExecutionException.
  • SyncModernMcpToolCallCustomizer - customizes each tool call with the ToolContext of that call, for example to set a handler for one call.
  • McpModernToolFilter and McpModernToolNamePrefixGenerator - select and name the tools, which are named <connection>_<tool> by default. A name longer than 64 characters keeps the connection prefix.

Annotated handlers (spring-ai-mcp-annotations)

The annotated client methods serve modern connections as well, and one method serves the legacy and the modern connections that it names.

  • @McpElicitation and @McpProgress - called for the input requests and the progress of a modern tool call. The SDK client answers the server and retries the call.
  • Schema types of either protocol - a method may keep its legacy types or take the modern ones, and the requests and results are converted. A legacy ElicitUrlRequest method cannot answer a modern URL request, which has no elicitationId.
  • ToolContext parameter - an elicitation or progress method may take the ToolContext of the tool call that it serves, to know which user to ask.
  • @McpToolListChanged, @McpPromptListChanged, @McpResourceListChanged - called for a modern connection that sets listen=true. A modern change carries no list, so the items are listed first, for the application.
  • Capabilities follow the handlers - a modern client declares form or URL elicitation only when a method can answer it.
  • Startup warnings - a method that names a modern connection but is not called for it is reported: @McpSampling and @McpLogging, which the modern client does not have, a list-changed method without listen=true, and a method that returns a Mono.

Change notifications

  • listen=true - keeps a listen stream open to the server of a modern connection, for the application and not for a caller. The stream is opened in the background, so an unreachable server does not fail the startup.
  • SyncModernMcpChangeSubscriber - opens the streams and opens a stream that ended again, with pauses that double from one second up to the unreachable-cool-down.
  • Kept listings - the tool callback provider drops a kept tool listing when the server reports a tool change, and when changes may have gone unreported.
  • SyncModernMcpChangeListener - beans of this type hear what the servers report. The list-changed methods are also called when a stream is established again after it had ended, as the server may have changed in between.

Auto-configuration

  • McpClientModernAutoConfiguration - creates a JDK HttpClient transport and a SyncModernMcpConnection for each modern connection. McpHttpRequestCustomizer beans are added to every modern transport, and McpClientCustomizer<McpClient.SyncBuilder> beans customize every modern client.
  • Client scope - client-scope is a property of the connection. PER_REQUEST, the default, creates a client each time the tools are resolved, so the client customizers run on the thread of the request and can capture the calling user. SHARED keeps one client for an application that calls the server under a single identity. CUSTOM asks a SyncModernMcpClientScope bean of the application, for example to keep one client for each user.
  • Global properties - spring.ai.mcp.client.modern.call-timeout bounds a whole call, including the time a user takes to answer an elicitation, and spring.ai.mcp.client.modern.unreachable-cool-down (30 s) sets how long an unreachable server is left alone.
  • Startup checks - a modern connection with spring.ai.mcp.client.type=ASYNC fails the startup. So does a McpToolFilter bean without a McpModernToolFilter bean, because the legacy filter does not apply to modern connections, and a connection with client-scope=CUSTOM without a SyncModernMcpClientScope bean.
  • Endpoint - the endpoint of a modern connection is appended to its url, and a query of the url stays behind it. A warning is logged for a url that already ends with the endpoint, as the legacy JDK transport accepts.
  • Tool-calling auto-configuration - SyncModernMcpToolCallbackProvider and SyncModernMcpToolCallback 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.

Tool listings and unreachable servers

A modern server can mark its tool listing as cacheable with ttlMs and a cache scope.
The provider keeps a public listing for all requests for as long as the server allows.
A private listing is kept only by a client that outlives the request: a SHARED client, or a client that a custom scope keeps, for example for a user.
A PER_REQUEST connection that receives a private listing with a ttlMs logs once that the listing could be kept with another scope.
A server built with Spring AI defaults to list-ttl=0, so its authors set list-ttl together with list-cache-scope=PUBLIC to let clients keep the listing.

A server that cannot be reached (I/O error, timeout, lost response, HTTP 5xx, 408 or 429) is left out of the tools with a warning instead of failing the request, and is not asked again during the cool-down.
Every other error, such as 401, 403 or 404, still fails the request.

Changes for legacy connections

  • Handlers - a synchronous client may have one @McpElicitation method for form requests and one for URL requests, and a client whose methods take URL requests only declares URL elicitation. The methods of a synchronous legacy client may take the modern schema types.
  • Tool callbacks - the legacy tool callbacks and their cache behave as before. SyncMcpToolCallbackProvider only gains modernToolCallbackProvider(...).
  • Asynchronous clients - their handlers behave as before: one elicitation method for each client, on the legacy schema types.

Tests and docs

  • ModernMcpClientServerIT and ModernMcpClientHandlersIT (WebFlux) and ModernMcpClientWebMvcServerIT (WebMVC on an embedded Tomcat) run the auto-configured client against the auto-configured modern servers over HTTP. They cover a legacy and a modern server behind one provider, an unreachable modern server next to a legacy one, kept public and per-user listings, a streaming ChatClient whose tool call runs on another thread than the request, annotated handlers of both schema families on both protocols, and a server that adds a tool or comes back with other tools.
  • Unit tests cover the provider, the change subscriber, the handler registry and the auto-configuration.
  • ToolCallingAutoConfigurationTests and ToolCallbackConverterAutoConfigurationIT check that the tool callback resolver and the MCP server leave the modern provider and its callbacks alone.
  • The MCP Client Boot Starter page gains the section "MCP 2026-07-28 ("Modern") Connections", which the SDK documentation links to.
  • Add a migrated Spring AI MCP client sample spring-ai-examples#154 adds a sample of a legacy client after its migration.

Not included

Only the synchronous client and Streamable HTTP connections are auto-configured.
STDIO and other transports are supported through a List<NamedModernClientMcpTransport> bean, and such a connection cannot set listen.
The asynchronous client, a WebClient transport and the updates of single resources are left for follow-up work.

@tzolov tzolov added this to the 2.1.0 milestone Nov 13, 2026
@tzolov tzolov added enhancement New feature or request mcp labels Nov 13, 2026
// client could not complete the exchange. Such failures are not conveyed to
// the model; they fail the model interaction, as the legacy callbacks do.
logger.error("MCP failure while calling tool: " + this.tool.name(), ex);
throw ex;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The legacy client threw TimeoutException on timeout, which was wrapped in ToolExecutionException allowing retry. The modern McpTimeoutException extend McpClientException and would fail hard, no allowing for a retry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Fixed in e8447b9: the callback now rethrows only the server's protocol error (McpException), as the legacy one does with McpError. A timeout and every other client failure becomes a ToolExecutionException, so the tool error handling applies.

}
builder.modernToolCallbackProvider(SyncModernMcpToolCallbackProvider.builder()
.connections(modernConnections)
.toolFilter(modernToolFilter.getIfUnique(McpModernToolFilter::acceptAll))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is more than one McpModernToolFilter the ObjectProvider modernToolFilter.getIfUnique will return null and fallback to McpModernToolFilter::acceptAll, which will expose all tools.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Fixed in e8447b9: several McpModernToolFilter beans without a primary one now fail the startup, and the same goes for McpModernToolNamePrefixGenerator. The legacy McpToolFilter has the same fallback on main; I left it out of this PR.

@tzolov
tzolov marked this pull request as ready for review November 13, 2026 15:52
Expose the tools of servers that speak the 2026-07-28 ("modern")
MCP revision as Spring AI tool callbacks, on top of the modern
client of the MCP Java SDK.

- `SyncModernMcpConnection` hands out the client that serves the
  current request: a client for each request, or one shared client.
- `SyncModernMcpToolCallbackProvider` lists the tools of every
  connection on each call and binds the callbacks to the client of
  that call. It keeps a listing only when the server marks it as
  public with a `ttlMs` above zero. It leaves out a server that
  cannot be reached and does not ask that server again before a
  cool-down time has passed.
- `SyncModernMcpToolCallback` calls a tool through that client.
- `McpModernToolFilter` and `McpModernToolNamePrefixGenerator`
  select and name the tools, which are named `<connection>_<tool>`
  by default.

`SyncMcpToolCallbackProvider` can take such a provider and returns
its callbacks next to those of the legacy clients, so that an
application registers a single provider for both protocols.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
A Streamable HTTP connection speaks the 2026-07-28 ("modern") MCP
revision when its `protocol` property is `MODERN`:

  spring.ai.mcp.client.streamable-http.connections.<name>.protocol

A connection without the property stays on the legacy protocol, so
an application can use legacy and modern servers together. The
legacy transport auto-configurations skip modern connections.

`McpClientModernAutoConfiguration` creates a transport on the JDK
`HttpClient` and a `SyncModernMcpConnection` for each modern
connection. The `client-scope` property of a connection selects a
client for each request (`PER_REQUEST`, the default) or one shared
client (`SHARED`). The tools are served by the existing
`SyncMcpToolCallbackProvider` bean.

Only the synchronous client type is supported: a modern connection
with `spring.ai.mcp.client.type=ASYNC` fails the startup. A
`McpToolFilter` bean without a `McpModernToolFilter` bean fails it
as well when there are modern connections, because the legacy
filter does not apply to them.

`spring.ai.mcp.client.modern.call-timeout` sets the timeout of a
whole call, and `spring.ai.mcp.client.modern.unreachable-cool-down`
the time an unreachable server is left alone.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Treat `SyncModernMcpToolCallbackProvider` and
`SyncModernMcpToolCallback` like their legacy counterparts: the
tool callback resolver does not list the provider's tools at
startup, and an MCP server does not expose them as its own tools.

This matters for an application that defines the modern provider
as a bean itself. The auto-configured modern tools already come
from the `SyncMcpToolCallbackProvider` bean.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Run the auto-configured modern client against the auto-configured
modern servers over HTTP:

- WebFlux server: a legacy and a modern server behind one
  provider, an unreachable modern server next to a legacy one, a
  kept public tool listing whose calls still run as the user of
  each request, and a streaming `ChatClient` whose tool call runs
  on another thread than the request.
- WebMVC server on an embedded Tomcat, with a user for each
  request, which needs the MCP client auto-configuration and
  `tomcat-embed-core` as test dependencies.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Add a section on connections that speak the 2026-07-28 ("modern")
MCP revision to the MCP Client Boot Starter page: how to enable
one, its properties, client scope and identity, streaming
responses, tool listings, tool names and filtering, the connect
timeout, and STDIO and other transports.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Let `@McpElicitation` and `@McpProgress` methods serve the
connections that speak the 2026-07-28 ("modern") MCP revision.

A handler method serves the connections that its `clients`
attribute names, whichever protocol they speak, so one method can
answer a legacy and a modern server. It may take the schema types
of either protocol: requests, results and progress are converted
when the types of the method differ from those of the connection.

- A method that takes the legacy types answers the form requests of
  a modern connection but not its URL requests, because the legacy
  `ElicitUrlRequest` requires an `elicitationId` that the modern
  one does not have. The `_meta` of a legacy result is not sent to
  a modern server.
- A connection can have one method for form requests and one for
  URL requests. Asynchronous clients keep a single method.
- A handler method can take the `ToolContext` of the tool call it
  serves, for example to find the user to ask. For that, the
  handlers are set on each tool call of a modern connection. The
  `ToolContext` is null for a legacy connection.
- `SyncModernMcpToolCallCustomizer` beans customize each tool call
  of the modern connections, after the annotated handlers are set.
- A modern client declares only the elicitation modes it has a
  handler for, and asks for progress only when it has a progress
  handler.
- `@McpSampling`, `@McpLogging` and the list-changed annotations
  are not supported for modern connections. A warning is logged at
  startup for them, and for elicitation and progress methods that
  cannot serve a modern connection.

Two changes also affect legacy connections:

- A progress method with the parameters (Double, String, String)
  receives the progress token as a string, whatever type the
  client sent. A numeric token failed the method call before.
- A client whose only `@McpElicitation` method takes a URL request
  no longer declares the elicitation capability, since a legacy
  client is only asked form requests.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Address two points of the review of the modern MCP client.

A modern tool callback rethrew every `McpClientException`, which
includes a timeout, a lost response and an HTTP error status. Such
a failure bypassed the tool error handling of the application and
failed the model interaction, while a legacy tool callback wraps a
timeout in a `ToolExecutionException`. The modern callback now
rethrows only the protocol error that the server answers with
(`McpException`), as the legacy callback does with `McpError`, and
throws every other failure as a `ToolExecutionException`.

With several `McpModernToolFilter` beans and none of them primary,
the auto-configuration fell back to the filter that accepts all
tools, so the filters of the application were dropped without
notice. Several such beans now fail the startup unless one of them
is primary. The same applies to `McpModernToolNamePrefixGenerator`
beans.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
A modern connection that sets `listen=true` keeps a listen stream
open to its server:

  spring.ai.mcp.client.streamable-http.connections.<name>.listen

When the server reports that its tools changed, the kept tool
listing of the connection is dropped, so the next request lists the
tools again and does not wait for the `ttlMs` of the listing.

`SyncModernMcpChangeSubscriber` owns the streams. It opens them when
the application has started, without waiting for the servers, and a
server that cannot be reached does not fail the startup. A stream
that ends or cannot be opened is opened again after one second, and
then after pauses that double up to
`spring.ai.mcp.client.modern.unreachable-cool-down`. The pauses
start over once a stream has stayed up for that time. The kept
listing is also dropped when a stream is established and when an
established stream ends, because changes may have gone unreported.

A stream is opened by a client of its own, outside of any request,
so it carries the identity of the application and not of a caller.

`SyncModernMcpChangeListener` beans hear about the reported
changes. An established stream also ends the unreachable cool-down
of its connection.

This also fixes the tool listing of a connection with
`client-scope=SHARED`. The provider kept the copy of a listing that
the shared client holds for another full `ttlMs`, so a public
listing could be served for up to twice that time. After its own
listing has expired or was dropped, the provider now lists from the
server.

The `@McpToolListChanged`, `@McpPromptListChanged` and
`@McpResourceListChanged` methods are not called for a modern
connection yet.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
The `@McpToolListChanged`, `@McpPromptListChanged` and
`@McpResourceListChanged` methods are now called for a connection that
uses the MODERN protocol and sets `listen=true`.

- A method may take its list in the legacy or the modern schema type,
  and one method serves the connections of both protocols.
- A change that a modern server reports carries no list, so the tools,
  prompts or resources are listed first, for the application, and
  then passed to the methods.
- The methods are also called when a listen stream is established
  again, since the server may have changed while it was down. They
  are not called for the first stream of a connection.
- The listen stream asks for prompt and resource changes only if a
  method wants to hear about them.
- The methods are called after the tool callback provider has dropped
  its kept listing, and also through a `SyncModernMcpChangeSubscriber`
  bean of the application.
- A listing stops after 100 pages, a definition that the schema type
  of a method does not accept is left out, and a failing method does
  not keep the others from being called.
- A warning is logged at startup for a list-changed method that names
  a modern connection without `listen=true`, and for one that returns
  a `Mono`.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
- Register legacy URL `@McpElicitation` methods again
- Treat 5xx, 408 and 429 listing failures as unreachable
- Open the listen streams when a listener is added late
- Keep the connection prefix in long tool names
- Keep a url query behind the endpoint, warn on a doubled one
- Log the listen stream recovery at INFO, close a listing race

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
- A `SyncModernMcpClientScope` bean selects the client of each
  request for a connection with `client-scope=CUSTOM`
- Each kept client lists from the server after a dropped listing
- Listen streams and list-changed listings use an application
  client, not the scope
- Log a hint once when a per-request connection gets a private
  listing that it cannot keep

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
@tzolov
tzolov merged commit b253ac5 into spring-projects:main Nov 13, 2026
3 checks passed
tzolov added a commit to spring-projects/spring-ai-examples that referenced this pull request Nov 13, 2026
Add modern-client-from-legacy, the legacy tickets client after its
migration to the 2026-07-28 protocol on the Spring AI client starter,
as spring-projects/spring-ai#7385 supports it:

- The connection sets protocol=MODERN and listen=true
- ClientHandlers keeps the @McpElicitation, @McpProgress and
  @McpToolListChanged methods of the legacy client, on the legacy
  schema types, for the modern and the legacy connection
- The elicitation method takes the ToolContext of its tool call
- A client for each caller, a tool filter, and a client scope bean
  that keeps one client for each role with client-scope=CUSTOM
- The mixed profile serves a legacy and a modern server from one
  tool callback provider

The READMEs describe the client migration.

Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
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.

2 participants