posthog icon indicating copy to clipboard operation
posthog copied to clipboard

feat(mcp): data catalog tools with typed-confirm promotion

Open thiagosalvatore opened this issue 4 months ago • 8 comments

Problem

The data catalog already exposes governed metrics, certifications, and relationships through REST and system.information_schema. Agents still need a safe MCP surface that can discover trusted definitions, propose changes, run metrics, and promote reviewed catalog entries without bypassing the catalog's authorization model.

Promotion needs two different protections. The conversation should pause for the user's literal confirm, and execution must remain bound to the exact validated arguments, user, action, and active project or organization. The MCP generator also needs to fail closed when a configuration combination cannot preserve that contract.

Changes

Before

flowchart LR
    User[User request] --> Agent{{Agent}}
    Agent --> Missing[No data catalog MCP surface]

    classDef phBlue fill:#1d4aff,stroke:#1d4aff,color:#fff;
    classDef phYellow fill:#f9bd2b,stroke:#f9bd2b,color:#000;
    classDef phGray fill:#e5e7eb,stroke:#c7ccd1,color:#000;
    class Agent phBlue;
    class User phYellow;
    class Missing phGray;

After

flowchart LR
    User[User request] --> Agent{{Agent}}
    Agent --> Prepare[Prepare validated action]
    Prepare --> Token[Signed arguments and scope]
    Token --> Confirm[Literal confirm guard]
    Confirm --> Execute[Strict execute schema]
    Execute --> Verify[Verify user, purpose, scope, TTL, and nonce]
    Verify --> API[Scoped catalog API]
    API --> Catalog[Governed catalog]

    classDef phBlue fill:#1d4aff,stroke:#1d4aff,color:#fff;
    classDef phRed fill:#f54e00,stroke:#f54e00,color:#fff;
    classDef phYellow fill:#f9bd2b,stroke:#f9bd2b,color:#000;
    classDef phGray fill:#e5e7eb,stroke:#c7ccd1,color:#000;
    class Agent phBlue;
    class API phRed;
    class User,Catalog phYellow;
    class Prepare,Token,Confirm,Execute,Verify phGray;

The YAML config contains 10 base operations. Code generation exposes five direct tools and five prepare/execute pairs, for 15 MCP tools in total.

Catalog operation MCP behavior
Metric create, update, and run Direct
Metric approve Confirmed prepare/execute pair
Certification propose Direct
Certification certify and deprecate Confirmed prepare/execute pairs
Relationship propose Direct
Relationship accept and reject Confirmed prepare/execute pairs
  • Discovery stays SQL-first through system.information_schema.metrics, certifications, and relationships. Tool descriptions require approved, non-drifted metrics for trusted answers.
  • All catalog tools are gated by product-data-catalog and declare the backend scopes they require. Promotion tools additionally require data_catalog_approval:write.
  • Confirmed-action prepare validates and signs the action arguments together with the action purpose, user identity, and active project or organization. Execute accepts only confirmation_hash and confirmation, recovers the signed arguments, checks TTL and replay state, verifies the active scope, and passes only verified arguments to the API.
  • confirmed_action is now rejected when combined with either a whole-tool input_schema or a ui_app, because those generator paths do not currently preserve the prepare/execute contract.
  • Information-schema queries through the agent SQL tool always calculate fresh results so approval, drift, certification, and relationship changes are immediately visible.
  • Catalog conflict paths return usable 409 responses with candidate or proposal IDs. Definition and date-override failures retain actionable messages, and metric confidence is constrained to 0-1 at MCP and API boundaries.
  • The ordinary local Hono development process preserves feature-flag overrides, including an explicit false override for the catalog.
  • This PR adds no approval UI.

[!WARNING] The literal confirm value is supplied through model-authored tool arguments. It is an instruction-backed workflow guard, not client-attested proof that a human typed the word. API scopes remain the authorization boundary; signed arguments, identity, purpose, scope, expiry, and replay checks protect execution integrity.

How did you test this code?

At release-candidate SHA 975d78384f39502d9456cf107b841f9789f99b35:

  • pnpm --filter=@posthog/mcp exec vitest run: 84 files and 2,238 tests passed.
  • Focused generator, signed-state, confirmed-action runtime, and catalog execute-schema suites: 120 tests passed.
  • pnpm --filter=@posthog/mcp typecheck: passed.
  • pnpm --filter=@posthog/mcp lint-tool-names: all 747 generated tool names passed.
  • hogli build:openapi: completed, generated the MCP artifacts, and left no diff.
  • Direct schema probes confirmed that all five catalog execute tools accept only confirmation_hash and confirmation.
  • hogli ci:preflight --strict: zero failures. Its cumulative branch-diff heuristic still reports OpenAPI inputs; both full generation and preflight fix mode leave a clean worktree.

The full live/API/MCP release matrix ran at f9fbc8328f275f8390762fd62e25fb24f1a3b819, before the final generator-only compatibility guard. It included 225 catalog backend tests, 22 execute-SQL MCP tests, feature gating, scopes, metric lifecycle and drift, certifications, relationship joins, conflict payloads, confirmation expiry/replay/binding, and an independently corroborated top-customers-by-MRR agent journey. The final SHA reran the complete MCP suite, generation, typecheck, lint, and strict execute-schema probes. All 129 cumulative QA cases pass.

No UI was changed, so screenshots are not applicable.

[!IMPORTANT] Before enabling product-data-catalog, deployment owners must verify that MCP_SIGNED_STATE_KEY is configured as a production secret of at least 32 bytes. A missing or short key disables confirmed-action support while leaving non-confirmed tools available. This PR intentionally adds no startup or readiness gate for that rollout check.

👉 Stay up-to-date with PostHog coding conventions for a smoother review.

Automatic notifications

  • [x] Publish to changelog?
  • [ ] Alert Sales and Marketing teams?

Docs update

Updated the MCP implementation handbook and MCP definitions README with the confirmed-action security model, signing-key requirement, strict execute schema, and unsupported input_schema/ui_app combinations.

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

The initial MCP surface was implemented with Claude Code. OpenAI Codex then ran release QA and implemented the focused safety fixes. Repo skills invoked: /implementing-mcp-tools, /writing-tests, and /running-ci-preflight.

The final review narrowed the confirmation model deliberately. We kept the existing best-effort conversational guard, strengthened signed execution and generator validation, and did not add approval UI, redesign authentication, or add a startup/readiness requirement. The human DRI remains the verified PR author and assignee.

thiagosalvatore avatar Jul 09 '26 00:07 thiagosalvatore

  • #69544 Graphite
  • #69542 Graphite
  • #69541 Graphite 👈 (View in Graphite)
  • #69540 Graphite
  • #69538 Graphite
  • #69536 Graphite
  • #69531 Graphite
  • #69530 Graphite
  • #69528 Graphite
  • #69527 Graphite
  • master

This stack of pull requests is managed by Graphite. Learn more about stacking.

thiagosalvatore avatar Jul 09 '26 00:07 thiagosalvatore

🤖 CI report

⚠️ Bundle size — 🔺 +2.1 KiB (+0.0%)

Uncompressed size of every built .js bundle, compared against the base branch.

Total: 64.22 MiB · 🔺 +2.1 KiB (+0.0%)

File Size Δ vs base
render-query/src/render-query/render-query.js 21.33 MiB 🔺 +2.1 KiB (+0.0%)

Posted automatically by build-bundle-size-report · uncompressed bytes from dist-report

✅ Eager graph — within budget

How much code each root ships on the eager path — downloaded and parsed before the surface is interactive. Measured from the esbuild output chunks (post-tree-shake, static imports only); lazy import() / React.lazy chunks are not counted.

Root Eager (shipped) Δ vs base Budget
entry (logged-out pages, app bootstrap)
src/index.tsx
1.23 MiB · 22 files no change ███░░░░░░░ 28.6% of 4.29 MiB
authenticated shell (every logged-in page)
src/scenes/AuthenticatedShell.tsx
8.16 MiB · 2,988 files 🔺 +2.0 KiB (+0.0%) █████████░ 88.2% of 9.25 MiB

🟢 node_modules/monaco-editor/ stays out of src/index.tsx 🟢 src/lib/components/ActivityLog/describers stays out of src/index.tsx 🟢 [object Object] stays out of src/index.tsx 🟢 [object Object] stays out of src/index.tsx 🟢 node_modules/monaco-editor/ stays out of src/scenes/AuthenticatedShell.tsx 🟢 src/lib/components/ActivityLog/describers stays out of src/scenes/AuthenticatedShell.tsx 🟢 [object Object] stays out of src/scenes/AuthenticatedShell.tsx 🟢 [object Object] stays out of src/scenes/AuthenticatedShell.tsx

Largest files eagerly shipped from src/index.tsx
Size File
126.8 KiB ../node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js
24.6 KiB ../node_modules/.pnpm/[email protected]/node_modules/buffer/index.js
6.3 KiB ../node_modules/.pnpm/[email protected]/node_modules/react/cjs/react.production.min.js
4.5 KiB ../node_modules/.pnpm/@[email protected]/node_modules/@jspm/core/nodelibs/browser/process.js
3.9 KiB ../node_modules/.pnpm/[email protected]/node_modules/scheduler/cjs/scheduler.production.min.js
1.4 KiB ../node_modules/.pnpm/[email protected]/node_modules/base64-js/index.js
1.3 KiB src/RootErrorBoundary.tsx
912 B ../node_modules/.pnpm/[email protected]/node_modules/ieee754/index.js
789 B src/scenes/ChunkLoadErrorBoundary.tsx
762 B src/index.tsx
Largest files eagerly shipped from src/scenes/AuthenticatedShell.tsx
Size File
281.3 KiB ../node_modules/.pnpm/[email protected]/node_modules/posthog-js/dist/rrweb.js
267.7 KiB ../node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@posthog/icons/dist/posthog-icons.es.js
235.5 KiB src/taxonomy/core-filter-definitions-by-group.json
223.2 KiB ../node_modules/.pnpm/[email protected]/node_modules/posthog-js/dist/module.js
164.0 KiB src/queries/validators.js
154.3 KiB ../node_modules/.pnpm/[email protected]/node_modules/re2js/build/index.esm.js
126.8 KiB ../node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom.production.min.js
105.8 KiB src/lib/api.ts
93.3 KiB ../node_modules/.pnpm/[email protected]/node_modules/prosemirror-view/dist/index.js
93.2 KiB ../packages/quill/packages/quill/dist/index.js

Posted automatically by check-eager-graph · sizes are eager output bytes (shipped, post-tree-shake) from the esbuild metafile · part of #32479

✅ Dist folder size — 🔺 +51.0 KiB (+0.0%)

Total size of the built frontend/dist folder (all assets), compared against the base branch.

Total: 1341.48 MiB · 🔺 +51.0 KiB (+0.0%)

ℹ️ MCP UI apps size — 31 app(s), 16602.7 KB JS

Built size of each MCP UI app (main.js + styles.css).

App JS CSS
debug 599.5 KB 187.4 KB
action 457.8 KB 187.4 KB
action-list 564.3 KB 187.4 KB
cohort 456.8 KB 187.4 KB
cohort-list 563.3 KB 187.4 KB
email-template 456.6 KB 187.4 KB
error-details 472.4 KB 187.4 KB
error-issue 457.5 KB 187.4 KB
error-issue-list 564.2 KB 187.4 KB
experiment 561.5 KB 187.4 KB
experiment-list 565.1 KB 187.4 KB
experiment-results 563.2 KB 187.4 KB
feature-flag 567.1 KB 187.4 KB
feature-flag-list 570.9 KB 187.4 KB
feature-flag-testing 461.0 KB 187.4 KB
insight-actors 562.1 KB 187.4 KB
invite-email-preview 456.0 KB 187.4 KB
llm-costs 559.5 KB 187.4 KB
session-recording 458.6 KB 187.4 KB
session-summary 463.9 KB 187.4 KB
survey 458.4 KB 187.4 KB
survey-global-stats 562.2 KB 187.4 KB
survey-list 565.0 KB 187.4 KB
survey-stats 562.2 KB 187.4 KB
trace-span 457.2 KB 187.4 KB
trace-span-list 564.2 KB 187.4 KB
workflow 457.1 KB 187.4 KB
workflow-list 563.7 KB 187.4 KB
query-results 744.8 KB 187.4 KB
render-ui 825.3 KB 187.4 KB
visual-review-snapshots 461.6 KB 187.4 KB
✅ Playwright — all passed

All tests passed.

View test results →

⚠️ Backend coverage — 97.0% of changed backend lines covered — 2 uncovered

🧪 Backend test coverage

Patch coverage — changed backend lines (products + core): ███████████████████░ 97.0% (78 / 80)

File Patch Uncovered changed lines
ee/hogai/tools/execute_sql/mcp_tool.py 83.3% 29–30

🤖 Agents: add a test covering the lines above, or note why under "How did you test this code?". Machine-readable gap list: the patch-coverage artifact on this run (gh run download 29618295476 -n patch-coverage), or the coverage-data block at the end of this comment.

Per-product line coverage (touched products)
Product Coverage Lines
platform_features ██░░░░░░░░░░░░░░░░░░ 12.1% 7 / 58
batch_exports ████████░░░░░░░░░░░░ 39.7% 8,416 / 21,225
demo ███████████░░░░░░░░░ 56.2% 1,497 / 2,663
warehouse_sources_queue ████████████░░░░░░░░ 59.2% 148 / 250
tasks █████████████░░░░░░░ 67.0% 25,933 / 38,686
data_tools ██████████████░░░░░░ 70.0% 63 / 90
ai_gateway ███████████████░░░░░ 75.0% 9 / 12
signals ████████████████░░░░ 79.3% 19,302 / 24,350
data_modeling ████████████████░░░░ 80.0% 4,834 / 6,045
cdp ████████████████░░░░ 80.7% 3,118 / 3,864
wizard ████████████████░░░░ 82.5% 772 / 936
notebooks █████████████████░░░ 85.1% 7,096 / 8,338
cohorts █████████████████░░░ 86.2% 4,065 / 4,717
agent_platform █████████████████░░░ 86.4% 3,807 / 4,405
actions █████████████████░░░ 86.6% 717 / 828
product_tours █████████████████░░░ 87.5% 1,266 / 1,447
exports ██████████████████░░ 88.4% 6,933 / 7,842
visual_review ██████████████████░░ 88.5% 5,565 / 6,287
business_knowledge ██████████████████░░ 88.5% 4,400 / 4,969
conversations ██████████████████░░ 89.0% 16,183 / 18,186
mcp_analytics ██████████████████░░ 89.2% 2,514 / 2,819
dashboards ██████████████████░░ 89.3% 5,841 / 6,540
engineering_analytics ██████████████████░░ 89.5% 5,507 / 6,154
alerts ██████████████████░░ 89.9% 4,045 / 4,499
early_access_features ██████████████████░░ 90.1% 1,031 / 1,144
error_tracking ██████████████████░░ 90.1% 9,855 / 10,935
streamlit_apps ██████████████████░░ 90.4% 2,501 / 2,767
slack_app ██████████████████░░ 90.6% 9,018 / 9,955
links ██████████████████░░ 90.6% 183 / 202
marketing_analytics ██████████████████░░ 90.8% 11,514 / 12,684
stamphog ██████████████████░░ 91.0% 3,993 / 4,387
product_analytics ██████████████████░░ 91.3% 5,757 / 6,304
mcp_store ██████████████████░░ 91.8% 3,685 / 4,012
data_warehouse ██████████████████░░ 92.4% 18,771 / 20,309
notifications ███████████████████░ 92.7% 1,031 / 1,112
workflows ███████████████████░ 92.8% 5,482 / 5,909
ai_observability ███████████████████░ 92.8% 14,916 / 16,077
web_analytics ███████████████████░ 92.9% 13,837 / 14,897
surveys ███████████████████░ 93.0% 5,724 / 6,157
posthog_ai ███████████████████░ 93.2% 1,325 / 1,421
approvals ███████████████████░ 93.3% 3,395 / 3,640
reminders ███████████████████░ 93.4% 468 / 501
tracing ███████████████████░ 93.4% 2,546 / 2,725
managed_migrations ███████████████████░ 93.8% 1,220 / 1,300
legal_documents ███████████████████░ 94.1% 1,568 / 1,667
endpoints ███████████████████░ 94.1% 8,606 / 9,143
revenue_analytics ███████████████████░ 94.5% 3,598 / 3,809
skills ███████████████████░ 94.5% 2,881 / 3,049
messaging ███████████████████░ 94.5% 2,530 / 2,677
review_hog ███████████████████░ 94.6% 6,796 / 7,181
logs ███████████████████░ 95.2% 9,439 / 9,911
experiments ███████████████████░ 95.7% 24,417 / 25,527
replay_vision ███████████████████░ 95.8% 13,776 / 14,383
growth ███████████████████░ 95.8% 2,837 / 2,960
annotations ███████████████████░ 96.2% 732 / 761
feature_flags ███████████████████░ 96.3% 16,196 / 16,822
user_interviews ███████████████████░ 96.4% 2,242 / 2,325
warehouse_sources ███████████████████░ 96.4% 287,393 / 297,985
access_control ███████████████████░ 96.8% 849 / 877
customer_analytics ███████████████████░ 97.2% 7,482 / 7,700
data_catalog ███████████████████░ 97.5% 2,347 / 2,408
analytics_platform ████████████████████ 98.0% 2,102 / 2,145
metrics ████████████████████ 98.2% 2,404 / 2,448
pulse ████████████████████ 98.4% 2,017 / 2,049
live_debugger ████████████████████ 99.2% 613 / 618
field_notes ████████████████████ 99.4% 158 / 159

Report-only. Patch coverage = changed backend lines covered vs origin/master. Sorted lowest first. Known gaps: lines covered only by Temporal tests show as uncovered; core line numbers may drift if master changed the same file.

ℹ️ Docs preview — preview build triggered

Docs from this PR will be published at posthog.com.

Project Preview Updated (UTC)
posthog.com Open preview Jul 17, 2026, 10:34 PM

The preview should be ready in about 10 minutes. Open the preview at /handbook/engineering/.

github-actions[bot] avatar Jul 09 '26 00:07 github-actions[bot]

Static Badge   Static Badge   Static Badge   Static Badge

Failed Test Failure Summary Logs
test_setup_receivers_match_baseline The test failed because unexpected signal receivers were connected during Django setup. Logs ↗︎

View Full Report ↗︎ ⋅ Docs

trunk-io[bot] avatar Jul 17 '26 15:07 trunk-io[bot]

🦔 Hogbox preview · ✅ ready

▶ Open the preview

🔑 Login [email protected] / 12345678 (demo data)
🧩 Running this PR's backend and frontend, on the PostHog :master base
🔗 Link stable across rebuilds — a re-push swaps the box underneath, the URL stays
🔒 Access tailnet only (PostHog VPN)
🛠️ Admin inspect & debug state in hogland
💤 Idle sleeps after ~30 min idle (snapshot to S3, zero node cost) and wakes on your next visit in ~30s, behind a brief "waking up" screen

commit 9403161 · box box-c2d87fd38591 · ready in 782s (push → usable) · build log · rebuilds on every push, torn down on close

github-actions[bot] avatar Jul 17 '26 16:07 github-actions[bot]

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 4 · PR risk: 0/10

veria-ai[bot] avatar Jul 17 '26 16:07 veria-ai[bot]

Thanks for the detail. I agree the signed, single-use state meaningfully protects action integrity: it prevents replay and prevents the execute call from changing the prepared target or arguments.

It does not, however, establish the claimed human-approval boundary. In the current generated tool, both confirmation_hash and the literal confirmation are still inputs to the model-controlled execute invocation, so an injected or misbehaving agent can perform prepare followed immediately by execute without a user interaction. Documentation of that limitation does not change the exploit path, particularly because this PR is what exposes these promotion actions as tools advertised as human-gated.

I agree the correct remediation belongs in the shared framework rather than being duplicated per generated tool. Until a trusted client-side confirmation/elicitation record is required, these actions should be described and authorized as agent-executable destructive operations—not as approval actions requiring a human gate. The integrity/replay guarantees are valuable defense-in-depth, but they are not a substitute for that gate.

parameterai[bot] avatar Jul 17 '26 16:07 parameterai[bot]

👋 Visual changes detected for this PR.

Review and approve in PostHog Visual Review

If these changes are unexpected, they may be caused by a flaky test or a broken snapshot on master. Don't approve — rerun the job or wait for a fix.

posthog[bot] avatar Jul 17 '26 18:07 posthog[bot]

Heads-up on two changes in this branch that are not part of the data-catalog work — both are pre-existing failures on current master that surfaced when I merged master in, and both block CI here until fixed:

  1. services/mcp/tests/unit/execute-sql-description.test.ts — the governed-metrics catalog rework on master grew the metric-discovery section of the execute-sql description, pushing the flag-gated data-catalog block past the test's 1200-char budget (~1444). Bumped the guard to 1500 so it still catches unreviewed prompt bloat while accommodating the now-merged content.
  2. posthog/test/setup_receivers_baseline.txt — test_setup_receivers_match_baseline fails on master itself (its Django run was cancelled, so it went unnoticed): three receivers connect at django.setup() but aren't recorded in the baseline (ai_observability invalidate_llm_prompt_label_cache post_save/post_delete, early_access_features create_waitlist_survey_on_concept_stage post_save). Recorded them, which is the fix the test prescribes.

Happy to drop either if master gets fixed independently and I re-merge.

🦉 via talyn.dev

thiagosalvatore avatar Jul 17 '26 19:07 thiagosalvatore