Repository navigation
fix(mcp): stop clickstack_sql callers guessing table and timestamp names #3354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cf7b0d8
1c68142
5fb2477
cc07b20
faf11ef
1efd7d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| --- | ||
| '@hyperdx/api': patch | ||
| --- | ||
|
|
||
| fix(mcp): stop `clickstack_sql` callers from guessing table and timestamp column | ||
| names. `clickstack_list_sources` now returns each source's database-qualified | ||
| `table` (and `database` for metric sources), the `sql` examples use `<table>` | ||
| and `<timestampColumn>` placeholders instead of `otel_logs` and `TimestampTime`, | ||
| and unknown-table errors from `clickstack_sql` point at `clickstack_list_sources`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -684,7 +684,7 @@ export async function runConfigTile( | |
| }); | ||
| return formatQueryResult(result); | ||
| } catch (e) { | ||
| return clickHouseErrorResult(e); | ||
| return clickHouseErrorResult(e, { rawSql: true }); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -841,11 +841,11 @@ function hasNodeErrorCode(val: unknown, codes: ReadonlySet<string>): boolean { | |
| */ | ||
| export function clickHouseErrorResult( | ||
| e: unknown, | ||
| context?: string | { prefix?: string; suffix?: string }, | ||
| context?: string | { prefix?: string; suffix?: string; rawSql?: boolean }, | ||
| ): McpErrorResult { | ||
| const { prefix, suffix } = | ||
| const { prefix, suffix, rawSql } = | ||
| typeof context === 'string' | ||
| ? { prefix: context, suffix: undefined } | ||
| ? { prefix: context, suffix: undefined, rawSql: undefined } | ||
| : (context ?? {}); | ||
|
|
||
| // Prefer .message, but fall back to .cause.message when the wrapper | ||
|
|
@@ -857,7 +857,7 @@ export function clickHouseErrorResult( | |
| (e.cause instanceof Error ? e.cause.message : '') || | ||
| String(e) | ||
| : String(e); | ||
| const hint = errorHint(raw, e); | ||
| const hint = errorHint(raw, e, { rawSql }); | ||
| const base = hint ? `${raw}\n\nHINT: ${hint}` : raw; | ||
| const text = `${prefix ? `${prefix}: ` : ''}${base}${suffix ? ` ${suffix}` : ''}`; | ||
|
|
||
|
|
@@ -897,7 +897,11 @@ export function isQueryOutOfTime(e: unknown): boolean { | |
| } | ||
|
|
||
| /** @internal Exported for testing only. */ | ||
| export function errorHint(msg: string, error?: unknown): string | null { | ||
| export function errorHint( | ||
| msg: string, | ||
| error?: unknown, | ||
| options?: { rawSql?: boolean }, | ||
| ): string | null { | ||
| const unknownVariableError = findCause( | ||
| error, | ||
| (e): e is UnknownVariableError => e instanceof UnknownVariableError, | ||
|
|
@@ -954,6 +958,22 @@ export function errorHint(msg: string, error?: unknown): string | null { | |
| 'Add a LIMIT, narrow the time range, or add filters to reduce the result set.' | ||
| ); | ||
| } | ||
| // Checked before unknown identifiers: "Unknown table expression identifier" | ||
| // names a table, so describe_source's column list would not help. Only raw | ||
| // SQL names its own table; source-based tools would be sent back to the | ||
| // same source table that just failed. | ||
| if ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 minor — New unknown-table hint is SQL-specific but shown for every MCP tool's ClickHouse errors
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in cc07b20:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 minor — The unknown-table hint also fires for source-based builder tools, where the agent never wrote a table name
Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in cc07b20: |
||
| options?.rawSql && | ||
| /Unknown table expression identifier|(Table|Database) \S+ does not exist|UNKNOWN_(TABLE|DATABASE)/.test( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔵 minor — Unknown-table regex misses the "doesn't exist" wording Older ClickHouse servers report Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond |
||
| msg, | ||
| ) | ||
| ) { | ||
| return ( | ||
| 'Use the `table` of a source from clickstack_list_sources, which is ' + | ||
| 'qualified with its database. Unqualified names resolve only in the ' + | ||
| "connection's default database." | ||
| ); | ||
| } | ||
| if ( | ||
| /Unknown (expression|identifier)|UNKNOWN_IDENTIFIER|Missing columns/i.test( | ||
| msg, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 minor — The rawSql hint reaches dashboard raw-SQL tiles too, not just clickstack_sql
runConfigTile's raw-SQL branch is also called byclickstack_query_tileandclickstack_query_tiles(tools/dashboards/queryTile.ts:85,queryTiles.ts:303). For a tile using$__sourceTable, the failing table is the source's own table, so pointing the agent at clickstack_list_sources just returns that same table. That is the case this PR deliberately excludes for builder tools. Fix: passrawSqlfromsql.tsvia arunConfigTileoption, or set it only whensavedConfig.sourceis unset.Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond