You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit e7bf95e
Browse filesBrowse the repository at this point in the historyBrowse files
chore(api): steer MCP agents to builder tools over raw SQL (#3853)
## Summary
Agents were reaching for `clickstack_sql` for work the builder tools already handle. The SQL fallback list named CTEs and subqueries broadly, so an agent could write a CTE for an aggregation and then cite that list as the reason for SQL. In a 14-day aggregate review of request intent across 212 teams, about 18% cited HAVING and about 37% said the builders could not extract JSON or regex.
The server instructions, `clickstack_sql`, and the shared builder-tool nudge now draw a sharper line. SQL is reserved for joins across sources or tables, window functions or per-row ordering, aggregation over an already-aggregated result, unregistered tables, and summary metrics. A shared "not reasons" list maps the common needs to the tool that covers them: HAVING, JSON/regex and derived fields, conditional counts and two-window comparisons, one trace with logs, and child-span time breakdowns. `clickstack_table` and `clickstack_timeseries` state their `having`, expression-valued `groupBy`/`valueExpression`, and per-item `where` support up front. Descriptions stay under the 2048-byte client limit.
No tool parameters or query behavior changed.
### How to reproduce
**Starting point:** an MCP client connected to a ClickStack instance.
**On `main`:**
1. Ask an agent to filter grouped results with HAVING, extract a JSON field, or compare counts across two windows. The generic CTE/subquery fallback wording invites raw SQL, while the builder descriptions only document HAVING and expression support in individual field schemas.
**On this branch:**
1. Run the same prompts. The server instructions and tool descriptions name the builder tool for each need and reserve SQL for cases builders cannot express.
### How to test on Vercel preview
N/A — non-UI change.
### References
- Aggregate review: 18% of 212 teams cited HAVING; 37% said builders could not extract JSON or regex (14-day window).
---
> [!NOTE]
> **Low Risk**
> Metadata and test-only changes; no tool parameters or query execution paths are modified.
>
> **Overview**
> Updates **MCP copy only** so agents default to builder query tools instead of `clickstack_sql`. Shared `SQL_FALLBACK_CRITERIA` in `builderCatalog` is narrowed (drops broad CTE/subquery wording; names joins, window/per-row ordering, already-aggregated results, unregistered tables, and summary metrics). A new **`NOT_REASONS_TO_USE_SQL`** list maps common needs (HAVING, JSON/regex via expressions, per-item `where`, trace waterfall/logs, child-span breakdown) to the right builder tools.
>
> That guidance is wired into **server instructions**, the **`clickstack_sql`** description, and the shared **PREFER builder** nudge. **`clickstack_table`** and **`clickstack_timeseries`** now call out `having`, expression `groupBy`/`valueExpression`, and per-select `where` **before** the SQL nudge.
>
> Tests in `toolDescriptions.test.ts` assert the 2048-byte Claude limit, that instructions/SQL omit “CTEs” but include both policy strings, and that builder capability phrases precede the SQL nudge.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit db0af2e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup>
0 commit comments