feat(mcp): capture model identifiers - #927
Merged
Merged
Conversation
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.
24 tasks
Contributor
posthog-python Compliance ReportDate: 2026-09-09 14:23:17 UTC ✅ All Tests Passed!111/111 tests passed Capture_V1 Tests✅ 94/94 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
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 |
gesh
reviewed
Sep 8, 2026
gesh
left a comment
Member
There was a problem hiding this comment.
Note
🤖 Automated comment by QA Swarm — not written by a human
QA Swarm review complete. See inline comments.
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.
gesh
reviewed
Sep 9, 2026
gesh
left a comment
Member
There was a problem hiding this comment.
Note
🤖 Automated comment by QA Swarm — not written by a human
QA Swarm round 2 @ 11c48e9. All four round 1 findings verified fixed. See inline comments.
gesh
approved these changes
Sep 9, 2026
gesh
left a comment
Member
There was a problem hiding this comment.
Approving to unblock, the robot comments look meaningful
Clarify that preserving strict schemas during context and conversation-ID injection also affects existing users with model capture disabled. Declare jsonschema>=4.0 in the test extra and refresh only its direct dependency entries in uv.lock. Verification: python -m pytest posthog/test/mcp --timeout=30 -q passed (267 tests, MCP SDK 1.29.0); uv lock --check and git diff --check passed. Configured pre-commit checks skipped these non-Python files. CodeScene reported no applicable changes. MCP v2 was not rerun locally for this documentation and dependency-only update.
Append model capture fields after the existing public dataclass fields. Existing positional MCPAnalyticsOptions calls keep their identity and callback bindings, and PreparedToolCall retains the fourth positional missing-capability flag. Add four parameterized regression cases and regenerate the public API snapshot. The cases failed before the field reorder and pass afterward. Verification: 271 MCP tests passed; public_api_snapshot and public_api_check passed; configured Ruff pre-commit checks passed; mypy baseline check found no issues in 230 source files; strict import and git diff checks passed. CodeScene checked both Python files with no issues. MCP v2 validation remains covered by CI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
Python MCP server owners can compare tool behavior by calling model, matching posthog-js#4829.
$mcp_llm_model_sourcedistinguishesclient_metadatafromself_reportedvalues.Public API snapshot updates and the shared injection predicate are mechanical.
💚 How did you test it?
📝 Checklist
If releasing new changes
sampo addto 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.