2727)
2828from openai .types .responses .response_reasoning_item import ResponseReasoningItem
2929
30+ from .._mcp_tool_metadata import collect_mcp_list_tools_metadata
3031from .._tool_identity import (
3132 build_function_tool_lookup_map ,
3233 get_function_tool_lookup_key ,
@@ -1271,6 +1272,7 @@ def process_model_response(
12711272 response : ModelResponse ,
12721273 output_schema : AgentOutputSchemaBase | None ,
12731274 handoffs : list [Handoff ],
1275+ existing_items : Sequence [RunItem ] | None = None ,
12741276) -> ProcessedResponse :
12751277 items : list [RunItem ] = []
12761278
@@ -1295,6 +1297,8 @@ def process_model_response(
12951297 for tool in all_tools
12961298 if isinstance (tool , HostedMCPTool )
12971299 }
1300+ hosted_mcp_tool_metadata = collect_mcp_list_tools_metadata (existing_items or ())
1301+ hosted_mcp_tool_metadata .update (collect_mcp_list_tools_metadata (response .output ))
12981302
12991303 def _dump_output_item (raw_item : Any ) -> dict [str , Any ]:
13001304 if isinstance (raw_item , dict ):
@@ -1506,19 +1510,15 @@ def _dump_output_item(raw_item: Any) -> dict[str, Any]:
15061510 elif isinstance (output , McpListTools ):
15071511 items .append (MCPListToolsItem (raw_item = output , agent = agent ))
15081512 elif isinstance (output , McpCall ):
1509- # Look up MCP tool description from the server's cached tools list.
1510- # Tool discovery is async I/O, but this function is sync, so this is best-effort and
1511- # only works if the server has cached tool metadata (e.g., when cache_tools_list is
1512- # enabled).
1513- _mcp_description : str | None = None
1514- for _server in agent .mcp_servers :
1515- if _server .name == output .server_label :
1516- for _tool in _server .cached_tools or []:
1517- if _tool .name == output .name :
1518- _mcp_description = _tool .description
1519- break
1520- break
1521- items .append (ToolCallItem (raw_item = output , agent = agent , description = _mcp_description ))
1513+ metadata = hosted_mcp_tool_metadata .get ((output .server_label , output .name ))
1514+ items .append (
1515+ ToolCallItem (
1516+ raw_item = output ,
1517+ agent = agent ,
1518+ description = metadata .description if metadata is not None else None ,
1519+ title = metadata .title if metadata is not None else None ,
1520+ )
1521+ )
15221522 tools_used .append ("mcp" )
15231523 elif isinstance (output , ImageGenerationCall ):
15241524 items .append (ToolCallItem (raw_item = output , agent = agent ))
@@ -1652,6 +1652,7 @@ def _dump_output_item(raw_item: Any) -> dict[str, Any]:
16521652 raw_item = output ,
16531653 agent = agent ,
16541654 description = func_tool .description ,
1655+ title = func_tool ._mcp_title ,
16551656 )
16561657 )
16571658 functions .append (
@@ -1696,6 +1697,7 @@ async def get_single_step_result_from_response(
16961697 response = new_response ,
16971698 output_schema = output_schema ,
16981699 handoffs = handoffs ,
1700+ existing_items = pre_step_items ,
16991701 )
17001702
17011703 tool_use_tracker .record_processed_response (agent , processed_response )
0 commit comments