Repository navigation
Support modern MCP client connections - #7385
Conversation
39e0b32 to
2a85049
Compare
| // 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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
If there is more than one McpModernToolFilter the ObjectProvider modernToolFilter.getIfUnique will return null and fallback to McpModernToolFilter::acceptAll, which will expose all tools.
There was a problem hiding this comment.
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.
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>
7077c2b to
dd454b7
Compare
Signed-off-by: Christian Tzolov <christian.tzolov@broadcom.com>
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>
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:
A connection without the
protocolproperty 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
SyncMcpToolCallbackProviderbean, so aChatClientsees 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 aSyncModernMcpClientScopeselects.SyncModernMcpToolCallbackProvider- lists the tools of every connection on each call and binds the callbacks to the client of that call.SyncMcpToolCallbackProvidertakes it throughmodernToolCallbackProvider(...)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 aToolExecutionException.SyncModernMcpToolCallCustomizer- customizes each tool call with theToolContextof that call, for example to set a handler for one call.McpModernToolFilterandMcpModernToolNamePrefixGenerator- 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.
@McpElicitationand@McpProgress- called for the input requests and the progress of a modern tool call. The SDK client answers the server and retries the call.ElicitUrlRequestmethod cannot answer a modern URL request, which has noelicitationId.ToolContextparameter - an elicitation or progress method may take theToolContextof the tool call that it serves, to know which user to ask.@McpToolListChanged,@McpPromptListChanged,@McpResourceListChanged- called for a modern connection that setslisten=true. A modern change carries no list, so the items are listed first, for the application.@McpSamplingand@McpLogging, which the modern client does not have, a list-changed method withoutlisten=true, and a method that returns aMono.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 theunreachable-cool-down.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 JDKHttpClienttransport and aSyncModernMcpConnectionfor each modern connection.McpHttpRequestCustomizerbeans are added to every modern transport, andMcpClientCustomizer<McpClient.SyncBuilder>beans customize every modern client.client-scopeis 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.SHAREDkeeps one client for an application that calls the server under a single identity.CUSTOMasks aSyncModernMcpClientScopebean of the application, for example to keep one client for each user.spring.ai.mcp.client.modern.call-timeoutbounds a whole call, including the time a user takes to answer an elicitation, andspring.ai.mcp.client.modern.unreachable-cool-down(30 s) sets how long an unreachable server is left alone.spring.ai.mcp.client.type=ASYNCfails the startup. So does aMcpToolFilterbean without aMcpModernToolFilterbean, because the legacy filter does not apply to modern connections, and a connection withclient-scope=CUSTOMwithout aSyncModernMcpClientScopebean.endpointof a modern connection is appended to itsurl, and a query of theurlstays behind it. A warning is logged for aurlthat already ends with the endpoint, as the legacy JDK transport accepts.SyncModernMcpToolCallbackProviderandSyncModernMcpToolCallbackare 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
ttlMsand a cache scope.The provider keeps a
publiclisting for all requests for as long as the server allows.A
privatelisting is kept only by a client that outlives the request: aSHAREDclient, or a client that a custom scope keeps, for example for a user.A
PER_REQUESTconnection that receives a private listing with attlMslogs once that the listing could be kept with another scope.A server built with Spring AI defaults to
list-ttl=0, so its authors setlist-ttltogether withlist-cache-scope=PUBLICto 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
@McpElicitationmethod 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.SyncMcpToolCallbackProvideronly gainsmodernToolCallbackProvider(...).Tests and docs
ModernMcpClientServerITandModernMcpClientHandlersIT(WebFlux) andModernMcpClientWebMvcServerIT(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 streamingChatClientwhose 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.ToolCallingAutoConfigurationTestsandToolCallbackConverterAutoConfigurationITcheck that the tool callback resolver and the MCP server leave the modern provider and its callbacks alone.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 setlisten.The asynchronous client, a
WebClienttransport and the updates of single resources are left for follow-up work.