Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/agent/service_input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ func TestToolRegistryNoDataAndDashboardPanelErrors(t *testing.T) {
}{
{"get_service_performance", "no_data", "spans=0, logs=0, metrics=1", map[string]any{"service": "collector", "from": from, "to": to}},
{"search_logs", "no_data", "INFO (1)", map[string]any{"service": "logger", "severity": "ERROR", "from": from, "to": to}},
{"create_dashboard", "tool_failed", "panels[0]: Collector requests", map[string]any{"dashboard": spec}},
{"create_dashboard", "invalid_spec", "panels[0]: Collector requests", map[string]any{"dashboard": spec}},
} {
input, _ := json.Marshal(tc.args)
result, err := registry.Execute(dashboard.WithOwner(t.Context(), "owner"), ToolCall{Name: tc.name, Input: string(input)})
var failure toolerror.Result
if err != nil || !result.IsError || result.Structured != nil || json.Unmarshal([]byte(result.Content), &failure) != nil || failure.Error == nil || failure.Error.Code != tc.code || !strings.Contains(failure.Error.Message, tc.want) {
if err != nil || !result.IsError || (result.Structured != nil) != (tc.name == "create_dashboard") || json.Unmarshal([]byte(result.Content), &failure) != nil || failure.Error == nil || failure.Error.Code != tc.code || !strings.Contains(failure.Error.Message, tc.want) {
t.Fatalf("%s: %+v %v", tc.name, result, err)
}
}
Expand Down
8 changes: 6 additions & 2 deletions internal/mcp/concurrent_receipts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ func TestConcurrentFileBackedSaveReceiptsNameOnlyTheirOwnEdit(t *testing.T) {
versions := map[int]bool{}
for i, a := range answers {
if a.err != nil {
if !strings.Contains(a.err.Error(), "the dashboard changed since you read it") {
t.Errorf("loser %d: unexpected error %v", i, a.err)
t.Errorf("writer %d: unexpected error %v", i, a.err)
continue
}
if failure := a.out.Error; failure != nil {
if failure.Code != "dashboard_version_conflict" || !strings.Contains(failure.Message, "the dashboard changed since you read it") {
t.Errorf("loser %d: unexpected failure %+v", i, failure)
}
if a.out.Receipt != nil || a.out.Dashboard != nil {
t.Errorf("loser %d has receipt %+v", i, a.out)
Expand Down
59 changes: 30 additions & 29 deletions internal/mcp/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type dashboardOutput struct {
Warnings []string `json:"warnings,omitempty"`
Receipt *dashboardReceipt `json:"receipt,omitempty"`
Error *toolerror.Error `json:"error,omitempty"`
Problems panel.Problems `json:"problems,omitempty"`
}

type savedPanelCheck struct {
Expand Down Expand Up @@ -119,17 +120,17 @@ var dashboardTools = [...]mcp.Tool{
},
{
Name: "create_dashboard", Title: "Create dashboard",
Description: "Create a dashboard for the authenticated user from a complete spec. Read get_telemetry_schema first and preview_panels until every non-text panel returns data. Empty or failing panels are rejected with their path, title and diagnosis. " + saveReceiptGuide + specGuide,
Description: "Create a dashboard for the authenticated user from a complete spec. Read get_telemetry_schema first and preview_panels until every non-text panel returns data. Empty or failing panels return error.code invalid_spec with their path, title and diagnosis, plus structured problems. " + saveReceiptGuide + specGuide,
Annotations: &mcp.ToolAnnotations{DestructiveHint: boolPtr(false), OpenWorldHint: boolPtr(false)},
},
{
Name: "replace_dashboard", Title: "Replace dashboard",
Description: "Every non-text panel must return data in the preview or the save is rejected. Replace a dashboard's whole spec; omitted panels are removed. Use only for a redesign the user asked for; use edit_dashboard to change a few panels. " + saveReceiptGuide + "See create_dashboard for the spec guide.",
Description: "Every non-text panel must return data in the preview or the save returns error.code invalid_spec with the panel path, title and diagnosis, plus structured problems. A stale base_version returns error.code dashboard_version_conflict. Replace a dashboard's whole spec; omitted panels are removed. Use only for a redesign the user asked for; use edit_dashboard to change a few panels. " + saveReceiptGuide + "See create_dashboard for the spec guide.",
Annotations: &mcp.ToolAnnotations{DestructiveHint: boolPtr(true), OpenWorldHint: boolPtr(false)},
},
{
Name: "edit_dashboard", Title: "Edit dashboard",
Description: "Every non-text panel must return data in the preview or the save is rejected. Change a dashboard with typed operations, applied in order and saved as one version: add_panel, update_panel (set replaces the named fields), remove_panel, move_panel, set_variable, remove_variable, set_time, rename. Panels not named are left unchanged. Only edit when the user asks to change that dashboard. " + saveReceiptGuide + "See create_dashboard for the spec guide.",
Description: "Every non-text panel must return data in the preview or the save returns error.code invalid_spec with the panel path, title and diagnosis, plus structured problems. A stale base_version returns error.code dashboard_version_conflict. Change a dashboard with typed operations, applied in order and saved as one version: add_panel, update_panel (set replaces the named fields), remove_panel, move_panel, set_variable, remove_variable, set_time, rename. Panels not named are left unchanged. Only edit when the user asks to change that dashboard. " + saveReceiptGuide + "See create_dashboard for the spec guide.",
Annotations: &mcp.ToolAnnotations{DestructiveHint: boolPtr(true), OpenWorldHint: boolPtr(false)},
},
{
Expand All @@ -139,7 +140,7 @@ var dashboardTools = [...]mcp.Tool{
},
{
Name: "restore_dashboard_version", Title: "Restore dashboard version",
Description: "Restore a saved historical version of the authenticated user's dashboard only on an explicit user request. The input version selects the historical spec; the result's version is the newly saved version. Every successful call appends another version, even when restoring the latest spec. A missing or pruned target returns error.code dashboard_version_not_found; list_dashboard_versions shows retained versions. " + saveReceiptGuide,
Description: "Restore a saved historical version of the authenticated user's dashboard only on an explicit user request. The input version selects the historical spec; the result's version is the newly saved version. Every successful call appends another version, even when restoring the latest spec. A missing or pruned target returns error.code dashboard_version_not_found; list_dashboard_versions shows retained versions. Spec validation failures return error.code invalid_spec with the presented problems. " + saveReceiptGuide,
Annotations: &mcp.ToolAnnotations{DestructiveHint: boolPtr(true), OpenWorldHint: boolPtr(false)},
},
}
Expand Down Expand Up @@ -179,7 +180,8 @@ func (s *Server) dashboardList(ctx context.Context, req *mcp.CallToolRequest, _
}
items, err := s.dashboards.List(ctx, owner)
if err != nil {
return nil, dashboardListOutput{}, dashboardToolError(err)
result, _, err := dashboardToolError(err)
return result, dashboardListOutput{}, err
}
summaries := make([]dashboardToolSummary, 0, len(items))
for _, item := range items {
Expand All @@ -195,7 +197,7 @@ func (s *Server) dashboardGet(ctx context.Context, req *mcp.CallToolRequest, inp
}
record, err := s.dashboards.Get(ctx, owner, strings.TrimSpace(input.ID))
if err != nil {
return nil, dashboardOutput{}, dashboardToolError(err)
return dashboardToolError(err)
}
return summary(fmt.Sprintf("Loaded %q, version %d, with %d panels.", record.Name, record.Version, len(record.Spec.Panels))), dashboardOutput{Dashboard: &record}, nil
}
Expand Down Expand Up @@ -223,7 +225,7 @@ func (s *Server) dashboardCreate(ctx context.Context, req *mcp.CallToolRequest,
}
record, err := s.dashboards.CreateWithChanges(dashboard.WithSaveCheck(ctx, s.checkDashboardPanels), owner, input.Dashboard, agentAuthor(owner))
if err != nil {
return nil, dashboardOutput{}, dashboardToolError(err)
return dashboardToolError(err)
}
return s.saved(ctx, "Created", record)
}
Expand All @@ -235,7 +237,7 @@ func (s *Server) dashboardReplace(ctx context.Context, req *mcp.CallToolRequest,
}
record, err := s.dashboards.ReplaceWithChanges(dashboard.WithSaveCheck(ctx, s.checkDashboardPanels), owner, strings.TrimSpace(input.ID), input.Dashboard, input.BaseVersion, agentAuthor(owner), input.Message)
if err != nil {
return nil, dashboardOutput{}, dashboardToolError(err)
return dashboardToolError(err)
}
return s.saved(ctx, "Replaced", record)
}
Expand All @@ -247,7 +249,7 @@ func (s *Server) dashboardEdit(ctx context.Context, req *mcp.CallToolRequest, in
}
record, err := s.dashboards.EditWithChanges(dashboard.WithSaveCheck(ctx, s.checkDashboardPanels), owner, strings.TrimSpace(input.ID), input.Operations, input.BaseVersion, agentAuthor(owner), input.Message)
if err != nil {
return nil, dashboardOutput{}, dashboardToolError(err)
return dashboardToolError(err)
}
return s.saved(ctx, "Updated", record)
}
Expand Down Expand Up @@ -304,7 +306,8 @@ func (s *Server) dashboardVersions(ctx context.Context, req *mcp.CallToolRequest
}
versions, err := s.dashboards.Versions(ctx, owner, strings.TrimSpace(input.ID))
if err != nil {
return nil, dashboardVersionsOutput{}, dashboardToolError(err)
result, _, err := dashboardToolError(err)
return result, dashboardVersionsOutput{}, err
}
return summary(fmt.Sprintf("Found %d saved versions.", len(versions))), dashboardVersionsOutput{Versions: versions}, nil
}
Expand All @@ -319,18 +322,7 @@ func (s *Server) dashboardRestore(ctx context.Context, req *mcp.CallToolRequest,
}
mutation, err := s.dashboards.RestoreWithChanges(ctx, owner, strings.TrimSpace(input.ID), input.Version, agentAuthor(owner))
if err != nil {
code := ""
if errors.Is(err, dashboard.ErrVersionNotFound) {
code = dashboard.VersionNotFoundCode
} else if errors.Is(err, dashboard.ErrAlreadyCurrent) {
code = dashboard.AlreadyCurrentCode
}
if code != "" {
result := summary(err.Error())
result.IsError = true
return result, dashboardOutput{Error: &toolerror.Error{Code: code, Message: err.Error()}}, nil
}
return nil, dashboardOutput{}, dashboardToolError(err)
return dashboardToolError(err)
}
return s.saved(ctx, "Restored", mutation)
}
Expand Down Expand Up @@ -455,20 +447,29 @@ func dashboardOwner(req *mcp.CallToolRequest) (string, error) {
return "", errors.New("authenticated dashboard owner is required")
}

func dashboardToolError(err error) error {
func dashboardToolError(err error) (*mcp.CallToolResult, dashboardOutput, error) {
var problems panel.Problems
code, message := "", ""
switch {
case errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded):
return safePanelToolError(err)
return nil, dashboardOutput{}, safePanelToolError(err)
case errors.As(err, &problems):
return panel.PresentProblems(problems)
problems = panel.PresentProblems(problems)
code, message = "invalid_spec", problems.Error()
case errors.Is(err, dashboard.ErrVersionNotFound):
code, message = dashboard.VersionNotFoundCode, err.Error()
case errors.Is(err, dashboard.ErrAlreadyCurrent):
code, message = dashboard.AlreadyCurrentCode, err.Error()
case errors.Is(err, dashboard.ErrNotFound):
return errors.New("dashboard not found")
return nil, dashboardOutput{}, errors.New("dashboard not found")
case errors.Is(err, dashboard.ErrConflict):
return errors.New("a dashboard with that name already exists")
return nil, dashboardOutput{}, errors.New("a dashboard with that name already exists")
case errors.Is(err, dashboard.ErrStale):
return errors.New("the dashboard changed since you read it; open it again and reapply the change")
code, message = "dashboard_version_conflict", "the dashboard changed since you read it; open it again and reapply the change"
default:
return safePanelToolError(err)
return nil, dashboardOutput{}, safePanelToolError(err)
}
result := summary(message)
result.IsError = true
return result, dashboardOutput{Error: &toolerror.Error{Code: code, Message: message}, Problems: problems}, nil
}
26 changes: 12 additions & 14 deletions internal/mcp/dashboards_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func TestStaleBaseVersionIsReportedAndMatchingOneSucceeds(t *testing.T) {
t.Fatalf("matching base_version = %+v %v", bumped.Dashboard, err)
}
const stale = "the dashboard changed since you read it"
if _, _, err := s.dashboardEdit(ctx, requestFor("owner"), DashboardEditInput{ID: id, Operations: rename, BaseVersion: 1}); err == nil || !strings.Contains(err.Error(), stale) {
t.Fatalf("stale edit error = %v", err)
if result, out, err := s.dashboardEdit(ctx, requestFor("owner"), DashboardEditInput{ID: id, Operations: rename, BaseVersion: 1}); err != nil || !result.IsError || out.Error == nil || out.Error.Code != "dashboard_version_conflict" || !strings.Contains(out.Error.Message, stale) {
t.Fatalf("stale edit = %+v %v", out, err)
}
replacement := textDashboard("Replaced")
if _, _, err := s.dashboardReplace(ctx, requestFor("owner"), DashboardReplaceInput{ID: id, Dashboard: replacement, BaseVersion: 1}); err == nil || !strings.Contains(err.Error(), stale) {
t.Fatalf("stale replace error = %v", err)
if result, out, err := s.dashboardReplace(ctx, requestFor("owner"), DashboardReplaceInput{ID: id, Dashboard: replacement, BaseVersion: 1}); err != nil || !result.IsError || out.Error == nil || out.Error.Code != "dashboard_version_conflict" || !strings.Contains(out.Error.Message, stale) {
t.Fatalf("stale replace = %+v %v", out, err)
}
_, replaced, err := s.dashboardReplace(ctx, requestFor("owner"), DashboardReplaceInput{ID: id, Dashboard: replacement, BaseVersion: 2})
if err != nil || replaced.Dashboard.Version != 3 {
Expand All @@ -113,19 +113,17 @@ func TestInvalidSpecsReturnProblemsWithPaths(t *testing.T) {
s := newPanelServer(t)
ctx := t.Context()
bad := panel.Dashboard{Name: "Bad", Panels: []panel.Panel{{ID: "typo", Title: "Typo", Viz: "stat", Query: &panel.Query{From: "spans", Measures: []string{"p95(duraton_ms)"}}}}}
_, _, err := s.dashboardCreate(ctx, requestFor("owner"), DashboardCreateInput{Dashboard: bad})
var problems panel.Problems
if !errors.As(err, &problems) || !strings.Contains(err.Error(), "panels[0]") {
t.Fatalf("create error = %v", err)
result, out, err := s.dashboardCreate(ctx, requestFor("owner"), DashboardCreateInput{Dashboard: bad})
if err != nil || !result.IsError || out.Error == nil || out.Error.Code != "invalid_spec" || !strings.Contains(out.Error.Message, "panels[0]") {
t.Fatalf("create = %+v %v", out, err)
}
_, created, err := s.dashboardCreate(ctx, requestFor("owner"), DashboardCreateInput{Dashboard: textDashboard("Good")})
if err != nil {
t.Fatal(err)
}
_, _, err = s.dashboardEdit(ctx, requestFor("owner"), DashboardEditInput{ID: created.Dashboard.ID, Operations: []dashboard.Operation{{Op: "frobnicate"}}})
problems = nil
if !errors.As(err, &problems) || !strings.Contains(err.Error(), "operations[0]") {
t.Fatalf("edit error = %v", err)
result, out, err = s.dashboardEdit(ctx, requestFor("owner"), DashboardEditInput{ID: created.Dashboard.ID, Operations: []dashboard.Operation{{Op: "frobnicate"}}})
if err != nil || !result.IsError || out.Error == nil || out.Error.Code != "invalid_spec" || !strings.Contains(out.Error.Message, "operations[0]") {
t.Fatalf("edit = %+v %v", out, err)
}
}

Expand Down Expand Up @@ -166,8 +164,8 @@ func TestSaveRejectsPanelsItCannotPreview(t *testing.T) {
panels := newPanelServer(t).panels
s := newToolServer(t, structural{}, panels)
spec := panel.Dashboard{Name: "Unchecked", Panels: []panel.Panel{{ID: "requests", Title: "Requests", Viz: "stat", Query: &panel.Query{From: "spans", Where: []string{"service = = 'x'"}, Measures: []string{"count()"}}}}}
_, out, err := s.dashboardCreate(t.Context(), requestFor("owner"), DashboardCreateInput{Dashboard: spec})
if err == nil || out.Dashboard != nil || out.Receipt != nil {
result, out, err := s.dashboardCreate(t.Context(), requestFor("owner"), DashboardCreateInput{Dashboard: spec})
if err != nil || !result.IsError || out.Error == nil || out.Error.Code != "invalid_spec" || out.Dashboard != nil || out.Receipt != nil {
t.Fatalf("save=%+v err=%v", out, err)
}
}
Expand Down
Loading
Loading