|
| 1 | +import { Tabs, TabItem } from '@astrojs/starlight/components' |
| 2 | + |
| 3 | +Connect a user's PostHog account and interact with PostHog's analytics, feature flags, experiments, and more through Scalekit. Scalekit handles the OAuth flow, token storage, and tool execution automatically. |
| 4 | + |
| 5 | +PostHog MCP is primarily used through Scalekit tools. Use `scalekit_client.actions.execute_tool()` to query analytics, manage feature flags, run experiments, and retrieve insights without calling the upstream MCP server directly. |
| 6 | + |
| 7 | +## Tool calling |
| 8 | + |
| 9 | +Use this connector when you want an agent to work with PostHog data and configuration. |
| 10 | + |
| 11 | +- List and search feature flags with `posthogmcp_feature_flag_get_all`. |
| 12 | +- Run trend, funnel, path, or HogQL queries using `posthogmcp_query_run`. |
| 13 | +- Create A/B tests via `posthogmcp_experiment_create` with custom metrics and variant splits. |
| 14 | +- Retrieve survey response data with `posthogmcp_survey_stats` after creating surveys using `posthogmcp_survey_create`. |
| 15 | +- Discover available events with `posthogmcp_event_definitions_list` before building queries or funnels. |
| 16 | + |
| 17 | +<Tabs syncKey="tech-stack"> |
| 18 | + <TabItem label="Python"> |
| 19 | + ```python title="examples/posthogmcp_list_flags.py" |
| 20 | + import os |
| 21 | + from scalekit.client import ScalekitClient |
| 22 | + |
| 23 | + scalekit_client = ScalekitClient( |
| 24 | + client_id=os.environ["SCALEKIT_CLIENT_ID"], |
| 25 | + client_secret=os.environ["SCALEKIT_CLIENT_SECRET"], |
| 26 | + env_url=os.environ["SCALEKIT_ENV_URL"], |
| 27 | + ) |
| 28 | + |
| 29 | + auth_link = scalekit_client.actions.get_authorization_link( |
| 30 | + connection_name="posthogmcp", |
| 31 | + identifier="user_123", |
| 32 | + ) |
| 33 | + print("Authorize PostHog MCP:", auth_link.link) |
| 34 | + input("Press Enter after authorizing...") |
| 35 | + |
| 36 | + connected_account = scalekit_client.actions.get_or_create_connected_account( |
| 37 | + connection_name="posthogmcp", |
| 38 | + identifier="user_123", |
| 39 | + ) |
| 40 | + |
| 41 | + tool_response = scalekit_client.actions.execute_tool( |
| 42 | + tool_name="posthogmcp_feature_flag_get_all", |
| 43 | + connected_account_id=connected_account.connected_account.id, |
| 44 | + tool_input={}, |
| 45 | + ) |
| 46 | + print("Feature flags:", tool_response) |
| 47 | + ``` |
| 48 | + </TabItem> |
| 49 | + <TabItem label="Node.js"> |
| 50 | + ```typescript title="examples/posthogmcp_list_flags.ts" |
| 51 | + import { ScalekitClient } from '@scalekit-sdk/node'; |
| 52 | + import 'dotenv/config'; |
| 53 | + |
| 54 | + const scalekit = new ScalekitClient( |
| 55 | + process.env.SCALEKIT_ENV_URL!, |
| 56 | + process.env.SCALEKIT_CLIENT_ID!, |
| 57 | + process.env.SCALEKIT_CLIENT_SECRET! |
| 58 | + ); |
| 59 | + const actions = scalekit.actions; |
| 60 | + |
| 61 | + const { link } = await actions.getAuthorizationLink({ |
| 62 | + connectionName: 'posthogmcp', |
| 63 | + identifier: 'user_123', |
| 64 | + }); |
| 65 | + console.log('Authorize PostHog MCP:', link); |
| 66 | + process.stdout.write('Press Enter after authorizing...'); |
| 67 | + await new Promise((resolve) => process.stdin.once('data', resolve)); |
| 68 | + |
| 69 | + const connectedAccount = await actions.getOrCreateConnectedAccount({ |
| 70 | + connectionName: 'posthogmcp', |
| 71 | + identifier: 'user_123', |
| 72 | + }); |
| 73 | + |
| 74 | + const toolResponse = await actions.executeTool({ |
| 75 | + toolName: 'posthogmcp_feature_flag_get_all', |
| 76 | + connectedAccountId: connectedAccount?.id, |
| 77 | + toolInput: {}, |
| 78 | + }); |
| 79 | + console.log('Feature flags:', toolResponse.data); |
| 80 | + ``` |
| 81 | + </TabItem> |
| 82 | +</Tabs> |
0 commit comments