Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 6662d2f

Browse files
committed
feat: add Prompt/Resource/Tool Chain
1 parent 6925f61 commit 6662d2f

18 files changed

+79
-56
lines changed

examples/cli/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
require __DIR__.'/vendor/autoload.php';
44

5-
use Symfony\Component\Console\Output\OutputInterface;
65
use Symfony\Component\Console as SymfonyConsole;
6+
use Symfony\Component\Console\Output\OutputInterface;
77

88
$debug = (bool) ($_SERVER['DEBUG'] ?? false);
99

examples/cli/src/Builder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace App;
44

5-
use App\Manager\PromptManager;
6-
use App\Manager\ResourceManager;
7-
use App\Manager\ToolManager;
5+
use PhpLlm\McpSdk\Capability\PromptChain;
6+
use PhpLlm\McpSdk\Capability\ResourceChain;
7+
use PhpLlm\McpSdk\Capability\ToolChain;
88
use PhpLlm\McpSdk\Server\NotificationHandler;
99
use PhpLlm\McpSdk\Server\NotificationHandler\InitializedHandler;
1010
use PhpLlm\McpSdk\Server\RequestHandler;
@@ -24,15 +24,15 @@ class Builder
2424
*/
2525
public static function buildRequestHandlers(): array
2626
{
27-
$promptManager = new PromptManager([
27+
$promptManager = new PromptChain([
2828
new ExamplePrompt(),
2929
]);
3030

31-
$resourceManager = new ResourceManager([
31+
$resourceManager = new ResourceChain([
3232
new ExampleResource(),
3333
]);
3434

35-
$toolManager = new ToolManager([
35+
$toolManager = new ToolChain([
3636
new ExampleTool(),
3737
]);
3838

examples/cli/src/ExamplePrompt.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
use PhpLlm\McpSdk\Capability\Prompt\PromptGet;
77
use PhpLlm\McpSdk\Capability\Prompt\PromptGetResult;
88
use PhpLlm\McpSdk\Capability\Prompt\PromptGetResultMessages;
9+
use PhpLlm\McpSdk\Capability\Prompt\PromptGetterInterface;
910

10-
class ExamplePrompt implements MetadataInterface
11+
class ExamplePrompt implements MetadataInterface, PromptGetterInterface
1112
{
12-
public function __invoke(PromptGet $request): PromptGetResult
13+
public function get(PromptGet $input): PromptGetResult
1314
{
14-
$firstName = $request->arguments['first name'] ?? null;
15+
$firstName = $input->arguments['first name'] ?? null;
1516

1617
return new PromptGetResult(
1718
$this->getDescription(),

examples/cli/src/ExampleResource.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
use PhpLlm\McpSdk\Capability\Resource\MetadataInterface;
66
use PhpLlm\McpSdk\Capability\Resource\ResourceRead;
7+
use PhpLlm\McpSdk\Capability\Resource\ResourceReaderInterface;
78
use PhpLlm\McpSdk\Capability\Resource\ResourceReadResult;
89

9-
class ExampleResource implements MetadataInterface
10+
class ExampleResource implements MetadataInterface, ResourceReaderInterface
1011
{
11-
public function __invoke(ResourceRead $request): ResourceReadResult
12+
public function read(ResourceRead $input): ResourceReadResult
1213
{
1314
return new ResourceReadResult(
1415
'Content of '.$this->getName(),

examples/cli/src/ExampleTool.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
use PhpLlm\McpSdk\Capability\Tool\MetadataInterface;
66
use PhpLlm\McpSdk\Capability\Tool\ToolCall;
77
use PhpLlm\McpSdk\Capability\Tool\ToolCallResult;
8+
use PhpLlm\McpSdk\Capability\Tool\ToolExecutorInterface;
89

9-
class ExampleTool implements MetadataInterface
10+
class ExampleTool implements MetadataInterface, ToolExecutorInterface
1011
{
11-
public function __invoke(ToolCall $toolCall): ToolCallResult
12+
public function call(ToolCall $input): ToolCallResult
1213
{
13-
$format = $toolCall->arguments['format'] ?? 'Y-m-d H:i:s';
14+
$format = $input->arguments['format'] ?? 'Y-m-d H:i:s';
1415

1516
return new ToolCallResult(
1617
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpLlm\McpSdk\Capability\Prompt;
4+
5+
interface IdentifierInterface
6+
{
7+
public function getName(): string;
8+
}

src/Capability/Prompt/MetadataInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
namespace PhpLlm\McpSdk\Capability\Prompt;
66

7-
interface MetadataInterface
7+
interface MetadataInterface extends IdentifierInterface
88
{
9-
public function getName(): string;
10-
119
public function getDescription(): ?string;
1210

1311
/**

src/Capability/Prompt/PromptGetterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ interface PromptGetterInterface
1111
* @throws PromptGetException if the prompt execution fails
1212
* @throws PromptNotFoundException if the prompt is not found
1313
*/
14-
public function get(PromptGet $request): PromptGetResult;
14+
public function get(PromptGet $input): PromptGetResult;
1515
}
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
11
<?php
22

3-
namespace App\Manager;
3+
namespace PhpLlm\McpSdk\Capability;
44

55
use PhpLlm\McpSdk\Capability\Prompt\CollectionInterface;
6+
use PhpLlm\McpSdk\Capability\Prompt\IdentifierInterface;
67
use PhpLlm\McpSdk\Capability\Prompt\MetadataInterface;
78
use PhpLlm\McpSdk\Capability\Prompt\PromptGet;
89
use PhpLlm\McpSdk\Capability\Prompt\PromptGetResult;
910
use PhpLlm\McpSdk\Capability\Prompt\PromptGetterInterface;
1011
use PhpLlm\McpSdk\Exception\PromptGetException;
1112
use PhpLlm\McpSdk\Exception\PromptNotFoundException;
1213

13-
class PromptManager implements PromptGetterInterface, CollectionInterface
14+
class PromptChain implements PromptGetterInterface, CollectionInterface
1415
{
1516
public function __construct(
1617
/**
17-
* @var (MetadataInterface | callable(PromptGet):PromptGetResult)[]
18+
* @var (IdentifierInterface & (MetadataInterface | PromptGetterInterface))[]
1819
*/
1920
private array $items,
2021
) {
2122
}
2223

2324
public function getMetadata(): array
2425
{
25-
return $this->items;
26+
return array_filter($this->items, fn ($item) => $item instanceof MetadataInterface);
2627
}
2728

28-
public function get(PromptGet $request): PromptGetResult
29+
public function get(PromptGet $input): PromptGetResult
2930
{
3031
foreach ($this->items as $item) {
31-
if ($request->name === $item->getName()) {
32+
if ($item instanceof PromptGetterInterface && $input->name === $item->getName()) {
3233
try {
33-
return $item($request);
34+
return $item->get($input);
3435
} catch (\Throwable $e) {
35-
throw new PromptGetException($request, $e);
36+
throw new PromptGetException($input, $e);
3637
}
3738
}
3839
}
3940

40-
throw new PromptNotFoundException($request);
41+
throw new PromptNotFoundException($input);
4142
}
4243
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace PhpLlm\McpSdk\Capability\Resource;
4+
5+
interface IdentifierInterface
6+
{
7+
public function getUri(): string;
8+
}

0 commit comments

Comments
 (0)