Skip to content

feat(appkit): expose named analytics queries as tools - #756

Open
MarioCadenas wants to merge 3 commits into
mainfrom
work/26-named-analytics-queries-as-mcp
Open

MarioCadenas wants to merge 3 commits into
mainfrom
work/26-named-analytics-queries-as-mcp

Conversation

@MarioCadenas

Copy link
Copy Markdown
Collaborator

What

Named analytics queries as MCP tools

Why

I've fixed the check failure. It was never the code: the check stopped at sh: 1: pnpm: not found, because this wiped machine had no pnpm where the turn-end hook looks for it.

  • The fix: I ran the repo's own documented setup step, corepack enable pnpm. It adds a pnpm shim to the nvm node folder, which is on the hook's PATH. corepack disable pnpm removes it.
  • Proof: I ran the board's exact check command in a shell with the hook's PATH and its 900-second timeout. It passed in 59 seconds, with all 81 test files and 1905 tests passing.
  • Rerun: the board runs the check only once per commit, so I added an empty commit, chore: re-run the check now that pnpm is on the check shell's PATH, to make it run again. did the same for the same problem.

The board had taken away the earlier closing note, so I rewrote it in full with this fix added.

Also noticed:

  • pnpm 11 ignores the machine's NPM_CONFIG_REGISTRY setting. A pnpm install that has to download anything would go to the blocked public registry. Today's check didn't need to, because everything was already installed.

How it was verified

pnpm install --frozen-lockfile && pnpm run check && pnpm run knip && pnpm build && pnpm run typecheck && pnpm vitest run --changed "$(git merge-base HEAD origin/main)" passed.

Screenshots

None

Notes

None

MarioCadenas and others added 3 commits December 3, 2026 10:01
…gen keys

- `:name` inside `/* ... */` is no longer read as a parameter or replaced
  during DESCRIBE.
- Typegen keeps `x.obo.sql` when `x.sql` also exists, matching the runtime,
  and warns instead of emitting the key twice.
- Query keys that aren't valid identifiers (e.g. `my-query`) are quoted in
  the generated declarations.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: mario.cadenas <mario.cadenas@databricks.com>
`plugins.analytics.queries({ only })` turns each `config/queries/*.sql`
file into a `query.<name>` tool in the `toolkit()` entry shape, so agents
(and the upcoming MCP plugin) can use them.

- `-- @description` and `-- @PARAM name TYPE [description]` comments are
  read at runtime and give the tool its description and JSON Schema; every
  parameter is required and untyped ones are strings.
- `.sql` tools run as the app even inside a caller scope; `.obo.sql` tools
  run as the caller and are refused without one (except the existing dev
  fallback).
- Every query tool is `effect: "read"`; files that fail the read-only SQL
  check are refused. Results are capped at 100 rows with a note.
- New `analytics.runQuery(name, params?, signal?)` runs a named query in
  process and returns its rows. The HTTP route's JSON path and the tools
  share its execution core, cache keys unchanged.
- Docs: "Named queries as tools" in the analytics plugin page.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: mario.cadenas <mario.cadenas@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: mario.cadenas <mario.cadenas@databricks.com>
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

📦 Bundle size report

Compared against bundle-size-baseline.json (main).

@databricks/appkit

npm tarball (packed): 1.4 MB (+7.5 KB) — gzipped download (dist + bin; excludes release-only docs/NOTICE).

dist raw gzip
JS (runtime) 1.4 MB (+8.1 KB) 504 KB (+2.8 KB)
Type declarations 483 KB (+1.1 KB) 174 KB (+277 B)
Source maps 2.7 MB (+17 KB) 936 KB (+5.6 KB)
Other 11 KB 3.7 KB
Total 4.6 MB (+26 KB) 1.6 MB (+8.7 KB)
Per-entry composition (own code — deps external (as shipped))
Entry Initial (gz) Lazy (gz) Total (gz) node_modules (min) Own code (min)
. 120 KB (+2.0 KB) 2.4 KB 123 KB (+2.0 KB) external 384 KB (+5.1 KB)
./beta 119 KB (+372 B) 4.5 KB 123 KB (+372 B) external 357 KB (+932 B)
./testing 47 KB (+326 B) 39 KB (+149 B) 85 KB (+475 B) external 247 KB (+1.2 KB)
./tsdown 630 B 0 B 630 B external 1.0 KB
./type-generator 31 KB (+179 B) 0 B 31 KB (+179 B) external 90 KB (+350 B)

Chunks:

Entry Chunk Load Size (gz)
. index.js initial 116 KB
. utils.js initial 4.6 KB
. remote-tunnel-manager.js lazy 2.4 KB
./beta beta.js initial 92 KB
./beta stream-manager.js initial 5.9 KB
./beta define-schema.js initial 5.8 KB
./beta service-context.js initial 4.3 KB
./beta modular.js initial 3.6 KB
./beta databricks.js initial 3.3 KB
./beta wide-event-emitter.js initial 2.8 KB
./beta client.js initial 645 B
./beta index.js initial 20 B
./beta index.js lazy 4.0 KB
./beta supervisor-api.js lazy 201 B
./beta databricks.js lazy 187 B
./beta index.js lazy 124 B
./testing manifest.js initial 34 KB
./testing index.js initial 10 KB
./testing wide-event-emitter.js initial 2.9 KB
./testing index.js lazy 34 KB
./testing remote-tunnel-manager.js lazy 2.4 KB
./testing utils.js lazy 1.8 KB
./tsdown index.js initial 630 B
./type-generator index.js initial 31 KB

@databricks/appkit-ui

npm tarball (packed): 396 KB — gzipped download (dist + bin; excludes release-only docs/NOTICE).

dist raw gzip
JS (runtime) 433 KB 147 KB
Type declarations 251 KB 93 KB
Source maps 843 KB 283 KB
CSS 16 KB 3.2 KB
Total 1.5 MB 527 KB
Per-entry composition (consumer bundle — deps bundled, peerDeps external)
Entry Initial (gz) Lazy (gz) Total (gz) node_modules (min) Own code (min)
./js 5.3 KB 49 KB 55 KB 208 KB 14 KB
./js/beta 2.4 KB 0 B 2.4 KB 0 B 5.3 KB
./react 442 KB 49 KB 490 KB 1.4 MB 181 KB
./react/beta 13 KB 0 B 13 KB 25 KB 13 KB

Chunks:

Entry Chunk Load Size (gz)
./js index.js initial 5.2 KB
./js chunk initial 120 B
./js apache-arrow lazy 49 KB
./js/beta beta.js initial 2.4 KB
./react index.js initial 439 KB
./react tslib initial 2.1 KB
./react apache-arrow lazy 49 KB
./react/beta beta.js initial 13 KB

@MarioCadenas

Copy link
Copy Markdown
Collaborator Author

Review 1 · Pass · 1 minor

Does the change do what the task asked? Yes. Gate: major and worse must be fixed.

1 finding (1 nit): nothing needs fixing. No findings: nothing needs fixing. The change meets the approved plan in the paths reviewed. The required check passed, including 81 test files and 1905 tests; I found no confirmed faults.

Nit (not required, below the gate)

  • minor: Security: For a service-principal (.sql) named query run as an agent tool, the caller's abort signal is merged into the shared cache work, so a cancelled, disconnected or timed-out tool call cancels the warehouse statement for every other user (HTTP or tool) who joined the same cache entry; it causes a transient error and exposes no data. (packages/appkit/src/plugins/analytics/analytics.ts:450)
    Proof: Read analytics.ts:445-450: AbortSignal.any([signal, sig]) is passed into _executeJsonArrayPath inside the cached executor.execute function, and the author left a 'shortcut: a caller abort can cancel shared cache work' comment at line 445. cache/index.ts:290-319: joiners wait on the same in-flight entry through _waitWithRefCount, which is designed so that one caller's abort does not poison the shared result. analytics.ts:1150 and 1175: .sql queries use executorKey 'global' outside the caller's scope, so all users share one key. This is from reading the code; I did not reproduce it.

@MarioCadenas
MarioCadenas marked this pull request as ready for review December 3, 2026 10:22
@MarioCadenas
MarioCadenas requested a review from a team as a code owner December 3, 2026 10:22
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

🤖 AppKit PR bot

🔬 Run evals

Start an eval for this PR from the evals-monitor app: Go to Evals Monitor →

📦 Try this PR's app template

Scaffolds a new app from this PR's SDK build. Run it in any folder (requires the GitHub CLI — gh auth login — and the Databricks CLI):

gh run download 47083618341 -R databricks/appkit -n appkit-template-0.106.0-pr.456e64c-work-26-named-analytics-queries-as-mcp-756 -D appkit-pr-756 \
  && unzip -o "appkit-pr-756/appkit-template-0.106.0-pr.456e64c-work-26-named-analytics-queries-as-mcp-756.zip" -d "appkit-pr-756" \
  && databricks apps init --template "appkit-pr-756"

The template pins @databricks/appkit and @databricks/appkit-ui to tarballs built from this branch, so the scaffolded app runs against this PR's code.

@MarioCadenas MarioCadenas changed the title Named analytics queries as MCP tools feat(appkit): expose named analytics queries as tools Dec 3, 2026
@MarioCadenas

Copy link
Copy Markdown
Collaborator Author

Review 2 · Pass · 2 minor, 3 pre-existing

Does the change do what the task asked? No. Gate: major and worse must be fixed.

5 findings (2 nits, 3 pre-existing): nothing needs fixing. 1 finding (1 nit): nothing needs fixing. The named-query tools, identity paths, docs, and required repository check work as planned, but the new timestamp tool schema accepts impossible calendar values and sends them to the warehouse. This is a minor input-validation fault. The brief names /xavier-review and /security-review, which are unavailable here; /ponytail-review was available and used.

Nit (not required, below the gate)

  • minor: Timestamp tool inputs accept impossible dates and times instead of returning an argument-validation message. (packages/appkit/src/plugins/analytics/query-tools.ts)
    Proof: Code-path inspection shows 2026-99-99T99:99:99Z matches both regexes, passes executeFromRegistry validation, and reaches runQuery. The required test chain passed but does not cover this input.
  • minor: Security: The new query path merges the caller's own abort signal into the execution that the cache shares between callers, so when one agent run or runQuery caller aborts, the warehouse statement is cancelled for every other caller on the same cache key. For .sql queries that includes other users, because the executor key is 'global'. (packages/appkit/src/plugins/analytics/analytics.ts:450)
    Proof: Read: line 450 passes AbortSignal.any([signal, sig]) into _executeJsonArrayPath inside executor.execute. This line is new in the diff, and the author left a shortcut comment at line 449 admitting it. It works around the reference-counted shared controller in packages/appkit/src/cache/index.ts:~290-301, which exists so that one caller's abort does not poison the shared result. Not run end to end.

Pre-existing (not required, there before this change)

  • major: Security: Any HTTP caller of POST /api/analytics/query/:key can supply its own workspaceId parameter, because the server injects workspaceId only when the client did not send one. A service-principal .sql query that filters cross-workspace data on workspace_id = :workspaceId can then return another workspace's rows. (packages/appkit/src/plugins/analytics/query.ts:23)
    Proof: Read: line 23 injects only when !processed.workspaceId, and the same line is in e7d16bb. A local probe (tsx) of processQueryParams with a client workspaceId of OTHER-WORKSPACE bound [{name: workspaceId, value: OTHER-WORKSPACE, type: STRING}]. The docs (docs/docs/plugins/analytics.md:111) say the server injects it. The new agent tools skip workspaceId (query-tools.ts:93), so this change does not widen it. Not shown against a deployed app.
  • minor: Security: When a query tool fails, the original ExecutionError, carrying the raw warehouse error text, goes back to the model and the end user. The HTTP SSE path sends only the sanitized clientMessage. A failing .sql tool can therefore expose service-principal warehouse details such as table names and statement fragments. (packages/appkit/src/plugins/analytics/analytics.ts:480)
    Proof: Read: the original error is rethrown at lines 480-481, and connectors/sql-warehouse/client.ts:379 builds it from status.error.message. agents/databricks.ts:677-689 and plugins/agents/tool-dispatch.ts:165-168 surface it as a tool error. Compare the sanitized path at stream/stream-manager.ts:310-320. The agent error surfacing and the raw query tool already behaved this way before this change.
  • trivial: Security: Typegen still writes the query name and column names into the generated .d.ts without escaping (only the interface key was fixed), so a file name or column alias containing a double quote produces a malformed declaration file. It is types only and the input comes from the developer, so there is no runtime effect. (packages/appkit/src/type-generator/query-registry.ts:201)
    Proof: Read: line 201 is name: "${queryName}" with no escaping, and columns at :186-188 and :223 are the same. The new quoting in render-query-types.ts:18-22 covers keys only.

@MarioCadenas

Copy link
Copy Markdown
Collaborator Author

Review 3 · Pass · 2 pre-existing

Does the change do what the task asked? Yes. Gate: major and worse must be fixed.

2 findings (2 pre-existing): nothing needs fixing. No findings: nothing needs fixing. The change meets the approved plan in the paths reviewed, and the required repository check passed. I found no new confirmed fault. The brief names /xavier-review and /security-review, which are unavailable here; I used /ponytail-review.

Pre-existing (not required, there before this change)

  • major: Security: The read-only SQL check only looks at the first keyword and accepts WITH as read-only, so a prompt-injected agent can send WITH ... INSERT/DELETE through the existing raw analytics.query tool (effect "read", autoInheritable, runs as the user) and write with no approval prompt. The new named query tools rely on the same check, so a developer-written WITH ... DML file in config/queries would also be (packages/appkit/src/core/agent/tools/sql-policy.ts:44)
    Proof: Ran classifyReadOnly via tsx ($TMPDIR/probe/policy.ts): 'WITH t AS (SELECT 1 AS a) INSERT INTO cat.sch.tbl SELECT * FROM t' and 'WITH t AS (SELECT 1) DELETE FROM x' both return {readOnly:true}. Read requiresApproval in plugins/agents/approval.ts:17-24 (effect "read" means no approval), the raw tool at plugins/analytics/analytics.ts:1209-1214, and the new tools at plugins/analytics/query-tools.ts:59,121-122. sql-policy.ts is unchanged since 95f17b5. Not run against a real warehouse.
  • trivial: Security: Before this change, query names went into the generated .d.ts without quotes, so a crafted query filename could inject type-level code at build time. This change closes that by quoting such names with JSON.stringify. (packages/appkit/src/type-generator/render-query-types.ts:18)
    Proof: Read the old and new render code: names that match the identifier regex cannot contain injection characters, and every other name becomes a JSON string literal. Only developer-owned files at build time were affected.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant