forked from modelcontextprotocol/php-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedDiscoverer.php
More file actions
99 lines (84 loc) · 2.94 KB
/
CachedDiscoverer.php
File metadata and controls
99 lines (84 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Capability\Discovery;
use Psr\Log\LoggerInterface;
use Psr\SimpleCache\CacheInterface;
/**
* Cached decorator for the Discoverer class.
*
* This decorator caches the results of file system operations and reflection
* to improve performance when discovery is called multiple times.
*
* @internal
*
* @author Xentixar <xentixar@gmail.com>
*/
final class CachedDiscoverer implements DiscovererInterface
{
private const CACHE_PREFIX = 'mcp_discovery_';
public function __construct(
private readonly DiscovererInterface $discoverer,
private readonly CacheInterface $cache,
private readonly LoggerInterface $logger,
) {
}
/**
* Discover MCP elements in the specified directories with caching.
*
* @param string $basePath the base path for resolving directories
* @param array<string> $directories list of directories (relative to base path) to scan
* @param array<string> $excludeDirs list of directories (relative to base path) to exclude from the scan
*/
public function discover(string $basePath, array $directories, array $excludeDirs = []): DiscoveryState
{
$cacheKey = $this->generateCacheKey($basePath, $directories, $excludeDirs);
$cachedResult = $this->cache->get($cacheKey);
if (null !== $cachedResult) {
$this->logger->debug('Using cached discovery results', [
'cache_key' => $cacheKey,
'base_path' => $basePath,
'directories' => $directories,
]);
return $cachedResult;
}
$this->logger->debug('Cache miss, performing fresh discovery', [
'cache_key' => $cacheKey,
'base_path' => $basePath,
'directories' => $directories,
]);
$discoveryState = $this->discoverer->discover($basePath, $directories, $excludeDirs);
$this->cache->set($cacheKey, $discoveryState);
return $discoveryState;
}
/**
* Generate a cache key based on discovery parameters.
*
* @param array<string> $directories
* @param array<string> $excludeDirs
*/
private function generateCacheKey(string $basePath, array $directories, array $excludeDirs): string
{
$keyData = [
'base_path' => $basePath,
'directories' => $directories,
'exclude_dirs' => $excludeDirs,
];
return self::CACHE_PREFIX.md5(serialize($keyData));
}
/**
* Clear the discovery cache.
* Useful for development or when files change.
*/
public function clearCache(): void
{
$this->cache->clear();
$this->logger->info('Discovery cache cleared');
}
}