refactor: integrate cache parameter into setDiscovery() method (#80)

* refactor: integrate cache parameter into setDiscovery() method

- Add cache as 4th parameter to setDiscovery()
- Rename $cache to $discoveryCache for clarity
- Remove separate setCache() method
- Update example to use new API

* docs: update discovery caching documentation to use new API

- Update examples to use cache parameter in setDiscovery() method
- Remove references to separate setCache() method calls
This commit is contained in:
xentixar 2025-09-25 20:26:11 +05:45 • committed by GitHub
commit 7d599f4f64
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 21 deletions

View file

@ -21,8 +21,7 @@ use Symfony\Component\Cache\Psr16Cache;
$server = Server::make()
->setServerInfo('My Server', '1.0.0')
->setDiscovery(__DIR__, ['.'])
->setCache(new Psr16Cache(new ArrayAdapter())) // Enable caching
->setDiscovery(__DIR__, ['.'], [], new Psr16Cache(new ArrayAdapter())) // Enable caching
->build();
```
@ -69,8 +68,7 @@ $cache = DoctrineProvider::wrap(new ArrayCache());
$cache = new Psr16Cache(new ArrayAdapter());
$server = Server::make()
->setDiscovery(__DIR__, ['.'])
->setCache($cache)
->setDiscovery(__DIR__, ['.'], [], $cache)
->build();
```
@ -81,8 +79,7 @@ $server = Server::make()
$cache = new Psr16Cache(new FilesystemAdapter('mcp-discovery', 0, '/var/cache'));
$server = Server::make()
->setDiscovery(__DIR__, ['.'])
->setCache($cache)
->setDiscovery(__DIR__, ['.'], [], $cache)
->build();
```

View file

@ -26,8 +26,7 @@ $server = Server::make()
->setServerInfo('Cached Discovery Calculator', '1.0.0', 'Calculator with cached discovery for better performance.')
->setContainer(container())
->setLogger(logger())
->setDiscovery(__DIR__, ['.'])
->setCache(new Psr16Cache(new ArrayAdapter()))
->setDiscovery(__DIR__, ['.'], [], new Psr16Cache(new ArrayAdapter()))
->build();
$transport = new StdioTransport(logger: logger());

View file

@ -58,7 +58,7 @@ final class ServerBuilder
private ?LoggerInterface $logger = null;
private ?CacheInterface $cache = null;
private ?CacheInterface $discoveryCache = null;
private ?ToolCallerInterface $toolCaller = null;
@ -220,20 +220,12 @@ final class ServerBuilder
string $basePath,
array $scanDirs = ['.', 'src'],
array $excludeDirs = [],
?CacheInterface $cache = null,
): self {
$this->discoveryBasePath = $basePath;
$this->discoveryScanDirs = $scanDirs;
$this->discoveryExcludeDirs = $excludeDirs;
return $this;
}
/**
* Enables discovery caching with the provided cache implementation.
*/
public function setCache(CacheInterface $cache): self
{
$this->cache = $cache;
$this->discoveryCache = $cache;
return $this;
}
@ -323,8 +315,8 @@ final class ServerBuilder
if (null !== $this->discoveryBasePath) {
$discovery = new Discoverer($registry, $logger);
if (null !== $this->cache) {
$discovery = new CachedDiscoverer($discovery, $this->cache, $logger);
if (null !== $this->discoveryCache) {
$discovery = new CachedDiscoverer($discovery, $this->discoveryCache, $logger);
}
$discovery->discover($this->discoveryBasePath, $this->discoveryScanDirs, $this->discoveryExcludeDirs);