You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Spring AI MCP (Model Context Protocol) Client Boot Starter provides auto-configuration for MCP client functionality in Spring Boot applications. It supports both synchronous and asynchronous client implementations with various transport options.
4
4
5
5
The MCP Client Boot Starter provides:
6
6
7
+
* Management of multiple client instances
7
8
* Automatic client initialization (if enabled)
8
9
* Support for multiple named transports
9
10
* Integration with Spring AI's tool execution framework
10
-
* Proper lifecycle management with automatic cleanup
11
+
* Proper lifecycle management with automatic cleanup of resources when the application context is closed
11
12
* Customizable client creation through customizers
It will connect, simultaneously, to one or more MCP Servers over `STDIO` (in-process) and/or `SSE` (remote) transports.
26
+
The core starter connects simultaneously to one or more MCP Servers over `STDIO` (in-process) and/or `SSE` (remote) transports.
27
27
The SSE connection uses the HttpClient-based transport implementation.
28
-
Every connection to an MCP Server creates a new MCP Client instance.
29
-
You can opt for either `SYNC` or `ASYNC` MCP Clients (Note: you cannot mix sync and async clients).
30
-
For more enterprise-ready deployment, it is recommended to use the WebFlux-based SSE connection using the `spring-ai-mcp-client-webflux-spring-boot-starter` starter.
28
+
Each connection to an MCP Server creates a new MCP Client instance.
29
+
You can choose either `SYNC` or `ASYNC` MCP Clients (note: you cannot mix sync and async clients).
30
+
For enterprise-ready deployment, we recommend using the WebFlux-based SSE connection with the `spring-ai-mcp-client-webflux-spring-boot-starter`.
31
31
32
-
=== WebFlux Starter
32
+
=== WebFlux Client
33
33
34
-
Similar to the core starter, it allows configuring one or more STDIO and SSE connections, but uses WebFlux-based SSE transport implementation.
34
+
The WebFlux starter provides similar functionality to the core starter but uses a WebFlux-based SSE transport implementation.
@@ -73,44 +72,14 @@ All common configuration properties are prefixed with `spring.ai.mcp.client`:
73
72
|`20s`
74
73
75
74
|`type`
76
-
|Client type (SYNC or ASYNC). You can not mix client type. All clients can be either sync or async
75
+
|Client type (SYNC or ASYNC). All clients must be either sync or async; mixing is not supported
77
76
|`SYNC`
78
77
79
78
|`root-change-notification`
80
79
|Enable/disable root change notifications for all clients
81
80
|`true`
82
81
|===
83
82
84
-
=== SSE Transport Properties
85
-
86
-
Properties for Server-Sent Events (SSE) transport are prefixed with `spring.ai.mcp.client.sse`:
87
-
88
-
[cols="2,4"]
89
-
|===
90
-
|Property |Description
91
-
92
-
|`connections`
93
-
|Map of named SSE connection configurations
94
-
95
-
|`connections.[name].url`
96
-
|URL endpoint for SSE communication with the MCP server
97
-
|===
98
-
99
-
Example configuration:
100
-
[source,yaml]
101
-
----
102
-
spring:
103
-
ai:
104
-
mcp:
105
-
client:
106
-
sse:
107
-
connections:
108
-
server1:
109
-
url: http://localhost:8080
110
-
server2:
111
-
url: http://otherserver:8081
112
-
----
113
-
114
83
=== Stdio Transport Properties
115
84
116
85
Properties for Standard I/O transport are prefixed with `spring.ai.mcp.client.stdio`:
@@ -160,7 +129,7 @@ spring:
160
129
DEBUG: "true"
161
130
----
162
131
163
-
Alternatively, you can configure stdio connections using an external JSON file using the link:https://modelcontextprotocol.io/quickstart/user[Claude Desctop format]:
132
+
Alternatively, you can configure stdio connections using an external JSON file using the link:https://modelcontextprotocol.io/quickstart/user[Claude Desktop format]:
164
133
165
134
[source,yaml]
166
135
----
@@ -172,7 +141,7 @@ spring:
172
141
servers-configuration: classpath:mcp-servers.json
173
142
----
174
143
175
-
The Claude Destop format looks like this:
144
+
The Claude Desktop format looks like this:
176
145
177
146
[source,json]
178
147
----
@@ -190,23 +159,47 @@ The Claude Destop format looks like this:
190
159
}
191
160
}
192
161
----
193
-
Currently the Claude Destop supports only STDIO connection types.
162
+
163
+
Currently, the Claude Desktop format supports only STDIO connection types.
164
+
165
+
=== SSE Transport Properties
166
+
167
+
Properties for Server-Sent Events (SSE) transport are prefixed with `spring.ai.mcp.client.sse`:
168
+
169
+
[cols="2,4"]
170
+
|===
171
+
|Property |Description
172
+
173
+
|`connections`
174
+
|Map of named SSE connection configurations
175
+
176
+
|`connections.[name].url`
177
+
|URL endpoint for SSE communication with the MCP server
178
+
|===
179
+
180
+
Example configuration:
181
+
[source,yaml]
182
+
----
183
+
spring:
184
+
ai:
185
+
mcp:
186
+
client:
187
+
sse:
188
+
connections:
189
+
server1:
190
+
url: http://localhost:8080
191
+
server2:
192
+
url: http://otherserver:8081
193
+
----
194
194
195
195
== Features
196
196
197
-
=== Client Types
197
+
=== Sync/Async Client Types
198
198
199
199
The starter supports two types of clients:
200
200
201
-
1. *Synchronous Client (SYNC)*
202
-
* Default client type
203
-
* Blocking operations
204
-
* Suitable for traditional request-response patterns
205
-
206
-
2. *Asynchronous Client (ASYNC)*
207
-
* Non-blocking operations
208
-
* Suitable for reactive applications
209
-
* Must be explicitly configured using `spring.ai.mcp.client.type=ASYNC`
201
+
* Synchronous (`SYNC`) - Default client type, suitable for traditional request-response patterns with blocking operations
202
+
* Asynchronous (`ASYNC`) - Suitable for reactive applications with non-blocking operations, configured using `spring.ai.mcp.client.type=ASYNC`
210
203
211
204
=== Client Customization
212
205
@@ -215,11 +208,21 @@ The auto-configuration supports customization through:
215
208
* `McpSyncClientCustomizer` for synchronous clients
216
209
* `McpAsyncClientCustomizer` for asynchronous clients
217
210
218
-
== Usage Example
211
+
=== Transport Support
219
212
220
-
1. Add the appropriate starter dependency to your project.
213
+
The auto-configuration supports multiple transport types:
214
+
215
+
* Standard I/O (Stdio) (activated by the `spring-ai-mcp-client-spring-boot-starter`)
216
+
* SSE HTTP (activated by the `spring-ai-mcp-client-spring-boot-starter`)
217
+
* SSE WebFlux (activated by the `spring-ai-starter-mcp-client-webflux`)
218
+
219
+
=== Integration with Spring AI
220
+
221
+
The starter automatically configures tool callbacks that integrate with Spring AI's tool execution framework, allowing MCP tools to be used as part of AI interactions.
222
+
223
+
== Usage Example
221
224
222
-
2. Configure the client in `application.properties` or `application.yml`:
225
+
Add the appropriate starter dependency to your project and configure the client in `application.properties` or `application.yml`:
223
226
224
227
[source,yaml]
225
228
----
@@ -250,8 +253,8 @@ spring:
250
253
API_KEY: your-api-key
251
254
DEBUG: "true"
252
255
----
253
-
+
254
-
3. The MCP client beans will be automatically configured and available for injection:
256
+
257
+
The MCP client beans will be automatically configured and available for injection:
At least one transport must be available for the clients to be created.
276
-
277
-
== Integration with Spring AI
278
-
279
-
The starter automatically configures tool callbacks that integrate with Spring AI's tool execution framework, allowing MCP tools to be used as part of AI interactions.
280
-
281
-
== Lifecycle Management
282
-
283
-
The auto-configuration includes proper lifecycle management:
284
-
285
-
* Automatic initialization of clients (if enabled)
286
-
* Proper cleanup of resources when the application context is closed
287
-
* Management of multiple client instances
288
-
289
-
== Best Practices
290
-
291
-
1. Choose the appropriate client type based on your application's needs:
292
-
* Use SYNC client for traditional applications
293
-
* Use ASYNC client for reactive applications
294
-
295
-
2. Configure appropriate timeout values based on your use case:
296
-
[source,yaml]
297
-
----
298
-
spring:
299
-
ai:
300
-
mcp:
301
-
client:
302
-
request-timeout: 30s
303
-
----
304
-
+
305
-
3. Use customizers for advanced client configuration:
270
+
Additionally, the registered MCP Tools with all MCP clients are provided as list of ToolCallback instances:
306
271
307
272
[source,java]
308
273
----
309
-
@Component
310
-
public class MyMcpClientCustomizer implements McpSyncClientCustomizer {
311
-
@Override
312
-
public void customize(String name, McpClient.SyncSpec clientSpec) {
313
-
// Custom configuration
314
-
}
315
-
}
274
+
@Autowired
275
+
private List<ToolCallback> toolCallbacks;
316
276
----
317
277
318
-
== Troubleshooting
319
-
320
-
Common issues and solutions:
321
-
322
-
1. *Client Not Created*
323
-
* Verify that at least one transport is available
324
-
* Check if the client is enabled in configuration
325
-
* Ensure required dependencies are present
326
-
327
-
2. *Timeout Issues*
328
-
* Adjust the `request-timeout` property
329
-
* Check network connectivity
330
-
* Verify server response times
331
-
332
-
3. *Integration Issues*
333
-
* Ensure proper transport configuration
334
-
* Check client initialization status
335
-
336
278
== Additional Resources
337
279
338
280
* link:https://docs.spring.io/spring-ai/reference/[Spring AI Documentation]
0 commit comments