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

fix(mcp): `clickstack_sql` now binds the bucket-size parameters its description
advertises. `{intervalSeconds:Int64}`, `{intervalMilliseconds:Int64}`,
`$__interval_s`, `$__timeInterval` and `$__timeInterval_ms` used to fail with
"Substitution intervalSeconds is not set", including the tool's own time-series
example.
70 changes: 70 additions & 0 deletions packages/api/src/mcp/__tests__/queryTool.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,76 @@ describe('MCP Query Tools', () => {

expect(result.isError).toBeFalsy();
});

// The tool description advertises these bucket-size parameters and
// macros, so each one has to resolve to a value rather than fail with
// "Substitution intervalSeconds is not set".
it('binds the interval parameters and $__interval_s to the same bucket size', async () => {
const result = await callTool(client, 'clickstack_sql', {
connectionId: connection._id.toString(),
sql:
'SELECT {intervalSeconds:Int64} AS s, ' +
'{intervalMilliseconds:Int64} AS ms, ' +
'$__interval_s AS macro_s',
startTime: new Date(Date.now() - 60 * 60 * 1000).toISOString(),
endTime: new Date().toISOString(),
});

expect(result.isError).toBeFalsy();
const [row] = JSON.parse(getFirstText(result)).result.data;
const seconds = Number(row.s);
expect(seconds).toBeGreaterThan(0);
expect(Number(row.ms)).toBe(seconds * 1000);
expect(Number(row.macro_s)).toBe(seconds);
});

it.each(['$__timeInterval', '$__timeInterval_ms'])(
'runs a time-series query bucketed with %s',
async macro => {
const now = Date.now();
await bulkInsertLogs(
[5, 10, 15, 20].map(minutesAgo => ({
Body: 'bucketed',
ServiceName: 'sql-buckets',
SeverityText: 'INFO',
Timestamp: new Date(now - minutesAgo * 60 * 1000),
})),
);

const result = await callTool(client, 'clickstack_sql', {
connectionId: connection._id.toString(),
sql:
`SELECT ${macro}(Timestamp) AS ts, ` +
'toUnixTimestamp(ts) % $__interval_s AS offset, count() AS cnt ' +
`FROM ${DEFAULT_DATABASE}.${DEFAULT_LOGS_TABLE} ` +
"WHERE $__timeFilter(Timestamp) AND ServiceName = 'sql-buckets' " +
'GROUP BY ts ORDER BY ts LIMIT 10',
startTime: new Date(now - 60 * 60 * 1000).toISOString(),
endTime: new Date(now).toISOString(),
});

expect(getFirstText(result)).not.toContain('Substitution');
expect(result.isError).toBeFalsy();
const rows: { offset: unknown; cnt: unknown }[] = JSON.parse(
getFirstText(result),
).result.data;
expect(rows).toHaveLength(4);
expect(rows.map(row => Number(row.offset))).toEqual([0, 0, 0, 0]);
expect(rows.map(row => Number(row.cnt))).toEqual([1, 1, 1, 1]);
},
);

it('returns plain query rows unchanged', async () => {
const result = await callTool(client, 'clickstack_sql', {
connectionId: connection._id.toString(),
sql: "SELECT 1 AS value, 'a' AS label",
});

expect(result.isError).toBeFalsy();
expect(JSON.parse(getFirstText(result)).result.data).toEqual([
{ value: 1, label: 'a' },
]);
});
});

// ─── ClickHouse query errors ──────────────────────────────────────────────────
Expand Down
6 changes: 5 additions & 1 deletion packages/api/src/mcp/tools/query/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,13 @@ export function registerSql({ context, registerTool }: ToolRegistrar) {
);
}

// Raw SQL only uses displayType to pick which query params to bind, and
// only time-series types bind the interval params this tool advertises
// ({intervalSeconds}, $__interval_s, $__timeInterval). Rows come back
// the same either way, and ClickHouse ignores params a query never uses.
const tile = buildTile('MCP SQL', 24, 6, {
configType: 'sql' as const,
displayType: 'table' as const,
displayType: 'line' as const,
connectionId: input.connectionId,
sqlTemplate: input.sql,
});
Expand Down
Loading