refactor: 将 MCP 配置和代码改为符合 Spring AI 标准规范
- 修改 application.properties 使用标准属性路径 spring.ai.mcp.client.stdio.connections - 将 args 参数格式从字符串改为数组格式 args[0] - 移除手动的 MCP 进程启动和 JSON-RPC 实现(60+ 行代码) - 使用 Spring AI 自动配置的 SyncMcpToolCallbackProvider 注入 MCP 工具 - 修复风险成本数值范围(1.2 ~ 2.5)与文档描述一致 - 优化 SKILL.md 描述,明确必须先使用主定价 Agent 参考文档: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs.html Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0260bc35cf
commit
7141a3f72c
4 files changed
+11
-125
No files matched your search
@@ -27,8 +27,8 @@ const server = new Server(
|
|||||||
|
|
||||||
// 生成随机风险成本(1.2 ~ 2.5)
|
// 生成随机风险成本(1.2 ~ 2.5)
|
||||||
function generateRiskCost() {
|
function generateRiskCost() {
|
||||||
const min = 4.2;
|
const min = 1.2;
|
||||||
const max = 7.5;
|
const max = 2.5;
|
||||||
const cost = Math.random() * (max - min) + min;
|
const cost = Math.random() * (max - min) + min;
|
||||||
return parseFloat(cost.toFixed(4)); // 保留4位小数
|
return parseFloat(cost.toFixed(4)); // 保留4位小数
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +1,17 @@
|
|||||||
package org.springaicommunity.skills;
|
package org.springaicommunity.skills;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.springaicommunity.agent.tools.BraveWebSearchTool;
|
|
||||||
import org.springaicommunity.agent.tools.FileSystemTools;
|
import org.springaicommunity.agent.tools.FileSystemTools;
|
||||||
import org.springaicommunity.agent.tools.ShellTools;
|
import org.springaicommunity.agent.tools.ShellTools;
|
||||||
import org.springaicommunity.agent.tools.SkillsTool;
|
import org.springaicommunity.agent.tools.SkillsTool;
|
||||||
|
|
||||||
import org.springaicommunity.mcp.annotation.McpTool;
|
|
||||||
import org.springframework.ai.chat.client.ChatClient;
|
import org.springframework.ai.chat.client.ChatClient;
|
||||||
import org.springframework.ai.chat.client.advisor.ToolCallAdvisor;
|
import org.springframework.ai.chat.client.advisor.ToolCallAdvisor;
|
||||||
import org.springframework.ai.chat.memory.ChatMemory;
|
import org.springframework.ai.chat.memory.ChatMemory;
|
||||||
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
|
||||||
import org.springframework.ai.chat.model.ChatModel;
|
import org.springframework.ai.chat.model.ChatModel;
|
||||||
import org.springframework.ai.tool.function.FunctionToolCallback;
|
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
@@ -28,16 +20,6 @@ import org.springframework.context.annotation.Bean;
|
|||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonClassDescription;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
||||||
|
|
||||||
import io.modelcontextprotocol.client.McpClient;
|
|
||||||
import io.modelcontextprotocol.client.transport.StdioClientTransport;
|
|
||||||
import io.modelcontextprotocol.client.transport.ServerParameters;
|
|
||||||
|
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@Configuration
|
@Configuration
|
||||||
@@ -81,67 +63,6 @@ public class Application {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* MCP 工具回调 - 获取客户风险成本
|
|
||||||
*/
|
|
||||||
@Bean
|
|
||||||
FunctionToolCallback riskCostToolCallback(
|
|
||||||
@Value("${spring.ai.mcp.client.risk-cost.command}") String command,
|
|
||||||
@Value("${spring.ai.mcp.client.risk-cost.args}") String args) {
|
|
||||||
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
|
||||||
|
|
||||||
return FunctionToolCallback.<GetCustomerRiskCostRequest, String>builder("get_customer_risk_cost", (request) -> {
|
|
||||||
try {
|
|
||||||
// 启动 MCP 服务器进程
|
|
||||||
ProcessBuilder pb = new ProcessBuilder(command, args);
|
|
||||||
pb.redirectErrorStream(false);
|
|
||||||
Process process = pb.start();
|
|
||||||
|
|
||||||
// 发送 JSON-RPC 请求
|
|
||||||
Map<String, Object> rpcRequest = Map.of(
|
|
||||||
"jsonrpc", "2.0",
|
|
||||||
"id", UUID.randomUUID().toString(),
|
|
||||||
"method", "tools/call",
|
|
||||||
"params", Map.of(
|
|
||||||
"name", "get_customer_risk_cost",
|
|
||||||
"arguments", Map.of("company_name", request.getCompanyName())
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
String requestJson = objectMapper.writeValueAsString(rpcRequest);
|
|
||||||
|
|
||||||
// 发送请求并读取响应
|
|
||||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
|
|
||||||
|
|
||||||
writer.write(requestJson);
|
|
||||||
writer.newLine();
|
|
||||||
writer.flush();
|
|
||||||
|
|
||||||
String responseLine = reader.readLine();
|
|
||||||
if (responseLine != null) {
|
|
||||||
JsonNode responseNode = objectMapper.readTree(responseLine);
|
|
||||||
if (responseNode.has("result")) {
|
|
||||||
JsonNode content = responseNode.get("result").get("content");
|
|
||||||
if (content.isArray() && content.size() > 0) {
|
|
||||||
return content.get(0).get("text").asText();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
process.destroyForcibly();
|
|
||||||
return "获取风险成本失败: 无响应";
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
return "获取风险成本失败: " + e.getMessage();
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.description("获取客户的风险成本率(%)。根据客户企业名称返回该客户的风险成本率,范围在 1.2% 到 2.5% 之间。用于定价计算、风险评估等场景。")
|
|
||||||
.inputType(GetCustomerRiskCostRequest.class)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用于非流式响应的 ChatClient.Builder (支持工具调用)
|
* 用于非流式响应的 ChatClient.Builder (支持工具调用)
|
||||||
@@ -149,24 +70,10 @@ public class Application {
|
|||||||
@Bean("chatClientBuilder")
|
@Bean("chatClientBuilder")
|
||||||
ChatClient.Builder chatClientBuilder(ChatModel chatModel,
|
ChatClient.Builder chatClientBuilder(ChatModel chatModel,
|
||||||
@Value("${agent.skills.dirs:Unknown}") List<Resource> agentSkillsDirs,
|
@Value("${agent.skills.dirs:Unknown}") List<Resource> agentSkillsDirs,
|
||||||
FunctionToolCallback riskCostToolCallback) {
|
SyncMcpToolCallbackProvider mcpToolCallbackProvider) {
|
||||||
|
|
||||||
// @formatter:off
|
// @formatter:off
|
||||||
return ChatClient.builder(chatModel)
|
return ChatClient.builder(chatModel)
|
||||||
.defaultSystem("""
|
|
||||||
You are a helpful AI assistant with access to various tools and skills.
|
|
||||||
|
|
||||||
CRITICAL RULES:
|
|
||||||
1. When user mentions "定价" or "pricing", you MUST call the Skill tool with command "客户定价"
|
|
||||||
|
|
||||||
Available tools:
|
|
||||||
- Skill: For business tasks like pricing, rating, etc.
|
|
||||||
- MCPTool: For getting customer risk cost rate (获取客户风险成本率)
|
|
||||||
- FileSystemTools: For file operations (read, write, list)
|
|
||||||
- ShellTools: For executing shell commands
|
|
||||||
|
|
||||||
Remember: USE TOOLS FIRST, ask questions later!
|
|
||||||
""")
|
|
||||||
|
|
||||||
// Skills tool
|
// Skills tool
|
||||||
.defaultToolCallbacks(SkillsTool.builder().addSkillsResources(agentSkillsDirs).build())
|
.defaultToolCallbacks(SkillsTool.builder().addSkillsResources(agentSkillsDirs).build())
|
||||||
@@ -178,8 +85,8 @@ public class Application {
|
|||||||
// Read, Write and Edit files tool
|
// Read, Write and Edit files tool
|
||||||
FileSystemTools.builder().build())// built-in file system tools
|
FileSystemTools.builder().build())// built-in file system tools
|
||||||
|
|
||||||
// MCP tools
|
// MCP tools (通过 Spring AI 自动配置注入)
|
||||||
.defaultToolCallbacks(riskCostToolCallback)
|
.defaultToolCallbacks(mcpToolCallbackProvider.getToolCallbacks())
|
||||||
|
|
||||||
.defaultAdvisors(
|
.defaultAdvisors(
|
||||||
// Tool Calling advisor
|
// Tool Calling advisor
|
||||||
@@ -220,26 +127,4 @@ public class Application {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取客户风险成本请求
|
|
||||||
*/
|
|
||||||
@JsonClassDescription("获取客户的风险成本率(%)。根据客户企业名称返回该客户的风险成本率,范围在 1.2% 到 2.5% 之间。用于定价计算、风险评估等场景。")
|
|
||||||
public static class GetCustomerRiskCostRequest {
|
|
||||||
|
|
||||||
@JsonProperty(required = true, value = "company_name")
|
|
||||||
@JsonPropertyDescription("客户的企业名称,例如:'某某科技有限公司'、'测试企业'等")
|
|
||||||
private String companyName;
|
|
||||||
|
|
||||||
public GetCustomerRiskCostRequest() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getCompanyName() {
|
|
||||||
return companyName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCompanyName(String companyName) {
|
|
||||||
this.companyName = companyName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
---
|
---
|
||||||
name: 客户定价
|
name: 客户定价
|
||||||
description: 微众银行科创贷主定价Agent,负责编排并串联整个定价流程。确保整个流程有序、可追溯,并与客户经理交互确认。
|
description: 微众银行科创贷主定价Agent,负责编排并串联整个定价流程。确保整个流程有序、可追溯,并与客户经理交互确认。在使用客户分层、定价方案选择、各种定价方案之前必须先使用该SKILL。
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
category: business
|
category: business
|
||||||
author: 微众银行
|
author: 微众银行
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ agent.model.knowledge.cutoff=2025-01-01
|
|||||||
# agent.model.knowledge.cutoff=Unknown
|
# agent.model.knowledge.cutoff=Unknown
|
||||||
|
|
||||||
## MCP Server Configuration
|
## MCP Server Configuration
|
||||||
|
# 参考文档: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs.html
|
||||||
|
|
||||||
# 客户风险成本 MCP 服务器
|
# 客户风险成本 MCP 服务器 (STDIO 方式)
|
||||||
spring.ai.mcp.client.risk-cost.command=node
|
spring.ai.mcp.client.stdio.connections.risk-cost.command=node
|
||||||
spring.ai.mcp.client.risk-cost.args=D:/project/spring-ai-skill/mcp-servers/risk-cost-server/index.js
|
spring.ai.mcp.client.stdio.connections.risk-cost.args[0]=D:/project/spring-ai-skill/mcp-servers/risk-cost-server/index.js
|
||||||
Reference in new issue
Block a user