mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-01 05:23:47 +08:00
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:
@@ -92,6 +92,18 @@ type MCPServerListItem struct {
|
||||
UpdateDate *string `json:"update_date"`
|
||||
}
|
||||
|
||||
type ExportMCPServer struct {
|
||||
Type string `json:"type"`
|
||||
URL string `json:"url"`
|
||||
Name string `json:"name"`
|
||||
AuthorizationToken interface{} `json:"authorization_token"`
|
||||
Tools interface{} `json:"tools"`
|
||||
}
|
||||
|
||||
type ExportMCPServerResponse struct {
|
||||
MCPServers map[string]ExportMCPServer `json:"mcpServers"`
|
||||
}
|
||||
|
||||
// ListMCPServersResponse is the response payload for listing MCP servers.
|
||||
type ListMCPServersResponse struct {
|
||||
MCPServers []*MCPServerListItem `json:"mcp_servers"`
|
||||
@@ -156,6 +168,55 @@ func (s *MCPService) CreateMCPServer(tenantID string, req CreateMCPServerRequest
|
||||
}, common.CodeSuccess, nil
|
||||
}
|
||||
|
||||
func (s *MCPService) GetMCPServer(tenantID, mcpID string) (*entity.MCPServer, common.ErrorCode, error) {
|
||||
server, err := s.mcpServerDAO.GetByIDAndTenant(mcpID, tenantID)
|
||||
if err != nil {
|
||||
if isMCPServerNotFound(err) {
|
||||
return nil, common.CodeDataError, mcpServerNotFoundError(mcpID, tenantID)
|
||||
}
|
||||
return nil, common.CodeServerError, fmt.Errorf("failed to get MCP server %s: %w", mcpID, err)
|
||||
}
|
||||
if server == nil {
|
||||
return nil, common.CodeDataError, mcpServerNotFoundError(mcpID, tenantID)
|
||||
}
|
||||
return server, common.CodeSuccess, nil
|
||||
}
|
||||
|
||||
func (s *MCPService) ExportMCPServer(userID, mcpID string) (*ExportMCPServerResponse, common.ErrorCode, error) {
|
||||
server, code, err := s.GetMCPServer(userID, mcpID)
|
||||
if err != nil {
|
||||
return nil, code, err
|
||||
}
|
||||
return newExportMCPServerResponse(server), common.CodeSuccess, nil
|
||||
}
|
||||
|
||||
func newExportMCPServerResponse(server *entity.MCPServer) *ExportMCPServerResponse {
|
||||
vars := server.Variables
|
||||
if vars == nil {
|
||||
vars = entity.JSONMap{}
|
||||
}
|
||||
|
||||
token := interface{}("")
|
||||
if value, ok := vars["authorization_token"]; ok {
|
||||
token = value
|
||||
}
|
||||
tools := vars["tools"]
|
||||
if tools == nil {
|
||||
tools = map[string]interface{}{}
|
||||
}
|
||||
return &ExportMCPServerResponse{
|
||||
MCPServers: map[string]ExportMCPServer{
|
||||
server.Name: {
|
||||
Type: server.ServerType,
|
||||
URL: server.URL,
|
||||
Name: server.Name,
|
||||
AuthorizationToken: token,
|
||||
Tools: tools,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateMCPServer updates an MCP server owned by a tenant.
|
||||
func (s *MCPService) UpdateMCPServer(tenantID, mcpID string, req UpdateMCPServerRequest) (*entity.MCPServer, common.ErrorCode, error) {
|
||||
server, err := s.mcpServerDAO.GetByIDAndTenant(mcpID, tenantID)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
@@ -86,6 +87,68 @@ func TestImportServersValidationErrors(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewExportMCPServerResponseMatchesPythonDownloadShape(t *testing.T) {
|
||||
response := newExportMCPServerResponse(&entity.MCPServer{
|
||||
Name: "weather",
|
||||
URL: "https://example.com/mcp",
|
||||
ServerType: mcpServerTypeStreamableHTTP,
|
||||
Variables: entity.JSONMap{
|
||||
"authorization_token": "secret-token",
|
||||
"tools": map[string]interface{}{
|
||||
"forecast": map[string]interface{}{"name": "forecast"},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
payload, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
t.Fatalf("marshal response: %v", err)
|
||||
}
|
||||
|
||||
var decoded map[string]map[string]map[string]interface{}
|
||||
if err := json.Unmarshal(payload, &decoded); err != nil {
|
||||
t.Fatalf("unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
server := decoded["mcpServers"]["weather"]
|
||||
if server["type"] != mcpServerTypeStreamableHTTP {
|
||||
t.Fatalf("type = %v, want %s", server["type"], mcpServerTypeStreamableHTTP)
|
||||
}
|
||||
if server["url"] != "https://example.com/mcp" {
|
||||
t.Fatalf("url = %v", server["url"])
|
||||
}
|
||||
if server["name"] != "weather" {
|
||||
t.Fatalf("name = %v", server["name"])
|
||||
}
|
||||
if server["authorization_token"] != "secret-token" {
|
||||
t.Fatalf("authorization_token = %v", server["authorization_token"])
|
||||
}
|
||||
tools, ok := server["tools"].(map[string]interface{})
|
||||
if !ok || tools["forecast"] == nil {
|
||||
t.Fatalf("tools = %#v, want forecast tool", server["tools"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewExportMCPServerResponseDefaultsMissingVariablesLikePython(t *testing.T) {
|
||||
response := newExportMCPServerResponse(&entity.MCPServer{
|
||||
Name: "empty-vars",
|
||||
URL: "https://example.com/mcp",
|
||||
ServerType: mcpServerTypeSSE,
|
||||
})
|
||||
|
||||
server := response.MCPServers["empty-vars"]
|
||||
if server.AuthorizationToken != "" {
|
||||
t.Fatalf("authorization_token = %#v, want empty string", server.AuthorizationToken)
|
||||
}
|
||||
tools, ok := server.Tools.(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatalf("tools type = %T, want map[string]interface{}", server.Tools)
|
||||
}
|
||||
if len(tools) != 0 {
|
||||
t.Fatalf("tools = %#v, want empty map", tools)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaginateMCPServersNegativeValuesMatchPythonSlice(t *testing.T) {
|
||||
servers := makeMCPServers(13)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user