Skip to content

Commit bca9cc6

Browse files
author
LittleCoinCoin
committed
fix(mcp): tools status after server reconnection
Ensure tool info is converted to provider-specific format and the cache updated for every "tool enabled" event (not only when the tool is new). This removes the stale-cache path that left tools without provider_format after MCP reconnection, which prevented tools from being injected into LLM request payloads.
1 parent a21938b commit bca9cc6

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

hatchling/mcp_utils/mcp_tool_lifecycle_subscriber.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,20 @@ def _handle_server_reachable_event(self, event: Event) -> None:
121121
def _handle_tool_enabled_event(self, event: Event) -> None:
122122
"""Handle tool enabled event."""
123123
tool_name = event.data.get("tool_name", "")
124-
125-
# Create or update tool info from event data
126-
if tool_name not in self._tool_cache:
127-
tool_info = event.data.get("tool_info", {})
124+
tool_info = event.data.get("tool_info", {})
128125

129-
if not tool_info:
130-
self.logger.error(f"'Tool enabled event' missing 'tool_info' for tool '{tool_name}'")
131-
return
132-
133-
# Convert tool to provider-specific format
134-
# Tool info is an in/out parameter in mcp_to_provider_tool
135-
# Hence, the provider_format field will be set
136-
# to the converted tool format
137-
self._mcp_to_provider_tool_func(tool_info)
126+
if not tool_info:
127+
self.logger.error(f"'Tool enabled event' missing 'tool_info' for tool '{tool_name}'")
128+
return
129+
130+
# Always convert tool to provider-specific format
131+
# This ensures that tools are properly formatted even during reconnection
132+
# when they already exist in the cache but need their provider_format refreshed
133+
self._mcp_to_provider_tool_func(tool_info)
138134

139-
self._tool_cache[tool_name] = tool_info
140-
self.logger.debug(f"Tool enabled: {tool_name}")
135+
# Update cache with the new tool info (whether new or existing)
136+
self._tool_cache[tool_name] = tool_info
137+
self.logger.debug(f"Tool enabled: {tool_name}")
141138

142139
def _handle_tool_disabled_event(self, event: Event) -> None:
143140
"""Handle tool disabled event."""

0 commit comments

Comments
 (0)