Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Workflow:Openai Openai agents python Tool Integrated Agent

From Leeroopedia
Knowledge Sources
Domains AI_Agents, Tool_Use, LLMs
Last Updated 2026-02-11 14:00 GMT

Overview

End-to-end process for creating an AI agent equipped with function tools, hosted tools, or MCP tools that the model can invoke to perform actions and retrieve data.

Description

This workflow demonstrates how to extend an agent's capabilities beyond text generation by attaching tools. The SDK supports multiple tool types: function tools (Python functions decorated with @function_tool), hosted tools (WebSearchTool, FileSearchTool, CodeInterpreterTool, ImageGenerationTool that run server-side), and MCP tools (connected via MCPServerStdio, MCPServerSse, or HostedMCPTool). When the model determines it needs external data or actions, it generates tool calls that the Runner executes, feeding results back to the model for a final response.

Usage

Execute this workflow when your agent needs to interact with external systems, retrieve real-time data, perform computations, or take actions beyond what the language model can do with its training data alone. Examples include fetching weather data, searching the web, executing code, or calling custom business logic APIs.

Execution Steps

Step 1: Define Function Tools

Create Python functions and decorate them with @function_tool to make them available to the agent. Each function's docstring becomes the tool description, and parameter annotations define the tool's input schema. Return values are automatically serialized and sent back to the model.

Key considerations:

  • Use type annotations (including Annotated types) to provide parameter descriptions
  • Pydantic BaseModel return types are serialized to JSON automatically
  • Functions can accept a RunContextWrapper parameter to access shared context
  • Functions can also accept a ToolContext parameter for tool-specific metadata

Step 2: Configure Hosted Tools (Optional)

For server-side capabilities, instantiate hosted tool classes such as WebSearchTool, FileSearchTool, CodeInterpreterTool, or ImageGenerationTool. These tools run on OpenAI's infrastructure and do not require local execution.

Key considerations:

  • WebSearchTool supports user_location and search_context_size configuration
  • FileSearchTool requires a vector_store_ids list for document search
  • HostedMCPTool connects to MCP servers hosted via the Responses API
  • Hosted tools can be mixed with function tools on the same agent

Step 3: Agent Assembly

Create an Agent instance and pass the tools list containing function tools, hosted tools, or both. The SDK automatically generates the appropriate JSON schemas for each tool to send to the model API.

Key considerations:

  • The tools parameter accepts a list of Tool objects
  • Tool choice can be configured via ModelSettings (auto, required, or specific tool)
  • Each tool's schema is validated for strict JSON compliance

Step 4: Runner Execution with Tool Loop

Call Runner.run() with the agent and user input. The internal run loop detects tool calls in the model response, executes each tool, and feeds results back to the model. This loop continues until the model produces a final text output or the max_turns limit is reached.

Key considerations:

  • The run loop automatically handles multiple rounds of tool calls
  • max_turns limits the number of model invocations (default is configurable)
  • Tool outputs are serialized and appended to the conversation as function call results
  • If a tool raises an exception, the error message is sent back to the model

Step 5: Result Processing

Extract the final output from the RunResult. The result contains the agent's text response that incorporates information gathered from tool calls. Inspect new_items to see the full sequence of tool calls and their outputs.

Key considerations:

  • result.final_output contains the model's synthesized response
  • result.new_items includes ToolCallItem and ToolCallOutputItem entries
  • Usage statistics track tokens consumed across all turns including tool calls

Execution Diagram

GitHub URL

Workflow Repository