Skip to content

feat(mcp): PostHog MCP analytics SDK for Python (posthog.mcp)#691

Open
lucasheriques wants to merge 9 commits into
mainfrom
posthog-code/mcp-analytics-python-sdk
Open

feat(mcp): PostHog MCP analytics SDK for Python (posthog.mcp)#691
lucasheriques wants to merge 9 commits into
mainfrom
posthog-code/mcp-analytics-python-sdk

Conversation

@lucasheriques

@lucasheriques lucasheriques commented Jun 22, 2026

Copy link
Copy Markdown

What

Adds posthog.mcp — a Python SDK for PostHog MCP analytics, the Python sibling of the TypeScript @posthog/mcp package. This is the #1 "what to prioritize next" item on the MCP analytics mega-issue, PostHog/posthog#64016: MCP analytics was TS-only, but many MCP servers are Python.

from posthog import Posthog
from posthog.mcp import instrument
from mcp.server.fastmcp import FastMCP

posthog = Posthog("phc_...", host="https://us.i.posthog.com")
server = FastMCP("my-server")
analytics = instrument(server, posthog)   # one line

Install: pip install posthog[mcp].

How it works

Packaged as a submodule of posthog (mirroring posthog.ai), guarded by an optional mcp extra so import posthog never requires mcp. The wire format is byte-identical to @posthog/mcp's constants.ts, so the existing MCP analytics dashboard ingests Python-server data with zero backend changes.

instrument(server, posthog_client) supports every common Python MCP server:

  • FastMCP and the low-level Server from the official modelcontextprotocol/python-sdk (the mcp package)
  • jlowin's standalone FastMCP 2.0 (the separate fastmcp package)
  • PostHogMCP (a Client subclass) for custom/edge dispatchers with no server object

Captures $mcp_tool_call, $mcp_tools_list, $mcp_initialize (lazy), $identify, $exception, and $mcp_missing_capability. Features: agent-intent capture via an injected context arg, identify, before_send, event_properties, report_missing (get_more_tools), and conversation_id.

Implementation notes:

  • FastMCP is instrumented via two central seams (ToolManager.call_tool + the ListToolsRequest handler) rather than per-tool wrapping — late-registered tools are covered automatically.
  • jlowin's FastMCP 2.0 routes through the low-level adapter (its _mcp_server subclasses the official Server); it validates against the function signature and rejects unexpected kwargs, so the injected context/conversation_id are stripped before dispatch.
  • UUIDv7 implemented inline (no new dependency); $exception_list reuses posthog.exception_utils.
  • $mcp_initialize is emitted lazily from client_params because the Python mcp SDK handles initialize in the session layer, not via request_handlers.
  • On a raw low-level Server, context/conversation_id are injected as optional schema properties (that schema is also the validation schema) so a call omitting them is never rejected.

Testing

  • 56 unit + end-to-end tests (posthog/test/mcp/), driving the real mcp / fastmcp SDK seams with a fake capture client. ruff + mypy clean.
  • Dogfooded against project 2: a demo server (examples/mcp_analytics_demo.py) sent the full $mcp_* event set; verified the events ingest with correct intent, error flag, and $mcp_source and are readable by the dashboard.

Links

Status

Alpha (minor changeset). Parity note: the TS instrument() does not emit resources/prompts events, so those are intentionally omitted here too.


Created with PostHog Code

Port the core of @posthog/mcp to Python as a posthog.mcp submodule: event
vocabulary, inline uuidv7 + FNV-1a ids, STDIO-safe logger, sanitization,
layered truncation, $mcp_* event building, $exception fan-out (reusing
posthog.exception_utils), and the sanitize->truncate->before_send->capture
sink. Adds the `mcp` optional extra. No server wrapping yet (M2).

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
…e (M2)

instrument(server, posthog_client) now wraps a FastMCP server by hooking two
central seams: ToolManager.call_tool (strip injected context before Pydantic
validation, time the call, capture $mcp_tool_call + $exception, re-raise) and
the ListToolsRequest handler ($mcp_tools_list + context-parameter injection).
$mcp_initialize is emitted lazily per session from client_params. Adds the
import guard, per-server state in a WeakKeyDictionary, session resolution with
an asyncio lock, identity dedup, and the custom-event handle.

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
A runnable FastMCP server instrumented with posthog.mcp that emits the full
$mcp_* event set. Verified end-to-end against project 2: tool calls, intent,
error capture, initialize, identify, and a custom event all ingest correctly.

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
instrument() now also wraps a low-level mcp.server.Server by hooking its public
request_handlers (CallToolRequest, ListToolsRequest). Errors are read from the
isError CallToolResult the low-level handler produces. context is injected as an
*optional* schema property there (the advertised schema is also the validation
schema) so a call omitting it is never rejected.

PostHogMCP is a posthog Client subclass for custom dispatchers with
capture_tool_call / capture_initialize / capture_tools_list /
capture_missing_capability + prepare_tool_list / prepare_tool_call, flowing
through the same pipeline. Shared tool-list helpers moved into instrumentation.

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
reportMissing advertises the get_more_tools virtual tool in tools/list; calling
it emits $mcp_missing_capability (not $mcp_tool_call) and returns the canned
acknowledgement, across FastMCP, low-level Server, and PostHogMCP.

enableConversationId injects an optional conversation_id parameter, mints one
when the agent omits it, captures it as $mcp_conversation_id, and appends a
prompt-back asking the agent to echo it on later calls.

Parity note: the TS instrument() does not emit resources/prompts events, so
those are intentionally omitted here too.

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
@greptile-apps

greptile-apps Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "chore(mcp): add changeset for the Python..." | Re-trigger Greptile

Comment thread posthog/mcp/instrumentation.py
Comment thread posthog/mcp/intent.py
Comment on lines +54 to +67
async def resolve_tool_call_intent(
data: MCPAnalyticsData,
request: Dict[str, Any],
extra: Optional[Dict[str, Any]] = None,
) -> Optional[ResolvedIntent]:
context_argument = _get_context_argument(request)
name = (request.get("params") or {}).get("name")
if (
is_context_enabled(data.options.context)
and name != "get_more_tools"
and context_argument
):
return (context_argument, "context_parameter")
return await _run_intent_fallback(data, request, extra)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 The get_more_tools check is hardcoded as a string literal instead of going through resolve_missing_capability_tool_name. When a user configures MCPAnalyticsOptions(missing_capability_tool_name="find_more_tools"), calls to find_more_tools with a context argument will be incorrectly classified as context_parameter intent rather than being skipped as a missing-capability probe — producing wrong analytics data for a documented configuration option.

Suggested change
async def resolve_tool_call_intent(
data: MCPAnalyticsData,
request: Dict[str, Any],
extra: Optional[Dict[str, Any]] = None,
) -> Optional[ResolvedIntent]:
context_argument = _get_context_argument(request)
name = (request.get("params") or {}).get("name")
if (
is_context_enabled(data.options.context)
and name != "get_more_tools"
and context_argument
):
return (context_argument, "context_parameter")
return await _run_intent_fallback(data, request, extra)
async def resolve_tool_call_intent(
data: MCPAnalyticsData,
request: Dict[str, Any],
extra: Optional[Dict[str, Any]] = None,
) -> Optional[ResolvedIntent]:
from .tools import resolve_missing_capability_tool_name
context_argument = _get_context_argument(request)
name = (request.get("params") or {}).get("name")
missing_name = resolve_missing_capability_tool_name(data.options)
if (
is_context_enabled(data.options.context)
and name != missing_name
and context_argument
):
return (context_argument, "context_parameter")
return await _run_intent_fallback(data, request, extra)

Comment on lines +20 to +64
def capture(
self,
event,
distinct_id=None,
properties=None,
timestamp=None,
uuid=None,
**kwargs,
):
self.events.append(
{"event": event, "distinct_id": distinct_id, "properties": properties or {}}
)
return None


def make_server():
server = FastMCP("test-server")

@server.tool()
def add(a: int, b: int) -> int:
return a + b

@server.tool()
def boom() -> str:
raise ValueError("explode")

return server


async def _flush():
"""Let fire-and-forget capture tasks run to completion."""
import posthog.mcp.instrumentation as instr

for _ in range(10):
await asyncio.sleep(0)
pending = [t for t in list(instr._BACKGROUND_TASKS) if not t.done()]
if pending:
await asyncio.gather(*pending, return_exceptions=True)
await asyncio.sleep(0)


def _events(client, name):
return [e for e in client.events if e["event"] == name]


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Duplicated test helpers across all MCP test files

FakeClient, _flush, and _events are defined identically in test_fastmcp.py, test_lowlevel.py, test_features_m4.py, and test_posthog_mcp.py. This is a direct violation of the OnceAndOnlyOnce simplicity rule. Moving them to a shared posthog/test/mcp/conftest.py (as pytest fixtures) would eliminate the repetition and make future changes to the fake client or flush logic apply everywhere automatically.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment thread posthog/mcp/version.py
instrument() now also accepts a fastmcp.FastMCP (jlowin's standalone FastMCP
2.0), distinct from the official SDK's mcp.server.fastmcp.FastMCP. Its
_mcp_server is a subclass of the official low-level Server, so it routes through
the low-level adapter via its request_handlers seam — but FastMCP 2.0 validates
tool args against the function signature and rejects unexpected kwargs, so the
injected context/conversation_id are stripped before dispatch (the low-level
adapter is parameterized: strip + required-advisory for fastmcp 2.0, optional +
no-strip for a raw Server). Adds fastmcp to the test extra; tests skip if absent.

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
@lucasheriques

Copy link
Copy Markdown
Author

Added support for jlowin's standalone FastMCP 2.0 (the separate fastmcp package), in addition to the official SDK's FastMCP + low-level Server. instrument() now accepts all three (plus PostHogMCP for custom dispatchers).

Implementation: jlowin's FastMCP._mcp_server is a subclass of the official low-level Server, so it routes through the low-level adapter's request_handlers seam. The one wrinkle — FastMCP 2.0 validates tool args against the function signature and rejects unexpected kwargs — is handled by stripping the injected context/conversation_id before dispatch (the low-level adapter is now parameterized: strip + required-advisory for fastmcp 2.0, optional + no-strip for a raw Server). Covered by posthog/test/mcp/test_fastmcp_v2.py (56 tests total; skipped when fastmcp isn't installed).

@github-actions

github-actions Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-06-22 20:15:19 UTC
Duration: 530129ms

✅ All Tests Passed!

45/45 tests passed


Capture Tests

29/29 tests passed

View Details
Test Status Duration
Format Validation.Event Has Required Fields 517ms
Format Validation.Event Has Uuid 10007ms
Format Validation.Event Has Lib Properties 10007ms
Format Validation.Distinct Id Is String 10007ms
Format Validation.Token Is Present 10007ms
Format Validation.Custom Properties Preserved 10007ms
Format Validation.Event Has Timestamp 10007ms
Retry Behavior.Retries On 503 18019ms
Retry Behavior.Does Not Retry On 400 12003ms
Retry Behavior.Does Not Retry On 401 10008ms
Retry Behavior.Respects Retry After Header 16015ms
Retry Behavior.Implements Backoff 30017ms
Retry Behavior.Retries On 500 13011ms
Retry Behavior.Retries On 502 16011ms
Retry Behavior.Retries On 504 16010ms
Retry Behavior.Max Retries Respected 30018ms
Deduplication.Generates Unique Uuids 7002ms
Deduplication.Preserves Uuid On Retry 16015ms
Deduplication.Preserves Uuid And Timestamp On Retry 23019ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 16005ms
Deduplication.No Duplicate Events In Batch 10003ms
Deduplication.Different Events Have Different Uuids 10007ms
Compression.Sends Gzip When Enabled 10007ms
Batch Format.Uses Proper Batch Structure 10006ms
Batch Format.Flush With No Events Sends Nothing 5006ms
Batch Format.Multiple Events Batched Together 10005ms
Error Handling.Does Not Retry On 403 12009ms
Error Handling.Does Not Retry On 413 10008ms
Error Handling.Retries On 408 14013ms

Feature_Flags Tests

16/16 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 9501ms
Request Payload.Flags Request Uses V2 Query Param 10007ms
Request Payload.Flags Request Hits Flags Path Not Decide 10007ms
Request Payload.Flags Request Omits Authorization Header 10007ms
Request Payload.Token In Flags Body Matches Init 10006ms
Request Payload.Groups Round Trip 10007ms
Request Payload.Groups Default To Empty Object 10007ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 10006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 10007ms
Request Payload.Disable Geoip Omitted Defaults To False 10007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 10006ms
Request Lifecycle.No Flags Request On Init Alone 5003ms
Request Lifecycle.No Flags Request On Normal Capture 10508ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 9510ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10003ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 10509ms

@lucasheriques lucasheriques requested a review from pauldambra June 22, 2026 19:07
@lucasheriques lucasheriques marked this pull request as ready for review June 22, 2026 19:07
@lucasheriques lucasheriques requested a review from a team as a code owner June 22, 2026 19:07
The new posthog.mcp submodule adds public API surface; refresh the griffe
snapshot so the "Public API snapshot" CI check passes.

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
@greptile-apps

greptile-apps Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "chore(mcp): regenerate public API snapsh..." | Re-trigger Greptile

…ersation_id, loop leak

- Isolate analytics from the tool path: record_tool_call / record_tools_list /
  record_missing_capability swallow+log internally so a capture failure can never
  change what the tool returns or raises.
- Apply event_properties to $mcp_tools_list / $mcp_initialize / $mcp_missing_capability
  (previously only $mcp_tool_call), matching the TS fan-out.
- conversation_id: only stamp $mcp_conversation_id when the prompt-back was actually
  delivered (inject first, then capture; clear on error / non-injectable results) so
  no orphan ids; strip conversation_id from $mcp_parameters; handle FastMCP's
  (content, structured) tuple result so the prompt-back lands.
- fire_and_forget: reuse one daemon background loop for sync hosts instead of leaking
  a new event loop per call; log background-task exceptions; add drain_pending() +
  McpAnalytics.flush() to await in-flight events before shutdown (demo uses it).
- Bound initialized_sessions (FIFO, cap 1000) to stop a per-session memory leak.
- $mcp_tools_list now carries $mcp_is_error=False; fix stale "see M3" docstring.

Generated-By: PostHog Code
Task-Id: b21bc954-5de3-4512-a0d5-6bec2371f782
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant