ergebnis/agent-detector
Detect the presence of coding agents in your PHP app by checking environment variables. Supports Amp, Antigravity, Augment, Claude Code, Codex, Cursor, Gemini CLI, GitHub Copilot, and more via a simple Detector::isAgentPresent() API.
AI_AGENT variable, allowing future-proofing for unknown agents without forking the package.isAgentPresent()) ensures thread safety in concurrent Laravel requests (critical for high-traffic APIs).AppServiceProvider::boot()) for global agent detection.Request object (via $_SERVER or getenv()).isAgentPresent() results for repeated requests).namespace App\Http\Middleware;
use Ergebnis\AgentDetector\Detector;
class DetectAgentMiddleware {
public function handle($request, Closure $next) {
$detector = new Detector();
if ($detector->isAgentPresent($_SERVER)) {
// Block, rate-limit, or log
}
return $next($request);
}
}
$app->bind(Detector::class, fn() => new Detector());
Then inject Detector into controllers/services via constructor DI.AgentDetected) when an agent is present.| Risk Area | Mitigation Strategy |
|---|---|
| False Positives | Test with real-world agent environments (e.g., GitHub Actions, Copilot CLI). |
| Performance Overhead | Cache results in Laravel’s cache layer (e.g., Cache::remember()). |
| PHP Version Support | Drop PHP 7.4/8.0 (EOL) and standardize on PHP 8.2+ for long-term support. |
| Agent Coverage Gaps | Extend the Detector class or submit PRs for missing agents (e.g., Perplexity). |
| Dependency Bloat | No transitive dependencies—safe for monolithic apps. |
isAgentPresent() in high-traffic endpoints (should be <1ms for most cases).app/Http/Kernel.php).AgentDetected event with agent type).$_SERVER/getenv() in PHPUnit for unit tests.composer.json:
"require": {
"ergebnis/agent-detector": "^1.2"
}
app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\DetectAgentMiddleware::class,
];
if ($detector->isAgentPresent($_SERVER)) {
abort(403, 'Agent traffic not allowed.');
}
AgentDetected) for analytics or logging.| Component | Compatibility Notes |
|---|---|
| Laravel | Works with Laravel 9+ (PHP 8.1+). Test with Lumen if using micro-framework. |
| PHP Extensions | No dependencies beyond PHP core. |
| Environment | Docker/Kubernetes: Ensure agent env vars (e.g., COPILOT_CLI) propagate. |
| Caching | Cache results in Redis/Memcached for high-throughput apps. |
| Testing | Mock $_SERVER in PHPUnit: |
```php
$_SERVER['COPILOT_CLI'] = 'true';
$this->assertTrue($detector->isAgentPresent($_SERVER));
``` |
/docs, /api/public) before production APIs.Detector class to add custom agent checks (e.g., internal tools).class CustomDetector extends \Ergebnis\AgentDetector\Detector {
protected function getAgentVariables(): array {
return array_merge(parent::getAgentVariables(), ['MY_CUSTOM_AGENT']);
}
}
1.2.x).COPILOT_GITHUB_TOKEN) for debugging.$cacheKey = 'agent_detected_' . md5(serialize($_SERVER));
return Cache::remember($cacheKey, now()->addMinute(), fn() => $detector->isAgentPresent($_SERVER));
null values).| Failure Scenario | Impact | Mitigation Strategy |
|---|---|---|
| False Positives | Legitimate traffic blocked | Whitelist env vars (e.g., `ALLOWED_AGENTS = ['CURSOR_AGENT |
How can I help you explore Laravel packages today?