shipfastlabs/agent-detector
Lightweight PHP 8.2+ utility to detect if your app is running inside an AI agent or automated dev environment. Supports Claude, Cursor, Gemini, Codex, Replit, Devin, and more via env vars or file checks, with a simple API and helper function.
GITHUB_ACTIONS, CLAUDE_CODE) that are already used in Laravel’s .env and CI/CD pipelines, reducing friction for adoption.AI_AGENT env var, allowing future-proofing for proprietary or niche tools. The knownAgent() method enables precise branching logic (e.g., "block Claude but allow Cursor").composer require command, and usage is as simple as a one-liner (AgentDetector::detect()). No database migrations, config changes, or complex setup are required.artisan commands (e.g., tinker, migrate).detectAgent() allows quick checks in one-off scripts or tests without instantiating a class.REPL_ID may not exist in Docker).APP_ENV === 'local', APP_DEBUG === false) for defense in depth. Test in all execution environments (CLI, HTTP, queues).php://input or $_SERVER['REMOTE_ADDR'] patterns in CI/CD IPs).php artisan tinker, php artisan queue:work, and HTTP requests.)APP_ENV checks)?laravel/agent-detector wrapper)?AgentDetector as a singleton for global access.AgentDetectorFacade for cleaner syntax (e.g., AgentDetector::detect() → Agent::detect()).GITHUB_ACTIONS, CI).artisan commands, tinker, and custom scripts.composer require shipfastlabs/agent-detector --dev
// app/Console/Commands/TestAgentDetection.php
use AgentDetector\AgentDetector;
public function handle() {
$result = AgentDetector::detect();
$this->line("Agent detected: " . ($result->isAgent ? $result->name : 'None'));
$this->line("Known agent: " . ($result->knownAgent() ? $result->knownAgent()->value : 'No'));
}
php artisan test:agent-detection # CLI
curl -X GET /test-agent # HTTP (if middleware is added)
CheckAgentMiddleware to block agent access to routes:
// app/Http/Middleware/CheckAgent.php
public function handle(Request $request, Closure $next) {
if (AgentDetector::detect()->isAgent) {
abort(403, 'Agents are not allowed to access this resource.');
}
return $next($request);
}
Register in app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\CheckAgent::class,
];
// app/Console/Commands/SensitiveCommand.php
public function handle() {
if (AgentDetector::detect()->isAgent) {
$this->error('This command is blocked in automated environments.');
return;
}
// Proceed with command logic...
}
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton('agentDetector', function () {
return new \AgentDetector\AgentDetector();
});
}
APP_DEBUG or APP_ENV based on agent context.AI_AGENT env vars in CI/CD pipelines for granular control..env system already supports env var injection, but ensure CI/CD pipelines propagate agent-specific vars (e.g., CLAUDE_CODE).artisan, tinker, custom scripts).Cache::forget() or use Cache::rememberForever() with a unique key).laravel/framework, spatie/laravel-permission). The package has no dependencies.artisan commands.APP_DEBUG adaptation).How can I help you explore Laravel packages today?