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 production 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,36 +159,102 @@ 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 - default client type, suitable for traditional request-response patterns with blocking operations
202
+
* Asynchronous - suitable for reactive applications with non-blocking operations, configured using `spring.ai.mcp.client.type=ASYNC`
210
203
211
204
=== Client Customization
212
205
213
-
The auto-configuration supports customization through:
206
+
The auto-configuration supports client spec customization.
207
+
Implement the `McpSyncClientCustomizer` callback interface to customize the `McpClient.SyncSpec` spec for a named server connection.
208
+
Similarly, the `McpAsyncClientCustomizer` interface allows customizing the `McpClient.AsyncSpec` spec for asynchronous clients.
214
209
215
-
* `McpSyncClientCustomizer` for synchronous clients
216
-
* `McpAsyncClientCustomizer` for asynchronous clients
210
+
[tabs]
211
+
======
212
+
Sync::
213
+
+
214
+
[source,java]
215
+
----
216
+
@Component
217
+
public class CustomMcpSyncClientCustomizer implements McpSyncClientCustomizer {
218
+
@Override
219
+
public void customize(String name, McpClient.SyncSpec spec) {
220
+
// Customize the sync client configuration
221
+
spec.requestTimeout(Duration.ofSeconds(30));
222
+
}
223
+
}
224
+
----
217
225
218
-
== Usage Example
226
+
Async::
227
+
+
228
+
[source,java]
229
+
----
230
+
@Component
231
+
public class CustomMcpAsyncClientCustomizer implements McpAsyncClientCustomizer {
232
+
@Override
233
+
public void customize(String name, McpClient.AsyncSpec spec) {
234
+
// Customize the async client configuration
235
+
spec.requestTimeout(Duration.ofSeconds(30));
236
+
}
237
+
}
238
+
----
239
+
======
240
+
241
+
The MCP client auto-configuration automatically detects and applies any customizers found in the application context.
242
+
243
+
=== Transport Support
219
244
220
-
1. Add the appropriate starter dependency to your project.
245
+
The auto-configuration supports multiple transport types:
246
+
247
+
* Standard I/O (Stdio) (activated by the `spring-ai-mcp-client-spring-boot-starter`)
248
+
* SSE HTTP (activated by the `spring-ai-mcp-client-spring-boot-starter`)
249
+
* SSE WebFlux (activated by the `spring-ai-starter-mcp-client-webflux`)
221
250
222
-
2. Configure the client in `application.properties` or `application.yml`:
251
+
=== Integration with Spring AI
252
+
253
+
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.
254
+
255
+
== Usage Example
256
+
257
+
Add the appropriate starter dependency to your project and configure the client in `application.properties` or `application.yml`:
223
258
224
259
[source,yaml]
225
260
----
@@ -250,8 +285,8 @@ spring:
250
285
API_KEY: your-api-key
251
286
DEBUG: "true"
252
287
----
253
-
+
254
-
3. The MCP client beans will be automatically configured and available for injection:
288
+
289
+
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:
302
+
Additionally, the registered MCP Tools with all MCP clients are provided as a list of ToolCallback instances:
306
303
307
304
[source,java]
308
305
----
309
-
@Component
310
-
public class MyMcpClientCustomizer implements McpSyncClientCustomizer {
311
-
@Override
312
-
public void customize(String name, McpClient.SyncSpec clientSpec) {
313
-
// Custom configuration
314
-
}
315
-
}
306
+
@Autowired
307
+
private List<ToolCallback> toolCallbacks;
316
308
----
317
309
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
310
== Additional Resources
337
311
338
312
* link:https://docs.spring.io/spring-ai/reference/[Spring AI Documentation]
0 commit comments