Skip to content

fix(mcp): anchor virtual tools to conversations - #963

Merged
gesh merged 2 commits into
mainfrom
posthog/mcp-virtual-tool-conversation-id
Sep 17, 2026
Merged

gesh merged 2 commits into
mainfrom
posthog/mcp-virtual-tool-conversation-id

Conversation

@gesh

@gesh gesh commented Sep 15, 2026

Copy link
Copy Markdown
Member

Problem

With enable_conversation_id=True, the virtual tools did not use conversation IDs. Their events could use a different session from related tool calls.

Change

  • Add optional conversation_id fields to enabled virtual tools.
  • Read or mint the ID and return new IDs to the client.
  • Use the ID for virtual-tool events and session selection.
  • Keep the existing virtual-tool intent fields unchanged.

This applies to both virtual tools, renamed tools, and MCP v1 and v2.

Tests

  • MCP v1: 518 passed.
  • Focused MCP v2: 102 passed and 6 skipped.
  • Ruff, mypy, public API, and import checks passed.

Created with PostHog Desktop

@gesh
gesh requested a review from a team as a code owner September 15, 2026 13:07
@gesh
gesh removed the request for review from a team September 15, 2026 13:08
@gesh
gesh marked this pull request as draft September 15, 2026 13:09
@github-actions

github-actions Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-09-17 10:48:37 UTC
Duration: 256462ms

✅ 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 518ms
Endpoint And Method.Does Not Use Legacy Endpoints 512ms
Required Headers.Has Authorization Bearer Header 511ms
Required Headers.Has Content Type Json 510ms
Required Headers.Has Posthog Sdk Info Format 511ms
Required Headers.Has Posthog Attempt Header 510ms
Required Headers.Has Posthog Request Id 511ms
Required Headers.Has Posthog Request Timestamp 510ms
Required Headers.Has User Agent 511ms
Body Format.Body Has Created At And Batch 511ms
Body Format.No Api Key In Body 512ms
Body Format.No Sent At In Body 511ms
Event Format.Event Has Required Root Fields 510ms
Event Format.Event Uuid Is Valid 510ms
Event Format.Event Timestamp Is Rfc3339 510ms
Event Format.Distinct Id Is String 511ms
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 511ms
Event Format.Groups Properties Preserved 510ms
Event Format.Sdk Generates Uuid If Not Provided 511ms
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 514ms
Event Format.Distinct Id At Root Not Properties Batch 514ms
Event Format.Custom Properties Preserved Batch 514ms
Event Format.Set Properties Preserved Batch 514ms
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 518ms
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 6519ms
Deduplication.Preserves Timestamp On Retry 6516ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 6524ms
Deduplication.No Duplicate Events In Batch 519ms
Header Behavior On Retry.Attempt Header Starts At One 510ms
Header Behavior On Retry.Attempt Header Increments On Retry 13529ms
Header Behavior On Retry.Request Id Preserved On Retry 6520ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3020ms
Header Behavior On Retry.Request Timestamp Changes On Retry 6520ms
Response Format Validation.Success Response Has Uuid Keyed Results 510ms
Response Format Validation.Success Response Has Ok For Each Event 514ms
Response Format Validation.Success No Retry After When All Ok 513ms
Response Format Validation.Success Retry After Present When Retry Events 1516ms
Response Format Validation.Success No Retry After When Drop Only 514ms
Response Format Validation.Response Echoes Request Id 510ms
Retry Behavior.Retries On 408 6516ms
Retry Behavior.Retries On 500 6519ms
Retry Behavior.Retries On 503 8525ms
Retry Behavior.Retries On 504 6520ms
Retry Behavior.Retryable Errors Have Retry After 3515ms
Retry Behavior.Respects Retry After On Retryable Error 11525ms
Retry Behavior.Does Not Retry On 400 2514ms
Retry Behavior.Does Not Retry On 401 2513ms
Retry Behavior.Does Not Retry On 402 2514ms
Retry Behavior.Does Not Retry On 413 2512ms
Retry Behavior.Does Not Retry On 415 2514ms
Retry Behavior.Non Retryable Errors Have No Retry After 2511ms
Retry Behavior.Implements Backoff 22527ms
Retry Behavior.Max Retries Respected 22526ms
Partial Batch Handling.Handles 200 Full Success 2513ms
Partial Batch Handling.Handles 200 With All Ok 3519ms
Partial Batch Handling.Does Not Retry Dropped Events 3516ms
Partial Batch Handling.Does Not Retry Limited Events 3517ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 6519ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 6519ms
Partial Batch Handling.Retries Only Retry Events From Partial 6524ms
Partial Batch Handling.Partial Retry Preserves Uuids 6521ms
Partial Batch Handling.Partial Retry Attempt Header Increments 6523ms
Partial Batch Handling.Partial Retry Request Id Preserved 6521ms
Partial Batch Handling.Respects Retry After On Partial 8520ms
Partial Batch Handling.Unknown Result Treated As Terminal 3513ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3521ms
Compression.Sends Gzip Content Encoding 512ms
Compression.No Content Encoding When Disabled 510ms
Compression.Compressed Body Is Decompressible 511ms
Error Handling.Does Not Retry On Unknown 4Xx 2513ms
Event Options.Cookieless Mode Override 511ms
Event Options.Disable Skew Correction Override 510ms
Event Options.Process Person Profile Override 510ms
Event Options.Product Tour Id Override 510ms
Event Options.Unset Options Omitted 511ms
Event Options.Options Override In Batch 514ms
Geoip And Historical Migration.Geoip Disable Injected Into Properties 510ms
Geoip And Historical Migration.Historical Migration Set In Body 510ms
Geoip And Historical Migration.Historical Migration Absent By Default 510ms

Feature_Flags Tests

17/17 tests passed

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

@greptile-apps

greptile-apps Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Retrigger

The PR appears safe to merge, with no new actionable findings or outstanding previous findings.

Reviews (2) · Last reviewed commit: "fix(mcp): anchor virtual tools to conver..."

Comment thread posthog/mcp/_instrument_fastmcp.py Outdated
Comment on lines +337 to +349
def _conversation_prompt_back(lifecycle: Any) -> Tuple[Any, bool]:
"""``(prompt_back_block_or_None, delivered)`` for a virtual tool's reply.

A minted handle rides a prompt-back block so the agent can echo it on its
next call and keep the whole exchange in one session; an echoed one needs no
delivery. ``delivered`` gates stamping the handle on the event, so a handle
the agent never received is never recorded."""
if lifecycle.conversation_id and lifecycle.minted_conversation_id:
block = mcp_types.TextContent(
type="text", text=build_prompt_back(lifecycle.conversation_id)["text"]
)
return block, True
return None, bool(lifecycle.conversation_id)

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 prompt-back policy

This helper repeats the same mint/delivery logic in _instrument_lowlevel.py and _instrument_v2.py, recreating the risk of adapters drifting apart. It violates the repository’s “says everything once and only once” requirement, which must be satisfied before merging. Move the shared policy into one helper and leave only adapter-specific result wrapping in each adapter.

Context Used: Be direct and concise: state the issue, its impact, and the fix, with no preamble or praise. Do not comment on alphabetical sorting, trailing commas, or formatting. Linters catch these. Judge code by four simplicity rules: it passes all the tests, ex... (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/mcp/_instrument_fastmcp.py
Line: 337-349

Comment:
**Duplicated prompt-back policy**

This helper repeats the same mint/delivery logic in `_instrument_lowlevel.py` and `_instrument_v2.py`, recreating the risk of adapters drifting apart. It violates the repository’s “says everything once and only once” requirement, which must be satisfied before merging. Move the shared policy into one helper and leave only adapter-specific result wrapping in each adapter.

**Context Used:** Be direct and concise: state the issue, its impact, and the fix, with no preamble or praise. Do not comment on alphabetical sorting, trailing commas, or formatting. Linters catch these. Judge code by four simplicity rules: it passes all the tests, ex... ([source](https://app.greptile.com/review/custom-context?memory=instruction-0))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

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/test/mcp/test_units.py Outdated
@gesh
gesh force-pushed the posthog/mcp-virtual-tool-conversation-id branch 2 times, most recently from 3bea84f to 31d5a92 Compare September 16, 2026 09:23
@gesh
gesh force-pushed the posthog/mcp-virtual-tool-first-page branch from 8daec48 to 3aac206 Compare September 16, 2026 09:29
@gesh
gesh force-pushed the posthog/mcp-virtual-tool-conversation-id branch from 31d5a92 to 24b200b Compare September 16, 2026 09:29
@gesh
gesh force-pushed the posthog/mcp-virtual-tool-conversation-id branch 2 times, most recently from af09f57 to dc9873c Compare September 16, 2026 09:59
Base automatically changed from posthog/mcp-virtual-tool-first-page to main September 17, 2026 09:34
@gesh
gesh force-pushed the posthog/mcp-virtual-tool-conversation-id branch from dc9873c to 4cd42e9 Compare September 17, 2026 09:54
Generated-By: PostHog Desktop
Task-Id: e71933e1-34f5-4e2f-b1dc-da92ab68ff26
@gesh
gesh force-pushed the posthog/mcp-virtual-tool-conversation-id branch from 4cd42e9 to 536b049 Compare September 17, 2026 10:32
@gesh gesh changed the title feat(mcp): anchor the virtual tools to the conversation fix(mcp): anchor virtual tools to conversations Sep 17, 2026
@gesh
gesh marked this pull request as ready for review September 17, 2026 10:35
@gesh

gesh commented Sep 17, 2026

Copy link
Copy Markdown
Member Author

Note

🤖 Automated comment by QA Swarm — not written by a human

Multi-perspective review: router (cheap-first pass) + delegated reviewers as warranted

Verdict: ✅ APPROVE (round 2 @ d12371b)

No actionable defect was found. The shared virtual-result helper preserves text order and prompt-back behavior across all MCP adapters.

Key findings

None.

Convergence

Both review rounds found the conversation and session behavior consistent.

Reviewer summaries

Reviewer Assessment
🧭 router Low danger and high confidence; no delegation needed.
Previous rounds (1)

round 1 @ 536b049 — APPROVE: a second reviewer validated all adapter paths.


Automated by QA Swarm — not a human review

Generated-By: PostHog Desktop
Task-Id: c9e11d68-ecf2-4b5a-aca5-5f8b15c973d0
@gesh
gesh requested review from a team September 17, 2026 11:38

@marandaneto marandaneto left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The code looks good; no qualifying findings. Verdict: correct.

@gesh
gesh merged commit 79fcb12 into main Sep 17, 2026
44 checks passed
@gesh
gesh deleted the posthog/mcp-virtual-tool-conversation-id branch September 17, 2026 11:46
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