article
README
🚀 ModelContextProtocolClient
本项目是一个基于Spring Boot的客户端应用程序,它展示了如何运用不同的传输方法(WebFlux SSE、Stdio)与MCP服务器进行通信,为开发者提供了多样化的通信解决方案。
🚀 快速开始
环境配置
- 在项目根目录下创建一个
.env文件,并添加以下内容:
BRAVE_SEARCH_API_KEY=你的Brave搜索API密钥
WEATHER_API_KEY=你的天气API密钥
- 克隆MCP服务器仓库:
git clone https://github.com/InnoBridge/ModelContextProtocolServer
- Docker环境配置:
- 更新
docker-compose.yml中的卷映射:
- 更新
modelcontextprotocolclient_application
...
volumes:
- .:/app
- /var/run/docker.sock:/var/run/docker.sock
- ./local/root:/root
- ../{ModelContextProtocolServer所在路径}:/ModelContextProtocolServer
- 启动Docker容器:
sudo docker compose up
- 构建并运行MCP服务器:
# 在新的终端窗口中
sudo docker exec -it modelcontextprotocolserver-application sh
cd /app
./mvnw clean install
./mvnw spring-boot:run
- 构建并运行客户端:
# 在另一个新的终端窗口中
docker exec -it modelcontextprotocolclient-application sh
./mvnw spring-boot:run
💻 使用示例
可用端点
WebFlux传输
GET /webflux/tools:列出所有可用工具。POST /webflux/calculate:使用计算器工具进行计算。
curl -X POST "http://localhost:8080/webflux/calculate?operation=add&a=5&b=3"
POST /webflux/weather:获取天气信息。
curl -X POST "http://localhost:8080/webflux/weather?location=San%20Francisco&format=celsius"
Stdio传输
GET /stdio/tools:列出所有可用工具。POST /stdio/calculate:使用计算器工具进行计算。
curl -X POST "http://localhost:8080/stdio/calculate?operation=add&a=5&b=3"
POST /stdio/weather:获取天气信息。
curl -X POST "http://localhost:8080/stdio/weather?location=San%20Francisco&format=celsius"
BraveSearch传输
GET /tools/bravesearch:列出可用工具。POST /bravesearch:执行网络搜索。
curl -X POST "http://localhost:8080/bravesearch?query=spring%20boot"
🔧 技术细节
配置
该应用程序支持多种传输配置:
- WebFlux SSE传输(默认):使用Server - Sent Events与服务器通信,服务器必须以WebFlux模式运行。
- Stdio传输:使用标准输入输出与服务器通信,会自动启动服务器JAR文件并以stdio模式运行。
- BraveSearch传输:使用BraveSearch API进行通信,需要在
.env文件中添加BRAVE_SEARCH_API_KEY,可从Brave Search API获取API密钥。 - 天气API:用于WebFlux和Stdio传输的天气信息查询,需要在
.env文件中添加WEATHER_API_KEY,可从WeatherAPI获取API密钥。
Scan to join WeChat group