Update example in brave directory that creates a McpSyncClient as a @Bean
- Does not use the spring ai mcp starter - Use latest deps - Update readme - Code cleanup
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# Spring AI - Model Context Protocol (MCP) Brave Search Example
|
||||
|
||||
This example demonstrates how to use the Spring AI Model Context Protocol (MCP) with the [Brave Search MCP Server](https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search). The application enables natural language interactions with Brave Search, allowing you to perform internet searches through a conversational interface.
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
<img src="spring-ai-mcp-brave.jpg" width="600"/>
|
||||
|
||||
@@ -11,7 +13,7 @@ This example demonstrates how to use the Spring AI Model Context Protocol (MCP)
|
||||
- npx package manager
|
||||
- Git
|
||||
- OpenAI API key
|
||||
- Brave Search API key
|
||||
- Brave Search API key (Get one at https://brave.com/search/api/)
|
||||
|
||||
## Setup
|
||||
|
||||
@@ -46,7 +48,7 @@ Run the application using Maven:
|
||||
./mvnw spring-boot:run
|
||||
```
|
||||
|
||||
The application will demonstrate the integration by asking a sample question about Spring AI and Model Context Protocol, utilizing Brave Search to gather information.
|
||||
The application will execute a single query asking about Spring AI's support for the Model Context Protocol. It uses the Brave Search MCP server to search the internet for relevant information, processes the results through the MCP client, and provides a detailed response before exiting.
|
||||
|
||||
## How it Works
|
||||
|
||||
@@ -55,16 +57,19 @@ The application integrates Spring AI with the Brave Search MCP server through se
|
||||
### MCP Client Setup
|
||||
|
||||
```java
|
||||
@Bean(destroyMethod = "close")
|
||||
@Bean
|
||||
public McpSyncClient mcpClient() {
|
||||
var stdioParams = ServerParameters.builder("npx")
|
||||
.args("-y", "@modelcontextprotocol/server-brave-search")
|
||||
.addEnvVar("BRAVE_API_KEY", System.getenv("BRAVE_API_KEY"))
|
||||
.build();
|
||||
|
||||
var mcpClient = McpClient.sync(new StdioServerTransport(stdioParams));
|
||||
var init = mcpClient.initialize();
|
||||
return mcpClient;
|
||||
// https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search
|
||||
var stdioParams = ServerParameters.builder("npx")
|
||||
.args("-y", "@modelcontextprotocol/server-brave-search")
|
||||
.addEnvVar("BRAVE_API_KEY", System.getenv("BRAVE_API_KEY"))
|
||||
.build();
|
||||
|
||||
var mcpClient = McpClient.sync(new StdioClientTransport(stdioParams)).build();
|
||||
var init = mcpClient.initialize();
|
||||
logger.info("MCP Initialized: {}", init);
|
||||
return mcpClient;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-bom</artifactId>
|
||||
<version>1.0.0-M5</version>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
@@ -40,12 +40,13 @@
|
||||
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.experimental</groupId>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-mcp</artifactId>
|
||||
<version>0.6.0</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package org.springframework.ai.mcp.samples.brave;
|
||||
|
||||
import io.modelcontextprotocol.client.McpClient;
|
||||
import io.modelcontextprotocol.client.McpSyncClient;
|
||||
import io.modelcontextprotocol.client.transport.ServerParameters;
|
||||
import io.modelcontextprotocol.client.transport.StdioClientTransport;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.mcp.client.McpClient;
|
||||
import org.springframework.ai.mcp.client.McpSyncClient;
|
||||
import org.springframework.ai.mcp.client.transport.ServerParameters;
|
||||
import org.springframework.ai.mcp.client.transport.StdioClientTransport;
|
||||
import org.springframework.ai.mcp.spring.McpFunctionCallback;
|
||||
import org.springframework.ai.mcp.SyncMcpToolCallback;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
@@ -15,33 +18,35 @@ import org.springframework.context.annotation.Bean;
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Application.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder,
|
||||
McpSyncClient mcpClient, ConfigurableApplicationContext context) {
|
||||
McpSyncClient mcpSyncClient, ConfigurableApplicationContext context) {
|
||||
|
||||
return args -> {
|
||||
|
||||
var chatClient = chatClientBuilder
|
||||
.defaultFunctions(mcpClient.listTools(null)
|
||||
.defaultTools(mcpSyncClient.listTools(null)
|
||||
.tools()
|
||||
.stream()
|
||||
.map(tool -> new McpFunctionCallback(mcpClient, tool))
|
||||
.toArray(McpFunctionCallback[]::new))
|
||||
.map(tool -> new SyncMcpToolCallback(mcpSyncClient, tool))
|
||||
.toArray(SyncMcpToolCallback[]::new))
|
||||
.build();
|
||||
|
||||
String question = "Does Spring AI supports the Model Context Protocol? Please provide some references.";
|
||||
System.out.println("QUESTION: " + question);
|
||||
System.out.println("ASSISTANT: " + chatClient.prompt(question).call().content());
|
||||
logger.info("QUESTION: {}\n", question);
|
||||
logger.info("ASSISTANT: {}\n", chatClient.prompt(question).call().content());
|
||||
|
||||
context.close();
|
||||
};
|
||||
}
|
||||
|
||||
@Bean(destroyMethod = "close")
|
||||
@Bean
|
||||
public McpSyncClient mcpClient() {
|
||||
|
||||
// https://github.com/modelcontextprotocol/servers/tree/main/src/brave-search
|
||||
@@ -52,7 +57,7 @@ public class Application {
|
||||
|
||||
var mcpClient = McpClient.sync(new StdioClientTransport(stdioParams)).build();
|
||||
var init = mcpClient.initialize();
|
||||
System.out.println("MCP Initialized: " + init);
|
||||
logger.info("MCP Initialized: {}", init);
|
||||
return mcpClient;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
spring.application.name=mcp
|
||||
spring.main.web-application-type=none
|
||||
|
||||
spring.ai.openai.api-key=${OPENAI_API_KEY}
|
||||
spring.ai.openai.api-key=${OPENAI_API_KEY}
|
||||
|
||||
# Logging configuration
|
||||
logging.level.root=INFO
|
||||
logging.level.org.springframework.ai.mcp=WARN
|
||||
logging.level.org.springframework.ai.mcp.client.transport.StdioClientTransport=WARN
|
||||
logging.level.org.springframework.ai.mcp.samples.brave=INFO
|
||||
logging.level.io.modelcontextprotocol.client=WARN
|
||||
logging.level.io.modelcontextprotocol.spec=WARN
|
||||
Reference in New Issue
Block a user