"Extending agent capabilities through external system integration"
Tool Use enables agents to interact with external systems, APIs, databases, and services. Instead of relying solely on pretrained knowledge, agents can retrieve real-time data, execute commands, and manipulate external resources.
- Tool Definition: Specify what the tool does and its parameters
- Tool Invocation: LLM decides when and how to use tools
- Sandboxing: Execute tools safely with appropriate restrictions
- Result Integration: Feed tool output back to the LLM
- Primary:
codex-rs/core/src/exec.rs - Tool Registry:
codex-rs/core/src/tools/registry.rs - MCP Tools:
codex-rs/core/src/mcp_tool_call.rs - Safety:
codex-rs/core/src/safety.rs
Codex has a comprehensive tool system with multiple execution modes:
// From codex-rs/core/src/exec.rs
pub async fn process_exec_tool_call(
params: ExecParams,
sandbox_type: SandboxType,
sandbox_policy: &SandboxPolicy,
sandbox_cwd: &Path,
codex_linux_sandbox_exe: &Option<PathBuf>,
stdout_stream: Option<StdoutStream>,
) -> Result<ExecToolCallOutput> {
match sandbox_type {
SandboxType::None => {
// Direct execution (dangerous!)
exec(params, sandbox_policy, stdout_stream).await
}
SandboxType::MacosSeatbelt => {
// Execute under Apple Seatbelt sandbox
let child = spawn_command_under_seatbelt(...).await?;
consume_truncated_output(child, timeout, stdout_stream).await
}
SandboxType::LinuxSeccomp => {
// Execute under Landlock + seccomp
let child = spawn_command_under_landlock(...).await?;
consume_truncated_output(child, timeout, stdout_stream).await
}
}
}-
File Operations
- Read files
- Write/patch files
- List directories
-
Shell Commands
- Execute in sandbox
- Network-disabled by default
- Output truncation for token limits
-
Git Operations
- Status, diff, commit
- Branch management
-
MCP Tools
- Dynamic tool discovery
- External server integration
-
Web Search (when enabled)
- Real-time information retrieval
Codex implements multi-layer security for tool execution:
- macOS: Apple Seatbelt (
sandbox-exec) - Linux: Landlock + seccomp
- Network: Disabled by default
- File Access: Limited to workspace
// From codex-rs/core/src/safety.rs
pub fn assess_command_safety(
command: &[String],
approval_policy: AskForApproval,
sandbox_policy: &SandboxPolicy,
approved: &HashSet<Vec<String>>,
) -> SafetyCheck {
if command_might_be_dangerous(command) {
return SafetyCheck::AskUser; // Request approval
}
if is_known_safe_command(command) {
return SafetyCheck::AutoApprove { ... };
}
// Apply policy
assess_safety_for_untrusted_command(...)
}- Known-safe commands:
ls,cat,grep, etc. - Dangerous commands:
rm -rf,dd,curl(network) - User approval required for dangerous operations
┌─────────────────────────────────────────────┐
│ LLM Decision Engine │
│ "I need to read file X to answer this" │
└──────────────────┬──────────────────────────┘
│
▼
┌──────────────────────┐
│ Tool Router │
│ (Pattern #2) │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ Safety Assessment │
│ • Is it safe? │
│ • Needs approval? │
│ • Sandbox required? │
└──────────┬───────────┘
│
┌────────┴────────┐
│ │
▼ ▼
┌──────────┐ ┌──────────┐
│ Approve │ │ Reject │
└─────┬────┘ └──────────┘
│
▼
┌──────────────────────┐
│ Execute in Sandbox │
│ • Network: OFF │
│ • File access: CWD │
│ • Timeout: 30s │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ Collect Output │
│ • stdout/stderr │
│ • exit code │
│ • Truncate if long │
└──────────┬───────────┘
│
▼
┌──────────────────────┐
│ Return to LLM │
│ "File contains..." │
└──────────────────────┘
See the example files:
pattern_simple.py: Basic tool use with function callingpattern_advanced.py: Tool registry with sandboxingtool_safety.py: Safety checks and validation
- ✅ Real-time Data: Access current information beyond training data
- ✅ Action Capability: Agents can modify external state
- ✅ Extensibility: Add new tools without retraining
- ✅ Safety Critical: Must sandbox and validate all executions
- ✅ User Control: Approval workflows for dangerous operations
- ✅ Need real-time or dynamic data
- ✅ Agent must interact with external systems
- ✅ Task requires actions beyond text generation
- ✅ Integration with existing APIs/services
- ❌ All information available in training data
- ❌ Read-only Q&A tasks
- ✅ Always sandbox tool execution
- ✅ Validate all tool inputs
- ✅ Implement approval workflows for dangerous operations
- ✅ Truncate/limit tool output
- ✅ Set execution timeouts
- ✅ Log all tool invocations
- ❌ Never trust tool input blindly
- ❌ Don't give unrestricted file system access
- ❌ Avoid network access unless explicitly needed
- ❌ Don't execute without output limits
- ❌ Never skip safety checks for convenience
- Codex Exec:
codex-rs/core/src/exec.rs - Tool Registry:
codex-rs/core/src/tools/registry.rs - Safety Module:
codex-rs/core/src/safety.rs - Textbook: Chapter 5 - Tool Use
- OpenAI Function Calling: https://platform.openai.com/docs/guides/function-calling
- Pattern 1: Prompt Chaining - Tools can be steps in chains
- Pattern 2: Routing - Routes to appropriate tools
- Pattern 3: Parallelization - Parallel tool execution
- Pattern 10: MCP Integration - Protocol for tool integration
- Pattern 13: Human-in-the-Loop - Approvals for tools