feat(go-api): migrate MCP server detail and download API to Go (#16113)

### What problem does this PR solve?

- Migrated MCP server detail and export (download) API from Python to
Go.
- Registered route: `GET /api/v1/mcp/servers/:mcp_id` (supporting
`?mode=download` query parameter).
This commit is contained in:
Hz_
2026-06-18 11:09:22 +08:00
committed by GitHub
parent f59332bc37
commit 69dbc44983
5 changed files with 206 additions and 2 deletions

View File

@@ -139,6 +139,56 @@ func (h *MCPHandler) ListMCPServers(c *gin.Context) {
})
}
func (h *MCPHandler) GetMCPServer(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
mcpID := c.Param("mcp_id")
if c.Query("mode") == "download" {
result, code, err := h.mcpService.ExportMCPServer(user.ID, mcpID)
if err != nil {
mcpDetailError(c, code, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"message": "success",
"data": result,
})
return
}
result, code, err := h.mcpService.GetMCPServer(user.ID, mcpID)
if err != nil {
mcpDetailError(c, code, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"message": "success",
"data": newMCPServerResponse(result),
})
}
func mcpDetailError(c *gin.Context, code common.ErrorCode, err error) {
if code == common.CodeDataError {
c.JSON(http.StatusOK, gin.H{
"code": code,
"message": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeExceptionError,
"message": err.Error(),
"data": nil,
})
}
// UpdateMCPServer updates an MCP server for the current user.
func (h *MCPHandler) UpdateMCPServer(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)

View File

@@ -19,9 +19,15 @@ package handler
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
"ragflow/internal/common"
"ragflow/internal/entity"
)
@@ -69,3 +75,28 @@ func TestNewMCPServerResponsePreservesNullDescriptionAndFormatsDates(t *testing.
t.Fatalf("payload %s includes timezone in date fields", payload)
}
}
func TestMCPDetailDataErrorOmitsDataFieldLikePython(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
c, _ := gin.CreateTestContext(recorder)
mcpDetailError(c, common.CodeDataError, errors.New("Cannot find MCP server mcp-id for user user-id"))
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusOK)
}
var body map[string]interface{}
if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
t.Fatalf("unmarshal body: %v", err)
}
if body["code"] != float64(common.CodeDataError) {
t.Fatalf("code = %v, want %d", body["code"], common.CodeDataError)
}
if body["message"] != "Cannot find MCP server mcp-id for user user-id" {
t.Fatalf("message = %v", body["message"])
}
if _, ok := body["data"]; ok {
t.Fatalf("body unexpectedly contains data field: %s", recorder.Body.String())
}
}