Files
ragflow/internal/service/mcp_test.go
Alexander Laurent 1748723971 feat: add Go MCP server list API (#15253)
## What
#15240 
Implements `GET /api/v1/mcp/servers` in the Go API server.

## Changes

- Added MCP server DAO list query with tenant scoping.
- Added MCP service response wrapper.
- Added MCP handler for list request parsing and response formatting.
- Wired `GET /api/v1/mcp/servers` under authenticated `/api/v1` routes.
- Initialized MCP service and handler in the Go server startup.
- update_time and update_date now both map to update_date
- create_time and create_date now both map to create_date
- default ordering now returns create_date
## API Behavior

Matches the Python endpoint behavior:

- Requires authenticated user.
- Lists MCP servers for the current user tenant.
- Supports `keywords`.
- Supports `mcp_id` and repeated/comma-separated `mcp_ids`.
- Supports `page`, `page_size`, `orderby`, and `desc`.
- Returns:

```json
{
  "code": 0,
  "message": "success",
  "data": {
    "mcp_servers": [],
    "total": 0
  }
}
```
2026-06-02 09:37:05 +08:00

66 lines
1.8 KiB
Go

//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package service
import (
"fmt"
"testing"
"ragflow/internal/entity"
)
func TestPaginateMCPServersNegativeValuesMatchPythonSlice(t *testing.T) {
servers := makeMCPServers(13)
got := paginateMCPServers(servers, -1, -2)
if len(got) != 0 {
t.Fatalf("expected empty page for negative pagination, got %d servers", len(got))
}
}
func TestPaginateMCPServersKeepsUnpagedList(t *testing.T) {
servers := makeMCPServers(3)
got := paginateMCPServers(servers, 0, 0)
if len(got) != len(servers) {
t.Fatalf("expected unpaged list length %d, got %d", len(servers), len(got))
}
}
func TestPaginateMCPServersPositiveValues(t *testing.T) {
servers := makeMCPServers(5)
got := paginateMCPServers(servers, 2, 2)
if len(got) != 2 {
t.Fatalf("expected 2 servers, got %d", len(got))
}
if got[0].ID != "server-3" || got[1].ID != "server-4" {
t.Fatalf("expected second page servers, got %q and %q", got[0].ID, got[1].ID)
}
}
func makeMCPServers(count int) []*entity.MCPServer {
servers := make([]*entity.MCPServer, 0, count)
for i := 1; i <= count; i++ {
servers = append(servers, &entity.MCPServer{ID: fmt.Sprintf("server-%d", i)})
}
return servers
}