diff --git a/changelog.d/fixed-admin-mcp-settings-read-whole-section.md b/changelog.d/fixed-admin-mcp-settings-read-whole-section.md new file mode 100644 index 0000000000..77e6e47e83 --- /dev/null +++ b/changelog.d/fixed-admin-mcp-settings-read-whole-section.md @@ -0,0 +1 @@ +- Admin MCP: `settings_read` returns a Settings section whole. It went through the list tools' row cap, which keeps the first 20 entries of a list by default, although `settings_read` has no `limit` argument to raise it. On a hive with more than 20 agents the governor settings came back with 20 agents (`agents_truncated: true`), and a section that is itself a list, such as `backends`, was cut to 20 entries, with no way to read the rest. A section is now bounded only by the 256 KiB text cap. The list tools (`agents_list`, `runs_list` and the others) keep their cap. diff --git a/src/cmd/hive-admin-mcp/main.go b/src/cmd/hive-admin-mcp/main.go index 15f11701c0..4f246d7237 100644 --- a/src/cmd/hive-admin-mcp/main.go +++ b/src/cmd/hive-admin-mcp/main.go @@ -366,6 +366,10 @@ func (p readProvider) Read(ctx context.Context, tool string, args map[string]any if err != nil { return nil, err } + if tool == adminmcp.ToolSettingsRead { + // A Settings view is not a list to page; see the dashboard provider. + return data, nil + } if tool == adminmcp.ToolIssuesByBand || tool == adminmcp.ToolPrsByBand { return adminmcp.BandReadResult(tool, data, args) } diff --git a/src/cmd/hive-admin-mcp/settings_read_cap_test.go b/src/cmd/hive-admin-mcp/settings_read_cap_test.go new file mode 100644 index 0000000000..685056f79c --- /dev/null +++ b/src/cmd/hive-admin-mcp/settings_read_cap_test.go @@ -0,0 +1,79 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/adminmcp" +) + +// settings_read went through the list tools' row cap (20 by default) although +// it has no limit argument, so the governor settings of a hive with 30 agents +// came back with 20 and no way to read the rest. +func TestStdioSettingsReadReturnsTheWholeSection(t *testing.T) { + agents := make([]any, 30) + for i := range agents { + agents[i] = map[string]any{"name": fmt.Sprintf("agent-%02d", i), "cadence": "15m"} + } + backends := make([]any, 25) + for i := range backends { + backends[i] = map[string]any{"name": fmt.Sprintf("backend-%02d", i)} + } + mux := http.NewServeMux() + mux.HandleFunc("GET /api/config/governor", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{"agents": agents, "cadenceScope": "global"}) + }) + mux.HandleFunc("GET /api/config/backends", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(backends) + }) + server := httptest.NewServer(mux) + defer server.Close() + r := &roster{hives: []hiveConfig{{Name: "active", Address: server.URL, Token: "token"}}, active: 0, timeout: time.Second} + p := readProvider{roster: r} + + data, err := p.Read(context.Background(), adminmcp.ToolSettingsRead, map[string]any{}) + if err != nil { + t.Fatal(err) + } + governor, _ := data.(map[string]any) + if kept, _ := governor["agents"].([]any); len(kept) != 30 { + t.Fatalf("governor settings list %d of 30 agents", len(kept)) + } + if _, cut := governor["_admin_mcp"]; cut { + t.Fatalf("governor settings are marked truncated: %v", governor["_admin_mcp"]) + } + + data, err = p.Read(context.Background(), adminmcp.ToolSettingsRead, map[string]any{"section": "backends"}) + if err != nil { + t.Fatal(err) + } + if list, _ := data.([]any); len(list) != 25 { + t.Fatalf("backends section = %T with %d entries, want the list of 25", data, len(list)) + } +} + +// The list tools keep their cap. +func TestStdioListToolsStillCapRows(t *testing.T) { + agents := make([]any, 30) + for i := range agents { + agents[i] = map[string]any{"name": fmt.Sprintf("agent-%02d", i)} + } + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(agents) + })) + defer server.Close() + r := &roster{hives: []hiveConfig{{Name: "active", Address: server.URL, Token: "token"}}, active: 0, timeout: time.Second} + data, err := (readProvider{roster: r}).Read(context.Background(), adminmcp.ToolAgentsList, map[string]any{}) + if err != nil { + t.Fatal(err) + } + capped, _ := data.(map[string]any) + if items, _ := capped["items"].([]any); len(items) != adminmcp.DefaultResultLimit || capped["truncated"] != true { + t.Fatalf("agents_list = %v", capped) + } +} diff --git a/src/docs/admin-mcp.md b/src/docs/admin-mcp.md index f823ac7bb4..33f62ab43f 100644 --- a/src/docs/admin-mcp.md +++ b/src/docs/admin-mcp.md @@ -234,7 +234,9 @@ section name, never a path. Each section is the endpoint that Settings tab reads, so the answer matches the page for the same caller: most sections need a verified owner, as the tabs do. Secret values are never returned. Settings name the environment variable, file or header that supplies a secret, and the answer -is scrubbed like every other read. +is scrubbed like every other read. `settings_read` takes no `limit`: a section +is returned whole, however many agents or entries it lists, bounded only by the +256 KiB text cap. - Global settings: `governor` (the default: modes, thresholds, labels, repos, budget, notifications, health, sensing, logging, security, features, hub, diff --git a/src/pkg/dashboard/admin_mcp.go b/src/pkg/dashboard/admin_mcp.go index 9664bf10d9..1804fbca06 100644 --- a/src/pkg/dashboard/admin_mcp.go +++ b/src/pkg/dashboard/admin_mcp.go @@ -133,7 +133,11 @@ func (p dashboardAdminMCPProvider) Read(_ context.Context, tool string, args map if err != nil { return nil, err } - return adminmcp.CapResult(data, adminmcp.LimitFromArgs(args)), nil + // A Settings view is not a list to page: settings_read has no limit + // argument, so the row cap would cut a hive's 21st agent out of its + // governor settings with no way to ask for it. The text cap still + // bounds the answer. + return data, nil } path, ok := adminMCPReadPath(tool, args) if !ok { diff --git a/src/pkg/dashboard/admin_mcp_settings_cap_test.go b/src/pkg/dashboard/admin_mcp_settings_cap_test.go new file mode 100644 index 0000000000..5fee008af8 --- /dev/null +++ b/src/pkg/dashboard/admin_mcp_settings_cap_test.go @@ -0,0 +1,53 @@ +package dashboard + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "testing" + + "github.com/hivecommons/hive/pkg/adminmcp" +) + +// settings_read went through the list tools' row cap (20 by default) although +// it has no limit argument, so the governor settings of a hive with 30 agents +// came back with 20 and no way to read the rest. +func TestDashboardAdminMCPSettingsReadReturnsTheWholeSection(t *testing.T) { + agents := make([]any, 30) + for i := range agents { + agents[i] = map[string]any{"name": fmt.Sprintf("agent-%02d", i), "cadence": "15m"} + } + backends := make([]any, 25) + for i := range backends { + backends[i] = map[string]any{"name": fmt.Sprintf("backend-%02d", i)} + } + s := &Server{mux: http.NewServeMux()} + s.mux.HandleFunc("GET /api/config/governor", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(map[string]any{"agents": agents, "cadenceScope": "global"}) + }) + s.mux.HandleFunc("GET /api/config/backends", func(w http.ResponseWriter, r *http.Request) { + _ = json.NewEncoder(w).Encode(backends) + }) + p := dashboardAdminMCPProvider{server: s, role: "owner", ownerVerified: true} + + data, err := p.Read(context.Background(), adminmcp.ToolSettingsRead, map[string]any{}) + if err != nil { + t.Fatal(err) + } + governor, _ := data.(map[string]any) + if kept, _ := governor["agents"].([]any); len(kept) != 30 { + t.Fatalf("governor settings list %d of 30 agents", len(kept)) + } + if _, cut := governor["_admin_mcp"]; cut { + t.Fatalf("governor settings are marked truncated: %v", governor["_admin_mcp"]) + } + + data, err = p.Read(context.Background(), adminmcp.ToolSettingsRead, map[string]any{"section": "backends"}) + if err != nil { + t.Fatal(err) + } + if list, _ := data.([]any); len(list) != 25 { + t.Fatalf("backends section = %T with %d entries, want the list of 25", data, len(list)) + } +}