2
2
3
3
namespace App \Manager ;
4
4
5
- use App \ExampleResource ;
6
5
use PhpLlm \McpSdk \Capability \Resource \CollectionInterface ;
6
+ use PhpLlm \McpSdk \Capability \Resource \MetadataInterface ;
7
7
use PhpLlm \McpSdk \Capability \Resource \ResourceRead ;
8
8
use PhpLlm \McpSdk \Capability \Resource \ResourceReaderInterface ;
9
9
use PhpLlm \McpSdk \Capability \Resource \ResourceReadResult ;
10
10
use PhpLlm \McpSdk \Exception \ResourceNotFoundException ;
11
+ use PhpLlm \McpSdk \Exception \ResourceReadException ;
11
12
12
13
class ResourceManager implements CollectionInterface, ResourceReaderInterface
13
14
{
14
- /**
15
- * @var mixed[]
16
- */
17
- private array $ items ;
18
-
19
15
public function __construct (
16
+ /**
17
+ * TODO this is bad design. What if we want to add resource/exists, then this becomes invalid and we need a BC break
18
+ * @var (MetadataInterface | callable(ResourceRead):ResourceReadResult)[]
19
+ */
20
+ private array $ items ,
20
21
) {
21
- $ this ->items = [
22
- new ExampleResource (),
23
- ];
24
22
}
25
23
26
24
public function getMetadata (): array
@@ -30,14 +28,13 @@ public function getMetadata(): array
30
28
31
29
public function read (ResourceRead $ request ): ResourceReadResult
32
30
{
33
- foreach ($ this ->items as $ resource ) {
34
- if ($ request ->uri === $ resource ->getUri ()) {
35
- // In a real implementation, you would read the resource from its URI.
36
- // Here we just return a dummy string for demonstration purposes.
37
- return new ResourceReadResult (
38
- 'Content of ' .$ resource ->getName (),
39
- $ resource ->getUri (),
40
- );
31
+ foreach ($ this ->items as $ item ) {
32
+ if ($ item instanceof ReadInterface && $ request ->uri === $ item ->getUri ()) {
33
+ try {
34
+ return $ item ($ request );
35
+ } catch (\Throwable $ e ) {
36
+ throw new ResourceReadException ($ request , $ e );
37
+ }
41
38
}
42
39
}
43
40
0 commit comments