Skip to content

Commit 248648f

Browse files
committed
Update tool.py
1 parent 01646be commit 248648f

File tree

1 file changed

+13
-18
lines changed
  • openhands-sdk/openhands/sdk/tool

1 file changed

+13
-18
lines changed

openhands-sdk/openhands/sdk/tool/tool.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,16 @@ def _get_tool_schema(
364364
action_type: type[Schema] | None = None,
365365
) -> dict[str, Any]:
366366
action_type = action_type or self.action_type
367+
action_type_with_risk = _create_action_type_with_risk(action_type)
367368

368-
if add_security_risk_prediction:
369-
# Always include security_risk field when prediction is enabled
370-
# This ensures consistent tool schemas regardless of tool type
371-
# (including read-only tools)
372-
action_type_with_risk = _create_action_type_with_risk(action_type)
373-
schema = action_type_with_risk.to_mcp_schema()
374-
else:
375-
schema = action_type.to_mcp_schema()
376-
369+
add_security_risk_prediction = add_security_risk_prediction and (
370+
self.annotations is None or (not self.annotations.readOnlyHint)
371+
)
372+
schema = (
373+
action_type_with_risk.to_mcp_schema()
374+
if add_security_risk_prediction
375+
else action_type.to_mcp_schema()
376+
)
377377
return schema
378378

379379
def to_openai_tool(
@@ -384,9 +384,10 @@ def to_openai_tool(
384384
"""Convert a Tool to an OpenAI tool.
385385
386386
Args:
387-
add_security_risk_prediction: Whether to include the `security_risk`
388-
field in the tool schema. When enabled, the field is included
389-
for all tool types (including read-only tools).
387+
add_security_risk_prediction: Whether to add a `security_risk` field
388+
to the action schema for LLM to predict. This is useful for
389+
tools that may have safety risks, so the LLM can reason about
390+
the risk level before calling the tool.
390391
action_type: Optionally override the action_type to use for the schema.
391392
This is useful for MCPTool to use a dynamically created action type
392393
based on the tool's input schema.
@@ -411,12 +412,6 @@ def to_responses_tool(
411412
412413
For Responses API, function tools expect top-level keys:
413414
{ "type": "function", "name": ..., "description": ..., "parameters": ... }
414-
415-
Args:
416-
add_security_risk_prediction: Whether to include the `security_risk`
417-
field in the tool schema. When enabled, the field is included
418-
for all tool types (including read-only tools).
419-
action_type: Optionally override the action_type to use for the schema.
420415
"""
421416

422417
return {

0 commit comments

Comments
 (0)