Skip to content

Commit aca5899

Browse files
committed
Improve MCP documentation
Signed-off-by: Christian Tzolov <[email protected]>
1 parent 17e8f6a commit aca5899

File tree

10 files changed

+537
-485
lines changed

10 files changed

+537
-485
lines changed
661 KB
Loading
840 KB
Loading

spring-ai-docs/src/main/antora/modules/ROOT/images/mcp/java-mcp-uml-classdiagram.svg

Lines changed: 1 addition & 0 deletions
Loading

spring-ai-docs/src/main/antora/modules/ROOT/images/mcp/mcp-stack.svg

Lines changed: 197 additions & 0 deletions
Loading

spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
* xref:api/functions.adoc[Function Calling (Deprecated)]
9999
** xref:api/function-callback.adoc[FunctionCallback API (Deprecated)]
100100
** xref:api/tools-migration.adoc[Migrating to ToolCallback API]
101-
* xref:api/model-context-protocol.adoc[Model Context Protocol (MCP)]
101+
* xref:api/mcp/mcp-overview.adoc[Model Context Protocol (MCP)]
102102
** xref:api/mcp/mcp-client-boot-starter-docs.adoc[MCP Client Boot Starters]
103103
** xref:api/mcp/mcp-server-boot-starter-docs.adoc[MCP Server Boot Starters]
104104
** xref:api/mcp/mcp-helpers.adoc[MCP Utilities]
@@ -115,4 +115,3 @@
115115
116116
* Appendices
117117
** xref:upgrade-notes.adoc[]
118-

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/mcp/mcp-client-boot-starter-docs.adoc

Lines changed: 102 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
1-
= Spring AI MCP Client Boot Starter
1+
= MCP Client Boot Starter
22

33
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.
44

55
The MCP Client Boot Starter provides:
66

7+
* Management of multiple client instances
78
* Automatic client initialization (if enabled)
89
* Support for multiple named transports
910
* 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
1112
* Customizable client creation through customizers
1213
13-
== Dependencies
14+
== Starters
1415

15-
=== Core Starter
16+
=== Standard MCP Client
1617

1718
[source,xml]
1819
----
1920
<dependency>
2021
<groupId>org.springframework.ai</groupId>
2122
<artifactId>spring-ai-mcp-client-spring-boot-starter</artifactId>
22-
<version>${spring-ai.version}</version>
2323
</dependency>
2424
----
2525

26-
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.
2727
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`.
3131

32-
=== WebFlux Starter
32+
=== WebFlux Client
3333

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.
3535

3636
[source,xml]
3737
----
3838
<dependency>
3939
<groupId>org.springframework.ai</groupId>
4040
<artifactId>spring-ai-mcp-client-webflux-spring-boot-starter</artifactId>
41-
<version>${spring-ai.version}</version>
4241
</dependency>
4342
----
4443

@@ -73,44 +72,14 @@ All common configuration properties are prefixed with `spring.ai.mcp.client`:
7372
|`20s`
7473

7574
|`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
7776
|`SYNC`
7877

7978
|`root-change-notification`
8079
|Enable/disable root change notifications for all clients
8180
|`true`
8281
|===
8382

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-
11483
=== Stdio Transport Properties
11584

11685
Properties for Standard I/O transport are prefixed with `spring.ai.mcp.client.stdio`:
@@ -160,7 +129,7 @@ spring:
160129
DEBUG: "true"
161130
----
162131

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]:
164133

165134
[source,yaml]
166135
----
@@ -172,7 +141,7 @@ spring:
172141
servers-configuration: classpath:mcp-servers.json
173142
----
174143

175-
The Claude Destop format looks like this:
144+
The Claude Desktop format looks like this:
176145

177146
[source,json]
178147
----
@@ -190,36 +159,102 @@ The Claude Destop format looks like this:
190159
}
191160
}
192161
----
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+
----
194194

195195
== Features
196196

197-
=== Client Types
197+
=== Sync/Async Client Types
198198

199199
The starter supports two types of clients:
200200

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`
210203

211204
=== Client Customization
212205

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.
214209

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+
----
217225
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
219244

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`)
221250

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`:
223258

224259
[source,yaml]
225260
----
@@ -250,8 +285,8 @@ spring:
250285
API_KEY: your-api-key
251286
DEBUG: "true"
252287
----
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:
255290

256291
[source,java]
257292
----
@@ -264,75 +299,14 @@ private List<McpSyncClient> mcpSyncClients; // For sync client
264299
private List<McpAsyncClient> mcpAsyncClients; // For async client
265300
----
266301

267-
== Transport Support
268-
269-
The auto-configuration supports multiple transport types:
270-
271-
* Standard I/O (Stdio)
272-
* SSE HTTP
273-
* SSE WebFlux (requires `spring-ai-starter-mcp-client-webflux`)
274-
275-
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:
306303

307304
[source,java]
308305
----
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;
316308
----
317309

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-
336310
== Additional Resources
337311

338312
* link:https://docs.spring.io/spring-ai/reference/[Spring AI Documentation]

0 commit comments

Comments
 (0)