Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

feat(php-sdk): add FirecrawlMap Laravel AI tool

Kris Oei86311863279c
diff --git a/apps/php-sdk/src/Laravel/Tools/FirecrawlMap.php b/apps/php-sdk/src/Laravel/Tools/FirecrawlMap.php
new file mode 100644
index 000000000..26884756c
--- /dev/null
+++ b/apps/php-sdk/src/Laravel/Tools/FirecrawlMap.php
@@ -0,0 +1,68 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Firecrawl\Laravel\Tools;
+
+use Firecrawl\Models\MapOptions;
+use Illuminate\Contracts\JsonSchema\JsonSchema;
+use Laravel\Ai\Tools\Request;
+
+class FirecrawlMap extends FirecrawlTool
+{
+ public function name(): string
+ {
+ return 'firecrawl_map';
+ }
+
+ public function description(): string
+ {
+ return 'Map a website with Firecrawl to discover the URLs it contains, returned as a JSON '
+ . 'array of {url, title} objects. Use this to find pages on a specific site — optionally '
+ . 'filtered by a search term — before scraping or crawling them.';
+ }
+
+ public function handle(Request $request): string
+ {
+ return $this->guard(function () use ($request): string {
+ $limit = min(max($request->integer('limit') ?: 100, 1), 5000);
+ $search = (string) $request->string('search');
+
+ $map = $this->client()->map(
+ (string) $request->string('url'),
+ MapOptions::with(
+ search: $search !== '' ? $search : null,
+ limit: $limit,
+ integration: self::INTEGRATION,
+ ),
+ );
+
+ $links = array_values(array_map(static function (array $link): array {
+ return array_filter(
+ ['url' => $link['url'] ?? null, 'title' => $link['title'] ?? null],
+ static fn (mixed $value): bool => $value !== null,
+ );
+ }, $map->getLinks()));
+
+ if ($links === []) {
+ return 'No URLs discovered.';
+ }
+
+ return json_encode($links, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
+ });
+ }
+
+ /** @return array<string, \Illuminate\JsonSchema\Types\Type> */
+ public function schema(JsonSchema $schema): array
+ {
+ return [
+ 'url' => $schema->string()
+ ->description('The base URL of the website to map (e.g. https://example.com).')
+ ->required(),
+ 'search' => $schema->string()
+ ->description('Optional term to filter the discovered URLs by relevance (e.g. "docs").'),
+ 'limit' => $schema->integer()->min(1)->max(5000)
+ ->description('Maximum number of URLs to return. Defaults to 100.'),
+ ];
+ }
+}
diff --git a/apps/php-sdk/tests/Unit/LaravelTools/FirecrawlMapTest.php b/apps/php-sdk/tests/Unit/LaravelTools/FirecrawlMapTest.php
new file mode 100644
index 000000000..512726387
--- /dev/null
+++ b/apps/php-sdk/tests/Unit/LaravelTools/FirecrawlMapTest.php
@@ -0,0 +1,71 @@
+<?php
+
+declare(strict_types=1);
+
+use Firecrawl\Laravel\Tools\FirecrawlMap;
+use GuzzleHttp\Psr7\Response;
+use Illuminate\JsonSchema\JsonSchemaTypeFactory;
+use Laravel\Ai\ObjectSchema;
+use Laravel\Ai\Tools\Request;
+
+it('maps a site and returns discovered URLs as JSON', function (): void {
+ $history = new ArrayObject();
+ $client = fakeFirecrawlClient([
+ new Response(200, [], json_encode([
+ 'success' => true,
+ 'data' => [
+ 'links' => [
+ ['url' => 'https://example.com/', 'title' => 'Home'],
+ 'https://example.com/pricing',
+ ],
+ ],
+ ])),
+ ], $history);
+
+ $result = (new FirecrawlMap($client))->handle(new Request(['url' => 'https://example.com']));
+
+ expect(json_decode($result, true))->toBe([
+ ['url' => 'https://example.com/', 'title' => 'Home'],
+ ['url' => 'https://example.com/pricing'],
+ ]);
+
+ $body = json_decode((string) $history[0]['request']->getBody(), true);
+ expect($body['url'])->toBe('https://example.com');
+ expect($body['limit'])->toBe(100);
+ expect($body['integration'])->toBe('laravel-ai');
+ expect($body)->not->toHaveKey('search');
+});
+
+it('passes the search filter through when provided', function (): void {
+ $history = new ArrayObject();
+ $client = fakeFirecrawlClient([
+ new Response(200, [], json_encode(['success' => true, 'data' => ['links' => []]])),
+ ], $history);
+
+ (new FirecrawlMap($client))->handle(new Request(['url' => 'https://example.com', 'search' => 'docs']));
+
+ $body = json_decode((string) $history[0]['request']->getBody(), true);
+ expect($body['search'])->toBe('docs');
+});
+
+it('reports when no URLs were discovered', function (): void {
+ $client = fakeFirecrawlClient([
+ new Response(200, [], json_encode(['success' => true, 'data' => ['links' => []]])),
+ ]);
+
+ $result = (new FirecrawlMap($client))->handle(new Request(['url' => 'https://example.com']));
+
+ expect($result)->toBe('No URLs discovered.');
+});
+
+it('exposes name and a schema with required url', function (): void {
+ $tool = new FirecrawlMap(fakeFirecrawlClient([]));
+
+ expect($tool->name())->toBe('firecrawl_map');
+
+ $types = $tool->schema(new JsonSchemaTypeFactory());
+ expect($types)->toHaveKeys(['url', 'search', 'limit']);
+
+ $jsonSchema = (new ObjectSchema($types))->toSchema();
+ expect($jsonSchema['required'])->toContain('url');
+});