Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(mcp): generate exec command reference for the posthog.com tools page (#73292)

Robbie6ba1e0acd625
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index a83ab449ae32..60279ee3c3e0 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -315,6 +315,9 @@ jobs:
- 'services/mcp/src/tools/generated/**/*'
- 'services/mcp/schema/generated-tool-definitions.json'
- 'services/mcp/schema/tool-definitions-all.json'
+ # Exec command reference templates - feed generate-exec-docs.ts
+ - 'services/mcp/src/templates/sections/**/*'
+ - 'services/mcp/schema/exec-command-reference.md'
detect-snapshot-mode:
name: Detect snapshot mode
@@ -2065,7 +2068,7 @@ jobs:
commit_message: 'chore: update OpenAPI generated types'
repo: ${{ github.repository }}
branch: ${{ github.event.pull_request.head.ref }}
- file_pattern: 'frontend/src/generated/** frontend/src/lib/agentScopes.generated.ts products/*/frontend/generated/** products/*/mcp/*.yaml services/mcp/definitions/*.yaml services/mcp/src/api/generated.ts services/mcp/src/generated/** services/mcp/schema/generated-tool-definitions.json services/mcp/schema/tool-definitions-all.json services/mcp/src/tools/generated/**'
+ file_pattern: 'frontend/src/generated/** frontend/src/lib/agentScopes.generated.ts products/*/frontend/generated/** products/*/mcp/*.yaml services/mcp/definitions/*.yaml services/mcp/src/api/generated.ts services/mcp/src/generated/** services/mcp/schema/generated-tool-definitions.json services/mcp/schema/tool-definitions-all.json services/mcp/schema/exec-command-reference.md services/mcp/src/tools/generated/**'
env:
GITHUB_TOKEN: ${{ steps.openapi-app-token.outputs.token }}
diff --git a/hogli.yaml b/hogli.yaml
index 822a7d83fd2c..04cd7f92b684 100644
--- a/hogli.yaml
+++ b/hogli.yaml
@@ -540,6 +540,10 @@ build:
cmd: pnpm --filter=@posthog/mcp run generate-cli-agent-scopes
description: Generate the CLI `agent` login use-case scope list from the MCP
tool definitions (must run after build:openapi-mcp-tools)
+ build:openapi-exec-docs:
+ cmd: pnpm --filter=@posthog/mcp run generate-exec-docs
+ description: Render the MCP exec tool's command reference into the markdown
+ fragment posthog.com embeds in the tools reference page
build:openapi:
steps:
- build:openapi-schema
@@ -550,6 +554,7 @@ build:
- build:openapi-mcp-tools
- build:openapi-mcp-scopes
- build:openapi-cli-agent-scopes
+ - build:openapi-exec-docs
description: Generate OpenAPI schema and TypeScript types from the backend
build:grammar:
cmd: pnpm grammar:build
diff --git a/services/mcp/README.md b/services/mcp/README.md
index b45696f149d8..aa66ac2f70c2 100644
--- a/services/mcp/README.md
+++ b/services/mcp/README.md
@@ -290,6 +290,9 @@ x-posthog-mcp-mode: tools
The header wins when both the header and the query parameter are set.
An explicit value always wins over the client auto-detection; any other value is ignored and the auto-detection takes over.
+The cli-mode command surface is documented publicly on [posthog.com/docs/model-context-protocol/tools](https://posthog.com/docs/model-context-protocol/tools), which embeds `schema/exec-command-reference.md` at build time.
+That fragment is generated from the templates in `src/templates/sections/` by `scripts/generate-exec-docs.ts` (part of `hogli build:openapi`); edit the templates, not the fragment.
+
### Consumer attribution
Wrapping apps and AI-tool plugins that install or proxy the PostHog MCP can self-identify so usage can be attributed to the install path (e.g. plugin-installed vs. manually-pasted URL). The wrapped MCP client (Claude Code, Cursor, …) is already captured separately via the MCP `clientInfo` handshake — this signal is only for the wrapping context.
diff --git a/services/mcp/package.json b/services/mcp/package.json
index 267accef4038..bb5c99333779 100644
--- a/services/mcp/package.json
+++ b/services/mcp/package.json
@@ -36,6 +36,7 @@
"generate-mcp-types": "node scripts/generate-mcp-types.mjs",
"generate-orval-schemas": "node scripts/generate-orval-schemas.mjs",
"generate-tools": "tsx scripts/generate-tools.ts",
+ "generate-exec-docs": "tsx scripts/generate-exec-docs.ts",
"generate-cli-agent-scopes": "tsx scripts/build-cli-agent-scopes.ts",
"scaffold-yaml": "tsx scripts/scaffold-yaml.ts",
"lint-tool-names": "tsx scripts/lint-tool-names.ts",
diff --git a/services/mcp/schema/exec-command-reference.md b/services/mcp/schema/exec-command-reference.md
new file mode 100644
index 000000000000..ddb858833598
--- /dev/null
+++ b/services/mcp/schema/exec-command-reference.md
@@ -0,0 +1,69 @@
+<!-- Generated by services/mcp/scripts/generate-exec-docs.ts (part of `hogli build:openapi`). Do not edit directly. -->
+
+## CLI mode commands
+
+In [CLI mode](/docs/model-context-protocol/faq#choosing-a-tool-mode), the default for most clients, the MCP server registers a single `exec` tool instead of one tool per endpoint. Agents pass it CLI-style command strings to list, search, inspect, and call the tools above on demand, which keeps context usage small.
+
+This section is generated from the same templates the server serves to agents at runtime, so it always matches the deployed command surface.
+
+```text
+learn <topic...> - load one or more learning topics
+tools — list available tool names
+search <regex_pattern> — search tools by JavaScript regex (matches name, title, description)
+info [--json] <tool_name> — show tool name, description, and input schema (summarized if too large). Pass `--json` for raw JSON output.
+schema <tool_name> [field_path] — drill into a specific field schema (supports dot-notation, e.g. series, breakdownFilter.breakdowns)
+call [--json] [--confirm] <tool_name> <json_input> — call a tool with JSON input (--json returns JSON instead of optimized output in supported tools. Informational responses remain tagged and escaped in both MCP and the agent CLI. --confirm is required by the CLI for destructive tools.)
+```
+
+**Namespaced references (`posthog:<tool-name>`):** strip the `posthog:` prefix and route through `exec`. Run `info <name>` to inspect, then `call <name> <json>`. E.g. `posthog:insights-list` → `posthog:exec({ "command": "info insights-list" })` then `posthog:exec({ "command": "call insights-list {}" })`. If the bare name isn't found, fall back to `search <pattern>` — it may have been renamed.
+
+The `learn` command is only registered on hosts that use the guided help catalog (currently Claude web and desktop). On hosts that support MCP apps, CLI mode also registers a separate `render-ui` tool for rendering interactive visualizations.
+
+### How agents use these commands
+
+This is the workflow the tool description asks agents to follow.
+
+PostHog: dashboards, insights, funnels, SQL, experiments, surveys, replay, error tracking, flags.
+
+Pass CLI-style commands in the `command` parameter for all PostHog interactions.
+
+**Requirements**
+
+1. Find unknown tools with `search` or `tools`.
+2. Run `info <tool_name>` once if its schema is not in context. Reuse it unless the tool changes or a schema error occurs.
+
+Never guess a schema or run `info` before every call.
+
+**Commands (in order):**
+
+```text
+# 1. Find unknown tools
+posthog:exec({ "command": "search <regex>" })
+posthog:exec({ "command": "tools" }) # fallback: list all
+
+# 2. Inspect once if the schema is missing
+posthog:exec({ "command": "info <tool_name>" })
+
+# 3. Drill into complex fields — REQUIRED for any field with a `hint`
+posthog:exec({ "command": "schema <tool_name> <field_path>" })
+
+# 4. Call; reuse the schema
+posthog:exec({ "command": "call <tool_name> <json_input>" })
+posthog:exec({ "command": "call --json <tool_name> <json_input>" })
+```
+
+**Schema drill-down:**
+
+- `info` returns the full schema if it fits the token budget; otherwise it auto-summarizes (names, types, required, enums, defaults) and attaches `hint` entries pointing to `schema <tool> <path>` for complex fields.
+- `schema <tool>` (no path) returns the summarized top-level schema.
+- `schema <tool> <path>` resolves a dot path, descending through:
+ - object `properties` (e.g. `query.source`)
+ - array `items` — numeric segments step into items (`events.0.properties`), or jump to a property on the item type (`events.id`)
+ - `anyOf`/`oneOf` — numeric segment picks a variant by index, or a property name matches any object variant defining it
+- Oversized sub-schemas are also summarized with a `note` to drill further.
+- Unknown paths return an error listing available child paths.
+
+**Not supported:**
+
+- `search` matches tool metadata only, not input schemas.
+- No pattern-based field projection — drill one path at a time.
diff --git a/services/mcp/scripts/generate-exec-docs.ts b/services/mcp/scripts/generate-exec-docs.ts
new file mode 100644
index 000000000000..c042c20764fa
--- /dev/null
+++ b/services/mcp/scripts/generate-exec-docs.ts
@@ -0,0 +1,47 @@
+#!/usr/bin/env tsx
+// Renders the exec tool's command reference into schema/exec-command-reference.md,
+// a markdown fragment posthog.com fetches at build time and embeds in the MCP tools
+// reference page, so the website documents the same syntax the server serves to
+// agents at runtime.
+import * as fs from 'node:fs'
+import * as path from 'node:path'
+
+import { LEARN_COMMAND_LINE } from '../src/tools/exec-help'
+
+const SECTIONS_DIR = path.join(__dirname, '../src/templates/sections')
+const OUTPUT_PATH = path.join(__dirname, '../schema/exec-command-reference.md')
+
+function readSection(name: string): string {
+ return fs.readFileSync(path.join(SECTIONS_DIR, name), 'utf8').trim()
+}
+
+// Leading lines that only make sense inside a tool parameter description are dropped;
+// the replaces are no-ops if the template wording changes, so drift shows up in review.
+const cliSyntax = readSection('cli-syntax.md')
+ .replace('CLI-style command string. Supported commands:', '')
+ .replace('{extra_commands}', LEARN_COMMAND_LINE)
+ .trim()
+
+const execBlurb = readSection('exec-tool-blurb.md').replace('### Using the `posthog` tool', '').trim()
+
+const fragment = `<!-- Generated by services/mcp/scripts/generate-exec-docs.ts (part of \`hogli build:openapi\`). Do not edit directly. -->
+
+## CLI mode commands
+
+In [CLI mode](/docs/model-context-protocol/faq#choosing-a-tool-mode), the default for most clients, the MCP server registers a single \`exec\` tool instead of one tool per endpoint. Agents pass it CLI-style command strings to list, search, inspect, and call the tools above on demand, which keeps context usage small.
+
+This section is generated from the same templates the server serves to agents at runtime, so it always matches the deployed command surface.
+
+${cliSyntax}
+
+The \`learn\` command is only registered on hosts that use the guided help catalog (currently Claude web and desktop). On hosts that support MCP apps, CLI mode also registers a separate \`render-ui\` tool for rendering interactive visualizations.
+
+### How agents use these commands
+
+This is the workflow the tool description asks agents to follow.
+
+${execBlurb}
+`
+
+fs.writeFileSync(OUTPUT_PATH, fragment)
+process.stdout.write(`Wrote ${path.relative(process.cwd(), OUTPUT_PATH)}\n`)
diff --git a/services/mcp/src/lib/instructions-formatter.ts b/services/mcp/src/lib/instructions-formatter.ts
index 2fcb8e1f9769..da173f766971 100644
--- a/services/mcp/src/lib/instructions-formatter.ts
+++ b/services/mcp/src/lib/instructions-formatter.ts
@@ -30,7 +30,7 @@ import RETRIEVING_DATA from '@/templates/sections/retrieving-data.md'
import SCHEMA_WORKFLOW from '@/templates/sections/schema-workflow.md'
import TOOL_SEARCH from '@/templates/sections/tool-search.md'
import URL_PATTERNS from '@/templates/sections/url-patterns.md'
-import type { ExecHelpEntry } from '@/tools/exec-help'
+import { type ExecHelpEntry, LEARN_COMMAND_LINE } from '@/tools/exec-help'
export interface InstructionsContext {
guidelines: string
@@ -172,7 +172,7 @@ export class InstructionsFormatter {
{
compact: false,
compactToolDomains: true,
- extraCommands: 'learn <topic...> - load one or more learning topics\n',
+ extraCommands: LEARN_COMMAND_LINE,
}
)
}
diff --git a/services/mcp/src/tools/exec-help.ts b/services/mcp/src/tools/exec-help.ts
index f398dfd89029..777bbb1a0907 100644
--- a/services/mcp/src/tools/exec-help.ts
+++ b/services/mcp/src/tools/exec-help.ts
@@ -1,3 +1,8 @@
+// Syntax line for the `learn` verb, appended to the command reference on hosts
+// that receive the help catalog. Shared with scripts/generate-exec-docs.ts so the
+// published docs can't drift from the runtime declaration.
+export const LEARN_COMMAND_LINE = 'learn <topic...> - load one or more learning topics\n'
+
export type ExecHelpEntryKind = 'guide' | 'skill'
export interface ExecHelpEntry {
diff --git a/tools/hogli-commands/hogli_commands/build.py b/tools/hogli-commands/hogli_commands/build.py
index aa6098c5290b..e81b6c75b8d6 100644
--- a/tools/hogli-commands/hogli_commands/build.py
+++ b/tools/hogli-commands/hogli_commands/build.py
@@ -30,6 +30,7 @@ TRIGGERS: dict[str, tuple[str, ...]] = {
"products/*/mcp/prompts/*",
"services/mcp/definitions/*",
"services/mcp/schema/tool-definitions.json",
+ "services/mcp/src/templates/sections/*",
# The generator formats its output, so lint/format scope decides the bytes
# that land in the generated trees — a config-only edit silently invalidates them.
".oxfmtrc.json",