diff --git a/internal/mcpclient/client.go b/internal/mcpclient/client.go index 5bc73a733..7f9575f9b 100644 --- a/internal/mcpclient/client.go +++ b/internal/mcpclient/client.go @@ -204,31 +204,59 @@ func (c *Client) ListTools() ([]Tool, error) { return c.ListToolsContext(context.Background()) } +const maxToolPages = 100 + // ListToolsContext is ListTools with a caller-supplied deadline/cancellation. func (c *Client) ListToolsContext(ctx context.Context) ([]Tool, error) { if err := c.ensureInit(ctx); err != nil { return nil, fmt.Errorf("mcp init: %w", err) } + var tools []Tool + seen := make(map[string]bool) + cursor := "" + for pageNumber := 0; pageNumber < maxToolPages; pageNumber++ { + page, next, err := c.listToolsPage(ctx, cursor) + if err != nil { + return nil, err + } + tools = append(tools, page...) + if next == "" { + return tools, nil + } + if seen[next] { + return nil, fmt.Errorf("mcp tools/list repeated its pagination cursor") + } + seen[next] = true + cursor = next + } + return nil, fmt.Errorf("mcp tools/list exceeded %d pages", maxToolPages) +} + +func (c *Client) listToolsPage(ctx context.Context, cursor string) ([]Tool, string, error) { c.mu.Lock() c.nextID++ id := c.nextID c.mu.Unlock() + params := map[string]any{} + if cursor != "" { + params["cursor"] = cursor + } req := rpcRequest{ JSONRPC: "2.0", ID: id, Method: "tools/list", - Params: map[string]any{}, + Params: params, } body, err := json.Marshal(req) if err != nil { - return nil, err + return nil, "", err } if ctx == nil { ctx = context.Background() } httpReq, err := http.NewRequestWithContext(ctx, "POST", c.url, bytes.NewReader(body)) if err != nil { - return nil, err + return nil, "", err } httpReq.Header.Set("Content-Type", "application/json") httpReq.Header.Set("Accept", "application/json, text/event-stream") @@ -237,31 +265,32 @@ func (c *Client) ListToolsContext(ctx context.Context) ([]Tool, error) { } resp, err := c.http.Do(httpReq) if err != nil { - return nil, fmt.Errorf("mcp tools/list: %w", err) + return nil, "", fmt.Errorf("mcp tools/list: %w", err) } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { body, _ := io.ReadAll(io.LimitReader(resp.Body, 8*1024)) - return nil, fmt.Errorf("gateway HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body))) + return nil, "", fmt.Errorf("gateway HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(body))) } raw, err := c.readResponse(resp) if err != nil { - return nil, fmt.Errorf("mcp tools/list: %w", err) + return nil, "", fmt.Errorf("mcp tools/list: %w", err) } var rpcResp rpcResponse if err := json.Unmarshal(raw, &rpcResp); err != nil { - return nil, fmt.Errorf("mcp decode tools/list: %w (body: %s)", err, string(raw)) + return nil, "", fmt.Errorf("mcp decode tools/list: %w (body: %s)", err, string(raw)) } if rpcResp.Error != nil { - return nil, fmt.Errorf("mcp tools/list error [%d]: %s", rpcResp.Error.Code, rpcResp.Error.Message) + return nil, "", fmt.Errorf("mcp tools/list error [%d]: %s", rpcResp.Error.Code, rpcResp.Error.Message) } var result struct { - Tools []Tool `json:"tools"` + Tools []Tool `json:"tools"` + NextCursor string `json:"nextCursor"` } if err := json.Unmarshal(rpcResp.Result, &result); err != nil { - return nil, fmt.Errorf("mcp tools/list result: %w", err) + return nil, "", fmt.Errorf("mcp tools/list result: %w", err) } - return result.Tools, nil + return result.Tools, result.NextCursor, nil } func (c *Client) ensureInit(ctx context.Context) error { diff --git a/internal/mcpclient/pagination_test.go b/internal/mcpclient/pagination_test.go new file mode 100644 index 000000000..a741ad51d --- /dev/null +++ b/internal/mcpclient/pagination_test.go @@ -0,0 +1,111 @@ +package mcpclient + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" +) + +func TestListToolsFollowsNextCursor(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var request struct { + ID int64 `json:"id"` + Method string `json:"method"` + Params struct { + Cursor string `json:"cursor"` + } `json:"params"` + } + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + switch request.Method { + case "initialize": + writeJSON(w, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{}}`, request.ID)) + case "notifications/initialized": + w.WriteHeader(http.StatusAccepted) + case "tools/list": + if request.Params.Cursor == "" { + writeJSON(w, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{"tools":[{"name":"one"}],"nextCursor":"next/+="}}`, request.ID)) + } else if request.Params.Cursor == "next/+=" { + writeJSON(w, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{"tools":[{"name":"two"}]}}`, request.ID)) + } else { + http.Error(w, "unexpected cursor", http.StatusBadRequest) + } + } + })) + defer server.Close() + + tools, err := New(server.URL, nil).ListTools() + if err != nil || len(tools) != 2 || tools[0].Name != "one" || tools[1].Name != "two" { + t.Fatalf("expected both catalog pages, got %v, %v", tools, err) + } +} + +func TestListToolsBoundsMalformedPagination(t *testing.T) { + for _, tc := range []struct { + name string + repeated bool + terminal bool + wantRequests int + wantError string + }{ + {name: "repeated cursor", repeated: true, wantRequests: 2, wantError: "repeated its pagination cursor"}, + {name: "unique cursors", wantRequests: 100, wantError: "exceeded 100 pages"}, + {name: "terminal page at limit", terminal: true, wantRequests: 100}, + } { + t.Run(tc.name, func(t *testing.T) { + var requests atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var request struct { + ID int64 `json:"id"` + Method string `json:"method"` + } + if err := json.NewDecoder(r.Body).Decode(&request); err != nil { + http.Error(w, "bad request", http.StatusBadRequest) + return + } + switch request.Method { + case "initialize": + writeJSON(w, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{}}`, request.ID)) + case "notifications/initialized": + w.WriteHeader(http.StatusAccepted) + case "tools/list": + requestNumber := requests.Add(1) + // Bound the unfixed client too, so the regression cannot hang. + if requestNumber > 100 { + http.Error(w, "test server page safety limit", http.StatusBadRequest) + return + } + next := fmt.Sprintf("page-%d", requestNumber) + if tc.repeated { + next = "same-cursor" + } + if tc.terminal && requestNumber == 100 { + next = "" + } + writeJSON(w, fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"result":{"tools":[{"name":"tool-%d"}],"nextCursor":%q}}`, request.ID, requestNumber, next)) + } + })) + defer server.Close() + tools, err := New(server.URL, nil).ListTools() + if tc.wantError != "" { + if err == nil || !strings.Contains(err.Error(), tc.wantError) { + t.Fatalf("expected error %q, got %v", tc.wantError, err) + } + if tools != nil { + t.Fatalf("partial catalog must not escape: %v", tools) + } + } else if err != nil || len(tools) != 100 { + t.Fatalf("expected complete 100-page catalog, got %d tools, %v", len(tools), err) + } + if got := requests.Load(); got != int64(tc.wantRequests) { + t.Fatalf("got %d requests, want %d", got, tc.wantRequests) + } + }) + } +}