Skip to content

Commit 37b0030

Browse files
eddumelendezmarkpollack
authored andcommitted
Add new mcp brave example using docker mcp gateway
Signed-off-by: Eddú Meléndez <eddu.melendez@gmail.com>
1 parent bede0b6 commit 37b0030

File tree

12 files changed

+752
-0
lines changed

12 files changed

+752
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
brave.api_key=${BRAVE_API_KEY}
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.4/apache-maven-3.8.4-bin.zip
2+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
services:
2+
env-setup:
3+
image: alpine:latest
4+
environment:
5+
- BRAVE_API_KEY=${BRAVE_API_KEY}
6+
volumes:
7+
- secrets-data:/tmp/secrets
8+
command: sh -c 'echo "brave.api_key=$${BRAVE_API_KEY}" > /tmp/secrets/mcp_secret'
9+
10+
mcp-gateway:
11+
image: docker/agents_gateway:v2
12+
depends_on:
13+
- env-setup
14+
ports:
15+
- 8811:8811
16+
volumes:
17+
- "/var/run/docker.sock:/var/run/docker.sock"
18+
- secrets-data:/run/secrets
19+
environment:
20+
- BRAVE_API_KEY=${BRAVE_API_KEY}
21+
command:
22+
- --transport=sse
23+
- --secrets=/run/secrets/mcp_secret
24+
- --servers=brave
25+
- --tools=*
26+
volumes:
27+
secrets-data:
28+
# secrets:
29+
# - mcp_secret
30+
#
31+
#secrets:
32+
# mcp_secret:
33+
# file: .env

0 commit comments

Comments
 (0)