Repository navigation
feat(mcp): resolve fresh low-level tool ownership #1009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+827
−63
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| pypi/posthog: minor | ||
| --- | ||
|
|
||
| Add `resolve_original_tool` for low-level MCP servers. Fresh server instances can now remove PostHog-owned arguments before strict tool validation. | ||
|
|
||
| Raw low-level servers also remove PostHog-owned arguments after `tools/list`. A tool-owned `context` remains tool data. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """Resolve ownership of arguments that PostHog adds to MCP tool schemas.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
| from collections.abc import Mapping | ||
| from typing import Any, Dict, FrozenSet, Optional, Tuple | ||
|
|
||
| from ._context_parameters import is_context_enabled, schema_has_param | ||
| from ._model_parameters import is_capture_model_enabled | ||
| from .logger import log | ||
|
|
||
| _COMPLEX_SCHEMA_KEYS = ("$ref", "oneOf", "allOf", "anyOf") | ||
|
|
||
|
|
||
| def analytics_owned_parameters( | ||
| options: Any, | ||
| input_schema: Any, | ||
| ) -> FrozenSet[str]: | ||
| """Return the enabled arguments that PostHog can add to this schema.""" | ||
| enabled = set() | ||
| if is_context_enabled(options.context): | ||
| enabled.add("context") | ||
| if options.enable_conversation_id: | ||
| enabled.add("conversation_id") | ||
| if is_capture_model_enabled(options.capture_model): | ||
| enabled.add("llm_model") | ||
|
|
||
| if isinstance(input_schema, dict) and any( | ||
| input_schema.get(key) for key in _COMPLEX_SCHEMA_KEYS | ||
| ): | ||
| return frozenset() | ||
|
gesh marked this conversation as resolved.
|
||
| return frozenset( | ||
| name for name in enabled if not schema_has_param(input_schema, name) | ||
| ) | ||
|
|
||
|
|
||
| def cache_listed_tool_ownership( | ||
| data: Any, tool: Any, *, schema_attribute: str | ||
| ) -> FrozenSet[str]: | ||
| """Cache ownership from the host schema before PostHog changes it.""" | ||
| name = getattr(tool, "name", None) | ||
| if not isinstance(name, str): | ||
| return frozenset() | ||
| schema = getattr(tool, schema_attribute, None) | ||
| ownership = analytics_owned_parameters(data.options, schema) | ||
| data.tool_analytics_parameter_ownership[name] = ownership | ||
| if isinstance(schema, dict): | ||
| data.tool_input_schemas[name] = schema | ||
| else: | ||
| data.tool_input_schemas.pop(name, None) | ||
| return ownership | ||
|
|
||
|
|
||
| async def resolve_lowlevel_tool_ownership( | ||
| data: Any, name: str | ||
| ) -> Tuple[Optional[FrozenSet[str]], Optional[Dict[str, Any]]]: | ||
| """Resolve one raw tool. A served listing on this instance has priority.""" | ||
| if name in data.tool_analytics_parameter_ownership: | ||
| return ( | ||
| data.tool_analytics_parameter_ownership[name], | ||
| data.tool_input_schemas.get(name), | ||
| ) | ||
|
|
||
| resolver = data.options.resolve_original_tool | ||
| if resolver is None: | ||
| return None, None | ||
|
|
||
| try: | ||
| descriptor = resolver(name) | ||
| if inspect.isawaitable(descriptor): | ||
| descriptor = await descriptor | ||
| if descriptor is None: | ||
| return None, None | ||
| schema = _descriptor_input_schema(descriptor) | ||
| if not isinstance(schema, dict): | ||
| log( | ||
| f"Warning: resolve_original_tool failed for tool {name!r}: " | ||
| "resolver returned no usable input schema" | ||
| ) | ||
| return None, None | ||
| return analytics_owned_parameters(data.options, schema), schema | ||
| except Exception as error: # noqa: BLE001 - analytics must not break dispatch | ||
| log(f"Warning: resolve_original_tool failed for tool {name!r}: {error}") | ||
| return None, None | ||
|
|
||
|
|
||
| def _descriptor_input_schema(descriptor: Any) -> Any: | ||
| """Read MCP 1.x, MCP 2.x, and dictionary tool descriptors.""" | ||
| if isinstance(descriptor, Mapping): | ||
| if "inputSchema" in descriptor: | ||
| return descriptor["inputSchema"] | ||
| return descriptor.get("input_schema") | ||
|
|
||
| schema = getattr(descriptor, "input_schema", None) | ||
| if schema is not None: | ||
| return schema | ||
| return getattr(descriptor, "inputSchema", None) | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.