[Bugfix][Responses] Clean up MCP tool sessions once before closing them (#56988)

Signed-off-by: 子华 <huaxi.shx@alibaba-inc.com>
Co-authored-by: Codex <noreply@openai.com>
This commit is contained in:
shaohuaxi
2026-09-16 14:07:06 +08:00
committed by GitHub
co-authored by Codex
parent b3b13c1292
commit e0301afb2a
2 changed files with 90 additions and 2 deletions
+7 -1
View File
@@ -565,6 +565,7 @@ class ParsableContext(ConversationContext):
mcp_tools: dict[str, Mcp],
):
if tool_server:
initialized_session = False
for tool_name in self.available_tools:
if tool_name in self._tool_sessions:
continue
@@ -577,6 +578,8 @@ class ParsableContext(ConversationContext):
tool_server.new_session(tool_name, request_id, headers)
)
self._tool_sessions[tool_name] = tool_session
initialized_session = True
if initialized_session:
exit_stack.push_async_exit(self.cleanup_session)
async def cleanup_session(self, *args, **kwargs) -> None:
@@ -863,6 +866,7 @@ class HarmonyContext(ConversationContext):
mcp_tools: dict[str, Mcp],
):
if tool_server:
initialized_session = False
for tool_name in self.available_tools:
if tool_name not in self._tool_sessions:
tool_type = _map_tool_name_to_tool_type(tool_name)
@@ -873,7 +877,9 @@ class HarmonyContext(ConversationContext):
tool_server.new_session(tool_name, request_id, headers)
)
self._tool_sessions[tool_name] = tool_session
exit_stack.push_async_exit(self.cleanup_session)
initialized_session = True
if initialized_session:
exit_stack.push_async_exit(self.cleanup_session)
async def call_container_tool(
self, tool_session: Union["ClientSession", Tool], last_msg: Message