使用说明
带Spring AI的MCP客户端
一个Spring Boot应用程序,将OpenAI的GPT模型与模型上下文协议(MCP)集成在一起,提供具有工具调用功能的AI聊天功能。
🚀 特性
- OpenAI集成:利用GPT-4.1-nano进行智能聊天响应
- MCP客户端支持:连接到MCP服务器,使用自定义工具扩展AI功能
- 工具回调:通过MCP协议自动发现和执行工具
- RESTful API:用于无状态聊天交互的简单HTTP端点
- 可观测性:内置提示、完成和错误日志
- 现代堆栈:Spring Boot 3.5.10、Spring AI 1.1.2、Java 21
📋 先决条件
- Java 21或更高版本
- Maven 3.9+
- OpenAI API密钥
- 正在运行的MCP服务器
http://localhost:8081/(或自行配置)
🛠️ 安装
-
克隆仓库
git clone <repository-url> cd mcp-client -
设置环境变量
export OPENAI_API_KEY=your_openai_api_key_here -
构建项目
./mvnw clean install
⚙️ 配置
应用程序通过以下方式配置 src/main/resources/application.yaml:
spring:
ai:
openai:
api-key: ${OPENAI_API_KEY}
chat:
options:
model: gpt-4.1-nano
temperature: 0.7
mcp:
client:
streamable-http:
connections:
server1:
url: http://localhost:8081/
关键配置选项
- OpenAI模型:默认值为
gpt-4.1-nano,可以更改为其他GPT模型 - 温度:设置为
0.7平衡创造力/决定论 - MCP服务器URL:配置运行MCP服务器的端点
🚦 运行应用程序
首先启动MCP服务器
重要:确保您的MCP服务器正在上运行 http://localhost:8081/ 在启动此应用程序之前。
如果您遇到错误:
Factory method 'mcpSyncClients' threw exception with message:
Client failed to initialize by explicit API call
这意味着无法访问MCP服务器。验证:
- MCP服务器正在运行
- URL在
application.yaml是正确的 - 不存在网络/防火墙问题
启动应用程序
./mvnw spring-boot:run
应用程序将于启动 http://localhost:8080
📡 API终点
聊天-无状态工具调用
端点: POST /v1/chat/stateless
请求:
curl -X POST http://localhost:8080/v1/chat/stateless \
-H "Content-Type: text/plain" \
-d "What is the weather like today?"
响应:
{
"conversationId": null,
"response": "The current weather is...",
"timeTaken": 1250
}
领域:
response:人工智能生成的响应timeTaken:处理请求所用的时间(毫秒)conversationId:当前未使用(保留用于未来的有状态对话)
🏗️ 项目结构
mcp-client/
├── src/
│ ├── main/
│ │ ├── java/com/navneet/mcp_client/
│ │ │ ├── McpClientApplication.java # Main Spring Boot application
│ │ │ ├── constants/
│ │ │ │ └── AgentConstants.java # System prompts and constants
│ │ │ ├── controller/
│ │ │ │ └── ChatController.java # REST API endpoints
│ │ │ ├── models/
│ │ │ │ ├── ChatRequest.java # Request DTOs
│ │ │ │ └── ChatResponse.java # Response DTOs
│ │ │ └── service/
│ │ │ ├── ChatService.java # Service interface
│ │ │ └── impl/
│ │ │ └── ChatServiceImpl.java # Chat service implementation
│ │ └── resources/
│ │ └── application.yaml # Application configuration
│ └── test/
│ └── java/com/navneet/mcp_client/
│ └── McpClientApplicationTests.java # Integration tests
├── pom.xml # Maven dependencies
└── README.md # This file
🔧 关键依赖
- 弹簧靴3.5.10:核心框架
- 春季AI 1.1.2:AI集成层
spring-ai-starter-mcp-client:MCP协议支持spring-ai-starter-model-openai:OpenAI集成@Tool注释支持:允许AI模型自动发现和调用工具
- 龙目:减少样板代码
- 春季网络:RESTful API支持
📚 服务组件
聊天服务
通过MCP工具回调支持处理人工智能驱动的聊天交互。
时间服务
提供时间和日期实用程序函数,作为AI模型的工具:
- 获取各种格式的当前时间/日期
- 在不同的时间表示(毫秒、秒、格式化字符串)之间进行转换
- 所有方法都带有注释
@Tool用于自动发现Spring AI
🐛 故障排除
MCP客户端初始化失败
错误: Factory method 'mcpSyncClients' threw exception
解决方案:
- 确保MCP服务器正在运行且可访问
- 在中检查URL
application.yaml - 验证与MCP服务器的网络连接
- 查看MCP服务器日志中的错误
OpenAI API错误
错误:身份验证或速率限制问题
解决方案:
- 验证
OPENAI_API_KEY环境变量设置正确 - 检查您的OpenAI帐户以获取API配额/信用
- 确保模型名称正确且可使用API密钥访问
工具回调不起作用
解决方案:
- 检查MCP服务器是否正确公开工具
- 查看应用程序日志中的工具发现消息
- 确保
toolcallback.enabled设置为true在配置中
📝 发展
运行测试
./mvnw test
生产大楼
./mvnw clean package
java -jar target/mcp-client-0.0.1-SNAPSHOT.jar
🤝 贡献
- 克隆该仓库
- 创建要素分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
📄 许可证
本项目根据 许可证 文件。
🔗 参考文献
👤 作者
纳瓦尼特·普拉巴卡尔
内置于❤️ 使用Spring AI和MCP
