DateMcpTool.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.shkpr.service.mcpcenterservice.mcptool;
  2. import com.global.base.tools.FastJsonUtil;
  3. import com.shkpr.service.mcpcenterservice.dto.McpAuthUser;
  4. import com.shkpr.service.mcpcenterservice.globalmgr.McpAuthContextMgr;
  5. import io.modelcontextprotocol.spec.McpSchema.CallToolResult;
  6. import io.modelcontextprotocol.spec.McpSchema.TextContent;
  7. import org.springaicommunity.mcp.annotation.McpTool;
  8. import org.springaicommunity.mcp.annotation.McpToolParam;
  9. import org.springframework.stereotype.Service;
  10. import java.time.LocalDate;
  11. import java.util.List;
  12. /**
  13. * 注:1) 使用@McpTool的工具方法,会被自动扫描注册,前提是所在类型添加了@Service或@Component注解
  14. * 2) 当@McpTool的工具方法返回的是CallToolResult结构时,服务不会在进行封装,而是直接返回
  15. * 3) 当@McpTool的工具方法返回的是非CallToolResult结构时,服务会自动封装成CallToolResult结构后再返回
  16. */
  17. @Service
  18. public class DateMcpTool {
  19. /**
  20. *
  21. * @param days
  22. * @return
  23. */
  24. @McpTool(name = "addDays", description = "Adds days to the current date")
  25. public CallToolResult addDays(@McpToolParam(description = "The number of days to add", required = true) Integer days) {
  26. if (days == null){
  27. return new CallToolResult(
  28. List.of(new TextContent("缺少必要参数days,无法完成存储操作。")),
  29. true
  30. );
  31. }
  32. if (days < 0){
  33. return new CallToolResult(
  34. List.of(new TextContent("参数days不能小于0,请重新输入。")),
  35. true
  36. );
  37. }
  38. return new CallToolResult(
  39. List.of(new TextContent(LocalDate.now().plusDays(days).toString())),
  40. false
  41. );
  42. }
  43. @McpTool(name = "subtractDays", description = "Subtracts days from the current date")
  44. public CallToolResult subtractDays(@McpToolParam(description = "The number of days to subtract", required = true) Integer days) {
  45. if (days == null){
  46. return new CallToolResult(
  47. List.of(new TextContent("缺少必要参数days,无法完成存储操作。")),
  48. true
  49. );
  50. }
  51. if (days < 0){
  52. return new CallToolResult(
  53. List.of(new TextContent("参数days不能小于0,请重新输入。")),
  54. true
  55. );
  56. }
  57. return new CallToolResult(
  58. List.of(new TextContent(LocalDate.now().minusDays(days).toString())),
  59. false
  60. );
  61. }
  62. //服务会自动将string封装成CallToolResult后再返回给前端
  63. @McpTool(name = "today", description = "Return current date")
  64. public String today() {
  65. return LocalDate.now().toString();
  66. }
  67. @McpTool(name = "myUser", description = "Returns current user info.")
  68. public CallToolResult myUser() {
  69. McpAuthUser user = McpAuthContextMgr.getCurrentUser();
  70. if (user == null) {
  71. return new CallToolResult(
  72. List.of(new TextContent("未检查到Token令牌")),
  73. true
  74. );
  75. }
  76. return new CallToolResult(
  77. List.of(new TextContent(FastJsonUtil.toJSON(user))),
  78. false
  79. );
  80. }
  81. }