Skip to content
Open
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
1 change: 1 addition & 0 deletions MCP.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ with:
| `clickstack_save_saved_search` | Create or update a saved search (reusable query against a data source) |
| `clickstack_get_alert` | List alerts (summary) or get full detail with evaluation history; filter by state |
| `clickstack_save_alert` | Create a new alert or update an existing one |
| `clickstack_get_slo` | List SLOs (summary) or get one with its status, per group for a grouped SLO, and a SQL where clause for its bad events. Only registered when SLOs are enabled |
| `clickstack_trace_waterfall` | Fetch all spans in a single trace as a parent/child waterfall tree with optional correlated logs |
| `clickstack_trace_top_time_consuming_ops` | Aggregate breakdown of child operations by cumulative time across matching parent traces |
| `clickstack_get_webhook` | List available webhook destinations for use as alert notification channels |
Expand Down
22 changes: 21 additions & 1 deletion packages/api/src/controllers/sloStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
} from '@hyperdx/common-utils/dist/core/sloStats';
import type { SloStatus } from '@hyperdx/common-utils/dist/slo';

import { getSloQueryContext } from '@/controllers/slo';
import { getSloQueryContext, SloQueryContext } from '@/controllers/slo';
import {
buildSloStatus,
computeSloBurn,
computeSloWindow,
hasSloQueryTime,
type SloBurnNumbers,
type SloDefinitionFields,
SloQueryDeadlineError,
type SloWindowNumbers,
} from '@/controllers/sloStatusQueries';
Expand Down Expand Up @@ -54,6 +55,25 @@ const refreshCounter = getCounter('hyperdx.slo.refresh', {
description: 'SLO status refreshes, by outcome',
});

/**
* The SLO's numbers as of now, for one SLO someone asked for. Not stored:
* the background refresh keeps the stored snapshot for lists.
*/
export async function computeSloStatusNow(
slo: SloDefinitionFields,
ctx: SloQueryContext,
now = new Date(),
): Promise<SloStatus> {
const window = await computeSloWindow(slo, ctx, now);
const burn = await computeSloBurn(
slo,
ctx,
now,
window.groups?.map(g => g.value),
);
return buildSloStatus(slo, window, burn, now.toISOString(), now);
}

/**
* Stores the burn numbers an SLO's alert check computed, so the list shows
* the alert's own verdict: for an ungrouped SLO its burn numbers, for a
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/sloStatusQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import type { ISlo } from '@/models/slo';
const SLO_QUERY_TIMEOUT_MS = 30_000;
const SLO_MIN_QUERY_MS = 5_000;

type SloDefinitionFields = Pick<
export type SloDefinitionFields = Pick<
ISlo,
'indicator' | 'target' | 'window' | 'groupBy'
>;
Expand Down
228 changes: 228 additions & 0 deletions packages/api/src/mcp/__tests__/slos.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
import { Types } from 'mongoose';

import * as config from '@/config';
import { getSlo, getSloQueryContext, listSlos } from '@/controllers/slo';
import { getSloAlerts } from '@/controllers/sloAlert';
import { computeSloStatusNow } from '@/controllers/sloStatus';
import { AlertState } from '@/models/alert';

import { callTool, createTestClient, getFirstText } from './mcpTestUtils';

jest.mock('@/controllers/slo', () => ({
...jest.requireActual('@/controllers/slo'),
listSlos: jest.fn(),
getSlo: jest.fn(),
getSloQueryContext: jest.fn(),
}));
jest.mock('@/controllers/sloAlert', () => ({ getSloAlerts: jest.fn() }));
jest.mock('@/controllers/sloStatus', () => ({
computeSloStatusNow: jest.fn(),
}));

const sloId = new Types.ObjectId();
const slo: any = {
_id: sloId,
version: 1,
team: new Types.ObjectId(),
name: 'Checkout availability',
tags: ['checkout'],
source: new Types.ObjectId(),
indicator: {
template: 'availability',
eligible: { where: "SpanKind = 'Server'", whereLanguage: 'sql' },
good: "StatusCode != 'Error'",
},
target: 99.5,
window: '30d',
alerts: { fastBurn: { channels: [{ type: 'webhook', webhookId: 'w' }] } },
status: {
state: 'burning',
sli: 0.99,
budgetRemaining: 0.4,
burn: [{ window: '1h', burnRate: 20 }],
},
createdAt: new Date('2026-01-01T00:00:00Z'),
updatedAt: new Date('2026-01-02T00:00:00Z'),
};

// An SQL where clause and SQL filters render without reading metadata.
const queryContext: any = {
source: {
id: 'source-1',
kind: 'trace',
connection: 'connection-1',
from: { databaseName: 'otel', tableName: 'otel_traces' },
timestampValueExpression: 'Timestamp',
},
clickhouseClient: {},
metadata: {},
};

const connect = () =>
createTestClient({ teamId: slo.team.toString(), userId: 'user-id' });

const toolNames = async () =>
(await (await connect()).listTools()).tools.map(tool => tool.name);

describe('MCP SLO tool', () => {
let slosEnabled: jest.ReplaceProperty<boolean>;

beforeEach(() => {
slosEnabled = jest.replaceProperty(config, 'IS_SLOS_ENABLED', true);
jest.mocked(listSlos).mockResolvedValue([slo]);
jest.mocked(getSlo).mockResolvedValue(slo);
jest.mocked(getSloQueryContext).mockResolvedValue(queryContext);
jest.mocked(getSloAlerts).mockResolvedValue(
new Map([
[
sloId.toString(),
{
fastBurn: {
id: 'alert-1',
state: AlertState.ALERT,
silencedUntil: null,
},
},
],
]),
);
});

afterEach(() => slosEnabled.restore());

it('is only registered while SLOs are enabled', async () => {
const slosTools = ['clickstack_get_slo'];
expect(await toolNames()).toEqual(expect.arrayContaining(slosTools));
slosEnabled.replaceValue(false);
const names = await toolNames();
expect(slosTools.filter(name => names.includes(name))).toEqual([]);
});

it('lists SLOs as a summary, filtered by state', async () => {
const client = await connect();
const all = JSON.parse(
getFirstText(await callTool(client, 'clickstack_get_slo')),
);
expect(all).toEqual([
{
id: sloId.toString(),
name: 'Checkout availability',
tags: ['checkout'],
target: 99.5,
window: '30d',
groupBy: null,
state: 'burning',
sli: 0.99,
budgetRemaining: 0.4,
burnRate: 20,
alertStates: { fastBurn: AlertState.ALERT, slowBurn: null },
},
]);
const healthy = JSON.parse(
getFirstText(
await callTool(client, 'clickstack_get_slo', { state: 'healthy' }),
),
);
expect(healthy).toEqual([]);
});

it('returns one SLO with its status as of now and the query for its bad events', async () => {
const live = {
...slo.status,
state: 'healthy',
burn: [{ window: '1h', burnRate: 1 }],
};
jest.mocked(computeSloStatusNow).mockResolvedValue(live);
const result = await callTool(await connect(), 'clickstack_get_slo', {
id: sloId.toString(),
});
const detail = JSON.parse(getFirstText(result));
expect(detail).toEqual(
expect.objectContaining({
id: sloId.toString(),
indicator: slo.indicator,
alerts: slo.alerts,
alertStates: {
fastBurn: {
id: 'alert-1',
state: AlertState.ALERT,
silencedUntil: null,
},
},
status: live,
statusSource: 'live',
// One where clause, which is all clickstack_search takes.
badEventsWhere:
"(SpanKind = 'Server') AND (NOT ifNull((StatusCode != 'Error'), 0))",
}),
);
// Read for the calling team only.
expect(getSlo).toHaveBeenCalledWith(slo.team.toString(), sloId.toString());
});

it('scopes the bad events of a grouped SLO to one group', async () => {
jest
.mocked(getSlo)
.mockResolvedValue({ ...slo, groupBy: "ResourceAttributes['k8s.ns']" });
jest.mocked(computeSloStatusNow).mockResolvedValue(slo.status);
const url = jest.replaceProperty(config, 'FRONTEND_URL', 'http://app');
try {
const result = await callTool(await connect(), 'clickstack_get_slo', {
id: sloId.toString(),
group: "o'brien",
});
expect(JSON.parse(getFirstText(result))).toEqual(
expect.objectContaining({
groupBy: "ResourceAttributes['k8s.ns']",
badEventsWhere:
"(SpanKind = 'Server') AND " +
"(toString(ResourceAttributes['k8s.ns']) = 'o''brien') AND " +
"(NOT ifNull((StatusCode != 'Error'), 0))",
url: `http://app/slos/${sloId}?group=o%27brien`,
}),
);
} finally {
url.restore();
}
});

it('refuses a group for an SLO that is not grouped', async () => {
const result = await callTool(await connect(), 'clickstack_get_slo', {
id: sloId.toString(),
group: 'checkout',
});
expect(result.isError).toBe(true);
expect(getFirstText(result)).toContain('not grouped');
});

it('falls back to the stored status when computing it fails', async () => {
jest
.mocked(computeSloStatusNow)
.mockRejectedValue(new Error('Timeout error'));
const result = await callTool(await connect(), 'clickstack_get_slo', {
id: sloId.toString(),
});
expect(JSON.parse(getFirstText(result))).toEqual(
expect.objectContaining({
status: slo.status,
statusSource: 'stored',
}),
);
});

it('reports a malformed or unknown id', async () => {
const client = await connect();
const malformed = await callTool(client, 'clickstack_get_slo', {
id: 'nope',
});
expect(malformed.isError).toBe(true);
expect(getFirstText(malformed)).toContain('Invalid SLO ID');

jest.mocked(getSlo).mockResolvedValue(null);
const unknown = await callTool(client, 'clickstack_get_slo', {
id: new Types.ObjectId().toString(),
});
expect(unknown.isError).toBe(true);
expect(getFirstText(unknown)).toContain('SLO not found');
});
});
4 changes: 3 additions & 1 deletion packages/api/src/mcp/mcpServer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import type { Implementation } from '@modelcontextprotocol/sdk/types.js';

import { CODE_VERSION, FRONTEND_URL } from '@/config';
import { CODE_VERSION, FRONTEND_URL, IS_SLOS_ENABLED } from '@/config';

import dashboardPrompts from './prompts/dashboards/index';
import alertsTools from './tools/alerts/index';
Expand All @@ -12,6 +12,7 @@ import {
} from './tools/query/builderCatalog';
import queryTools from './tools/query/index';
import savedSearchesTools from './tools/savedSearches/index';
import slosTools from './tools/slos/index';
import sourcesTools from './tools/sources/index';
import traceTools from './tools/trace/index';
import { McpContext } from './tools/types';
Expand Down Expand Up @@ -85,6 +86,7 @@ export function createServer(context: McpContext) {
dashboardsTools(registrar);
queryTools(registrar);
savedSearchesTools(registrar);
if (IS_SLOS_ENABLED) slosTools(registrar);
traceTools(registrar);
dashboardPrompts(server, context);

Expand Down
Loading
Loading