Skip to main content
Glama
posthog-code-mode-mcp

posthog-code-mode-mcp

posthog-code-mode-mcp

A compact MCP server for PostHog that replaces the default ~70k token PostHog MCP server with a single tool.

Inspired by Cloudflare's code mode pattern: instead of exposing dozens of individual tools (one per API endpoint), this server exposes a single posthog tool that executes JavaScript code in a sandboxed VM with a typed PostHog API client.

Why?

The official PostHog MCP server ships ~70k tokens of tool definitions. That's a huge chunk of context window before you've even asked a question. This server achieves the same coverage with a single tool definition (~2k tokens) by letting the LLM write code against a typed client.

Related MCP server: PostHog MCP

Setup

1. Install dependencies

npm install

2. Configure environment

Copy the example and fill in your values:

cp .env.example .env
POSTHOG_API_KEY=phx_your_personal_api_key
POSTHOG_PROJECT_ID=12345

3. Add to Claude Code

Add this to your Claude Code MCP config (~/.claude/settings.json or project-level):

{
  "mcpServers": {
    "posthog-code-mode": {
      "command": "node",
      "args": ["/path/to/posthog-code-mode/index.js"],
      "env": {
        "POSTHOG_API_KEY": "phx_your_key",
        "POSTHOG_PROJECT_ID": "12345"
      }
    }
  }
}

Or if using a .env file, point to the directory and load env vars however you prefer.

Usage

The server exposes a single posthog tool. The LLM writes JavaScript that runs in a sandboxed VM with a posthog client object available as a global.

Examples

Count events by type (last 7 days):

const result = await posthog.query({
  kind: "HogQLQuery",
  query: "SELECT event, count() FROM events WHERE timestamp > now() - interval 7 day GROUP BY event ORDER BY count() DESC LIMIT 20"
});
console.log(result.results);

List feature flags:

const flags = await posthog.featureFlags.list({ limit: 50 });
console.log(flags.results.map(f => ({ key: f.key, active: f.active })));

Use the escape hatch for any endpoint:

const result = await posthog.api("GET", "/session_recordings", { limit: 5 });
console.log(result.results);

API Coverage

The posthog client provides typed methods for:

  • HogQL queries — posthog.query()

  • Insights — CRUD

  • Dashboards — CRUD

  • Events — list

  • Persons — list, get

  • Feature Flags — CRUD

  • Experiments — CRUD + results

  • Cohorts — CRUD

  • Annotations — list, get, create

  • Actions — CRUD

  • Surveys — CRUD

  • Error Tracking — list, get, update

  • Escape hatch — posthog.api(method, path, body) for anything not covered above

License

ISC

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      B
      quality
      D
      maintenance
      Enables interaction with PostHog analytics platform, allowing users to list projects, create annotations, and search insights through natural language in Claude Desktop.
      5
      MIT
    • A
      license
      Not graded
      quality
      Not graded
      maintenance
      Enables interaction with PostHog's product analytics platform through natural language, allowing users to manage feature flags, view errors, and access other platform features.
      1
      MIT
    • A
      license
      B
      quality
      D
      maintenance
      Enables AI agents to query PostHog analytics data directly via tool calls, including insights, events, feature flags, trends, and persons.
      5
      MIT