feat: 集成 MCP 工具支持
- 添加 spring-ai-starter-mcp-client 依赖 - 创建 Node.js MCP 服务器(risk-cost-server) - 在 Application.java 中配置 MCP 工具回调 - 添加 get_customer_risk_cost 工具(返回 1.2%-2.5% 的风险成本率) - 支持通过 FunctionToolCallback 调用 MCP 工具 - 更新系统提示,指导 AI 使用 MCP 工具 - 降级 Spring AI 到 1.1.2 版本 技术实现: - 使用 FunctionToolCallback 包装 MCP 工具 - 通过 Stdio 方式与 MCP 服务器通信 - JSON-RPC 协议调用工具 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c6f3eef29b
commit
581f731de3
10 files changed
+1045
-98
No files matched your search
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* 客户风险成本 MCP 服务器
|
||||
* 提供获取客户风险成本的工具(随机生成 1.2-2.5 之间的数值)
|
||||
*/
|
||||
|
||||
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||
import {
|
||||
CallToolRequestSchema,
|
||||
ListToolsRequestSchema,
|
||||
} from '@modelcontextprotocol/sdk/types.js';
|
||||
|
||||
// 创建服务器实例
|
||||
const server = new Server(
|
||||
{
|
||||
name: 'risk-cost-server',
|
||||
version: '1.0.0',
|
||||
},
|
||||
{
|
||||
capabilities: {
|
||||
tools: {},
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
// 生成随机风险成本(1.2 ~ 2.5)
|
||||
function generateRiskCost() {
|
||||
const min = 4.2;
|
||||
const max = 7.5;
|
||||
const cost = Math.random() * (max - min) + min;
|
||||
return parseFloat(cost.toFixed(4)); // 保留4位小数
|
||||
}
|
||||
|
||||
// 注册工具列表处理器
|
||||
server.setRequestHandler(ListToolsRequestSchema, async () => {
|
||||
return {
|
||||
tools: [
|
||||
{
|
||||
name: 'get_customer_risk_cost',
|
||||
description: '获取客户的风险成本率(%)。根据客户企业名称返回该客户的风险成本率,范围在 1.2% 到 2.5% 之间。',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
company_name: {
|
||||
type: 'string',
|
||||
description: '客户的企业名称',
|
||||
},
|
||||
},
|
||||
required: ['company_name'],
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
});
|
||||
|
||||
// 注册工具调用处理器
|
||||
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
||||
if (request.params.name === 'get_customer_risk_cost') {
|
||||
const companyName = request.params.arguments?.company_name;
|
||||
|
||||
if (!companyName) {
|
||||
throw new Error('缺少必需参数: company_name');
|
||||
}
|
||||
|
||||
// 生成风险成本
|
||||
const riskCost = generateRiskCost();
|
||||
|
||||
// 返回结果
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: 'text',
|
||||
text: JSON.stringify({
|
||||
company_name: companyName,
|
||||
risk_cost_rate: riskCost,
|
||||
unit: '%',
|
||||
description: `客户"${companyName}"的风险成本率为 ${riskCost}%`,
|
||||
timestamp: new Date().toISOString(),
|
||||
}, null, 2),
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error(`未知工具: ${request.params.name}`);
|
||||
});
|
||||
|
||||
// 启动服务器
|
||||
async function main() {
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error('客户风险成本 MCP 服务器已启动');
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error('服务器启动失败:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user