Skip to content

Commit 234d3ff

Browse files
committed
refactor(mcp): update to use Spring Boot autoconfiguration and rename tool callbacks
Update MCP examples to use Spring Boot autoconfiguration instead of manual configuration. Replace deprecated defaultTools method with defaultToolCallbacks across all examples. Update documentation to reflect these changes and provide more detailed configuration examples. - Update API from defaultTools to defaultToolCallbacks in all MCP examples - Update README files with more detailed configuration examples Signed-off-by: Christian Tzolov <[email protected]>
1 parent 2c9fa4d commit 234d3ff

File tree

17 files changed

+96
-218
lines changed

17 files changed

+96
-218
lines changed

model-context-protocol/brave/README.md

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Spring AI - Model Context Protocol (MCP) Brave Search Example
22

3-
This example demonstrates how to create a Spring AI Model Context Protocol (MCP) client that communicates with the [Brave Search MCP Server](https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search). The application shows how to build an MCP client that enables natural language interactions with Brave Search, allowing you to perform internet searches through a conversational interface. Instead of using Spring Boot autoconfiguration, this example demonstrates how to manually configure the MCP client transport using an `@Bean` definition.
3+
This example demonstrates how to create a Spring AI Model Context Protocol (MCP) client that communicates with the [Brave Search MCP Server](https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search). The application shows how to build an MCP client that enables natural language interactions with Brave Search, allowing you to perform internet searches through a conversational interface. This example uses Spring Boot autoconfiguration to set up the MCP client through configuration files.
44

55
When run, the application demonstrates the MCP client's capabilities by asking a specific question: "Does Spring AI supports the Model Context Protocol? Please provide some references." The MCP client uses Brave Search to find relevant information and returns a comprehensive answer. After providing the response, the application exits.
66

@@ -52,56 +52,46 @@ The application will execute a single query asking about Spring AI's support for
5252

5353
## How it Works
5454

55-
The application integrates Spring AI with the Brave Search MCP server through several components:
55+
The application integrates Spring AI with the Brave Search MCP server through Spring Boot autoconfiguration:
5656

57-
### MCP Client Setup
57+
### MCP Client Configuration
5858

59-
```java
60-
@Bean
61-
public McpSyncClient mcpClient() {
62-
63-
// https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search
64-
var stdioParams = ServerParameters.builder("npx")
65-
.args("-y", "@modelcontextprotocol/server-brave-search")
66-
.addEnvVar("BRAVE_API_KEY", System.getenv("BRAVE_API_KEY"))
67-
.build();
68-
69-
var mcpClient = McpClient.sync(new StdioClientTransport(stdioParams)).build();
70-
var init = mcpClient.initialize();
71-
logger.info("MCP Initialized: {}", init);
72-
return mcpClient;
73-
}
74-
```
75-
76-
The MCP client is configured to:
77-
1. Use the Brave Search MCP server via npx
78-
2. Pass the Brave API key from environment variables
79-
3. Initialize a synchronous connection to the server
59+
The MCP client is configured using configuration files:
8060

81-
### Function Callbacks
82-
83-
The application automatically discovers and registers available Brave Search tools:
61+
1. `application.properties`:
62+
```properties
63+
spring.ai.mcp.client.stdio.servers-configuration=classpath:/mcp-servers-config.json
64+
```
8465

85-
```java
86-
List<McpFunctionCallback> functionCallbacks = mcpClient.listTools(null)
87-
.tools()
88-
.stream()
89-
.map(tool -> new McpFunctionCallback(mcpClient, tool))
90-
.toList();
66+
2. `mcp-servers-config.json`:
67+
```json
68+
{
69+
"mcpServers": {
70+
"brave-search": {
71+
"command": "npx",
72+
"args": [
73+
"-y",
74+
"@modelcontextprotocol/server-brave-search"
75+
],
76+
"env": {
77+
}
78+
}
79+
}
80+
}
9181
```
9282

93-
These callbacks enable the ChatClient to:
94-
- Access Brave Search tools during conversations
95-
- Handle function calls requested by the AI model
96-
- Execute search queries against the Brave Search API
83+
This configuration:
84+
1. Uses the Brave Search MCP server via npx
85+
2. The Brave API key is passed from environment variables
86+
3. Initializes a synchronous connection to the server
9787

9888
### Chat Integration
9989

100-
The ChatClient is configured with the Brave Search function callbacks:
90+
The ChatClient is configured with the MCP tool callbacks in the Application class:
10191

10292
```java
10393
var chatClient = chatClientBuilder
104-
.defaultFunctions(functionCallbacks.toArray(new McpFunctionCallback[0]))
94+
.defaultToolCallbacks(new SyncMcpToolCallbackProvider(mcpSyncClients))
10595
.build();
10696
```
10797

model-context-protocol/brave/src/main/java/org/springframework/ai/mcp/samples/brave/Application.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.springframework.boot.CommandLineRunner;
1212
import org.springframework.boot.SpringApplication;
1313
import org.springframework.boot.autoconfigure.SpringBootApplication;
14-
import org.springframework.context.ConfigurableApplicationContext;
1514
import org.springframework.context.annotation.Bean;
1615

1716
@SpringBootApplication
@@ -20,24 +19,22 @@ public class Application {
2019
private static final Logger logger = LoggerFactory.getLogger(Application.class);
2120

2221
public static void main(String[] args) {
23-
SpringApplication.run(Application.class, args);
22+
SpringApplication.run(Application.class, args).close();
2423
}
2524

2625
@Bean
2726
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder,
28-
List<McpSyncClient> mcpSyncClients, ConfigurableApplicationContext context) {
27+
List<McpSyncClient> mcpSyncClients) {
2928

3029
return args -> {
3130

3231
var chatClient = chatClientBuilder
33-
.defaultTools(new SyncMcpToolCallbackProvider(mcpSyncClients))
32+
.defaultToolCallbacks(new SyncMcpToolCallbackProvider(mcpSyncClients))
3433
.build();
3534

3635
String question = "Does Spring AI supports the Model Context Protocol? Please provide some references.";
3736
logger.info("QUESTION: {}\n", question);
3837
logger.info("ASSISTANT: {}\n", chatClient.prompt(question).call().content());
39-
40-
context.close();
4138
};
4239
}
4340
}

model-context-protocol/client-starter/starter-default-client/README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Follow the [MCP Client Boot Starter](https://docs.spring.io/spring-ai/reference/
66

77
## Overview
88

9-
The project uses Spring Boot and Spring AI to create a command-line application that demonstrates MCP server integration. The application:
9+
The project uses Spring Boot 3.3.6 and Spring AI 1.0.0-SNAPSHOT to create a command-line application that demonstrates MCP server integration. The application:
1010
- Connects to MCP servers using STDIO and/or SSE (HttpClient-based) transports
1111
- Integrates with Spring AI's chat capabilities
1212
- Demonstrates tool execution through MCP servers
@@ -19,7 +19,7 @@ For example, running the application with `-Dai.user.input="Does Spring AI suppo
1919
- Java 17 or later
2020
- Maven 3.6+
2121
- Anthropic API key (Claude) (Get one at https://docs.anthropic.com/en/docs/initial-setup)
22-
- Brave Search API key (Get one at https://brave.com/search/api/)
22+
- Brave Search API key (for the Brave Search MCP server) (Get one at https://brave.com/search/api/)
2323

2424
## Dependencies
2525

@@ -54,6 +54,9 @@ spring.main.web-application-type=none
5454

5555
# AI Provider Configuration
5656
spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY}
57+
58+
# Enable the MCP client tool-callback auto-configuration
59+
spring.ai.mcp.client.toolcallback.enabled=true
5760
```
5861

5962
#### STDIO Transport Properties
@@ -117,10 +120,12 @@ The application demonstrates a simple command-line interaction with an AI model
117120

118121
## Running the Application
119122

120-
1. Set the required environment variable:
123+
1. Set the required environment variables:
121124
```bash
122125
export ANTHROPIC_API_KEY=your-api-key
123-
export BRAVE_API_KEY='your-brave-api-key-here'
126+
127+
# For the Brave Search MCP server
128+
export BRAVE_API_KEY=your-brave-api-key
124129
```
125130

126131
2. Build the application:
@@ -130,10 +135,14 @@ The application demonstrates a simple command-line interaction with an AI model
130135

131136
3. Run the application:
132137
```bash
138+
# Run with the default question from application.properties
139+
java -jar target/mcp-starter-default-client-0.0.1-SNAPSHOT.jar
140+
141+
# Or specify a custom question
133142
java -Dai.user.input='Does Spring AI support MCP?' -jar target/mcp-starter-default-client-0.0.1-SNAPSHOT.jar
134143
```
135144

136-
The application will execute the question "Does Spring AI support MCP?", use the provided brave (or other tools) to answer it, and display the AI assistant's response.
145+
The application will execute the question, use the configured MCP tools to answer it, and display the AI assistant's response.
137146

138147
## Additional Resources
139148

model-context-protocol/client-starter/starter-default-client/src/main/java/org/springframework/ai/mcp/samples/client/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilde
4141
return args -> {
4242

4343
var chatClient = chatClientBuilder
44-
.defaultTools(tools)
44+
.defaultToolCallbacks(tools)
4545
.build();
4646

4747
System.out.println("\n>>> QUESTION: " + userInput);

model-context-protocol/client-starter/starter-webflux-client/README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ Follow the [MCP Client Boot Starter](https://docs.spring.io/spring-ai/reference/
66

77
## Overview
88

9-
The project uses Spring Boot and Spring AI to create a command-line application that demonstrates MCP server integration with WebFlux. The application:
9+
The project uses Spring Boot 3.3.6 and Spring AI 1.0.0-SNAPSHOT to create a command-line application that demonstrates MCP server integration with WebFlux. The application:
1010
- Connects to MCP servers using STDIO and/or SSE (WebFlux-based) transports
1111
- Integrates with Spring AI's chat capabilities
1212
- Demonstrates tool execution through MCP servers
1313
- Takes a user-defined question via the `-Dai.user.input` command-line property, which is mapped to a Spring `@Value` annotation in the code
1414

15-
For example, running the application with `-Dai.user.input="Does Spring AI support MCP?"` will inject this question into the application through Spring's property injection, and the application will use it to query the MCP server using WebFlux's reactive programming model.
15+
For example, running the application with `-Dai.user.input="What tools are available?"` will inject this question into the application through Spring's property injection, and the application will use it to query the MCP server using WebFlux's reactive programming model.
1616

1717
## Prerequisites
1818

1919
- Java 17 or later
2020
- Maven 3.6+
2121
- Anthropic API key (Claude) (Get one at https://docs.anthropic.com/en/docs/initial-setup)
22-
- Brave Search API key (Get one at https://brave.com/search/api/)
22+
- OpenAI API key (optional, commented out by default) (Get one at https://platform.openai.com/api-keys)
23+
- Brave Search API key (for the Brave Search MCP server) (Get one at https://brave.com/search/api/)
2324

2425
## Dependencies
2526

@@ -35,6 +36,11 @@ The project uses the following main dependencies:
3536
<groupId>org.springframework.ai</groupId>
3637
<artifactId>spring-ai-starter-model-anthropic</artifactId>
3738
</dependency>
39+
<!-- OpenAI dependency is commented out by default but can be enabled -->
40+
<!-- <dependency>
41+
<groupId>org.springframework.ai</groupId>
42+
<artifactId>spring-ai-starter-model-openai</artifactId>
43+
</dependency> -->
3844
</dependencies>
3945
```
4046

@@ -54,6 +60,10 @@ spring.main.web-application-type=none
5460

5561
# AI Provider Configuration
5662
spring.ai.anthropic.api-key=${ANTHROPIC_API_KEY}
63+
spring.ai.openai.api-key=${OPENAI_API_KEY}
64+
65+
# Enable the MCP client tool-callback auto-configuration
66+
spring.ai.mcp.client.toolcallback.enabled=true
5767
```
5868

5969
#### STDIO Transport Properties
@@ -111,16 +121,19 @@ The application demonstrates a simple command-line interaction with an AI model
111121

112122
1. The application starts and configures multiple MCP Clients (one for each provided STDIO or SSE connection configuration)
113123
2. It builds a ChatClient with the configured MCP tools
114-
3. Sends a predefined question (set vi the `ai.user.input` property) to the AI model
124+
3. Sends a predefined question (set via the `ai.user.input` property) to the AI model
115125
4. Displays the AI's response
116126
5. Automatically closes the application
117127

118128
## Running the Application
119129

120-
1. Set the required environment variable:
130+
1. Set the required environment variables:
121131
```bash
122132
export ANTHROPIC_API_KEY=your-api-key
123-
export BRAVE_API_KEY='your-brave-api-key-here'
133+
# If using OpenAI (uncomment the dependency in pom.xml first)
134+
# export OPENAI_API_KEY=your-openai-api-key
135+
# For the Brave Search MCP server
136+
export BRAVE_API_KEY=your-brave-api-key
124137
```
125138

126139
2. Build the application:
@@ -130,10 +143,14 @@ The application demonstrates a simple command-line interaction with an AI model
130143

131144
3. Run the application:
132145
```bash
146+
# Run with the default question from application.properties
147+
java -jar target/mcp-starter-webflux-client-0.0.1-SNAPSHOT.jar
148+
149+
# Or specify a custom question
133150
java -Dai.user.input='Does Spring AI support MCP?' -jar target/mcp-starter-webflux-client-0.0.1-SNAPSHOT.jar
134151
```
135152

136-
The application will execute the question "Does Spring AI support MCP?", use the provided brave (or other tools) to answer it, and display the AI assistant's response.
153+
The application will execute the question, use the configured MCP tools to answer it, and display the AI assistant's response.
137154

138155
## Additional Resources
139156

model-context-protocol/client-starter/starter-webflux-client/src/main/java/org/springframework/ai/mcp/samples/client/Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilde
4141
return args -> {
4242

4343
var chatClient = chatClientBuilder
44-
.defaultTools(tools)
44+
.defaultToolCallbacks(tools)
4545
.build();
4646

4747
System.out.println("\n>>> QUESTION: " + userInput);

model-context-protocol/dynamic-tool-update/server/src/test/java/org/springframework/ai/mcp/sample/client/ClientSse.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

model-context-protocol/dynamic-tool-update/server/src/test/java/org/springframework/ai/mcp/sample/client/ClientStdio.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)