Repository navigation
feat(mcp): record input_required rounds as $mcp_input_required #1305
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| pypi/posthog: minor | ||
| --- | ||
|
|
||
| `posthog.mcp`: record `input_required` rounds as `$mcp_input_required`, not as tool calls. On the 2026-07-28 revision, a tool that asks for more input, such as a confirmation form, sent a `$mcp_tool_call` for the round and another for the retry, so one confirmed call counted twice. `instrument()` now sends `$mcp_input_required` with `$mcp_tool_name`, `$mcp_duration_ms`, and `$mcp_input_request_methods`, and only the retry that completes the call is a `$mcp_tool_call`. A round returned to a client on an earlier revision, which cannot receive it, is a failed `$mcp_tool_call` with `$mcp_error_type: "input_required"`. Custom dispatchers record rounds with the new `PostHogMCP.capture_input_required()`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """``input_required`` rounds from the 2026-07-28 revision. | ||
|
|
||
| A tool that needs more input, such as a confirmation form, answers | ||
| ``tools/call`` with an ``InputRequiredResult`` and runs when the client retries | ||
| with the answers. The round is not a tool call. The SDK records it as | ||
| ``$mcp_input_required``, and the retry that completes the call is the one | ||
| ``$mcp_tool_call``. | ||
| """ | ||
|
|
||
| from typing import Any, Iterable, List, Optional | ||
|
|
||
| from ._truncation import truncate_text | ||
|
|
||
| INPUT_REQUIRED_ERROR_TYPE = "input_required" | ||
| LEGACY_INPUT_REQUIRED_MESSAGE = ( | ||
| "The tool asked for more input, which a client on a protocol revision " | ||
| "before 2026-07-28 cannot receive" | ||
| ) | ||
|
|
||
| _INPUT_REQUIRED_RESULT_TYPE = "input_required" | ||
| _FIRST_INPUT_REQUIRED_REVISION = "2026-07-28" | ||
| _MAX_METHODS = 100 | ||
| _MAX_METHOD_LENGTH = 256 | ||
|
|
||
|
|
||
| def input_request_methods(result: Any) -> Optional[List[str]]: | ||
| """The ``method`` of each request an ``input_required`` result carries, | ||
| ordered by key with duplicates kept, or ``None`` when the result is not | ||
| one. Never the requests' content.""" | ||
| if isinstance(result, dict): | ||
| result_type = result.get("resultType") | ||
| requests = result.get("inputRequests") | ||
| else: | ||
| result_type = getattr(result, "result_type", None) | ||
| requests = getattr(result, "input_requests", None) | ||
| if result_type != _INPUT_REQUIRED_RESULT_TYPE: | ||
| return None | ||
| if not isinstance(requests, dict): | ||
| return [] | ||
| return bound_input_request_methods( | ||
| _method_of(requests[key]) for key in sorted(requests) | ||
| ) | ||
|
|
||
|
|
||
| def bound_input_request_methods(methods: Iterable[Any]) -> List[str]: | ||
| strings = [method for method in methods if isinstance(method, str)] | ||
| return [ | ||
| truncate_text(method, _MAX_METHOD_LENGTH) for method in strings[:_MAX_METHODS] | ||
| ] | ||
|
|
||
|
|
||
| def is_legacy_revision(protocol_version: Optional[str]) -> bool: | ||
| """Whether the request declared a revision whose clients cannot receive an | ||
| ``input_required`` result. An unknown revision is not assumed legacy: the | ||
| framework only lets the result through on the modern wire.""" | ||
| return ( | ||
| isinstance(protocol_version, str) | ||
| and protocol_version < _FIRST_INPUT_REQUIRED_REVISION | ||
| ) | ||
|
|
||
|
|
||
| def _method_of(request: Any) -> Any: | ||
| if isinstance(request, dict): | ||
| return request.get("method") | ||
| return getattr(request, "method", None) |
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -40,6 +40,12 @@ | |||||||||||
| resolve_collect_feedback_options, | ||||||||||||
| resolve_send_feedback_tool_name, | ||||||||||||
| ) | ||||||||||||
| from ._input_required import ( | ||||||||||||
| INPUT_REQUIRED_ERROR_TYPE, | ||||||||||||
| LEGACY_INPUT_REQUIRED_MESSAGE, | ||||||||||||
| input_request_methods, | ||||||||||||
| is_legacy_revision, | ||||||||||||
| ) | ||||||||||||
| from ._intent import resolve_tool_call_intent, set_event_intent | ||||||||||||
| from ._internal import ( | ||||||||||||
| MCPAnalyticsData, | ||||||||||||
|
|
@@ -693,6 +699,29 @@ async def record_result( | |||||||||||
| ) -> None: | ||||||||||||
| conversation_id = self._anchored_conversation_id(conversation_id_delivered) | ||||||||||||
| session_id = await self.prepare_session(conversation_id) | ||||||||||||
| failure: Any = error | ||||||||||||
| error_type: Optional[str] = None | ||||||||||||
| methods = input_request_methods(result) | ||||||||||||
| if methods is not None and not is_legacy_revision(self.protocol_version): | ||||||||||||
| await record_input_required( | ||||||||||||
| self.data, | ||||||||||||
| session_id, | ||||||||||||
| conversation_id=conversation_id, | ||||||||||||
| name=self.name, | ||||||||||||
| arguments=self.arguments, | ||||||||||||
| methods=methods, | ||||||||||||
| duration_ms=duration_ms, | ||||||||||||
| client_name=self.client_name, | ||||||||||||
| client_version=self.client_version, | ||||||||||||
| protocol_version=self.protocol_version, | ||||||||||||
| extra=self.extra, | ||||||||||||
| ) | ||||||||||||
| return | ||||||||||||
| if methods is not None: | ||||||||||||
| # A legacy client cannot receive the round, so the framework answers | ||||||||||||
| # with an error and the call ends there. | ||||||||||||
| result, error_type = None, INPUT_REQUIRED_ERROR_TYPE | ||||||||||||
| failure = error or LEGACY_INPUT_REQUIRED_MESSAGE | ||||||||||||
| await record_tool_call( | ||||||||||||
| self.data, | ||||||||||||
| session_id, | ||||||||||||
|
|
@@ -702,7 +731,8 @@ async def record_result( | |||||||||||
| allow_self_reported_model=self.allow_self_reported_model, | ||||||||||||
| analytics_owned_parameters=self.analytics_owned_parameters, | ||||||||||||
| result=result, | ||||||||||||
| error=error, | ||||||||||||
| error=failure, | ||||||||||||
| error_type=error_type, | ||||||||||||
| duration_ms=duration_ms, | ||||||||||||
| client_name=self.client_name, | ||||||||||||
| client_version=self.client_version, | ||||||||||||
|
|
@@ -781,6 +811,7 @@ async def record_tool_call( | |||||||||||
| allow_self_reported_model: bool = False, | ||||||||||||
| result: Any = None, | ||||||||||||
| error: Any = None, | ||||||||||||
| error_type: Optional[str] = None, | ||||||||||||
| duration_ms: Optional[float] = None, | ||||||||||||
| client_name: Optional[str] = None, | ||||||||||||
| client_version: Optional[str] = None, | ||||||||||||
|
|
@@ -845,6 +876,7 @@ async def record_tool_call( | |||||||||||
| if error is not None: | ||||||||||||
| event["is_error"] = True | ||||||||||||
| event["error"] = capture_exception(error) | ||||||||||||
| event["error_type"] = error_type | ||||||||||||
| event["error_status"] = http_status(error) | ||||||||||||
|
|
||||||||||||
| props = await resolve_event_properties(data, request, extra) | ||||||||||||
|
|
@@ -1460,6 +1492,43 @@ async def record_unknown_tool( | |||||||||||
| log(f"record_unknown_tool failed (event dropped): {err}") | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| async def record_input_required( | ||||||||||||
| data: MCPAnalyticsData, | ||||||||||||
| session_id: str, | ||||||||||||
| *, | ||||||||||||
| conversation_id: Optional[str], | ||||||||||||
| name: str, | ||||||||||||
| arguments: Optional[Dict[str, Any]], | ||||||||||||
| methods: List[str], | ||||||||||||
| duration_ms: float, | ||||||||||||
| client_name: Optional[str], | ||||||||||||
| client_version: Optional[str], | ||||||||||||
| protocol_version: Optional[str], | ||||||||||||
| extra: Optional[Dict[str, Any]], | ||||||||||||
| ) -> None: | ||||||||||||
| """Record an ``input_required`` round as ``$mcp_input_required``. The retry | ||||||||||||
| that completes the call is its ``$mcp_tool_call``.""" | ||||||||||||
| try: | ||||||||||||
| event: Dict[str, Any] = { | ||||||||||||
| "event_type": MCPAnalyticsEventType.MCP_INPUT_REQUIRED, | ||||||||||||
| "session_id": session_id, | ||||||||||||
| "conversation_id": conversation_id, | ||||||||||||
| "resource_name": name, | ||||||||||||
| "duration": duration_ms, | ||||||||||||
| "input_request_methods": methods, | ||||||||||||
| "client_name": client_name, | ||||||||||||
| "client_version": client_version, | ||||||||||||
| "protocol_version": protocol_version, | ||||||||||||
| } | ||||||||||||
| await _apply_event_properties( | ||||||||||||
| data, event, build_tool_call_request(name, arguments), extra | ||||||||||||
| ) | ||||||||||||
| stamp_transport_identity(event, extra) | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Medium: Input-required events bypass configured privacy options Unlike
Suggested change
|
||||||||||||
| fire_and_forget(capture_event(data, event), data) | ||||||||||||
|
Comment on lines
+1523
to
+1527
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1 · Apply configured event options to input-required events When |
||||||||||||
| except Exception as err: # noqa: BLE001 - isolate analytics from the tool path | ||||||||||||
| log(f"record_input_required failed (event dropped): {err}") | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| async def record_feedback( | ||||||||||||
| data: MCPAnalyticsData, | ||||||||||||
| session_id: str, | ||||||||||||
|
|
||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P2 · Classify legacy FastMCP 4 rejections as input_required
Standalone FastMCP 4 rejects a legacy tool's InputRequiredResult inside _on_call_tool(), before the wrapped handler returns. _wrap_v2_call_tool() therefore calls record_error(), which never sets INPUT_REQUIRED_ERROR_TYPE. Reproduced with FastMCP 4.2.1 and protocol 2025-11-25: the failed $mcp_tool_call has $mcp_error_type: "MCPError". This branch therefore misses the new legacy classification for a supported adapter. Observe FastMCP's interim result before its legacy rejection, or recognize this specific rejection in the error path. Pass INPUT_REQUIRED_ERROR_TYPE through record_error() while preserving the original exception. Add a legacy FastMCP 4 wire test.