Skip to content

feat(mcp): emit $mcp_protocol_version on events - #795

Open
gesh wants to merge 1 commit into
mainfrom
posthog-code/mcp-protocol-version
Open

feat(mcp): emit $mcp_protocol_version on events#795
gesh wants to merge 1 commit into
mainfrom
posthog-code/mcp-protocol-version

Conversation

@gesh

@gesh gesh commented Jul 28, 2026

Copy link
Copy Markdown
Member

What

Emit $mcp_protocol_version (the MCP spec version, e.g. 2025-06-18) on MCP analytics events — closing a parity gap with the TypeScript SDK, which already emits it.

Why it was missing

The session token already carried the protocol version (pv), but it never reached events. Root cause: capture_event's normalizer rebuilds the event from a fixed key list that omitted protocol_version, so it was silently dropped before reaching the property builder.

Changes

  • constants / _posthog_events: add $mcp_protocol_version, emitted on the primary $mcp_* event and the $exception sibling.
  • _capture: the event normalizer now keeps protocol_version (the missing link).
  • instrument() adapters: read the live protocol version off client_params, and backfill from the replayed token on stateless pods (via resolve_session_and_client), threading it through prepare_request / record_*.
  • PostHogMCP: capture_tool_call / capture_initialize / capture_tools_list / capture_missing_capability gain a protocol_version argument.

Validation

141 unit tests (incl. new primary+$exception emission tests), mypy + ruff clean, public-API snapshot updated. Verified on the 2-pod stateless cluster: $mcp_protocol_version=2025-06-18 lands on $mcp_initialize and every $mcp_tool_call across both pods (recovered from the token on the pod that never saw initialize).


Created with PostHog Code

The MCP protocol (spec) version was carried in the session token but never
stamped on events (JS emits it). Thread it through:

- constants: add $mcp_protocol_version; _posthog_events emits it on the primary
  event and the $exception sibling.
- _capture: the event normalizer now keeps protocol_version (it was silently
  dropped by the fixed key list -- the reason nothing surfaced before).
- instrument() adapters read the live protocol version off client_params and
  backfill from the token on stateless pods (via resolve_session_and_client),
  threading it to prepare_request / record_*.
- PostHogMCP capture_* methods gain a protocol_version argument.

Verified on the 2-pod stateless cluster: $mcp_protocol_version lands on
$mcp_initialize and every $mcp_tool_call across both pods.

Generated-By: PostHog Code
Task-Id: f44ec5e0-b836-4d13-98c4-c26887dd7ec2
@gesh
gesh requested a review from a team as a code owner July 28, 2026 10:02
@gesh
gesh marked this pull request as draft July 28, 2026 10:03
@greptile-apps

greptile-apps Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Reviews (1): Last reviewed commit: "feat(mcp): emit $mcp_protocol_version on..." | Re-trigger Greptile

@gesh
gesh marked this pull request as ready for review July 28, 2026 10:06
@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-07-28 10:08:51 UTC
Duration: 338856ms

✅ All Tests Passed!

111/111 tests passed


Capture_V1 Tests

94/94 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint 516ms
Endpoint And Method.Does Not Use Legacy Endpoints 1009ms
Required Headers.Has Authorization Bearer Header 1008ms
Required Headers.Has Content Type Json 1009ms
Required Headers.Has Posthog Sdk Info Format 1008ms
Required Headers.Has Posthog Attempt Header 1009ms
Required Headers.Has Posthog Request Id 1008ms
Required Headers.Has Posthog Request Timestamp 1007ms
Required Headers.Has User Agent 1009ms
Body Format.Body Has Created At And Batch 1009ms
Body Format.No Api Key In Body 1008ms
Body Format.No Sent At In Body 1008ms
Event Format.Event Has Required Root Fields 1009ms
Event Format.Event Uuid Is Valid 1008ms
Event Format.Event Timestamp Is Rfc3339 1008ms
Event Format.Distinct Id Is String 1009ms
Event Format.Distinct Id At Root Not Properties 1008ms
Event Format.Custom Properties Preserved 1009ms
Event Format.Set Properties Preserved 1008ms
Event Format.Set Once Properties Preserved 1008ms
Event Format.Groups Properties Preserved 1010ms
Event Format.Sdk Generates Uuid If Not Provided 1008ms
Event Format.Event Has Required Root Fields Batch 1013ms
Event Format.Event Uuid Is Valid Batch 1012ms
Event Format.Event Timestamp Is Rfc3339 Batch 1013ms
Event Format.Distinct Id Is String Batch 1012ms
Event Format.Distinct Id At Root Not Properties Batch 1012ms
Event Format.Custom Properties Preserved Batch 1012ms
Event Format.Set Properties Preserved Batch 1012ms
Event Format.Set Once Properties Preserved Batch 1013ms
Event Format.Groups Properties Preserved Batch 1012ms
Event Format.Sdk Generates Uuid If Not Provided Batch 1011ms
Batch Behavior.Multiple Events In Single Batch 1507ms
Batch Behavior.Batch Envelope Smoke 1014ms
Batch Behavior.Flush With No Events Sends Nothing 1005ms
Batch Behavior.Flush At Triggers Batch 1508ms
Batch Behavior.Created At Reflects Batch Creation Time 1011ms
Deduplication.Generates Unique Uuids 1506ms
Deduplication.Different Events Same Content Different Uuids 1507ms
Deduplication.Preserves Uuid On Retry 7513ms
Deduplication.Preserves Timestamp On Retry 7514ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 7511ms
Deduplication.No Duplicate Events In Batch 1504ms
Header Behavior On Retry.Attempt Header Starts At One 1008ms
Header Behavior On Retry.Attempt Header Increments On Retry 14523ms
Header Behavior On Retry.Request Id Preserved On Retry 7505ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3517ms
Header Behavior On Retry.Request Timestamp Changes On Retry 7514ms
Response Format Validation.Success Response Has Uuid Keyed Results 1007ms
Response Format Validation.Success Response Has Ok For Each Event 1507ms
Response Format Validation.Success No Retry After When All Ok 1507ms
Response Format Validation.Success Retry After Present When Retry Events 2510ms
Response Format Validation.Success No Retry After When Drop Only 1507ms
Response Format Validation.Response Echoes Request Id 1009ms
Retry Behavior.Retries On 408 7513ms
Retry Behavior.Retries On 500 7512ms
Retry Behavior.Retries On 503 9517ms
Retry Behavior.Retries On 504 7512ms
Retry Behavior.Retryable Errors Have Retry After 4509ms
Retry Behavior.Respects Retry After On Retryable Error 12518ms
Retry Behavior.Does Not Retry On 400 3503ms
Retry Behavior.Does Not Retry On 401 3508ms
Retry Behavior.Does Not Retry On 402 3508ms
Retry Behavior.Does Not Retry On 413 3507ms
Retry Behavior.Does Not Retry On 415 3508ms
Retry Behavior.Non Retryable Errors Have No Retry After 3506ms
Retry Behavior.Implements Backoff 23525ms
Retry Behavior.Max Retries Respected 23523ms
Partial Batch Handling.Handles 200 Full Success 3007ms
Partial Batch Handling.Handles 200 With All Ok 4505ms
Partial Batch Handling.Does Not Retry Dropped Events 4509ms
Partial Batch Handling.Does Not Retry Limited Events 4509ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 7510ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 7511ms
Partial Batch Handling.Retries Only Retry Events From Partial 7516ms
Partial Batch Handling.Partial Retry Preserves Uuids 7512ms
Partial Batch Handling.Partial Retry Attempt Header Increments 7512ms
Partial Batch Handling.Partial Retry Request Id Preserved 7512ms
Partial Batch Handling.Respects Retry After On Partial 9511ms
Partial Batch Handling.Unknown Result Treated As Terminal 4506ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 4506ms
Compression.Sends Gzip Content Encoding 1010ms
Compression.No Content Encoding When Disabled 1010ms
Compression.Compressed Body Is Decompressible 1009ms
Error Handling.Does Not Retry On Unknown 4Xx 3509ms
Event Options.Cookieless Mode Override 1007ms
Event Options.Disable Skew Correction Override 1009ms
Event Options.Process Person Profile Override 1008ms
Event Options.Product Tour Id Override 1008ms
Event Options.Unset Options Omitted 1009ms
Event Options.Options Override In Batch 1012ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 1009ms
Geoip And Historical Migration.Historical Migration Set In Body 1009ms
Geoip And Historical Migration.Historical Migration Absent By Default 1008ms

Feature_Flags Tests

17/17 tests passed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 1007ms
Request Payload.Flags Request Uses V2 Query Param 1007ms
Request Payload.Flags Request Hits Flags Path Not Decide 1007ms
Request Payload.Flags Request Omits Authorization Header 1006ms
Request Payload.Token In Flags Body Matches Init 1007ms
Request Payload.Groups Round Trip 1006ms
Request Payload.Groups Default To Empty Object 1006ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 1006ms
Request Payload.Disable Geoip Omitted Defaults To False 1007ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 1007ms
Request Lifecycle.No Flags Request On Init Alone 503ms
Request Lifecycle.No Flags Request On Normal Capture 1507ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 1010ms
Request Lifecycle.Mock Response Value Is Returned To Caller 1002ms
Retry Behavior.Retries Flags On 502 1007ms
Retry Behavior.Retries Flags On 504 1006ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 1509ms

@gesh
gesh requested a review from a team July 28, 2026 10:06
@greptile-apps

greptile-apps Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Reviews (2): Last reviewed commit: "feat(mcp): emit $mcp_protocol_version on..." | Re-trigger Greptile

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