11package system
22
33import (
4- "fmt"
4+ "strings"
5+
56 "github.com/flipped-aurora/gin-vue-admin/server/global"
7+ mcpTool "github.com/flipped-aurora/gin-vue-admin/server/mcp"
68 "github.com/flipped-aurora/gin-vue-admin/server/mcp/client"
79 "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
810 "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
911 "github.com/gin-gonic/gin"
1012 "github.com/mark3labs/mcp-go/mcp"
1113)
1214
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]
2215func (a * AutoCodeTemplateApi ) MCP (c * gin.Context ) {
2316 var info request.AutoMcpTool
24- err := c .ShouldBindJSON (& info )
25- if err != nil {
17+ if err := c .ShouldBindJSON (& info ); err != nil {
2618 response .FailWithMessage (err .Error (), c )
2719 return
2820 }
@@ -36,109 +28,146 @@ func (a *AutoCodeTemplateApi) MCP(c *gin.Context) {
3628 response .OkWithMessage ("创建成功,MCP Tool路径:" + toolFilePath , c )
3729}
3830
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 ) {
31+ func (a * AutoCodeTemplateApi ) MCPStatus (c * gin.Context ) {
32+ response .OkWithData (gin.H {
33+ "status" : mcpTool .GetManagedStandaloneStatus (c .Request .Context ()),
34+ "mcpServerConfig" : buildMCPServerConfig (),
35+ }, c )
36+ }
4937
50- baseUrl := fmt .Sprintf ("http://127.0.0.1:%d%s" , global .GVA_CONFIG .System .Addr , global .GVA_CONFIG .MCP .SSEPath )
38+ func (a * AutoCodeTemplateApi ) MCPStart (c * gin.Context ) {
39+ status , err := mcpTool .StartManagedStandalone (c .Request .Context ())
40+ if err != nil {
41+ response .FailWithDetailed (gin.H {
42+ "status" : status ,
43+ "mcpServerConfig" : buildMCPServerConfig (),
44+ }, err .Error (), c )
45+ return
46+ }
5147
52- testClient , err := client .NewClient (baseUrl , "testClient" , "v1.0.0" , global .GVA_CONFIG .MCP .Name )
53- defer testClient .Close ()
54- toolsRequest := mcp.ListToolsRequest {}
48+ response .OkWithDetailed (gin.H {
49+ "status" : status ,
50+ "mcpServerConfig" : buildMCPServerConfig (),
51+ }, "MCP独立服务已启动" , c )
52+ }
5553
56- list , err := testClient .ListTools (c .Request .Context (), toolsRequest )
54+ func (a * AutoCodeTemplateApi ) MCPStop (c * gin.Context ) {
55+ status , err := mcpTool .StopManagedStandalone (c .Request .Context ())
56+ if err != nil {
57+ response .FailWithDetailed (gin.H {
58+ "status" : status ,
59+ "mcpServerConfig" : buildMCPServerConfig (),
60+ }, err .Error (), c )
61+ return
62+ }
5763
64+ response .OkWithDetailed (gin.H {
65+ "status" : status ,
66+ "mcpServerConfig" : buildMCPServerConfig (),
67+ }, "MCP独立服务已停用" , c )
68+ }
69+
70+ func (a * AutoCodeTemplateApi ) MCPList (c * gin.Context ) {
71+ baseURL := mcpTool .ResolveMCPServiceURL ()
72+ testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , mcpServerName (), incomingMCPHeaders (c ))
5873 if err != nil {
59- response .FailWithMessage ("创建失败" , c )
60- global .GVA_LOG .Error (err .Error ())
74+ response .FailWithDetailed (gin.H {
75+ "status" : mcpTool .GetManagedStandaloneStatus (c .Request .Context ()),
76+ "mcpServerConfig" : buildMCPServerConfig (),
77+ }, "连接MCP服务失败:" + err .Error (), c )
6178 return
6279 }
80+ defer testClient .Close ()
6381
64- mcpServerConfig := map [string ]interface {}{
65- "mcpServers" : map [string ]interface {}{
66- global .GVA_CONFIG .MCP .Name : map [string ]string {
67- "url" : baseUrl ,
68- },
69- },
82+ list , err := testClient .ListTools (c .Request .Context (), mcp.ListToolsRequest {})
83+ if err != nil {
84+ response .FailWithDetailed (gin.H {
85+ "status" : mcpTool .GetManagedStandaloneStatus (c .Request .Context ()),
86+ "mcpServerConfig" : buildMCPServerConfig (),
87+ }, "获取工具列表失败:" + err .Error (), c )
88+ return
7089 }
90+
7191 response .OkWithData (gin.H {
72- "mcpServerConfig" : mcpServerConfig ,
92+ "status" : mcpTool .GetManagedStandaloneStatus (c .Request .Context ()),
93+ "mcpServerConfig" : buildMCPServerConfig (),
7394 "list" : list ,
7495 }, c )
7596}
7697
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]
98+ func (a * AutoCodeTemplateApi ) MCPRoutes (c * gin.Context ) {
99+ response .OkWithData (gin.H {
100+ "routes" : global .GVA_ROUTERS ,
101+ }, c )
102+ }
103+
86104func (a * AutoCodeTemplateApi ) MCPTest (c * gin.Context ) {
87- // 定义接口请求结构
88105 var testRequest struct {
89- Name string `json:"name" binding:"required"` // 工具名称
90- Arguments map [string ]interface {} `json:"arguments" binding:"required"` // 工具参数
106+ Name string `json:"name" binding:"required"`
107+ Arguments map [string ]interface {} `json:"arguments" binding:"required"`
91108 }
92-
93- // 绑定JSON请求体
94109 if err := c .ShouldBindJSON (& testRequest ); err != nil {
95110 response .FailWithMessage ("参数解析失败:" + err .Error (), c )
96111 return
97112 }
98113
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 )
114+ baseURL := mcpTool .ResolveMCPServiceURL ()
115+ testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , mcpServerName (), incomingMCPHeaders (c ))
102116 if err != nil {
103- response .FailWithMessage ("创建MCP客户端失败 :" + err .Error (), c )
117+ response .FailWithMessage ("连接MCP服务失败 :" + err .Error (), c )
104118 return
105119 }
106120 defer testClient .Close ()
107121
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
122+ callRequest := mcp.CallToolRequest {}
123+ callRequest .Params .Name = testRequest .Name
124+ callRequest .Params .Arguments = testRequest .Arguments
128125
129- // 调用工具
130- result , err := testClient .CallTool (ctx , request )
126+ result , err := testClient .CallTool (c .Request .Context (), callRequest )
131127 if err != nil {
132128 response .FailWithMessage ("工具调用失败:" + err .Error (), c )
133129 return
134130 }
135-
136- // 处理响应结果
137131 if len (result .Content ) == 0 {
138132 response .FailWithMessage ("工具未返回任何内容" , c )
139133 return
140134 }
141135
142- // 返回结果
143136 response .OkWithData (result .Content , c )
144137}
138+
139+ func incomingMCPHeaders (c * gin.Context ) map [string ]string {
140+ headerName := mcpTool .ConfiguredAuthHeader ()
141+ headerValue := c .GetHeader (headerName )
142+ if headerValue == "" {
143+ return nil
144+ }
145+
146+ return map [string ]string {
147+ headerName : headerValue ,
148+ }
149+ }
150+
151+ func buildMCPServerConfig () map [string ]interface {} {
152+ baseURL := mcpTool .ResolveMCPServiceURL ()
153+ authHeader := mcpTool .ConfiguredAuthHeader ()
154+ serverName := mcpServerName ()
155+
156+ return map [string ]interface {}{
157+ "mcpServers" : map [string ]interface {}{
158+ serverName : map [string ]interface {}{
159+ "url" : baseURL ,
160+ "headers" : map [string ]string {
161+ authHeader : "${YOUR_GVA_TOKEN}" ,
162+ },
163+ },
164+ },
165+ }
166+ }
167+
168+ func mcpServerName () string {
169+ if name := strings .TrimSpace (global .GVA_CONFIG .MCP .Name ); name != "" {
170+ return name
171+ }
172+ return "gva"
173+ }
0 commit comments