11package system
22
33import (
4- "fmt"
54 "github.com/flipped-aurora/gin-vue-admin/server/global"
5+ mcpTool "github.com/flipped-aurora/gin-vue-admin/server/mcp"
66 "github.com/flipped-aurora/gin-vue-admin/server/mcp/client"
77 "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
88 "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
99 "github.com/gin-gonic/gin"
1010 "github.com/mark3labs/mcp-go/mcp"
1111)
1212
13- // Create
14- // @Tags mcp
15- // @Summary 自动McpTool
16- // @Security ApiKeyAuth
17- // @accept application/json
18- // @Produce application/json
19- // @Param data body request.AutoMcpTool true "创建自动代码"
20- // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
21- // @Router /autoCode/mcp [post]
2213func (a * AutoCodeTemplateApi ) MCP (c * gin.Context ) {
2314 var info request.AutoMcpTool
24- err := c .ShouldBindJSON (& info )
25- if err != nil {
15+ if err := c .ShouldBindJSON (& info ); err != nil {
2616 response .FailWithMessage (err .Error (), c )
2717 return
2818 }
@@ -36,109 +26,88 @@ func (a *AutoCodeTemplateApi) MCP(c *gin.Context) {
3626 response .OkWithMessage ("创建成功,MCP Tool路径:" + toolFilePath , c )
3727}
3828
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]
4829func (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 )
30+ baseURL := mcpTool .ResolveMCPServiceURL ()
31+ testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , global .GVA_CONFIG .MCP .Name , incomingMCPHeaders (c ))
32+ if err != nil {
33+ response .FailWithMessage ("连接MCP服务失败:" + err .Error (), c )
34+ return
35+ }
5336 defer testClient .Close ()
54- toolsRequest := mcp.ListToolsRequest {}
55-
56- list , err := testClient .ListTools (c .Request .Context (), toolsRequest )
5737
38+ list , err := testClient .ListTools (c .Request .Context (), mcp.ListToolsRequest {})
5839 if err != nil {
59- response .FailWithMessage ("创建失败" , c )
60- global .GVA_LOG .Error (err .Error ())
40+ response .FailWithMessage ("获取工具列表失败:" + err .Error (), c )
6141 return
6242 }
6343
44+ authHeader := mcpTool .ConfiguredAuthHeader ()
6445 mcpServerConfig := map [string ]interface {}{
6546 "mcpServers" : map [string ]interface {}{
66- global .GVA_CONFIG .MCP .Name : map [string ]string {
67- "url" : baseUrl ,
47+ global .GVA_CONFIG .MCP .Name : map [string ]interface {}{
48+ "url" : baseURL ,
49+ "headers" : map [string ]string {
50+ authHeader : "${YOUR_GVA_TOKEN}" ,
51+ },
6852 },
6953 },
7054 }
55+
7156 response .OkWithData (gin.H {
7257 "mcpServerConfig" : mcpServerConfig ,
7358 "list" : list ,
7459 }, c )
7560}
7661
77- // Create
78- // @Tags mcp
79- // @Summary 测试McpTool
80- // @Security ApiKeyAuth
81- // @accept application/json
82- // @Produce application/json
83- // @Param data body object true "调用MCP Tool的参数"
84- // @Success 200 {object} response.Response "{"success":true,"data":{},"msg":"测试成功"}"
85- // @Router /autoCode/mcpTest [post]
62+ func (a * AutoCodeTemplateApi ) MCPRoutes (c * gin.Context ) {
63+ response .OkWithData (gin.H {
64+ "routes" : global .GVA_ROUTERS ,
65+ }, c )
66+ }
67+
8668func (a * AutoCodeTemplateApi ) MCPTest (c * gin.Context ) {
87- // 定义接口请求结构
8869 var testRequest struct {
89- Name string `json:"name" binding:"required"` // 工具名称
90- Arguments map [string ]interface {} `json:"arguments" binding:"required"` // 工具参数
70+ Name string `json:"name" binding:"required"`
71+ Arguments map [string ]interface {} `json:"arguments" binding:"required"`
9172 }
92-
93- // 绑定JSON请求体
9473 if err := c .ShouldBindJSON (& testRequest ); err != nil {
9574 response .FailWithMessage ("参数解析失败:" + err .Error (), c )
9675 return
9776 }
9877
99- // 创建MCP客户端
100- baseUrl := fmt .Sprintf ("http://127.0.0.1:%d%s" , global .GVA_CONFIG .System .Addr , global .GVA_CONFIG .MCP .SSEPath )
101- testClient , err := client .NewClient (baseUrl , "testClient" , "v1.0.0" , global .GVA_CONFIG .MCP .Name )
78+ baseURL := mcpTool .ResolveMCPServiceURL ()
79+ testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , global .GVA_CONFIG .MCP .Name , incomingMCPHeaders (c ))
10280 if err != nil {
103- response .FailWithMessage ("创建MCP客户端失败 :" + err .Error (), c )
81+ response .FailWithMessage ("连接MCP服务失败 :" + err .Error (), c )
10482 return
10583 }
10684 defer testClient .Close ()
10785
108- ctx := c .Request .Context ()
109-
110- // 初始化MCP连接
111- initRequest := mcp.InitializeRequest {}
112- initRequest .Params .ProtocolVersion = mcp .LATEST_PROTOCOL_VERSION
113- initRequest .Params .ClientInfo = mcp.Implementation {
114- Name : "testClient" ,
115- Version : "v1.0.0" ,
116- }
117-
118- _ , err = testClient .Initialize (ctx , initRequest )
119- if err != nil {
120- response .FailWithMessage ("初始化MCP连接失败:" + err .Error (), c )
121- return
122- }
123-
124- // 构建工具调用请求
125- request := mcp.CallToolRequest {}
126- request .Params .Name = testRequest .Name
127- request .Params .Arguments = testRequest .Arguments
86+ callRequest := mcp.CallToolRequest {}
87+ callRequest .Params .Name = testRequest .Name
88+ callRequest .Params .Arguments = testRequest .Arguments
12889
129- // 调用工具
130- result , err := testClient .CallTool (ctx , request )
90+ result , err := testClient .CallTool (c .Request .Context (), callRequest )
13191 if err != nil {
13292 response .FailWithMessage ("工具调用失败:" + err .Error (), c )
13393 return
13494 }
135-
136- // 处理响应结果
13795 if len (result .Content ) == 0 {
13896 response .FailWithMessage ("工具未返回任何内容" , c )
13997 return
14098 }
14199
142- // 返回结果
143100 response .OkWithData (result .Content , c )
144101}
102+
103+ func incomingMCPHeaders (c * gin.Context ) map [string ]string {
104+ headerName := mcpTool .ConfiguredAuthHeader ()
105+ headerValue := c .GetHeader (headerName )
106+ if headerValue == "" {
107+ return nil
108+ }
109+
110+ return map [string ]string {
111+ headerName : headerValue ,
112+ }
113+ }
0 commit comments