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
83 changes: 81 additions & 2 deletions internal/mcp/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log/slog"
"slices"
"strings"
"time"

"github.com/labstack/fanout/internal/dashboard"
"github.com/modelcontextprotocol/go-sdk/mcp"
Expand Down Expand Up @@ -95,7 +96,7 @@ func (s *Server) dashboardCreate(ctx context.Context, req *mcp.CallToolRequest,
if err != nil {
return nil, dashboardOutput{}, dashboardToolError(err)
}
return summary(fmt.Sprintf("Created dashboard %q with %d widgets.", item.Name, len(item.State.Widgets))), dashboardOutput{Dashboard: item}, nil
return summary(fmt.Sprintf("Created dashboard %q with %d widgets.%s", item.Name, len(item.State.Widgets), s.emptyWidgetNote(ctx, item.State))), dashboardOutput{Dashboard: item}, nil
}

func (s *Server) dashboardUpdate(ctx context.Context, req *mcp.CallToolRequest, input DashboardUpdateInput) (*mcp.CallToolResult, dashboardOutput, error) {
Expand All @@ -107,7 +108,85 @@ func (s *Server) dashboardUpdate(ctx context.Context, req *mcp.CallToolRequest,
if err != nil {
return nil, dashboardOutput{}, dashboardToolError(err)
}
return summary(fmt.Sprintf("Updated dashboard %q with %d widgets.", item.Name, len(item.State.Widgets))), dashboardOutput{Dashboard: item}, nil
return summary(fmt.Sprintf("Updated dashboard %q with %d widgets.%s", item.Name, len(item.State.Widgets), s.emptyWidgetNote(ctx, item.State))), dashboardOutput{Dashboard: item}, nil
}

// probeLimit bounds the work a single create or update can trigger. A design
// with more filtered cards than this is reported on its first few; the point is
// to catch a dashboard built entirely out of empty cards, not to audit each one.
const probeLimit = 8

// probeBudget caps how long the whole check may take. Whatever has answered by
// then is reported; the rest are left unmentioned rather than delaying a
// dashboard that is already saved.
const probeBudget = 3 * time.Second

// emptyWidgetNote reports which of the new dashboard's filtered cards have
// nothing to show right now.
//
// A design is composed from what the model believes is there, and a filter it
// invents can match nothing at all — a demo dashboard shipped with "Error
// logs" and "Warnings" cards that were empty on arrival and stayed empty,
// because the service in question logs no such levels. Nothing in the tool
// result said so, so the model described them to the user as working views.
// Saying it here lets the model drop the card or explain it in the same turn.
//
// Probes never fail the call and never hold it open: an empty note is the
// honest answer when a probe cannot run, and a dashboard that saved correctly
// must not be reported as an error — or left waiting — because a follow-up
// query was slow. A log query over a busy window has been seen to take twelve
// seconds, and eight of those would be the answer's latency.
func (s *Server) emptyWidgetNote(ctx context.Context, state dashboard.State) string {
if s.queries == nil {
return ""
}
ctx, cancel := context.WithTimeout(ctx, probeBudget)
defer cancel()
scope, err := s.scope(QueryInput{Window: state.Filters.Window, Namespace: state.Filters.Namespace})
if err != nil {
return ""
}
var empty []string
probes := 0
for _, widget := range state.Widgets {
if probes >= probeLimit || ctx.Err() != nil {
break
}
service := widgetConfigString(widget, "service")
switch widget.Type {
case "logs":
severity, search := widgetConfigString(widget, "severity"), widgetConfigString(widget, "search")
if service == "" && severity == "" && search == "" {
continue
}
probes++
result, probeErr := s.queries.Logs(ctx, scope, service, severity, search, 1)
if probeErr == nil && len(result.Data.Entries) == 0 {
empty = append(empty, widget.Title)
}
case "trace":
if service == "" && widgetConfigString(widget, "trace_id") == "" {
continue
}
probes++
result, probeErr := s.queries.Trace(ctx, scope, widgetConfigString(widget, "trace_id"), service, 1)
if probeErr == nil && len(result.Data.Spans) == 0 {
empty = append(empty, widget.Title)
}
}
}
if len(empty) == 0 {
return ""
}
return fmt.Sprintf(" No data matches these cards in the dashboard's own window, so they will render empty: %s. Tell the user, or replace them.", strings.Join(empty, ", "))
}

func widgetConfigString(widget dashboard.Widget, key string) string {
value, ok := widget.Config[key].(string)
if !ok {
return ""
}
return strings.TrimSpace(value)
}

// dashboardOwner resolves the authenticated owner for a dashboard tool call.
Expand Down
79 changes: 79 additions & 0 deletions internal/mcp/dashboards_probe_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package mcp

import (
"context"
"strings"
"testing"

mcpgoauth "github.com/modelcontextprotocol/go-sdk/auth"
"github.com/modelcontextprotocol/go-sdk/mcp"

"github.com/labstack/fanout/internal/dashboard"
controlstore "github.com/labstack/fanout/internal/store"
)

func newDashboardToolFixture(t *testing.T, queries Observability) (*Server, *mcp.CallToolRequest) {
t.Helper()
database, err := controlstore.NewSQLite(":memory:")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = database.Close() })
if _, err := database.DB.ExecContext(context.Background(), `INSERT INTO users(id,email,name,role,active) VALUES('owner','owner@example.test','Owner','admin',1)`); err != nil {
t.Fatal(err)
}
server := New(queries, dashboard.New(database.DB, 30), "test")
req := &mcp.CallToolRequest{Params: &mcp.CallToolParamsRaw{}, Extra: &mcp.RequestExtra{TokenInfo: &mcpgoauth.TokenInfo{UserID: "owner", Scopes: []string{dashboard.OAuthScope}}}}
return server, req
}

func filteredLogsDashboard() dashboard.State {
return dashboard.State{
Filters: dashboard.Filters{Window: "6h"},
Widgets: []dashboard.Widget{
{ID: "errors", Type: "logs", Title: "Checkout Error Logs", Config: map[string]any{"service": "checkout", "severity": "ERROR"}, Enabled: true},
{ID: "health", Type: "overview", Title: "System health", Enabled: true},
},
Layout: []dashboard.Layout{
{I: "errors", X: 0, Y: 0, W: 6, H: 3},
{I: "health", X: 6, Y: 0, W: 6, H: 3},
},
}
}

func createText(t *testing.T, server *Server, req *mcp.CallToolRequest, name string, state dashboard.State) string {
t.Helper()
result, _, err := server.dashboardCreate(context.Background(), req, DashboardCreateInput{Name: name, State: state})
if err != nil {
t.Fatalf("dashboardCreate: %v", err)
}
var text strings.Builder
for _, content := range result.Content {
if message, ok := content.(*mcp.TextContent); ok {
text.WriteString(message.Text)
}
}
return text.String()
}

func TestDashboardCreateReportsWidgetsWithNothingToShow(t *testing.T) {
// The model composes a dashboard from what it believes is there. A filter
// that matches nothing produced a card that was empty on arrival and was
// still described to the user as a working view.
server, req := newDashboardToolFixture(t, &fakeObservability{})
text := createText(t, server, req, "Checkout Order Path", filteredLogsDashboard())
if !strings.Contains(text, "Checkout Error Logs") {
t.Fatalf("summary = %q, want the empty card named", text)
}
if !strings.Contains(text, "render empty") {
t.Fatalf("summary = %q, want it to say the card has nothing to show", text)
}
}

func TestDashboardCreateStaysQuietWhenWidgetsHaveData(t *testing.T) {
server, req := newDashboardToolFixture(t, &fakeObservability{logEntries: 3})
text := createText(t, server, req, "Checkout Order Path", filteredLogsDashboard())
if strings.Contains(text, "render empty") {
t.Fatalf("summary = %q, want no warning when the card has data", text)
}
}
8 changes: 6 additions & 2 deletions internal/mcp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (

type fakeObservability struct {
scope observability.Scope
// Zero means the query matches nothing, which is what a widget probe is
// looking for; a test that needs a populated answer sets these.
logEntries int
traceSpans int
}

type fakeIntelligence struct {
Expand Down Expand Up @@ -130,12 +134,12 @@ func (f *fakeObservability) Performance(_ context.Context, scope observability.S

func (f *fakeObservability) Trace(_ context.Context, scope observability.Scope, _, _ string, _ int) (observability.Result[observability.TraceDetail], error) {
f.scope = scope
return observability.Result[observability.TraceDetail]{Schema: observability.TraceSchema, Summary: "trace"}, nil
return observability.Result[observability.TraceDetail]{Schema: observability.TraceSchema, Summary: "trace", Data: observability.TraceDetail{Spans: make([]observability.TraceSpan, f.traceSpans)}}, nil
}

func (f *fakeObservability) Logs(_ context.Context, scope observability.Scope, _, _, _ string, _ int) (observability.Result[observability.Logs], error) {
f.scope = scope
return observability.Result[observability.Logs]{Schema: observability.LogsSchema, Summary: "logs"}, nil
return observability.Result[observability.Logs]{Schema: observability.LogsSchema, Summary: "logs", Data: observability.Logs{Entries: make([]observability.LogEntry, f.logEntries)}}, nil
}

func TestOverviewReturnsSummaryAndStructuredOutput(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions internal/ui/dist/assets/auth-d1anOsyP.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions internal/ui/dist/assets/chat._threadId-CtOVI6Uj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import{t as e}from"./chat-Cxa6DFwb.js";var t=e;export{t as component};
1 change: 0 additions & 1 deletion internal/ui/dist/assets/chat._threadId-u0MOvyAV.js

This file was deleted.

1 change: 1 addition & 0 deletions internal/ui/dist/assets/chat.index-CtOVI6Uj.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import{t as e}from"./chat-Cxa6DFwb.js";var t=e;export{t as component};
1 change: 0 additions & 1 deletion internal/ui/dist/assets/chat.index-u0MOvyAV.js

This file was deleted.

1 change: 1 addition & 0 deletions internal/ui/dist/assets/copy-Bx9ZWqiL.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/ui/dist/assets/copy-DGK5qe2D.js

This file was deleted.

24 changes: 24 additions & 0 deletions internal/ui/dist/assets/dashboard-BxZStHQX.js

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions internal/ui/dist/assets/dashboard-qOWEIeHc.js

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Loading
Loading