Tutorials

Create custom MCP server for PostHog - without code

H

Hoshang Mehta

Create custom MCP server for PostHog - without code

PostHog is your product analytics powerhouse. It tracks user events, feature flags, session recordings, and conversion funnels. But when you want to build an AI agent that can answer "What's the conversion rate for our new onboarding flow?" or "Which features are power users engaging with?", you face a challenge.

The PostHog API is powerful but requires understanding event schemas, aggregations, and time-series queries. Building a custom analytics backend just to expose insights to your agent is overkill.

Pylar makes it effortless.

Pylar syncs your PostHog data to a SQL-queryable warehouse and lets you build MCP (Model Context Protocol) tools with natural language prompts—no code, no complex event queries.

Why Pylar for PostHog?

  • SQL Over Events: Query your product analytics data with standard SQL instead of learning PostHog's query syntax.
  • Cross-Platform Insights: Join PostHog events with Stripe revenue or HubSpot customer data to understand the full user journey.
  • Fast Analytics: Run complex queries without impacting your PostHog instance performance.

Step-by-Step Walkthrough

Step 1: Connect PostHog to Pylar

  1. In Pylar, go to Connections -> PostHog.
  2. Enter your PostHog Project API Key and Instance URL (e.g., https://app.posthog.com or your self-hosted URL).
  3. Pylar will begin syncing your PostHog data (events, persons, feature flags, etc.).

PostHog connection setup

You'll receive an email when the sync completes.

Step 2: Create a Sandboxed View

Let's say you want an agent to help your product team understand feature adoption.

In Pylar's SQL IDE, create a view:

-- view: feature_adoption
SELECT 
  p.distinct_id,
  p.email,
  p.created_at as user_created_at,
  COUNT(CASE WHEN e.event = 'feature_used_dashboard' THEN 1 END) as dashboard_uses,
  COUNT(CASE WHEN e.event = 'feature_used_export' THEN 1 END) as export_uses,
  MAX(e.timestamp) as last_activity
FROM posthog.persons p
LEFT JOIN posthog.events e ON p.distinct_id = e.distinct_id
WHERE e.timestamp > NOW() - INTERVAL '30 days'
GROUP BY p.distinct_id, p.email, p.created_at

Creating a sandboxed view in SQL IDE

This view gives the agent a clean summary of feature usage per user.

Step 3: Auto-Build the MCP Tool

  1. Select your view (feature_adoption) in the right sidebar.
  2. Click "Create MCP Tool" and choose "Create with AI".
  3. Type a prompt: "Create a tool that shows feature usage for a specific user by email."
  4. Pylar generates the tool configuration:
    • Name: get_user_feature_usage
    • Description: "Returns feature adoption metrics for a user given their email."
    • Parameters: email (string)

Auto-generating MCP tool from view

Step 4: Publish & Connect

  1. Click "Publish" in the right sidebar.
  2. Click "Generate Token".
  3. Copy your MCP HTTP Stream URL (https://mcp.publish.pylar.ai/mcp) and Authorization Bearer Token.

Publishing MCP server

Connect to Internal Product Dashboard: You can now build a custom dashboard that lets your team query PostHog data via natural language.

from langchain_mcp import MCPTool

posthog_tool = MCPTool(
    url="https://api.pylar.ai/mcp/v1/server/YOUR_SERVER_ID",
    api_key="YOUR_API_KEY"
)
# Agent can now answer: "Which features is user@example.com using?"

Advanced Use Cases for PostHog Agents

1. Activation Funnel Analysis

Goal: Identify where users drop off in your onboarding flow.

  • View: Aggregate events by funnel step and calculate conversion rates.
  • Tool Prompt: "Create a tool that calculates the conversion rate from signup to first feature use."
  • User Query: "What's our activation rate this week?"

2. Power User Identification

Goal: Find users who are deeply engaged with your product.

  • View: Aggregate event counts per user and filter by threshold.
  • Tool Prompt: "Create a tool that finds users with more than 100 events in the last 7 days."
  • User Query: "Who are our most active users?"

Conclusion

PostHog is a powerful product analytics platform, but giving AI agents access to event data has always required complex API integrations and custom data pipelines. With Pylar, you can transform your PostHog product analytics into an AI-ready resource in under 2 minutes—no coding required.

By connecting Pylar to your PostHog instance (including self-hosted deployments), creating sandboxed views that aggregate event data, and auto-generating MCP tools with natural language, you unlock the full potential of your product analytics for AI agents while maintaining complete security and governance.

Whether you're building a funnel analysis tool, a power user identification system, or any other product analytics-powered AI application, Pylar gives you the governance layer you need. Your views act as security boundaries, and with support for all custom events and feature flag states, you can query your product analytics just like any SQL database.

Turn your PostHog product analytics into an AI-ready resource. Start building intelligent product agents today.

Start building your PostHog MCP server for free at pylar.ai.