|
| 1 | +# Spring AI - Model Context Protocol (MCP) Brave Search Example |
| 2 | + |
| 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. |
| 4 | + |
| 5 | +When run, the application demonstrates the MCP client's capabilities by asking a specific question: "Does Spring AI support 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. |
| 6 | + |
| 7 | +<img src="spring-ai-mcp-brave.jpg" width="600"/> |
| 8 | + |
| 9 | +## Prerequisites |
| 10 | + |
| 11 | +- Java 17 or higher |
| 12 | +- Maven 3.6+ |
| 13 | +- Docker Desktop 4.41 or later |
| 14 | +- Git |
| 15 | +- OpenAI API key |
| 16 | +- Brave Search API key (Get one at https://brave.com/search/api/) |
| 17 | + |
| 18 | +## Setup |
| 19 | + |
| 20 | +1. **Start the Dockerized Services:** |
| 21 | + Make sure Docker (e.g., Docker Desktop 4.41 or later, or Docker Engine with Docker Compose CLI) is installed and running. Then, in the project's root directory (where `compose.yml` is located), start the necessary services: |
| 22 | + ```bash |
| 23 | + docker compose up |
| 24 | + ``` |
| 25 | + |
| 26 | +2. Clone the repository: |
| 27 | + ```bash |
| 28 | + git clone https://github.com/spring-projects/spring-ai-examples.git |
| 29 | + cd model-context-protocol/brave-docker-agents-gateway |
| 30 | + ``` |
| 31 | + |
| 32 | +3. Set up your API keys: |
| 33 | + ```bash |
| 34 | + export OPENAI_API_KEY='your-openai-api-key-here' |
| 35 | + export BRAVE_API_KEY='your-brave-api-key-here' |
| 36 | + ``` |
| 37 | + |
| 38 | +4. Build the application: |
| 39 | + ```bash |
| 40 | + ./mvnw clean install |
| 41 | + ``` |
| 42 | + |
| 43 | +## Running the Application |
| 44 | + |
| 45 | +Run the application using Maven: |
| 46 | +```bash |
| 47 | +./mvnw spring-boot:run |
| 48 | +``` |
| 49 | + |
| 50 | +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. |
| 51 | +
|
| 52 | +## How it Works |
| 53 | +
|
| 54 | +The application integrates Spring AI with the Brave Search MCP server through Spring Boot autoconfiguration: |
| 55 | +
|
| 56 | +### MCP Client Configuration |
| 57 | +
|
| 58 | +The MCP client is configured using configuration files: |
| 59 | +
|
| 60 | +1. `application.properties`: |
| 61 | +```properties |
| 62 | +spring.ai.mcp.client.see.gateway.url=http://localhost:8811 |
| 63 | +``` |
| 64 | +
|
| 65 | +This configuration: |
| 66 | +1. Uses the Brave Search MCP server via Docker MCP Gateway |
| 67 | +2. The Brave API key is passed from environment variables |
| 68 | +3. Initializes a synchronous connection to the server |
| 69 | +
|
| 70 | +### Chat Integration |
| 71 | +
|
| 72 | +The ChatClient is configured with the MCP tool callbacks in the Application class: |
| 73 | +
|
| 74 | +```java |
| 75 | +var chatClient = chatClientBuilder |
| 76 | + .defaultToolCallbacks(new SyncMcpToolCallbackProvider(mcpSyncClients)) |
| 77 | + .build(); |
| 78 | +``` |
| 79 | +
|
| 80 | +This setup allows the AI model to: |
| 81 | +- Understand when to use Brave Search |
| 82 | +- Format queries appropriately |
| 83 | +- Process and incorporate search results into responses |
| 84 | +
|
| 85 | +## Dependencies |
| 86 | +
|
| 87 | +The project uses: |
| 88 | +- Spring Boot 3.3.6 |
| 89 | +- Spring AI 1.1.0-SNAPSHOT |
| 90 | +- spring-ai-starter-model-openai |
| 91 | +- spring-ai-starter-mcp-client |
0 commit comments