Skip to content

Commit 7c5cbcc

Browse files
committed
improve discoverer dependency
1 parent 61ed415 commit 7c5cbcc

5 files changed

Lines changed: 71 additions & 20 deletions

File tree

src/Capability/Discovery/CachedDiscoverer.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020
* This decorator caches the results of file system operations and reflection
2121
* to improve performance when discovery is called multiple times.
2222
*
23+
* @internal
24+
* @final
25+
*
2326
* @author Xentixar <xentixar@gmail.com>
2427
*/
25-
class CachedDiscoverer
28+
class CachedDiscoverer implements DiscovererInterface
2629
{
2730
private const CACHE_PREFIX = 'mcp_discovery_';
2831

2932
public function __construct(
30-
private readonly Discoverer $discoverer,
33+
private readonly DiscovererInterface $discoverer,
3134
private readonly CacheInterface $cache,
3235
private readonly LoggerInterface $logger,
3336
) {

src/Capability/Discovery/Discoverer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@
4242
* resourceTemplates: int,
4343
* }
4444
*
45+
* @internal
46+
* @final
47+
*
4548
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
4649
*/
47-
class Discoverer
50+
class Discoverer implements DiscovererInterface
4851
{
4952
public function __construct(
5053
private readonly LoggerInterface $logger = new NullLogger(),
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Capability\Discovery;
13+
14+
/**
15+
* Discovers MCP elements (tools, resources, prompts, resource templates) in directories.
16+
*
17+
* Implementations can use different strategies:
18+
* - File system scanning with reflection
19+
* - Cached discovery
20+
* - Pre-configured discovery
21+
*
22+
* @internal
23+
*
24+
* @author Antoine Bluchet <soyuka@gmail.com>
25+
*/
26+
interface DiscovererInterface
27+
{
28+
/**
29+
* Discover MCP elements in the specified directories and return the discovery state.
30+
*
31+
* @param string $basePath the base path for resolving directories
32+
* @param array<string> $directories list of directories (relative to base path) to scan
33+
* @param array<string> $excludeDirs list of directories (relative to base path) to exclude from the scan
34+
*/
35+
public function discover(string $basePath, array $directories, array $excludeDirs = []): DiscoveryState;
36+
}

src/Capability/Registry/Loader/DiscoveryLoader.php

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111

1212
namespace Mcp\Capability\Registry\Loader;
1313

14-
use Mcp\Capability\Discovery\CachedDiscoverer;
15-
use Mcp\Capability\Discovery\Discoverer;
16-
use Mcp\Capability\Discovery\SchemaGeneratorInterface;
14+
use Mcp\Capability\Discovery\DiscovererInterface;
1715
use Mcp\Capability\RegistryInterface;
18-
use Psr\Log\LoggerInterface;
19-
use Psr\SimpleCache\CacheInterface;
2016

2117
/**
2218
* @author Antoine Bluchet <soyuka@gmail.com>
@@ -31,22 +27,13 @@ public function __construct(
3127
private string $basePath,
3228
private array $scanDirs,
3329
private array $excludeDirs,
34-
private LoggerInterface $logger,
35-
private ?CacheInterface $cache = null,
36-
private ?SchemaGeneratorInterface $schemaGenerator = null,
30+
private DiscovererInterface $discoverer,
3731
) {
3832
}
3933

4034
public function load(RegistryInterface $registry): void
4135
{
42-
// This now encapsulates the discovery process
43-
$discoverer = new Discoverer($this->logger, null, $this->schemaGenerator);
44-
45-
$cachedDiscoverer = $this->cache
46-
? new CachedDiscoverer($discoverer, $this->cache, $this->logger)
47-
: $discoverer;
48-
49-
$discoveryState = $cachedDiscoverer->discover($this->basePath, $this->scanDirs, $this->excludeDirs);
36+
$discoveryState = $this->discoverer->discover($this->basePath, $this->scanDirs, $this->excludeDirs);
5037

5138
$registry->setDiscoveryState($discoveryState);
5239
}

src/Server/Builder.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Mcp\Server;
1313

14+
use Mcp\Capability\Discovery\DiscovererInterface;
1415
use Mcp\Capability\Discovery\SchemaGeneratorInterface;
1516
use Mcp\Capability\Registry;
1617
use Mcp\Capability\Registry\Container;
@@ -61,6 +62,8 @@ final class Builder
6162

6263
private ?SchemaGeneratorInterface $schemaGenerator = null;
6364

65+
private ?DiscovererInterface $discoverer = null;
66+
6467
private ?SessionFactoryInterface $sessionFactory = null;
6568

6669
private ?SessionStoreInterface $sessionStore = null;
@@ -296,6 +299,13 @@ public function setSchemaGenerator(SchemaGeneratorInterface $schemaGenerator): s
296299
return $this;
297300
}
298301

302+
public function setDiscoverer(DiscovererInterface $discoverer): self
303+
{
304+
$this->discoverer = $discoverer;
305+
306+
return $this;
307+
}
308+
299309
public function setSession(
300310
SessionStoreInterface $sessionStore,
301311
SessionFactoryInterface $sessionFactory = new SessionFactory(),
@@ -480,7 +490,8 @@ public function build(): Server
480490
];
481491

482492
if (null !== $this->discoveryBasePath) {
483-
$loaders[] = new DiscoveryLoader($this->discoveryBasePath, $this->discoveryScanDirs, $this->discoveryExcludeDirs, $logger, $this->discoveryCache, $this->schemaGenerator);
493+
$discoverer = $this->discoverer ?? $this->createDiscoverer($logger);
494+
$loaders[] = new DiscoveryLoader($this->discoveryBasePath, $this->discoveryScanDirs, $this->discoveryExcludeDirs, $discoverer);
484495
}
485496

486497
foreach ($loaders as $loader) {
@@ -537,4 +548,15 @@ public function build(): Server
537548

538549
return new Server($protocol, $logger);
539550
}
551+
552+
private function createDiscoverer(LoggerInterface $logger): DiscovererInterface
553+
{
554+
$discoverer = new \Mcp\Capability\Discovery\Discoverer($logger, null, $this->schemaGenerator);
555+
556+
if (null !== $this->discoveryCache) {
557+
return new \Mcp\Capability\Discovery\CachedDiscoverer($discoverer, $this->discoveryCache, $logger);
558+
}
559+
560+
return $discoverer;
561+
}
540562
}

0 commit comments

Comments
 (0)