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. The application enables natural language interactions with Brave Search, allowing you to perform internet searches through a conversational interface.
Prerequisites
- Java 17 or higher
- Maven 3.6+
- npx package manager
- OpenAI API key
- Brave Search API key
Setup
-
Install npx (Node Package eXecute): First, make sure to install npm and then run:
npm install -g npx -
Clone the repository:
git clone https://github.com/spring-projects/spring-ai-examples.git cd model-context-protocol/brave-starter -
Set up your API keys:
export OPENAI_API_KEY='your-openai-api-key-here' export BRAVE_API_KEY='your-brave-api-key-here' -
Build the application:
./mvnw clean install
Running the Application
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.
How it Works
The application integrates Spring AI with the Brave Search MCP server through several components:
MCP Client Autoconfiguration
- Add the Spring AI MCP boot-starter to your POM:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp-spring-boot-starter</artifactId>
</dependency>
- Application properties:
spring.ai.mcp.client.stdio.enabled=true
spring.ai.mcp.client.stdio.servers-configuration=classpath:/mcp-servers-config.json
The servers-configuration should point to Anthropic Desktop-compatible JSON configuration like:
{
"mcpServers": {
"brave-search": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-brave-search"
],
"env": {
}
}
}
}
The MCP auto-configuration will use the servers-configuration to initialize MCP client connection to the provided servers and create an McpToolAdapter bean, providing access to the MCP tools exposed by the configured MCP servers.
Chat Integration
The ChatClient is configured with the Brave Search function callbacks:
var chatClient = chatClientBuilder
.defaultTools(mcpToolAdapter.toolCallbacks())
.build();
This setup allows the AI model to do the following:
- Understand when to use Brave Search
- Format queries appropriately
- Process and incorporate search results into responses