Skip to content

Commit 7e7159a

Browse files
fix(mcp): improve error hints and improve readonly mode for query safety settings (#2433)
## Summary Improves two error hints that fire when agents hit ClickHouse errors during investigations: - **DateTime64 cast errors** now recommend `parseDateTime64BestEffort()` (works on both DateTime and DateTime64 columns) instead of `toDateTime64('...', 9)` (DateTime64-only) - **Unknown column/identifier errors** now hint to call `describe_source` before retrying MCP query tools sent `readonly=1` alongside `max_execution_time` and `max_result_rows`, but ClickHouse's readonly mode 1 rejects all setting changes — so the safety limits were silently ignored. This switches to `readonly=2` (blocks writes, allows setting overrides) and adds an integration test that catches the regression. ### Eval results Ran the full 5-scenario eval suite (5 runs each, feature vs main) with synchronized seed data. Overall **+3% improvement** across the board: | Scenario | Feature | Main | Δ | |---|---|---|---| | error-root-cause | 99% | 95% | +4% | | latency-spike | 83% | 70% | **+14%** | | noisy-signals | 66% | 60% | +6% | | segmented-regression | 67% | 79% | -12% | | service-health-check | 59% | 56% | +3% | The latency-spike scenario saw the largest gain (+14%) — the improved DateTime64 hint helps agents recover from timestamp cast errors in one retry instead of 2-3. The segmented-regression dip is within variance (judge scores are tied at 78%; the gap is entirely from tool-error penalty noise). ### Related issues - [HDX-4473](https://linear.app/clickhouse/issue/HDX-4473/improve-datetime64-error-hint-to-recommend-universal-safe) — Improve DateTime64 error hint to recommend parseDateTime64BestEffort - [HDX-4474](https://linear.app/clickhouse/issue/HDX-4474/strengthen-describesource-nudge-and-add-unknown-column-error-hint) — Add unknown-column error hint (nudge portion deferred — eval showed proactive nudges hurt turn budget) - [HDX-4472](https://linear.app/clickhouse/issue/HDX-4472/fix-per-select-where-timestamp-example-to-use) — Fix per-select where timestamp example (deferred — eval showed inline warnings increase agent verbosity) - [HDX-4475](https://linear.app/clickhouse/issue/HDX-4475/restructure-sql-tool-timestamp-docs-with-clear-macro-hierarchy-and) — Restructure SQL tool timestamp docs (deferred — eval showed restructured docs increase SQL usage without improving scores) ### Other Fixes `deniedTools` in the eval setup template to use correct `clickstack_*` tool name prefix (the old `hyperdx_*` names didn't match any tools, so no tools were actually denied).
1 parent 5bd1c68 commit 7e7159a

5 files changed

Lines changed: 71 additions & 19 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
'@hyperdx/api': patch
3+
---
4+
5+
fix(mcp): improve error hints and fix readonly mode for query safety settings
6+
7+
Switch MCP ClickHouse safety settings from readonly=1 to readonly=2 so
8+
max_execution_time and max_result_rows are actually applied (readonly=1
9+
silently rejects all setting changes).
10+
11+
Improve DateTime64 cast error hint to recommend parseDateTime64BestEffort()
12+
which works on both DateTime and DateTime64 columns, replacing
13+
toDateTime64() which only works on DateTime64.
14+
15+
Add error hint for unknown column/identifier errors directing agents to
16+
call describe_source before retrying.

‎packages/api/src/mcp/__tests__/query.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,15 @@ describe('errorHint', () => {
188188
"Cannot convert string '2025-01-01T00:00:00Z' to type DateTime64(9)",
189189
);
190190
expect(hint).not.toBeNull();
191-
expect(hint).toContain('toDateTime64');
191+
expect(hint).toContain('parseDateTime64BestEffort');
192192
});
193193

194194
it('should match DateTime64 parse errors', () => {
195195
const hint = errorHint(
196196
"Cannot parse string '2025-01-01' as type DateTime64",
197197
);
198198
expect(hint).not.toBeNull();
199-
expect(hint).toContain('toDateTime64');
199+
expect(hint).toContain('parseDateTime64BestEffort');
200200
});
201201

202202
it('should match AS alias syntax errors with word boundary', () => {

‎packages/api/src/mcp/__tests__/queryTool.test.ts‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,15 +782,36 @@ describe('MCP Query Tools', () => {
782782
expect(parsed.result?.data?.[0]?.value).toBe(100000);
783783
});
784784

785-
it('should propagate readonly=1 to ClickHouse', async () => {
785+
it('should propagate readonly=2 to ClickHouse', async () => {
786786
const result = await callTool(client, 'clickstack_sql', {
787787
connectionId: connection._id.toString(),
788788
sql: "SELECT getSetting('readonly') AS value",
789789
});
790790

791791
expect(result.isError).toBeFalsy();
792792
const parsed = JSON.parse(getFirstText(result));
793-
expect(parsed.result?.data?.[0]?.value).toBe(1);
793+
expect(parsed.result?.data?.[0]?.value).toBe(2);
794+
});
795+
796+
it('should apply all safety settings together without readonly conflicts', async () => {
797+
// readonly=1 rejects setting changes, so max_execution_time and
798+
// max_result_rows would be silently ignored. readonly=2 allows
799+
// setting changes while still blocking writes. This test verifies
800+
// all three settings are applied in a single query.
801+
const result = await callTool(client, 'clickstack_sql', {
802+
connectionId: connection._id.toString(),
803+
sql: `SELECT
804+
getSetting('readonly') AS readonly_mode,
805+
getSetting('max_execution_time') AS max_exec_time,
806+
getSetting('max_result_rows') AS max_rows`,
807+
});
808+
809+
expect(result.isError).toBeFalsy();
810+
const parsed = JSON.parse(getFirstText(result));
811+
const row = parsed.result?.data?.[0];
812+
expect(row?.readonly_mode).toBe(2);
813+
expect(row?.max_exec_time).toBe(30);
814+
expect(row?.max_rows).toBe(100000);
794815
});
795816
});
796817
});

‎packages/api/src/mcp/tools/query/helpers.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ export const SAFE_BODY_EXPR_CHARS = /^[\w.':\[\]\-]+$/;
6363

6464
// ─── Safety limits ───────────────────────────────────────────────────────────
6565

66-
/** ClickHouse settings applied to all MCP query-tool executions. */
66+
/** ClickHouse settings applied to all MCP query-tool executions.
67+
* readonly=2 so max_execution_time and max_result_rows can be set
68+
* (readonly=1 rejects all setting changes). */
6769
const MCP_CLICKHOUSE_SETTINGS = {
6870
max_execution_time: 30,
6971
max_result_rows: '100000',
70-
readonly: 1,
72+
readonly: 2,
7173
} as const;
7274

7375
/**
@@ -451,10 +453,10 @@ export function errorHint(msg: string): string | null {
451453
/Cannot (convert|parse) string .* (to|as) (type )?DateTime64/i.test(msg)
452454
) {
453455
return (
454-
"Wrap ISO timestamps with `toDateTime64('YYYY-MM-DD HH:MM:SS', 9)` " +
455-
'or use the supplied macros: `$__timeFilter(Timestamp)` (the easiest), ' +
456-
'`{startDateMilliseconds:Int64}`, `{endDateMilliseconds:Int64}`. ' +
457-
'Bare ISO 8601 strings will NOT auto-cast to DateTime64.'
456+
"Wrap ISO timestamps with `parseDateTime64BestEffort('YYYY-MM-DDTHH:MM:SSZ')` — " +
457+
'this works for both DateTime and DateTime64 columns. For the sql tool, prefer ' +
458+
'`$__timeFilter(Timestamp)` which handles casting automatically. ' +
459+
'Bare ISO 8601 strings will NOT auto-cast to DateTime/DateTime64.'
458460
);
459461
}
460462
if (/Syntax error.*\bAS\b/.test(msg)) {
@@ -479,5 +481,15 @@ export function errorHint(msg: string): string | null {
479481
'Add a LIMIT, narrow the time range, or add filters to reduce the result set.'
480482
);
481483
}
484+
if (
485+
/Unknown (expression|identifier)|UNKNOWN_IDENTIFIER|Missing columns/i.test(
486+
msg,
487+
)
488+
) {
489+
return (
490+
'Call clickstack_describe_source to discover available columns and ' +
491+
'map attribute keys before retrying.'
492+
);
493+
}
482494
return null;
483495
}

‎packages/hdx-eval/src/hyperdx/setup.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,18 @@ export async function runSetup(opts: SetupOptions): Promise<SetupResult> {
124124
label: 'HyperDX',
125125
brandTerms: ['HyperDX', 'hyperdx'],
126126
deniedTools: [
127-
'mcp__hyperdx__hyperdx_delete_dashboard',
128-
'mcp__hyperdx__hyperdx_get_dashboard',
129-
'mcp__hyperdx__hyperdx_save_dashboard',
130-
'mcp__hyperdx__hyperdx_query_tile',
131-
'mcp__hyperdx__hyperdx_get_saved_search',
132-
'mcp__hyperdx__hyperdx_save_saved_search',
133-
'mcp__hyperdx__hyperdx_get_alert',
134-
'mcp__hyperdx__hyperdx_get_webhook',
135-
'mcp__hyperdx__hyperdx_save_alert',
127+
'mcp__hyperdx__clickstack_delete_dashboard',
128+
'mcp__hyperdx__clickstack_get_dashboard',
129+
'mcp__hyperdx__clickstack_get_dashboard_tile',
130+
'mcp__hyperdx__clickstack_save_dashboard',
131+
'mcp__hyperdx__clickstack_patch_dashboard',
132+
'mcp__hyperdx__clickstack_search_dashboards',
133+
'mcp__hyperdx__clickstack_query_tile',
134+
'mcp__hyperdx__clickstack_get_saved_search',
135+
'mcp__hyperdx__clickstack_save_saved_search',
136+
'mcp__hyperdx__clickstack_get_alert',
137+
'mcp__hyperdx__clickstack_get_webhook',
138+
'mcp__hyperdx__clickstack_save_alert',
136139
],
137140
};
138141

0 commit comments

Comments
 (0)