Repository navigation
fix(mcp): steer agents toward builder query tools instead of raw SQL (HDX-4892) #2840
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
Merged
kodiakhq
merged 11 commits into
main
from
brandon/brandon-mcp-sql-tool-use-investigation
Aug 12, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9e1b0d6
fix(mcp): steer agents toward builder query tools instead of raw SQL …
brandon-pereira 2de2e5e
Merge remote-tracking branch 'origin/main' into brandon/brandon-mcp-s…
brandon-pereira 5f1ea5a
feat(hdx-eval): preflight MCP reachability before running a batch
brandon-pereira 1d78236
Merge branch 'main' into brandon/brandon-mcp-sql-tool-use-investigation
brandon-pereira 42a5b08
fix(hdx-eval): run MCP preflight before seed detection/reseed
brandon-pereira 29cbc62
refactor(mcp): centralize builder-tool steering catalog
brandon-pereira 5c1af4c
refactor(mcp): simplify builder-tool steering to shared string constants
brandon-pereira ce47c11
test(hdx-eval): trim preflight test to essential cases
brandon-pereira 6b82767
Merge branch 'main' into brandon/brandon-mcp-sql-tool-use-investigation
brandon-pereira 7534841
docs(mcp): note BUILDER_TOOLS_LIST must be updated alongside tool table
brandon-pereira f46af10
Merge remote-tracking branch 'origin/main' into brandon/brandon-mcp-s…
brandon-pereira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@hyperdx/api': patch | ||
| --- | ||
|
|
||
| fix(mcp): steer agents toward builder query tools instead of raw SQL (HDX-4892). Telemetry showed agents (notebook investigations) using `clickstack_sql` for ~73% of data queries — usually for single-source aggregations, top-N, and time-series that the builder tools express more reliably (raw SQL also had ~2x the error rate). Reworded the `clickstack_sql` description to mark it a last resort, added a reciprocal "prefer me over SQL" nudge to `clickstack_table`, `clickstack_timeseries`, and `clickstack_search`, and added a server-level `instructions` tool-selection policy so the guidance is surfaced on `initialize` rather than only via the opt-in `query_guide` prompt. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /** | ||
| * Shared "prefer the builder tools over raw SQL" steering, defined once so the | ||
| * server instructions, the clickstack_sql description, and the builder tools' | ||
| * descriptions can't drift apart. | ||
| */ | ||
|
|
||
| /** Bulleted list of the builder (non-SQL) query tools and what they're for. */ | ||
| export const BUILDER_TOOLS_LIST = [ | ||
| ' • clickstack_table — aggregations, top-N, single-value KPIs, breakdowns', | ||
| ' • clickstack_timeseries — trends / metrics over time', | ||
| ' • clickstack_search — browsing individual log/trace rows', | ||
| ' • clickstack_event_patterns — recurring log/event pattern discovery', | ||
| ' • clickstack_event_deltas — attributes that differ between two row groups', | ||
| ' • clickstack_emerging_signals — patterns new or gone vs a baseline window', | ||
| ' • clickstack_trace_waterfall — one trace as a parent/child span tree', | ||
| ' • clickstack_trace_top_time_consuming_operations — slowest child operations in a trace', | ||
| ].join('\n'); | ||
|
|
||
| /** When raw SQL is actually warranted. */ | ||
| export const SQL_FALLBACK_CRITERIA = | ||
| 'JOINs, sub-queries, CTEs, window functions, tables not registered as sources, or ' + | ||
| 'summary-type metrics (the metricTables.summary table on a metric source, which the ' + | ||
| 'builder tools cannot query)'; | ||
|
|
||
| /** | ||
| * Reciprocal nudge dropped into each builder tool's description. Keep it | ||
| * generic so a single string works for every builder tool. | ||
| */ | ||
| export const PREFER_BUILDER_OVER_SQL_NUDGE = | ||
| 'PREFER THIS over clickstack_sql. Only drop to raw SQL for things the ' + | ||
| `builder tools cannot express — ${SQL_FALLBACK_CRITERIA}.`; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { probeMcp } from '@/harness/preflight'; | ||
| import type { McpDefinition } from '@/harness/types'; | ||
|
|
||
| const httpDef: McpDefinition = { | ||
| type: 'http', | ||
| url: 'http://localhost:30199/mcp', | ||
| headers: { Authorization: 'Bearer test' }, | ||
| toolPattern: 'mcp__hyperdx__*', | ||
| label: 'HyperDX', | ||
| }; | ||
|
|
||
| /** Mock fetch so each queued value is returned as an SSE JSON-RPC response. */ | ||
| function mockFetch(...jsonRpc: unknown[]) { | ||
| const fn = jest.fn(); | ||
| for (const payload of jsonRpc) { | ||
| fn.mockResolvedValueOnce({ | ||
| status: 200, | ||
| headers: { get: () => null }, | ||
| text: async () => `event: message\ndata: ${JSON.stringify(payload)}\n\n`, | ||
| }); | ||
| } | ||
| global.fetch = fn as unknown as typeof fetch; | ||
| } | ||
|
|
||
| const originalFetch = global.fetch; | ||
| afterEach(() => { | ||
| global.fetch = originalFetch; | ||
| }); | ||
|
|
||
| describe('probeMcp', () => { | ||
| it('returns ok with a tool count when initialize + tools/list succeed', async () => { | ||
| mockFetch( | ||
| { result: {} }, | ||
| { result: { tools: [{ name: 'a' }, { name: 'b' }] } }, | ||
| ); | ||
| expect(await probeMcp('hyperdx', httpDef)).toEqual({ | ||
| mcp: 'hyperdx', | ||
| ok: true, | ||
| toolCount: 2, | ||
| }); | ||
| }); | ||
|
|
||
| it('fails when the server is reachable but serves zero tools', async () => { | ||
| mockFetch({ result: {} }, { result: { tools: [] } }); | ||
| const res = await probeMcp('hyperdx', httpDef); | ||
| expect(res.ok).toBe(false); | ||
| expect(res.error).toMatch(/0 tools/); | ||
| }); | ||
|
|
||
| it('fails with an actionable message when the connection is refused', async () => { | ||
| global.fetch = jest | ||
| .fn() | ||
| .mockRejectedValueOnce( | ||
| new Error('fetch failed'), | ||
| ) as unknown as typeof fetch; | ||
| const res = await probeMcp('hyperdx', httpDef); | ||
| expect(res.ok).toBe(false); | ||
| expect(res.error).toMatch(/could not connect.*API server/s); | ||
| }); | ||
|
|
||
| it('treats stdio MCPs as ok without probing', async () => { | ||
| const stdioDef: McpDefinition = { | ||
| type: 'stdio', | ||
| command: 'uvx', | ||
| toolPattern: 'mcp__clickhouse__*', | ||
| label: 'ClickHouse', | ||
| }; | ||
| expect(await probeMcp('clickhouse', stdioDef)).toEqual({ | ||
| mcp: 'clickhouse', | ||
| ok: true, | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.