Agent with File System Tools
Overview¶
Agents can be equipped with file system capabilities to read, write, search, and edit files within a specified working directory. This enables code analysis, documentation generation, refactoring assistance, and other file-based operations. The agent determines which file operations to perform based on the task.
Key concepts:
- UseFileTools: Capability that adds file system tools to the agent
- Working directory: Root path for all file operations (security boundary)
- Available tools: read_file, write_file, edit_file, list_dir, search_files
- AgentConsoleLogger: Provides visibility into agent execution stages
Example¶
<?php
require 'examples/boot.php';
use Cognesy\Agents\AgentBuilder\AgentBuilder;
use Cognesy\Agents\AgentBuilder\Capabilities\File\UseFileTools;
use Cognesy\Agents\Core\Data\AgentState;
use Cognesy\Agents\Events\AgentConsoleLogger;
use Cognesy\Messages\Messages;
// Create console logger for execution visibility
$logger = new AgentConsoleLogger(
useColors: true,
showTimestamps: true,
showContinuation: true,
showToolArgs: true,
);
// Configure working directory (security boundary)
$workDir = dirname(__DIR__, 3); // Project root
// Build agent with file system capabilities
$agent = AgentBuilder::base()
->withCapability(new UseFileTools($workDir))
->build()
->wiretap($logger->wiretap());
// Create task that requires file access
$task = <<<TASK
Read the composer.json file and tell me:
1. What is the project name?
2. What PHP version is required?
3. List the first 5 dependencies (require section only).
Be concise.
TASK;
// Execute with initial state
$state = AgentState::empty()->withMessages(
Messages::fromString($task)
);
echo "=== Agent Execution Log ===\n\n";
// Execute agent until completion
$finalState = $agent->execute($state);
echo "\n=== Result ===\n";
$response = $finalState->finalResponse()->toString() ?: 'No response';
echo "Answer: {$response}\n";
echo "Steps: {$finalState->stepCount()}\n";
echo "Tokens: {$finalState->usage()->total()}\n";
echo "Status: {$finalState->status()->value}\n";
?>