fix: MCP Tool mode issue (#7944)

* refactor to ensure proper initiallisations

* [autofix.ci] apply automated fixes

* fix lint

* add comment on lint issue

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Edwin Jose 2025-05-07 17:42:28 -04:00 committed by GitHub
commit ee706073db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 17 deletions

View file

@ -501,6 +501,7 @@ class MCPToolsComponent(Component):
func=create_tool_func(tool.name, args_schema, client.session),
coroutine=create_tool_coroutine(tool.name, args_schema, client.session),
tags=[tool.name],
metadata={},
)
tool_list.append(tool_obj)
self._tool_cache[tool.name] = tool_obj
@ -528,4 +529,3 @@ class MCPToolsComponent(Component):
msg = "SSE URL is not set"
raise ValueError(msg)
return await self.update_tools()
# return self.tools

View file

@ -1140,7 +1140,7 @@ class Component(CustomComponent):
list[Tool]: Filtered list of tools.
"""
# Convert metadata to a list of dicts if it's a DataFrame
metadata_dict = None
metadata_dict = None # Initialize as None to avoid lint issues with empty dict
if isinstance(metadata, pd.DataFrame):
metadata_dict = metadata.to_dict(orient="records")
@ -1163,24 +1163,25 @@ class Component(CustomComponent):
tool_status = {item["name"]: item.get("status", True) for item in metadata_dict}
return [tool for tool in tools if tool_status.get(tool.name, True)]
def _build_tool_data(self, tool: Tool) -> dict:
if tool.metadata is None:
tool.metadata = {}
return {
"name": tool.name,
"description": tool.description,
"tags": tool.tags if hasattr(tool, "tags") and tool.tags else [tool.name],
"status": True, # Initialize all tools with status True
"display_name": tool.metadata.get("display_name", tool.name),
"display_description": tool.metadata.get("display_description", tool.description),
"readonly": tool.metadata.get("readonly", False),
"args": tool.args,
# "args_schema": tool.args_schema,
}
async def _build_tools_metadata_input(self):
tools = await self._get_tools()
# Always use the latest tool data
tool_data = [
{
"name": tool.name,
"description": tool.description,
"tags": tool.tags if hasattr(tool, "tags") and tool.tags else [tool.name],
"status": True, # Initialize all tools with status True
"display_name": tool.metadata.get("display_name", tool.name),
"display_description": tool.metadata.get("display_description", tool.description),
"readonly": tool.metadata.get("readonly", False),
"args": tool.args,
# "args_schema": tool.args_schema,
}
for tool in tools
]
tool_data = [self._build_tool_data(tool) for tool in tools]
# print(tool_data)
if hasattr(self, TOOLS_METADATA_INPUT_NAME):
old_tags = self._extract_tools_tags(self.tools_metadata)