2323 is_context_enabled ,
2424 schema_has_param ,
2525)
26- from ._conversation_id import add_conversation_id_to_schema , resolve_conversation_id
26+ from ._conversation_id import (
27+ add_conversation_id_to_schema ,
28+ build_prompt_back ,
29+ resolve_conversation_id ,
30+ )
2731from ._event_types import MCPAnalyticsEventType
2832from ._exceptions import capture_exception
2933from .feedback import (
@@ -470,11 +474,25 @@ async def prime_session(self) -> None:
470474 self .data , mcp_session_id = self .mcp_session_id , token = self .token
471475 )
472476
473- async def record_missing_capability (self ) -> None :
474- session_id = await self .prepare_session (None )
477+ def prompt_back_text (self ) -> Optional [str ]:
478+ if not self .conversation_id or not self .minted_conversation_id :
479+ return None
480+ return build_prompt_back (self .conversation_id )["text" ]
481+
482+ def _anchored_conversation_id (self , delivered : bool ) -> Optional [str ]:
483+ if self .minted_conversation_id and not delivered :
484+ return None
485+ return self .conversation_id
486+
487+ async def record_missing_capability (
488+ self , * , conversation_id_delivered : bool = False
489+ ) -> None :
490+ conversation_id = self ._anchored_conversation_id (conversation_id_delivered )
491+ session_id = await self .prepare_session (conversation_id )
475492 await record_missing_capability (
476493 self .data ,
477494 session_id ,
495+ conversation_id = conversation_id ,
478496 tool_name = self .missing_name or self .name ,
479497 context = (self .arguments or {}).get ("context" ),
480498 arguments = self .arguments ,
@@ -486,15 +504,17 @@ async def record_missing_capability(self) -> None:
486504 extra = self .extra ,
487505 )
488506
489- async def record_feedback (self ) -> str :
507+ async def record_feedback (self , * , conversation_id_delivered : bool = False ) -> str :
490508 """Capture the ``$mcp_feedback`` event, then run the host's ``on_feedback``
491509 handler and return the reply text for the agent. The event is captured
492510 whether or not the handler raises."""
493511 report = parse_feedback_report (self .arguments , self .feedback_options )
494- session_id = await self .prepare_session (None )
512+ conversation_id = self ._anchored_conversation_id (conversation_id_delivered )
513+ session_id = await self .prepare_session (conversation_id )
495514 await record_feedback (
496515 self .data ,
497516 session_id ,
517+ conversation_id = conversation_id ,
498518 report = report ,
499519 tool_name = self .feedback_name or self .name ,
500520 arguments = self .arguments ,
@@ -509,7 +529,7 @@ async def record_feedback(self) -> str:
509529 async def record_error (self , error : Any , duration_ms : float ) -> None :
510530 # A freshly minted handle cannot anchor or be captured when dispatch
511531 # raised: no adapter had an opportunity to deliver it to the agent.
512- conversation_id = None if self .minted_conversation_id else self . conversation_id
532+ conversation_id = self ._anchored_conversation_id ( False )
513533 session_id = await self .prepare_session (conversation_id )
514534 await record_tool_call (
515535 self .data ,
@@ -530,9 +550,7 @@ async def record_error(self, error: Any, duration_ms: float) -> None:
530550 async def record_result (
531551 self , result : Any , duration_ms : float , * , conversation_id_delivered : bool
532552 ) -> None :
533- conversation_id = self .conversation_id
534- if self .minted_conversation_id and not conversation_id_delivered :
535- conversation_id = None
553+ conversation_id = self ._anchored_conversation_id (conversation_id_delivered )
536554 session_id = await self .prepare_session (conversation_id )
537555 await record_tool_call (
538556 self .data ,
@@ -573,11 +591,7 @@ def start_tool_call_lifecycle(
573591 # running the host's `on_feedback` handler read the configured options.
574592 feedback_options = resolve_collect_feedback_options (data .options .collect_feedback )
575593 conversation_id , minted = resolve_conversation_id (
576- data .options .enable_conversation_id ,
577- arguments ,
578- name ,
579- missing_name ,
580- feedback_name ,
594+ data .options .enable_conversation_id , arguments
581595 )
582596 return ToolCallLifecycle (
583597 data = data ,
@@ -1010,10 +1024,8 @@ def mutate_tool_schema(
10101024 data .tool_model_parameter_injected [tool .name ] = (
10111025 not app_owns_model and schema_has_param (schema , "llm_model" )
10121026 )
1013- if (
1014- not is_sdk_virtual_tool
1015- and data .options .enable_conversation_id
1016- and not schema_has_param (schema , "conversation_id" )
1027+ if data .options .enable_conversation_id and not schema_has_param (
1028+ schema , "conversation_id"
10171029 ):
10181030 schema = add_conversation_id_to_schema (schema , tool .name )
10191031 if schema is not original_schema :
@@ -1138,6 +1150,7 @@ async def record_missing_capability(
11381150 data : MCPAnalyticsData ,
11391151 session_id : str ,
11401152 * ,
1153+ conversation_id : Optional [str ] = None ,
11411154 tool_name : str ,
11421155 context : Optional [str ],
11431156 arguments : Optional [Dict [str , Any ]],
@@ -1155,6 +1168,7 @@ async def record_missing_capability(
11551168 event : Dict [str , Any ] = {
11561169 "event_type" : MCPAnalyticsEventType .MCP_MISSING_CAPABILITY ,
11571170 "session_id" : session_id ,
1171+ "conversation_id" : conversation_id ,
11581172 "resource_name" : tool_name ,
11591173 "parameters" : build_captured_mcp_parameters (
11601174 request , strip_llm_model = allow_self_reported_model
@@ -1186,6 +1200,7 @@ async def record_feedback(
11861200 data : MCPAnalyticsData ,
11871201 session_id : str ,
11881202 * ,
1203+ conversation_id : Optional [str ] = None ,
11891204 report : FeedbackReport ,
11901205 tool_name : str ,
11911206 arguments : Optional [Dict [str , Any ]],
@@ -1206,6 +1221,7 @@ async def record_feedback(
12061221 event : Dict [str , Any ] = {
12071222 "event_type" : MCPAnalyticsEventType .MCP_FEEDBACK ,
12081223 "session_id" : session_id ,
1224+ "conversation_id" : conversation_id ,
12091225 "resource_name" : tool_name ,
12101226 "client_name" : client_name ,
12111227 "client_version" : client_version ,
0 commit comments