auto_code_mcp.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package system
  2. import (
  3. "fmt"
  4. "github.com/flipped-aurora/gin-vue-admin/server/global"
  5. "github.com/flipped-aurora/gin-vue-admin/server/mcp/client"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  8. "github.com/gin-gonic/gin"
  9. "github.com/mark3labs/mcp-go/mcp"
  10. )
  11. // Create
  12. // @Tags mcp
  13. // @Summary 自动McpTool
  14. // @Security ApiKeyAuth
  15. // @accept application/json
  16. // @Produce application/json
  17. // @Param data body request.AutoMcpTool true "创建自动代码"
  18. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  19. // @Router /autoCode/mcp [post]
  20. func (a *AutoCodeTemplateApi) MCP(c *gin.Context) {
  21. var info request.AutoMcpTool
  22. err := c.ShouldBindJSON(&info)
  23. if err != nil {
  24. response.FailWithMessage(err.Error(), c)
  25. return
  26. }
  27. toolFilePath, err := autoCodeTemplateService.CreateMcp(c.Request.Context(), info)
  28. if err != nil {
  29. response.FailWithMessage("创建失败", c)
  30. global.GVA_LOG.Error(err.Error())
  31. return
  32. }
  33. response.OkWithMessage("创建成功,MCP Tool路径:"+toolFilePath, c)
  34. }
  35. // Create
  36. // @Tags mcp
  37. // @Summary 自动McpTool
  38. // @Security ApiKeyAuth
  39. // @accept application/json
  40. // @Produce application/json
  41. // @Param data body request.AutoMcpTool true "创建自动代码"
  42. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  43. // @Router /autoCode/mcpList [post]
  44. func (a *AutoCodeTemplateApi) MCPList(c *gin.Context) {
  45. baseUrl := fmt.Sprintf("http://127.0.0.1:%d%s", global.GVA_CONFIG.System.Addr, global.GVA_CONFIG.MCP.SSEPath)
  46. testClient, err := client.NewClient(baseUrl, "testClient", "v1.0.0", global.GVA_CONFIG.MCP.Name)
  47. defer testClient.Close()
  48. toolsRequest := mcp.ListToolsRequest{}
  49. list, err := testClient.ListTools(c.Request.Context(), toolsRequest)
  50. if err != nil {
  51. response.FailWithMessage("创建失败", c)
  52. global.GVA_LOG.Error(err.Error())
  53. return
  54. }
  55. mcpServerConfig := map[string]interface{}{
  56. "mcpServers": map[string]interface{}{
  57. global.GVA_CONFIG.MCP.Name: map[string]string{
  58. "url": baseUrl,
  59. },
  60. },
  61. }
  62. response.OkWithData(gin.H{
  63. "mcpServerConfig": mcpServerConfig,
  64. "list": list,
  65. }, c)
  66. }
  67. // Create
  68. // @Tags mcp
  69. // @Summary 测试McpTool
  70. // @Security ApiKeyAuth
  71. // @accept application/json
  72. // @Produce application/json
  73. // @Param data body object true "调用MCP Tool的参数"
  74. // @Success 200 {object} response.Response "{"success":true,"data":{},"msg":"测试成功"}"
  75. // @Router /autoCode/mcpTest [post]
  76. func (a *AutoCodeTemplateApi) MCPTest(c *gin.Context) {
  77. // 定义接口请求结构
  78. var testRequest struct {
  79. Name string `json:"name" binding:"required"` // 工具名称
  80. Arguments map[string]interface{} `json:"arguments" binding:"required"` // 工具参数
  81. }
  82. // 绑定JSON请求体
  83. if err := c.ShouldBindJSON(&testRequest); err != nil {
  84. response.FailWithMessage("参数解析失败:"+err.Error(), c)
  85. return
  86. }
  87. // 创建MCP客户端
  88. baseUrl := fmt.Sprintf("http://127.0.0.1:%d%s", global.GVA_CONFIG.System.Addr, global.GVA_CONFIG.MCP.SSEPath)
  89. testClient, err := client.NewClient(baseUrl, "testClient", "v1.0.0", global.GVA_CONFIG.MCP.Name)
  90. if err != nil {
  91. response.FailWithMessage("创建MCP客户端失败:"+err.Error(), c)
  92. return
  93. }
  94. defer testClient.Close()
  95. ctx := c.Request.Context()
  96. // 初始化MCP连接
  97. initRequest := mcp.InitializeRequest{}
  98. initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
  99. initRequest.Params.ClientInfo = mcp.Implementation{
  100. Name: "testClient",
  101. Version: "v1.0.0",
  102. }
  103. _, err = testClient.Initialize(ctx, initRequest)
  104. if err != nil {
  105. response.FailWithMessage("初始化MCP连接失败:"+err.Error(), c)
  106. return
  107. }
  108. // 构建工具调用请求
  109. request := mcp.CallToolRequest{}
  110. request.Params.Name = testRequest.Name
  111. request.Params.Arguments = testRequest.Arguments
  112. // 调用工具
  113. result, err := testClient.CallTool(ctx, request)
  114. if err != nil {
  115. response.FailWithMessage("工具调用失败:"+err.Error(), c)
  116. return
  117. }
  118. // 处理响应结果
  119. if len(result.Content) == 0 {
  120. response.FailWithMessage("工具未返回任何内容", c)
  121. return
  122. }
  123. // 返回结果
  124. response.OkWithData(result.Content, c)
  125. }