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