Skip to content

fix(mcp): keep only the response envelope on $mcp_tools_list - #994

Merged
lucasheriques merged 3 commits into
mainfrom
lucas/mcp-tools-list-envelope
Sep 30, 2026
Merged

lucasheriques merged 3 commits into
mainfrom
lucas/mcp-tools-list-envelope

Conversation

@lucasheriques

@lucasheriques lucasheriques commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Why

$mcp_tools_list events copied the whole tools/list result into $mcp_response, including every tool descriptor with its full input schema. On a large catalogue that copy goes through sanitization and the truncation ladder on every list, and nothing in PostHog reads it. $mcp_listed_tool_names already carries every tool name.

This mirrors PostHog/posthog-js#5160.

Scope

  • New tools_list_envelope() in posthog/mcp/_instrumentation.py keeps only the fields the handler set, minus tools (nextCursor, ttlMs, cacheScope, _meta), for the 1.x ServerResult root and the 2.x result. It returns None for anything that is not a model.
  • ToolsListLifecycle.record_result takes the raw result, and record_tools_list builds the envelope inside its analytics guard. The lowlevel, FastMCP and v2 adapters pass result=result.
  • A result with nothing but tools sets no $mcp_response.
  • The client reply, $mcp_listed_tool_names, and the manual PostHogMCP.capture_tools_list are unchanged.

Tradeoffs

exclude_unset drops the 2.x SDK defaults (ttlMs: 0, cacheScope: "private", resultType: "complete") that the handler never set. Otherwise every 2.x event would carry them, and the JS SDK records only what the handler returned.

Blast Radius

Only the $mcp_response property of $mcp_tools_list changes. No PostHog query or insight reads the tool list from it. Building the envelope inside the guard means an odd handler result can no longer fail the client's tools/list.

Verification

  • pytest posthog/test/mcp on mcp 1.30.0: 565 passed, 1 skipped.
  • pytest posthog/test/mcp on mcp 2.2.0: 455 passed, 21 skipped.
  • Parametrized tests on both majors assert the literal envelope for paginated and single-page results. A unit table covers None and a bare list, which raised before.
  • ruff 0.11.12 check and format: clean.

Every tools/list result (all descriptors, input schemas, injected virtual tools) was serialized into $mcp_response, then sanitized and truncated on the request path. On large catalogues it exceeded MAX_EVENT_BYTES and re-measured through the truncation ladder. Nothing reads the list from $mcp_response, and $mcp_listed_tool_names already carries every name. Mirrors PostHog/posthog-js#5160.

tools_list_envelope() dumps only the fields the handler set, minus tools (exclude_unset, exclude_none, by_alias), for the 1.x ServerResult root and the 2.x result. The lowlevel, FastMCP and v2 adapters use it. An empty envelope sets no $mcp_response. The client reply, $mcp_listed_tool_names and PostHogMCP.capture_tools_list are unchanged.

Tested: pytest posthog/test/mcp on mcp 1.30.0 (561 passed, 1 skipped) and mcp 2.2.0 (451 passed, 21 skipped); ruff 0.11.12 check and format clean.
@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-09-30T17:56:05.488488+00:00
Duration: 259812ms

✅ All Tests Passed!

121/121 tests passed


Capture_V1 Tests

✅ 95/95 tests passed

View Details
Test Status Duration
Endpoint And Method.Targets V1 Endpoint ✅ 516ms
Endpoint And Method.Does Not Use Legacy Endpoints ✅ 510ms
Required Headers.Has Authorization Bearer Header ✅ 509ms
Required Headers.Has Content Type Json ✅ 511ms
Required Headers.Has Posthog Sdk Info Format ✅ 510ms
Required Headers.Has Posthog Attempt Header ✅ 509ms
Required Headers.Has Posthog Request Id ✅ 509ms
Required Headers.Has Posthog Request Timestamp ✅ 509ms
Required Headers.Has User Agent ✅ 509ms
Body Format.Body Has Created At And Batch ✅ 510ms
Body Format.No Api Key In Body ✅ 510ms
Body Format.No Sent At In Body ✅ 512ms
Event Format.Event Has Required Root Fields ✅ 510ms
Event Format.Event Uuid Is Valid ✅ 511ms
Event Format.Event Timestamp Is Rfc3339 ✅ 512ms
Event Format.Non Utc Event Timestamp Is Converted To Utc ✅ 516ms
Event Format.Distinct Id Is String ✅ 510ms
Event Format.Distinct Id At Root Not Properties ✅ 510ms
Event Format.Custom Properties Preserved ✅ 510ms
Event Format.Set Properties Preserved ✅ 510ms
Event Format.Set Once Properties Preserved ✅ 510ms
Event Format.Groups Properties Preserved ✅ 511ms
Event Format.Sdk Generates Uuid If Not Provided ✅ 510ms
Event Format.Event Has Required Root Fields Batch ✅ 514ms
Event Format.Event Uuid Is Valid Batch ✅ 514ms
Event Format.Event Timestamp Is Rfc3339 Batch ✅ 514ms
Event Format.Distinct Id Is String Batch ✅ 515ms
Event Format.Distinct Id At Root Not Properties Batch ✅ 514ms
Event Format.Custom Properties Preserved Batch ✅ 513ms
Event Format.Set Properties Preserved Batch ✅ 513ms
Event Format.Set Once Properties Preserved Batch ✅ 514ms
Event Format.Groups Properties Preserved Batch ✅ 514ms
Event Format.Sdk Generates Uuid If Not Provided Batch ✅ 514ms
Batch Behavior.Multiple Events In Single Batch ✅ 517ms
Batch Behavior.Batch Envelope Smoke ✅ 516ms
Batch Behavior.Flush With No Events Sends Nothing ✅ 506ms
Batch Behavior.Flush At Triggers Batch ✅ 1012ms
Batch Behavior.Created At Reflects Batch Creation Time ✅ 511ms
Deduplication.Generates Unique Uuids ✅ 517ms
Deduplication.Different Events Same Content Different Uuids ✅ 512ms
Deduplication.Preserves Uuid On Retry ✅ 6520ms
Deduplication.Preserves Timestamp On Retry ✅ 6517ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry ✅ 6518ms
Deduplication.No Duplicate Events In Batch ✅ 519ms
Header Behavior On Retry.Attempt Header Starts At One ✅ 511ms
Header Behavior On Retry.Attempt Header Increments On Retry ✅ 13527ms
Header Behavior On Retry.Request Id Preserved On Retry ✅ 6518ms
Header Behavior On Retry.Different Requests Have Different Request Ids ✅ 3019ms
Header Behavior On Retry.Request Timestamp Changes On Retry ✅ 6519ms
Response Format Validation.Success Response Has Uuid Keyed Results ✅ 510ms
Response Format Validation.Success Response Has Ok For Each Event ✅ 513ms
Response Format Validation.Success No Retry After When All Ok ✅ 512ms
Response Format Validation.Success Retry After Present When Retry Events ✅ 1515ms
Response Format Validation.Success No Retry After When Drop Only ✅ 513ms
Response Format Validation.Response Echoes Request Id ✅ 510ms
Retry Behavior.Retries On 408 ✅ 6517ms
Retry Behavior.Retries On 500 ✅ 6520ms
Retry Behavior.Retries On 503 ✅ 8522ms
Retry Behavior.Retries On 504 ✅ 6519ms
Retry Behavior.Retryable Errors Have Retry After ✅ 3516ms
Retry Behavior.Respects Retry After On Retryable Error ✅ 11522ms
Retry Behavior.Does Not Retry On 400 ✅ 2514ms
Retry Behavior.Does Not Retry On 401 ✅ 2513ms
Retry Behavior.Does Not Retry On 402 ✅ 2513ms
Retry Behavior.Does Not Retry On 413 ✅ 2513ms
Retry Behavior.Does Not Retry On 415 ✅ 2512ms
Retry Behavior.Non Retryable Errors Have No Retry After ✅ 2512ms
Retry Behavior.Implements Backoff ✅ 22530ms
Retry Behavior.Max Retries Respected ✅ 22529ms
Partial Batch Handling.Handles 200 Full Success ✅ 2512ms
Partial Batch Handling.Handles 200 With All Ok ✅ 3514ms
Partial Batch Handling.Does Not Retry Dropped Events ✅ 3513ms
Partial Batch Handling.Does Not Retry Limited Events ✅ 3515ms
Partial Batch Handling.Prunes Ok Events On Partial Retry ✅ 6520ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry ✅ 6520ms
Partial Batch Handling.Retries Only Retry Events From Partial ✅ 6522ms
Partial Batch Handling.Partial Retry Preserves Uuids ✅ 6516ms
Partial Batch Handling.Partial Retry Attempt Header Increments ✅ 6520ms
Partial Batch Handling.Partial Retry Request Id Preserved ✅ 6516ms
Partial Batch Handling.Respects Retry After On Partial ✅ 8519ms
Partial Batch Handling.Unknown Result Treated As Terminal ✅ 3516ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry ✅ 3519ms
Compression.Sends Gzip Content Encoding ✅ 510ms
Compression.No Content Encoding When Disabled ✅ 509ms
Compression.Compressed Body Is Decompressible ✅ 508ms
Error Handling.Does Not Retry On Unknown 4Xx ✅ 2511ms
Event Options.Cookieless Mode Override ✅ 509ms
Event Options.Disable Skew Correction Override ✅ 509ms
Event Options.Process Person Profile Override ✅ 509ms
Event Options.Product Tour Id Override ✅ 509ms
Event Options.Unset Options Omitted ✅ 509ms
Event Options.Options Override In Batch ✅ 512ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties ✅ 509ms
Geoip And Historical Migration.Historical Migration Set In Body ✅ 508ms
Geoip And Historical Migration.Historical Migration Absent By Default ✅ 511ms

Capture_Ai Tests

✅ 5/5 tests passed

View Details
Test Status Duration
Routing.Capture Ai Posts To Ai Endpoint ✅ 509ms
Routing.Capture Does Not Reroute Ai Named Events ✅ 510ms
Identity.Capture Ai Event Has Uuid ✅ 509ms
Identity.Capture Ai Keeps Supplied Uuid ✅ 510ms
Timestamp Format.Non Utc Event Timestamp Is Converted To Utc ✅ 510ms

Feature_Flags Tests

✅ 17/17 tests passed

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

Feature_Flags_Local_Evaluation Tests

✅ 4/4 tests passed

View Details
Test Status Duration
Versioned Boolean Matching.Matching Version Missing ✅ 70ms
Versioned Boolean Matching.Matching Version 1 ✅ 67ms
Versioned Boolean Matching.Matching Version 2 ✅ 68ms
Versioned Boolean Matching.Version Only Reload 1 2 1 2 Missing ✅ 35ms

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Retrigger

[Medium risk] Changes how tool listing events capture response data.

The PR appears safe to merge; the outstanding test-coverage gap is non-blocking.

Reviews (2) · Last reviewed commit: "fix(mcp): build the tools/list envelope ..."

Comment thread posthog/test/mcp/test_v2_lowlevel.py
Review follow-up. The adapters built the envelope as a record_result argument, outside record_tools_list's try, and model_dump raised on a result that is not a model, failing the client's tools/list. ToolsListLifecycle.record_result now takes the raw result and record_tools_list shapes it inside the guard, so the rule lives in one place. tools_list_envelope returns None for non-models; the unused dict branch is gone. Renamed the tools/list duration test to match what it asserts.

Tested: pytest posthog/test/mcp on mcp 1.30.0 (565 passed, 1 skipped) and 2.2.0 (455 passed, 21 skipped); ruff 0.11.12 clean. New tools_list_envelope rows for None and a bare list fail on the previous helper.
Review follow-up (Greptile). Adds a tools_list_envelope row asserting _meta keeps its wire alias alongside nextCursor.

Tested: pytest posthog/test/mcp on mcp 1.30.0 (566 passed) and 2.2.0 (456 passed); ruff clean.
@lucasheriques
lucasheriques merged commit bcf9d34 into main Sep 30, 2026
43 checks passed
@lucasheriques
lucasheriques deleted the lucas/mcp-tools-list-envelope branch September 30, 2026 18:20
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.

2 participants