4141ActionT = TypeVar ("ActionT" , bound = Action )
4242ObservationT = TypeVar ("ObservationT" , bound = Observation )
4343_action_types_with_risk : dict [type , type ] = {}
44+ _action_types_with_summary : dict [type , type ] = {}
4445
4546
4647def _camel_to_snake (name : str ) -> str :
@@ -364,17 +365,18 @@ def _get_tool_schema(
364365 action_type : type [Schema ] | None = None ,
365366 ) -> dict [str , Any ]:
366367 action_type = action_type or self .action_type
367- action_type_with_risk = create_action_type_with_risk (action_type )
368368
369+ # Apply security risk enhancement if enabled
369370 add_security_risk_prediction = add_security_risk_prediction and (
370371 self .annotations is None or (not self .annotations .readOnlyHint )
371372 )
372- schema = (
373- action_type_with_risk .to_mcp_schema ()
374- if add_security_risk_prediction
375- else action_type .to_mcp_schema ()
376- )
377- return schema
373+ if add_security_risk_prediction :
374+ action_type = create_action_type_with_risk (action_type )
375+
376+ # Always add summary field for transparency and explainability
377+ action_type = _create_action_type_with_summary (action_type )
378+
379+ return action_type .to_mcp_schema ()
378380
379381 def to_openai_tool (
380382 self ,
@@ -391,14 +393,19 @@ def to_openai_tool(
391393 action_type: Optionally override the action_type to use for the schema.
392394 This is useful for MCPTool to use a dynamically created action type
393395 based on the tool's input schema.
396+
397+ Note:
398+ Summary field is always added to the schema for transparency and
399+ explainability of agent actions.
394400 """
395401 return ChatCompletionToolParam (
396402 type = "function" ,
397403 function = ChatCompletionToolParamFunctionChunk (
398404 name = self .name ,
399405 description = self .description ,
400406 parameters = self ._get_tool_schema (
401- add_security_risk_prediction , action_type
407+ add_security_risk_prediction ,
408+ action_type ,
402409 ),
403410 ),
404411 )
@@ -412,14 +419,23 @@ def to_responses_tool(
412419
413420 For Responses API, function tools expect top-level keys:
414421 { "type": "function", "name": ..., "description": ..., "parameters": ... }
422+
423+ Args:
424+ add_security_risk_prediction: Whether to add a `security_risk` field
425+ action_type: Optional override for the action type
426+
427+ Note:
428+ Summary field is always added to the schema for transparency and
429+ explainability of agent actions.
415430 """
416431
417432 return {
418433 "type" : "function" ,
419434 "name" : self .name ,
420435 "description" : self .description ,
421436 "parameters" : self ._get_tool_schema (
422- add_security_risk_prediction , action_type
437+ add_security_risk_prediction ,
438+ action_type ,
423439 ),
424440 "strict" : False ,
425441 }
@@ -479,3 +495,38 @@ def create_action_type_with_risk(action_type: type[Schema]) -> type[Schema]:
479495 )
480496 _action_types_with_risk [action_type ] = action_type_with_risk
481497 return action_type_with_risk
498+
499+
500+ def _create_action_type_with_summary (action_type : type [Schema ]) -> type [Schema ]:
501+ """Create a new action type with summary field for LLM to predict.
502+
503+ This dynamically adds a 'summary' field to the action schema, allowing
504+ the LLM to provide a brief explanation of what each action does.
505+
506+ Args:
507+ action_type: The original action type to enhance
508+
509+ Returns:
510+ A new type that includes the summary field
511+ """
512+ action_type_with_summary = _action_types_with_summary .get (action_type )
513+ if action_type_with_summary :
514+ return action_type_with_summary
515+
516+ action_type_with_summary = type (
517+ f"{ action_type .__name__ } WithSummary" ,
518+ (action_type ,),
519+ {
520+ "summary" : Field (
521+ default = None ,
522+ description = (
523+ "A concise summary (approximately 10 words) describing what "
524+ "this specific action does. Focus on the key operation and target. "
525+ "Example: 'List all Python files in current directory'"
526+ ),
527+ ),
528+ "__annotations__" : {"summary" : str | None },
529+ },
530+ )
531+ _action_types_with_summary [action_type ] = action_type_with_summary
532+ return action_type_with_summary
0 commit comments