Skip to content

feat(mcp): capture model identifiers - #927

Open
lucasheriques wants to merge 2 commits into
mainfrom
codex/mcp-model-client-metadata
Open

feat(mcp): capture model identifiers#927
lucasheriques wants to merge 2 commits into
mainfrom
codex/mcp-model-client-metadata

Conversation

@lucasheriques

@lucasheriques lucasheriques commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

💡 Motivation and Context

Python MCP server owners can compare tool behavior by calling model, matching posthog-js#4829.

  • Model capture is opt-in and prefers recognized Codex metadata over an injected self-report field.
  • $mcp_llm_model_source distinguishes client_metadata from self_reported values.
  • Application-owned fields and complex schemas remain untouched by model injection.
  • Custom dispatchers preserve model-field ownership across repeated listings by copying object tools before injection.
  • Analytics schema injection preserves existing restrictions on undeclared arguments.
  • Raw low-level and standalone FastMCP adapters keep the model field optional for validation compatibility.
  • Both model sources remain unverified and suit product analytics, not billing or access control.

Public API snapshot updates and the shared injection predicate are mechanical.

💚 How did you test it?

  • Regression coverage checks default-off and explicit opt-out behavior through prepared calls and captured events.
  • Object-tool cases cover repeated listings with and without an explicit original tool across both MCP SDK majors.
  • Schema coverage checks accepted analytics fields and rejected undeclared arguments, including after a low-level tool-cache rebuild.
  • Existing wire coverage checks metadata precedence, argument stripping, capture properties and tool execution.
  • The full non-MCP suite was not rerun locally for the review fixes; CI covers the broader matrix.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran sampo add to generate a changeset file

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

Codex authored the change and review fixes with terminal and GitHub CLI tools. Skills: debugging MCP analytics, writing tests, writing dataclasses, and writing PR descriptions.

Model resolution respects opt-in on custom dispatchers. Optional fields remain optional where stripping them before validation would otherwise reject calls.

Capture model identifiers from recognized client metadata, with an SDK-owned self-report fallback for other clients. Preserve source provenance and fail closed when the application owns the field.\n\nVerify both MCP Python SDK 1.x and 2.x, including a 2026-07-28 wire-level call.
@lucasheriques
lucasheriques requested a review from a team as a code owner September 7, 2026 17:23
@lucasheriques lucasheriques self-assigned this Sep 7, 2026
@lucasheriques
lucasheriques requested a review from a team September 7, 2026 17:24
@github-actions

github-actions Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

posthog-python Compliance Report

Date: 2026-09-08 18:45:26 UTC
Duration: 256352ms

✅ 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 511ms
Required Headers.Has Authorization Bearer Header 509ms
Required Headers.Has Content Type Json 509ms
Required Headers.Has Posthog Sdk Info Format 510ms
Required Headers.Has Posthog Attempt Header 508ms
Required Headers.Has Posthog Request Id 510ms
Required Headers.Has Posthog Request Timestamp 510ms
Required Headers.Has User Agent 510ms
Body Format.Body Has Created At And Batch 510ms
Body Format.No Api Key In Body 510ms
Body Format.No Sent At In Body 509ms
Event Format.Event Has Required Root Fields 509ms
Event Format.Event Uuid Is Valid 508ms
Event Format.Event Timestamp Is Rfc3339 509ms
Event Format.Distinct Id Is String 508ms
Event Format.Distinct Id At Root Not Properties 509ms
Event Format.Custom Properties Preserved 508ms
Event Format.Set Properties Preserved 509ms
Event Format.Set Once Properties Preserved 509ms
Event Format.Groups Properties Preserved 509ms
Event Format.Sdk Generates Uuid If Not Provided 509ms
Event Format.Event Has Required Root Fields Batch 514ms
Event Format.Event Uuid Is Valid Batch 513ms
Event Format.Event Timestamp Is Rfc3339 Batch 513ms
Event Format.Distinct Id Is String Batch 513ms
Event Format.Distinct Id At Root Not Properties Batch 513ms
Event Format.Custom Properties Preserved Batch 514ms
Event Format.Set Properties Preserved Batch 513ms
Event Format.Set Once Properties Preserved Batch 514ms
Event Format.Groups Properties Preserved Batch 513ms
Event Format.Sdk Generates Uuid If Not Provided Batch 514ms
Batch Behavior.Multiple Events In Single Batch 517ms
Batch Behavior.Batch Envelope Smoke 515ms
Batch Behavior.Flush With No Events Sends Nothing 506ms
Batch Behavior.Flush At Triggers Batch 1011ms
Batch Behavior.Created At Reflects Batch Creation Time 510ms
Deduplication.Generates Unique Uuids 517ms
Deduplication.Different Events Same Content Different Uuids 512ms
Deduplication.Preserves Uuid On Retry 6518ms
Deduplication.Preserves Timestamp On Retry 6520ms
Deduplication.Preserves Uuid And Timestamp On Batch Retry 6523ms
Deduplication.No Duplicate Events In Batch 518ms
Header Behavior On Retry.Attempt Header Starts At One 509ms
Header Behavior On Retry.Attempt Header Increments On Retry 13521ms
Header Behavior On Retry.Request Id Preserved On Retry 6519ms
Header Behavior On Retry.Different Requests Have Different Request Ids 3018ms
Header Behavior On Retry.Request Timestamp Changes On Retry 6513ms
Response Format Validation.Success Response Has Uuid Keyed Results 511ms
Response Format Validation.Success Response Has Ok For Each Event 512ms
Response Format Validation.Success No Retry After When All Ok 512ms
Response Format Validation.Success Retry After Present When Retry Events 1514ms
Response Format Validation.Success No Retry After When Drop Only 511ms
Response Format Validation.Response Echoes Request Id 509ms
Retry Behavior.Retries On 408 6516ms
Retry Behavior.Retries On 500 6518ms
Retry Behavior.Retries On 503 8521ms
Retry Behavior.Retries On 504 6519ms
Retry Behavior.Retryable Errors Have Retry After 3514ms
Retry Behavior.Respects Retry After On Retryable Error 11521ms
Retry Behavior.Does Not Retry On 400 2513ms
Retry Behavior.Does Not Retry On 401 2514ms
Retry Behavior.Does Not Retry On 402 2513ms
Retry Behavior.Does Not Retry On 413 2511ms
Retry Behavior.Does Not Retry On 415 2514ms
Retry Behavior.Non Retryable Errors Have No Retry After 2513ms
Retry Behavior.Implements Backoff 22533ms
Retry Behavior.Max Retries Respected 22522ms
Partial Batch Handling.Handles 200 Full Success 2512ms
Partial Batch Handling.Handles 200 With All Ok 3517ms
Partial Batch Handling.Does Not Retry Dropped Events 3516ms
Partial Batch Handling.Does Not Retry Limited Events 3516ms
Partial Batch Handling.Prunes Ok Events On Partial Retry 6518ms
Partial Batch Handling.Prunes Dropped Events On Partial Retry 6521ms
Partial Batch Handling.Retries Only Retry Events From Partial 6523ms
Partial Batch Handling.Partial Retry Preserves Uuids 6521ms
Partial Batch Handling.Partial Retry Attempt Header Increments 6522ms
Partial Batch Handling.Partial Retry Request Id Preserved 6520ms
Partial Batch Handling.Respects Retry After On Partial 8522ms
Partial Batch Handling.Unknown Result Treated As Terminal 3516ms
Partial Batch Handling.Mixed Ok Drop Limited No Retry 3517ms
Compression.Sends Gzip Content Encoding 511ms
Compression.No Content Encoding When Disabled 510ms
Compression.Compressed Body Is Decompressible 510ms
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 511ms
Event Options.Product Tour Id Override 510ms
Event Options.Unset Options Omitted 510ms
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 511ms
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 11ms
Request Payload.Flags Request Uses V2 Query Param 10ms
Request Payload.Flags Request Hits Flags Path Not Decide 9ms
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 10ms
Request Payload.Disable Geoip Omitted Defaults To False 10ms
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 14ms
Request Lifecycle.Mock Response Value Is Returned To Caller 10ms
Retry Behavior.Retries Flags On 502 313ms
Retry Behavior.Retries Flags On 504 313ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 512ms

@greptile-apps

greptile-apps Bot commented Sep 7, 2026

Copy link
Copy Markdown
Contributor
Prompt To Fix All With AI
### Issue 1
posthog/mcp/_model_parameters.py:68-69
**Strict schemas become permissive**

When model capture is enabled, this removes `additionalProperties: false` from otherwise strict input schemas even though `llm_model` has already been added to `properties`. Raw low-level servers use the advertised schema for validation and do not strip other keys, so clients can submit undeclared arguments that were previously rejected. Preserve the strictness constraint while adding the declared analytics property.

### Issue 2
posthog/mcp/posthog_mcp.py:319-322
**Injected field loses ownership**

For object-based tools, `prepare_tool_list()` mutates the original object by adding `llm_model`. If that same object is then passed as `original_tool`, as the README recommends, this check mistakes the SDK-injected field for an application-owned field. As a result, `prepare_tool_call()` neither captures nor strips the self-reported model, and dispatch can receive an unexpected `llm_model` argument. Record ownership before mutating object schemas or avoid inferring it from an already-mutated object.

### Issue 3
posthog/mcp/_instrumentation.py:690-694
**Low-level model field optional**

Model requiredness is tied to the adapter-specific `context_required` flag. Both low-level adapters pass this flag as false, so compatible tool schemas advertise `llm_model` as optional even though the documented feature contract says the self-report field is required. Schema-driven clients may therefore omit it, causing model capture to silently produce no value. Give model injection its own requiredness decision or explicitly exclude these adapters from the documented contract.

---

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

Reviews (1): Last reviewed commit: "feat(mcp): capture model identifiers" | Re-trigger Greptile

Comment thread posthog/mcp/_model_parameters.py Outdated
Comment on lines +68 to +69
if schema.get("additionalProperties") is False:
schema.pop("additionalProperties", None)

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 Strict schemas become permissive

When model capture is enabled, this removes additionalProperties: false from otherwise strict input schemas even though llm_model has already been added to properties. Raw low-level servers use the advertised schema for validation and do not strip other keys, so clients can submit undeclared arguments that were previously rejected. Preserve the strictness constraint while adding the declared analytics property.

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/mcp/_model_parameters.py
Line: 68-69

Comment:
**Strict schemas become permissive**

When model capture is enabled, this removes `additionalProperties: false` from otherwise strict input schemas even though `llm_model` has already been added to `properties`. Raw low-level servers use the advertised schema for validation and do not strip other keys, so clients can submit undeclared arguments that were previously rejected. Preserve the strictness constraint while adding the declared analytics property.

---

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 11c48e9. Model injection now preserves additionalProperties: false. Context and conversation-ID injection also preserve it, so later stages cannot relax the constraint. Regression coverage verifies that declared analytics fields remain valid and undeclared arguments are rejected, including after a low-level tool-cache rebuild.

Comment on lines +319 to +322
if original_tool is not None:
analytics_owns_model = can_inject_model_parameter(
_tool_schema(original_tool)
)

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 Injected field loses ownership

For object-based tools, prepare_tool_list() mutates the original object by adding llm_model. If that same object is then passed as original_tool, as the README recommends, this check mistakes the SDK-injected field for an application-owned field. As a result, prepare_tool_call() neither captures nor strips the self-reported model, and dispatch can receive an unexpected llm_model argument. Record ownership before mutating object schemas or avoid inferring it from an already-mutated object.

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/mcp/posthog_mcp.py
Line: 319-322

Comment:
**Injected field loses ownership**

For object-based tools, `prepare_tool_list()` mutates the original object by adding `llm_model`. If that same object is then passed as `original_tool`, as the README recommends, this check mistakes the SDK-injected field for an application-owned field. As a result, `prepare_tool_call()` neither captures nor strips the self-reported model, and dispatch can receive an unexpected `llm_model` argument. Record ownership before mutating object schemas or avoid inferring it from an already-mutated object.

---

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 11c48e9. Model injection now copies object tools before changing their schemas, leaving the original application tool available for ownership checks. Regression cases cover repeated listings with and without original_tool, using SDK tool objects and custom descriptors across both MCP SDK majors. Self-reported values are captured and stripped before dispatch.

Comment on lines +690 to +694
schema = add_model_parameter_to_schema(
schema,
tool.name,
get_model_description(data.options.capture_model),
required=context_required,

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 Low-level model field optional

Model requiredness is tied to the adapter-specific context_required flag. Both low-level adapters pass this flag as false, so compatible tool schemas advertise llm_model as optional even though the documented feature contract says the self-report field is required. Schema-driven clients may therefore omit it, causing model capture to silently produce no value. Give model injection its own requiredness decision or explicitly exclude these adapters from the documented contract.

Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/mcp/_instrumentation.py
Line: 690-694

Comment:
**Low-level model field optional**

Model requiredness is tied to the adapter-specific `context_required` flag. Both low-level adapters pass this flag as false, so compatible tool schemas advertise `llm_model` as optional even though the documented feature contract says the self-report field is required. Schema-driven clients may therefore omit it, causing model capture to silently produce no value. Give model injection its own requiredness decision or explicitly exclude these adapters from the documented contract.

---

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed through the documented-contract option in 11c48e9. The README now distinguishes required fields on custom dispatchers and official high-level adapters from optional fields on raw low-level servers and standalone FastMCP. The standalone adapter strips injected fields before input validation, so making llm_model required there would reject calls after stripping. Requiredness is unchanged; the low-level regression also verifies that a call omitting the field succeeds.

@gesh gesh 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.

Note

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

QA Swarm review complete. See inline comments.

Comment thread posthog/mcp/posthog_mcp.py Outdated
)
else:
analytics_owns_model = self._model_parameter_injected.get(name, False)
llm_model, llm_model_source = resolve_model(

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.

Note

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

[convergent: router + validation] 🟠 HIGH

resolve_model() is called outside the capture_model gate. Line 318 gates only analytics_owns_model, and resolve_model() checks Codex x-codex-turn-metadata before it consults allow_self_reported (_model_parameters.py:123-127). So the client-metadata branch runs even when capture_model is left at its default of False.

Reproduced at this head: a PostHogMCP built without capture_model and called as prepare_tool_call("search", {"q": "hello"}, request_meta={"x-codex-turn-metadata": {"model": "..."}}) returns a populated llm_model with source client_metadata, where both should be None.

There is no downstream gate to catch it. _apply_model() writes the fields onto the event whenever they are non-empty, and capture_tool_call() / capture_missing_capability() take them as plain parameters without re-checking the option. A host that plumbs PreparedToolCall through therefore emits the model identifier with no opt-in anywhere in the chain.

This is the documented custom-dispatcher path, and it contradicts the PR's own "off by default" claim. The instrument() adapters get this right — both resolve_model() call sites in _instrumentation.py sit inside if is_capture_model_enabled(...).

Fix: gate the call itself, matching the adapters.

llm_model, llm_model_source = None, None
if is_capture_model_enabled(self._capture_model):
    llm_model, llm_model_source = resolve_model(
        request_meta, args, allow_self_reported=analytics_owns_model
    )

Worth a test too: every capture_model test in the suite constructs the client with capture_model=True, so the default-off case is unpinned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Confirmed and fixed in 11c48e9. The resolve_model() call itself now sits inside the capture_model gate, with both prepared fields initialized to None. The capture test now covers enabled, explicit False, and default settings with Codex metadata present, checking both prepared values and emitted event properties.

pass
return tool

def _inject_models(self, tools: List[Any]) -> List[Any]:

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.

Note

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

[router] 🟢 LOW

_inject_models() clears self._model_parameter_injected and then rebuilds it, while prepare_tool_call() reads the same dict at line 324. Between the clear() and the update(), a reader sees an empty map, concludes the SDK does not own llm_model for that tool, and skips stripping it from prepared_args — so the injected argument reaches the real tool.

Reachability is limited: there is no await between the two statements, so asyncio alone cannot interleave here. It needs genuine OS threads sharing one PostHogMCP and re-listing tools while a call is in flight. That is a narrow window, but the class is explicitly built for reuse across concurrent requests, and context never had a comparable shared ownership cache.

Cheapest fix is to swap rather than mutate: build the new dict locally and assign it in one statement, so a concurrent reader sees either the old map or the new one, never an empty one. If you would rather not spend anything here, a comment stating that prepare_tool_list() must not run concurrently with prepare_tool_call() would at least make the assumption explicit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 11c48e9. Ownership is now built in a local dictionary, including injection failures, and assigned to the instance only after preparation completes. The existing map remains available during rebuilding; the clear()/update() window is gone.

return schema


def can_inject_model_parameter(input_schema: Any) -> bool:

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.

Note

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

[router] 🟢 LOW

can_inject_model_parameter() re-derives the same three conditions that add_model_parameter_to_schema() already implements at lines 32-49: does the schema own llm_model, and is it a $ref / oneOf / allOf / anyOf schema. The two are kept in sync by hand.

Since those conditions are exactly what makes the feature fail closed, an edit to one copy that misses the other reintroduces the ownership bug this design exists to prevent. Consider extracting one predicate and having add_model_parameter_to_schema() call it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 11c48e9. add_model_parameter_to_schema() now calls can_inject_model_parameter() rather than duplicating the ownership and complex-schema checks. Existing coverage for application-owned fields and complex schemas passes.

request_meta: Optional[JsonRecord] = None,
original_tool: Any = None,
) -> PreparedToolCall:
"""Pull the agent's intent off the injected ``context`` argument, strip

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.

Note

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

[router] ⚪ NIT

The docstring still describes only the context behavior. This method now also resolves the model, records the source, and strips llm_model from the arguments — worth a line, since this is the documented entry point for the new feature on the custom-dispatcher path.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated in 11c48e9. The prepare_tool_call() docstring now describes opt-in model resolution, source attribution, and stripping the SDK-owned llm_model argument. The list helper and README also explain object copying and original-tool ownership.

Gate custom-dispatcher model resolution behind capture_model and copy
object tools before model injection so repeated listings preserve ownership.
Publish the completed ownership map without an intermediate empty state.

Keep additionalProperties constraints in all analytics schema injectors.
Document adapter-specific requiredness rather than requiring fields that
standalone FastMCP strips before input validation. Share the eligibility
predicate and update prepare helper documentation and the changeset.

Verification:
- MCP v1 suite: 267 passed.
- MCP v2 suite: 245 passed, 13 expected skips.
- Ruff check and format check passed repository-wide.
- Repository mypy/baseline check passed (229 source files).
- Public API snapshot and git diff --check passed.
- Regression cases failed before the fix for disabled capture, object
  ownership and strict validation through a low-level tool-cache rebuild.

Full non-MCP test suite not rerun locally; CI covers the broader matrix.
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