diff --git a/mcp-servers/risk-cost-server/index.js b/mcp-servers/risk-cost-server/index.js index 7d61870..1097240 100644 --- a/mcp-servers/risk-cost-server/index.js +++ b/mcp-servers/risk-cost-server/index.js @@ -27,8 +27,8 @@ const server = new Server( // 生成随机风险成本(1.2 ~ 2.5) function generateRiskCost() { - const min = 4.2; - const max = 7.5; + const min = 1.2; + const max = 2.5; const cost = Math.random() * (max - min) + min; return parseFloat(cost.toFixed(4)); // 保留4位小数 } diff --git a/src/main/java/org/springaicommunity/skills/Application.java b/src/main/java/org/springaicommunity/skills/Application.java index e88f544..a367195 100644 --- a/src/main/java/org/springaicommunity/skills/Application.java +++ b/src/main/java/org/springaicommunity/skills/Application.java @@ -1,25 +1,17 @@ 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.Map; -import java.util.UUID; -import org.springaicommunity.agent.tools.BraveWebSearchTool; import org.springaicommunity.agent.tools.FileSystemTools; import org.springaicommunity.agent.tools.ShellTools; 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.advisor.ToolCallAdvisor; import org.springframework.ai.chat.memory.ChatMemory; import org.springframework.ai.chat.memory.MessageWindowChatMemory; 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.Value; import org.springframework.boot.SpringApplication; @@ -28,16 +20,6 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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 @Configuration @@ -81,67 +63,6 @@ public class Application { .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.builder("get_customer_risk_cost", (request) -> { - try { - // 启动 MCP 服务器进程 - ProcessBuilder pb = new ProcessBuilder(command, args); - pb.redirectErrorStream(false); - Process process = pb.start(); - - // 发送 JSON-RPC 请求 - Map 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 (支持工具调用) @@ -149,24 +70,10 @@ public class Application { @Bean("chatClientBuilder") ChatClient.Builder chatClientBuilder(ChatModel chatModel, @Value("${agent.skills.dirs:Unknown}") List agentSkillsDirs, - FunctionToolCallback riskCostToolCallback) { + SyncMcpToolCallbackProvider mcpToolCallbackProvider) { // @formatter:off 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 .defaultToolCallbacks(SkillsTool.builder().addSkillsResources(agentSkillsDirs).build()) @@ -178,8 +85,8 @@ public class Application { // Read, Write and Edit files tool FileSystemTools.builder().build())// built-in file system tools - // MCP tools - .defaultToolCallbacks(riskCostToolCallback) + // MCP tools (通过 Spring AI 自动配置注入) + .defaultToolCallbacks(mcpToolCallbackProvider.getToolCallbacks()) .defaultAdvisors( // Tool Calling advisor @@ -220,26 +127,4 @@ public class Application { .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; - } - } - } diff --git a/src/main/resources/.claude/skills/pricing/SKILL.md b/src/main/resources/.claude/skills/pricing/SKILL.md index 0264f80..cfa0fea 100644 --- a/src/main/resources/.claude/skills/pricing/SKILL.md +++ b/src/main/resources/.claude/skills/pricing/SKILL.md @@ -1,6 +1,6 @@ --- name: 客户定价 -description: 微众银行科创贷主定价Agent,负责编排并串联整个定价流程。确保整个流程有序、可追溯,并与客户经理交互确认。 +description: 微众银行科创贷主定价Agent,负责编排并串联整个定价流程。确保整个流程有序、可追溯,并与客户经理交互确认。在使用客户分层、定价方案选择、各种定价方案之前必须先使用该SKILL。 version: 1.0.0 category: business author: 微众银行 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3eb1aaf..c27c06b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -35,7 +35,8 @@ agent.model.knowledge.cutoff=2025-01-01 # agent.model.knowledge.cutoff=Unknown ## MCP Server Configuration +# 参考文档: https://docs.spring.io/spring-ai/reference/api/mcp/mcp-client-boot-starter-docs.html -# 客户风险成本 MCP 服务器 -spring.ai.mcp.client.risk-cost.command=node -spring.ai.mcp.client.risk-cost.args=D:/project/spring-ai-skill/mcp-servers/risk-cost-server/index.js +# 客户风险成本 MCP 服务器 (STDIO 方式) +spring.ai.mcp.client.stdio.connections.risk-cost.command=node +spring.ai.mcp.client.stdio.connections.risk-cost.args[0]=D:/project/spring-ai-skill/mcp-servers/risk-cost-server/index.js