11package system
22
33import (
4+ "strings"
5+
46 "github.com/flipped-aurora/gin-vue-admin/server/global"
57 mcpTool "github.com/flipped-aurora/gin-vue-admin/server/mcp"
68 "github.com/flipped-aurora/gin-vue-admin/server/mcp/client"
@@ -26,35 +28,69 @@ func (a *AutoCodeTemplateApi) MCP(c *gin.Context) {
2628 response .OkWithMessage ("创建成功,MCP Tool路径:" + toolFilePath , c )
2729}
2830
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+ }
37+
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+ }
47+
48+ response .OkWithDetailed (gin.H {
49+ "status" : status ,
50+ "mcpServerConfig" : buildMCPServerConfig (),
51+ }, "MCP独立服务已启动" , c )
52+ }
53+
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+ }
63+
64+ response .OkWithDetailed (gin.H {
65+ "status" : status ,
66+ "mcpServerConfig" : buildMCPServerConfig (),
67+ }, "MCP独立服务已停用" , c )
68+ }
69+
2970func (a * AutoCodeTemplateApi ) MCPList (c * gin.Context ) {
3071 baseURL := mcpTool .ResolveMCPServiceURL ()
31- testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , global . GVA_CONFIG . MCP . Name , incomingMCPHeaders (c ))
72+ testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , mcpServerName () , incomingMCPHeaders (c ))
3273 if err != nil {
33- response .FailWithMessage ("连接MCP服务失败:" + err .Error (), c )
74+ response .FailWithDetailed (gin.H {
75+ "status" : mcpTool .GetManagedStandaloneStatus (c .Request .Context ()),
76+ "mcpServerConfig" : buildMCPServerConfig (),
77+ }, "连接MCP服务失败:" + err .Error (), c )
3478 return
3579 }
3680 defer testClient .Close ()
3781
3882 list , err := testClient .ListTools (c .Request .Context (), mcp.ListToolsRequest {})
3983 if err != nil {
40- response .FailWithMessage ("获取工具列表失败:" + err .Error (), c )
84+ response .FailWithDetailed (gin.H {
85+ "status" : mcpTool .GetManagedStandaloneStatus (c .Request .Context ()),
86+ "mcpServerConfig" : buildMCPServerConfig (),
87+ }, "获取工具列表失败:" + err .Error (), c )
4188 return
4289 }
4390
44- authHeader := mcpTool .ConfiguredAuthHeader ()
45- mcpServerConfig := map [string ]interface {}{
46- "mcpServers" : map [string ]interface {}{
47- global .GVA_CONFIG .MCP .Name : map [string ]interface {}{
48- "url" : baseURL ,
49- "headers" : map [string ]string {
50- authHeader : "${YOUR_GVA_TOKEN}" ,
51- },
52- },
53- },
54- }
55-
5691 response .OkWithData (gin.H {
57- "mcpServerConfig" : mcpServerConfig ,
92+ "status" : mcpTool .GetManagedStandaloneStatus (c .Request .Context ()),
93+ "mcpServerConfig" : buildMCPServerConfig (),
5894 "list" : list ,
5995 }, c )
6096}
@@ -76,7 +112,7 @@ func (a *AutoCodeTemplateApi) MCPTest(c *gin.Context) {
76112 }
77113
78114 baseURL := mcpTool .ResolveMCPServiceURL ()
79- testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , global . GVA_CONFIG . MCP . Name , incomingMCPHeaders (c ))
115+ testClient , err := client .NewClient (baseURL , "testClient" , "v1.0.0" , mcpServerName () , incomingMCPHeaders (c ))
80116 if err != nil {
81117 response .FailWithMessage ("连接MCP服务失败:" + err .Error (), c )
82118 return
@@ -111,3 +147,27 @@ func incomingMCPHeaders(c *gin.Context) map[string]string {
111147 headerName : headerValue ,
112148 }
113149}
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