MCPcopy
hub / github.com/openai/openai-agents-python / FunctionTool

Class FunctionTool

src/agents/tool.py:381–523  ·  view source on GitHub ↗

A tool that wraps a function. In most cases, you should use the `function_tool` helpers to create a FunctionTool, as they let you easily wrap a Python function.

Source from the content-addressed store, hash-verified

379
380@dataclass
381class FunctionTool:
382 """A tool that wraps a function. In most cases, you should use the `function_tool` helpers to
383 create a FunctionTool, as they let you easily wrap a Python function.
384 """
385
386 name: str
387 """The name of the tool, as shown to the LLM. Generally the name of the function."""
388
389 description: str
390 """A description of the tool, as shown to the LLM."""
391
392 params_json_schema: dict[str, Any]
393 """The JSON schema for the tool's parameters."""
394
395 on_invoke_tool: Callable[[ToolContext[Any], str], Awaitable[Any]]
396 """A function that invokes the tool with the given context and parameters. The params passed
397 are:
398 1. The tool run context.
399 2. The arguments from the LLM, as a JSON string.
400
401 You must return one of the structured tool output types (e.g. ToolOutputText, ToolOutputImage,
402 ToolOutputFileContent) or a string representation of the tool output, or a list of them,
403 or something we can call `str()` on.
404 In case of errors, you can either raise an Exception (which will cause the run to fail) or
405 return a string error message (which will be sent back to the LLM).
406 """
407
408 strict_json_schema: bool = True
409 """Whether the JSON schema is in strict mode. We **strongly** recommend setting this to True,
410 as it increases the likelihood of correct JSON input."""
411
412 is_enabled: bool | Callable[[RunContextWrapper[Any], AgentBase], MaybeAwaitable[bool]] = True
413 """Whether the tool is enabled. Either a bool or a Callable that takes the run context and agent
414 and returns whether the tool is enabled. You can use this to dynamically enable/disable a tool
415 based on your context/state."""
416
417 # Keep guardrail fields before needs_approval to preserve v0.7.0 positional
418 # constructor compatibility for public FunctionTool callers.
419 # Tool-specific guardrails.
420 tool_input_guardrails: list[ToolInputGuardrail[Any]] | None = None
421 """Optional list of input guardrails to run before invoking this tool."""
422
423 tool_output_guardrails: list[ToolOutputGuardrail[Any]] | None = None
424 """Optional list of output guardrails to run after invoking this tool."""
425
426 needs_approval: (
427 bool | Callable[[RunContextWrapper[Any], dict[str, Any], str], Awaitable[bool]]
428 ) = False
429 """Whether the tool needs approval before execution. If True, the run will be interrupted
430 and the tool call will need to be approved using RunState.approve() or rejected using
431 RunState.reject() before continuing. Can be a bool (always/never needs approval) or a
432 function that takes (run_context, tool_parameters, call_id) and returns whether this
433 specific call needs approval."""
434
435 # Keep timeout fields after needs_approval to preserve positional constructor compatibility.
436 timeout_seconds: float | None = None
437 """Optional timeout (seconds) for each tool invocation."""
438

Calls

no outgoing calls