posthog-code-mode-mcp
Provides a single [posthog](/mcp/integrations/posthog) tool that executes JavaScript in a sandboxed VM against a typed PostHog API client, enabling HogQL queries and management of insights, dashboards, events, persons, feature flags, experiments, cohorts, annotations, actions, surveys and error tracking, plus a generic posthog.api(method, path, body) escape hatch for any other PostHog endpoint.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
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 install2. Configure environment
Copy the example and fill in your values:
cp .env.example .envPOSTHOG_API_KEY=phx_your_personal_api_key
POSTHOG_PROJECT_ID=12345API Key: Create a personal API key at https://us.posthog.com/settings/user-api-keys
Project ID: Find your project ID in your PostHog project settings URL
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…
This server cannot be deployed
Maintenance
Related MCP Connectors
Query analytics, manage feature flags and experiments, and investigate errors in PostHog.
Lets AI agents analyze API metrics, request logs, and traces, and manage apps and alerts.
Product analytics, error tracking, feature flags and SaaS operations with scoped OAuth access.
Email inboxes, webhooks, mock APIs, 2FA codes, approvals and 170 tools for AI agents.
Related MCP Servers
- AlicenseBqualityDmaintenanceEnables interaction with PostHog analytics platform, allowing users to list projects, create annotations, and search insights through natural language in Claude Desktop.5MIT
- AlicenseNot gradedqualityNot gradedmaintenanceEnables interaction with PostHog's product analytics platform through natural language, allowing users to manage feature flags, view errors, and access other platform features.1MIT
- AlicenseNot gradedqualityDmaintenanceEnables interaction with PostHog product analytics API, supporting project listing, event queries, insight analysis, feature flag management, and annotation creation.MIT
- AlicenseBqualityDmaintenanceEnables AI agents to query PostHog analytics data directly via tool calls, including insights, events, feature flags, trends, and persons.5MIT