| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package com.shkpr.service.mcpcenterservice.mcptool;
- import com.global.base.tools.FastJsonUtil;
- import com.shkpr.service.mcpcenterservice.dto.McpAuthUser;
- import com.shkpr.service.mcpcenterservice.globalmgr.McpAuthContextMgr;
- import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
- import io.modelcontextprotocol.spec.McpSchema.TextContent;
- import org.springaicommunity.mcp.annotation.McpTool;
- import org.springaicommunity.mcp.annotation.McpToolParam;
- import org.springframework.stereotype.Service;
- import java.time.LocalDate;
- import java.util.List;
- /**
- * 注:1) 使用@McpTool的工具方法,会被自动扫描注册,前提是所在类型添加了@Service或@Component注解
- * 2) 当@McpTool的工具方法返回的是CallToolResult结构时,服务不会在进行封装,而是直接返回
- * 3) 当@McpTool的工具方法返回的是非CallToolResult结构时,服务会自动封装成CallToolResult结构后再返回
- */
- @Service
- public class DateMcpTool {
- /**
- *
- * @param days
- * @return
- */
- @McpTool(name = "addDays", description = "Adds days to the current date")
- public CallToolResult addDays(@McpToolParam(description = "The number of days to add", required = true) Integer days) {
- if (days == null){
- return new CallToolResult(
- List.of(new TextContent("缺少必要参数days,无法完成存储操作。")),
- true
- );
- }
- if (days < 0){
- return new CallToolResult(
- List.of(new TextContent("参数days不能小于0,请重新输入。")),
- true
- );
- }
- return new CallToolResult(
- List.of(new TextContent(LocalDate.now().plusDays(days).toString())),
- false
- );
- }
- @McpTool(name = "subtractDays", description = "Subtracts days from the current date")
- public CallToolResult subtractDays(@McpToolParam(description = "The number of days to subtract", required = true) Integer days) {
- if (days == null){
- return new CallToolResult(
- List.of(new TextContent("缺少必要参数days,无法完成存储操作。")),
- true
- );
- }
- if (days < 0){
- return new CallToolResult(
- List.of(new TextContent("参数days不能小于0,请重新输入。")),
- true
- );
- }
- return new CallToolResult(
- List.of(new TextContent(LocalDate.now().minusDays(days).toString())),
- false
- );
- }
- //服务会自动将string封装成CallToolResult后再返回给前端
- @McpTool(name = "today", description = "Return current date")
- public String today() {
- return LocalDate.now().toString();
- }
- @McpTool(name = "myUser", description = "Returns current user info.")
- public CallToolResult myUser() {
- McpAuthUser user = McpAuthContextMgr.getCurrentUser();
- if (user == null) {
- return new CallToolResult(
- List.of(new TextContent("未检查到Token令牌")),
- true
- );
- }
- return new CallToolResult(
- List.of(new TextContent(FastJsonUtil.toJSON(user))),
- false
- );
- }
- }
|