feat: handle none value in mcp tool node

This commit is contained in:
Novice
2025-06-20 14:48:22 +08:00
parent c1884c2e40
commit db001e1511
2 changed files with 31 additions and 0 deletions

View File

@ -149,10 +149,30 @@ def cast_parameter_value(typ: enum.StrEnum, value: Any, /):
return value
case PluginParameterType.ARRAY:
if not isinstance(value, list):
# Try to parse JSON string for arrays
if isinstance(value, str):
try:
import json
parsed_value = json.loads(value)
if isinstance(parsed_value, list):
return parsed_value
except (json.JSONDecodeError, ValueError):
pass
return [value]
return value
case PluginParameterType.OBJECT:
if not isinstance(value, dict):
# Try to parse JSON string for objects
if isinstance(value, str):
try:
import json
parsed_value = json.loads(value)
if isinstance(parsed_value, dict):
return parsed_value
except (json.JSONDecodeError, ValueError):
pass
return {}
return value
case _:

View File

@ -40,6 +40,7 @@ class MCPTool(Tool):
) -> Generator[ToolInvokeMessage, None, None]:
try:
with MCPClient(self.server_url, self.provider_id, self.tenant_id, authed=True) as mcp_client:
tool_parameters = self._handle_none_parameter(tool_parameters)
result = mcp_client.invoke_tool(tool_name=self.entity.identity.name, tool_args=tool_parameters)
except MCPAuthError as e:
raise ValueError("Please auth the tool first")
@ -62,3 +63,13 @@ class MCPTool(Tool):
server_url=self.server_url,
provider_id=self.provider_id,
)
def _handle_none_parameter(self, parameter: dict[str, Any]) -> dict[str, Any]:
"""
in mcp tool invoke, if the parameter is empty, it will be set to None
"""
return {
key: value
for key, value in parameter.items()
if value is not None and not (isinstance(value, str) and value.strip() == "")
}