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
15 changes: 15 additions & 0 deletions .changeset/slo-mcp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@hyperdx/api': patch
---

feat: SLO tools for MCP, registered when SLOs are enabled

Lists SLOs as a summary from their stored status, optionally by state, or
returns one SLO with its definition, its status computed when asked (per
group for a grouped SLO), its alerts' states and a SQL where clause that
selects its bad events for `clickstack_search`, for one group when asked.
`clickstack_preview_slo` dry runs a definition (good, bad and excluded
events, SLI, budget left, group count and the counts SQL),
`clickstack_save_slo` creates or updates 1 to 20 SLOs with an error per
failed item, and `clickstack_delete_slo` deletes an SLO with its alerts. They
use the same checks and team scoping as the SLO pages.
3 changes: 3 additions & 0 deletions MCP.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ with:
| `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_preview_slo` | Dry run an SLO definition: good, bad and excluded events, SLI, budget left, group count and the counts SQL. Only registered when SLOs are enabled |
| `clickstack_save_slo` | Create or update 1 to 20 SLOs, each validated and saved on its own, with an error per failed item. Only registered when SLOs are enabled |
| `clickstack_delete_slo` | Permanently delete an SLO and its burn alerts. 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
222 changes: 222 additions & 0 deletions packages/api/src/mcp/__tests__/slos.int.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import { SourceKind } from '@hyperdx/common-utils/dist/types';
import type { Client } from '@modelcontextprotocol/sdk/client/index.js';
import mongoose from 'mongoose';

import * as config from '@/config';
import {
bulkInsertData,
DEFAULT_DATABASE,
DEFAULT_TRACES_TABLE,
getLoggedInAgent,
getServer,
} from '@/fixtures';
import type { McpContext } from '@/mcp/tools/types';
import Alert, { AlertSource } from '@/models/alert';
import Connection from '@/models/connection';
import Slo from '@/models/slo';
import { Source } from '@/models/source';
import Webhook from '@/models/webhook';

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

// ClickHouse is shared across tests and files, so scope the spans to this run.
const SERVICE = `mcp-slo-${Date.now()}`;

describe('MCP SLO write tools', () => {
const server = getServer();
let client: Client;
let teamId: string;
let sourceId: string;
let webhookId: string;
let slosEnabled: jest.ReplaceProperty<boolean>;

const definition = (overrides: Record<string, unknown> = {}) => ({
name: 'Checkout availability',
source: sourceId,
indicator: {
template: 'availability',
eligible: {
where: `startsWith(ServiceName, '${SERVICE}-')`,
whereLanguage: 'sql',
},
good: "StatusCode != 'Error'",
},
target: 99,
window: '7d',
...overrides,
});

const call = async (name: string, args: Record<string, unknown>) => {
const result = await callTool(client, name, args);
return { isError: result.isError, text: getFirstText(result) };
};

beforeAll(async () => {
await server.start();
const now = Date.now();
// One service with every span good, one with a fifth of them bad.
await bulkInsertData(
`${DEFAULT_DATABASE}.${DEFAULT_TRACES_TABLE}`,
[`${SERVICE}-a`, `${SERVICE}-b`].flatMap((service, s) =>
Array.from({ length: 50 }, (_, i) => ({
Timestamp: new Date(now - (i + 1) * 60_000),
TraceId: `${service}-trace-${i}`,
SpanId: `${service}-span-${i}`,
ParentSpanId: '',
SpanName: 'POST /checkout',
SpanKind: 'Server',
ServiceName: service,
Duration: 100 * 1e6,
StatusCode: s === 1 && i < 10 ? 'Error' : 'Ok',
StatusMessage: '',
SpanAttributes: {},
ResourceAttributes: {},
})),
),
);
});

beforeEach(async () => {
slosEnabled = jest.replaceProperty(config, 'IS_SLOS_ENABLED', true);
const { team, user } = await getLoggedInAgent(server);
teamId = team._id.toString();
const connection = await Connection.create({
team: team._id,
name: 'Default',
host: config.CLICKHOUSE_HOST,
username: config.CLICKHOUSE_USER,
password: config.CLICKHOUSE_PASSWORD,
});
const source = await Source.create({
kind: SourceKind.Trace,
team: team._id,
from: { databaseName: DEFAULT_DATABASE, tableName: DEFAULT_TRACES_TABLE },
timestampValueExpression: 'Timestamp',
connection: connection._id,
name: 'Traces',
traceIdExpression: 'TraceId',
spanIdExpression: 'SpanId',
parentSpanIdExpression: 'ParentSpanId',
spanNameExpression: 'SpanName',
spanKindExpression: 'SpanKind',
durationExpression: 'Duration',
durationPrecision: 9,
statusCodeExpression: 'StatusCode',
});
sourceId = source._id.toString();
const webhook = await Webhook.create({
name: 'On call',
service: 'generic',
url: 'https://example.com/hook',
team: team._id,
});
webhookId = webhook._id.toString();
const context: McpContext = { teamId, userId: user._id.toString() };
client = await createTestClient(context);
});

afterEach(async () => {
await client?.close();
slosEnabled.restore();
await server.clearDBs();
});

afterAll(async () => {
await server.stop();
});

it('previews a grouped definition from the source', async () => {
const { isError, text } = await call('clickstack_preview_slo', {
definition: definition({ name: undefined, groupBy: 'ServiceName' }),
});
expect(isError).toBeFalsy();
expect(JSON.parse(text)).toMatchObject({
eligible: 100,
good: 90,
bad: 10,
sli: 0.9,
groupCount: 2,
trackedGroups: 2,
sql: expect.stringContaining('countIf'),
});
expect(await Slo.countDocuments()).toBe(0);
});

it('saves a batch with the checks of the SLO pages, then updates and deletes', async () => {
const saved = JSON.parse(
(
await call('clickstack_save_slo', {
slos: [
definition({
groupBy: 'ServiceName',
alerts: {
fastBurn: { channels: [{ type: 'webhook', webhookId }] },
},
}),
// ClickHouse rejects the condition, so this one is not saved.
definition({
name: 'Typo',
indicator: {
...definition().indicator,
good: "StatusCod != 'Error'",
},
}),
],
})
).text,
);
expect(saved).toMatchObject({ saved: 1, failed: 1 });
expect(saved.results[1]).toMatchObject({
index: 1,
ok: false,
error: expect.stringContaining('StatusCod'),
});
const id = saved.results[0].id;
const slo = await Slo.findById(id).lean();
expect(slo).toMatchObject({
name: 'Checkout availability',
groupBy: 'ServiceName',
});
expect(slo?.team.toString()).toBe(teamId);
expect(
await Alert.countDocuments({ slo: id, source: AlertSource.SLO }),
).toBe(1);

// An update replaces the definition: without alerts, the alert goes.
const updated = JSON.parse(
(
await call('clickstack_save_slo', {
slos: [definition({ id, name: 'Checkout success' })],
})
).text,
);
expect(updated.results[0]).toMatchObject({ ok: true, id });
expect((await Slo.findById(id).lean())?.name).toBe('Checkout success');
expect(await Alert.countDocuments({ slo: id })).toBe(0);

expect(
JSON.parse((await call('clickstack_delete_slo', { id })).text),
).toEqual({ deleted: true, id });
expect(await Slo.exists({ _id: id })).toBeNull();
});

it("leaves another team's SLO alone", async () => {
const other = await Slo.create({
...definition(),
source: new mongoose.Types.ObjectId(),
team: new mongoose.Types.ObjectId(),
});
const id = other._id.toString();
const deleted = await call('clickstack_delete_slo', { id });
expect(deleted.isError).toBe(true);
expect(deleted.text).toContain('SLO not found');
const updated = JSON.parse(
(await call('clickstack_save_slo', { slos: [definition({ id })] })).text,
);
expect(updated.results[0]).toMatchObject({
ok: false,
error: 'SLO not found',
});
expect(await Slo.exists({ _id: id })).toBeTruthy();
});
});
Loading
Loading