Initial commit: Fintec AI Framework with Agent, RAG, and MCP modules
This commit is contained in:
BIN
mcp-server-demo/.DS_Store
vendored
Normal file
BIN
mcp-server-demo/.DS_Store
vendored
Normal file
Binary file not shown.
41
mcp-server-demo/pom.xml
Normal file
41
mcp-server-demo/pom.xml
Normal file
@@ -0,0 +1,41 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ccb.fintec</groupId>
|
||||
<artifactId>fintec-framework-parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>mcp-server-demo</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<!-- MCP Server Starter (已包含 WebFlux 和 OpenAI) -->
|
||||
<dependency>
|
||||
<groupId>com.ccb.fintec</groupId>
|
||||
<artifactId>fintec-framework-mcp-server-spring-boot-starter</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ccb.fintec.mcp.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* MCP Server Demo 应用
|
||||
*
|
||||
* 使用 Spring AI 的原生 @Tool 注解,框架自动添加监控指标。
|
||||
*
|
||||
* MCP Server 端点:
|
||||
* - SSE: http://localhost:8081/sse
|
||||
* - Message: http://localhost:8081/message
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class McpServerDemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(McpServerDemoApplication.class, args);
|
||||
|
||||
System.out.println("\n========================================");
|
||||
System.out.println(" MCP Server 已启动!");
|
||||
System.out.println(" SSE 端点: http://localhost:8081/sse");
|
||||
System.out.println(" Message 端点: http://localhost:8081/message");
|
||||
System.out.println("========================================\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.ccb.fintec.mcp.demo.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* MCP Server 演示控制器
|
||||
*
|
||||
* 核心优势:
|
||||
* 1. 使用 Spring AI 标准 @Tool 注解,无需学习新注解
|
||||
* 2. 自动扫描并注册为 MCP 工具,零配置
|
||||
* 3. 与 Spring AI 生态完全兼容
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/mcp")
|
||||
public class McpController {
|
||||
|
||||
/**
|
||||
* 健康检查
|
||||
*/
|
||||
@GetMapping("/health")
|
||||
public Map<String, Object> health() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("status", "UP");
|
||||
result.put("service", "MCP Server Demo");
|
||||
result.put("features", new String[]{
|
||||
"自动扫描 @Tool 注解",
|
||||
"零配置注册 MCP 工具",
|
||||
"使用 Spring AI 标准注解",
|
||||
"支持 SSE 传输协议"
|
||||
});
|
||||
result.put("endpoints", new String[]{
|
||||
"SSE: /mcp/sse (客户端连接)",
|
||||
"Message: /mcp/message (接收消息)"
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用说明
|
||||
*/
|
||||
@GetMapping("/usage")
|
||||
public Map<String, Object> usage() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("title", "MCP Server 使用指南");
|
||||
result.put("steps", new String[]{
|
||||
"1. 在任意 @Component/@Service Bean 的方法上添加 @Tool 注解",
|
||||
"2. 启动应用,工具自动注册为 MCP Server",
|
||||
"3. MCP Client 连接到 /mcp/sse 端点",
|
||||
"4. Client 可以 discover 和 call 这些工具"
|
||||
});
|
||||
result.put("example", """
|
||||
@Service
|
||||
public class MyTools {
|
||||
@Tool(description = "获取当前时间")
|
||||
public String getCurrentTime() {
|
||||
return LocalDateTime.now().toString();
|
||||
}
|
||||
}
|
||||
""");
|
||||
result.put("note", "相比之前,现在使用 Spring AI 标准的 @Tool 注解,而非自定义的 @McpTool");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.ccb.fintec.mcp.demo.tools;
|
||||
|
||||
import org.springframework.ai.tool.annotation.Tool;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* MCP 工具示例
|
||||
*
|
||||
* 这个类展示了如何定义 MCP 工具,AI 可以根据需要自动调用这些工具。
|
||||
* 每个带 @Tool 注解的方法都会被自动注册为 MCP 工具。
|
||||
*
|
||||
* 启动后,MCP Server 会在 /mcp 端点提供 SSE 服务,
|
||||
* MCP Client 可以连接并调用这些工具。
|
||||
*/
|
||||
@Component
|
||||
public class SimpleTools {
|
||||
|
||||
/**
|
||||
* 获取当前时间
|
||||
*/
|
||||
@Tool(description = "获取当前的日期和时间,格式为 yyyy-MM-dd HH:mm:ss")
|
||||
public String getCurrentTime() {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
return now.format(formatter);
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的计算器 - 加法
|
||||
*/
|
||||
@Tool(description = "计算两个数字的和。参数:a - 第一个数字,b - 第二个数字")
|
||||
public double add(double a, double b) {
|
||||
System.out.println("🔧 工具被调用: add(" + a + ", " + b + ")");
|
||||
return a + b;
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单的计算器 - 乘法
|
||||
*/
|
||||
@Tool(description = "计算两个数字的乘积。参数:a - 第一个数字,b - 第二个数字")
|
||||
public double multiply(double a, double b) {
|
||||
System.out.println("🔧 工具被调用: multiply(" + a + ", " + b + ")");
|
||||
return a * b;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息(模拟)
|
||||
*/
|
||||
@Tool(description = "获取指定用户的详细信息。参数:userId - 用户ID")
|
||||
public Map<String, Object> getUserInfo(String userId) {
|
||||
System.out.println("🔧 工具被调用: getUserInfo(" + userId + ")");
|
||||
|
||||
Map<String, Object> userInfo = new HashMap<>();
|
||||
userInfo.put("userId", userId);
|
||||
userInfo.put("name", "用户" + userId);
|
||||
userInfo.put("email", userId + "@example.com");
|
||||
userInfo.put("age", new Random().nextInt(30) + 20);
|
||||
userInfo.put("department", Arrays.asList("技术部", "产品部", "运营部").get(new Random().nextInt(3)));
|
||||
userInfo.put("registerDate", "2024-01-01");
|
||||
userInfo.put("status", "active");
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 字符串转换工具
|
||||
*/
|
||||
@Tool(description = "将字符串转换为大写。参数:text - 要转换的文本")
|
||||
public String toUpperCase(String text) {
|
||||
System.out.println("🔧 工具被调用: toUpperCase(" + text + ")");
|
||||
return text.toUpperCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 天气查询(模拟)
|
||||
*/
|
||||
@Tool(description = "查询指定城市的天气信息。参数:city - 城市名称")
|
||||
public Map<String, Object> getWeather(String city) {
|
||||
System.out.println("🔧 工具被调用: getWeather(" + city + ")");
|
||||
|
||||
Map<String, Object> weather = new HashMap<>();
|
||||
weather.put("city", city);
|
||||
weather.put("temperature", new Random().nextInt(20) + 15); // 15-35度
|
||||
weather.put("condition", Arrays.asList("晴", "多云", "小雨", "阴").get(new Random().nextInt(4)));
|
||||
weather.put("humidity", new Random().nextInt(40) + 40); // 40-80%
|
||||
weather.put("windSpeed", new Random().nextInt(20) + 5); // 5-25 km/h
|
||||
return weather;
|
||||
}
|
||||
}
|
||||
26
mcp-server-demo/src/main/resources/application.properties
Normal file
26
mcp-server-demo/src/main/resources/application.properties
Normal file
@@ -0,0 +1,26 @@
|
||||
# ============================================
|
||||
# 服务器配置
|
||||
# ============================================
|
||||
server.port=8081
|
||||
|
||||
# ============================================
|
||||
# MCP Server 配置
|
||||
# ============================================
|
||||
# MCP Server 名称和版本(用于标识)
|
||||
spring.ai.mcp.server.name=mcp-server-demo
|
||||
spring.ai.mcp.server.version=1.0.0
|
||||
# 注意:SSE 端点默认为 /sse,Message 端点默认为 /message
|
||||
#spring.ai.mcp.server.sse-message-endpoint=/mcp/message
|
||||
|
||||
# ============================================
|
||||
# MCP 模块配置
|
||||
# ============================================
|
||||
# MCP Server 业务标识(与 spring.ai.mcp.server.name 配合使用)
|
||||
fintec.ai.mcp.server-name=fintec-mcp-server
|
||||
fintec.ai.mcp.server-version=1.0.0
|
||||
|
||||
# ============================================
|
||||
# 日志配置
|
||||
# ============================================
|
||||
logging.level.root=INFO
|
||||
logging.level.com.ccb.fintec=DEBUG
|
||||
Reference in New Issue
Block a user