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

Commit 6925f61

Browse files
committed
cleanup
1 parent 296bf75 commit 6925f61

File tree

8 files changed

+76
-60
lines changed

8 files changed

+76
-60
lines changed

examples/cli/index.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
require __DIR__.'/vendor/autoload.php';
44

55
use Symfony\Component\Console\Output\OutputInterface;
6+
use Symfony\Component\Console as SymfonyConsole;
67

78
$debug = (bool) ($_SERVER['DEBUG'] ?? false);
89

910
// Setup input, output and logger
10-
$input = new Symfony\Component\Console\Input\ArgvInput($argv);
11-
$output = new Symfony\Component\Console\Output\ConsoleOutput($debug ? OutputInterface::VERBOSITY_VERY_VERBOSE : OutputInterface::VERBOSITY_NORMAL);
12-
$logger = new Symfony\Component\Console\Logger\ConsoleLogger($output);
11+
$input = new SymfonyConsole\Input\ArgvInput($argv);
12+
$output = new SymfonyConsole\Output\ConsoleOutput($debug ? OutputInterface::VERBOSITY_VERY_VERBOSE : OutputInterface::VERBOSITY_NORMAL);
13+
$logger = new SymfonyConsole\Logger\ConsoleLogger($output);
1314

14-
// Configure the JsonRpcHandler
15+
// Configure the JsonRpcHandler and build the functionality
1516
$jsonRpcHandler = new PhpLlm\McpSdk\Server\JsonRpcHandler(
1617
new PhpLlm\McpSdk\Message\Factory(),
1718
App\Builder::buildRequestHandlers(),

examples/cli/src/Builder.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ class Builder
2424
*/
2525
public static function buildRequestHandlers(): array
2626
{
27-
$promptManager = new PromptManager();
28-
$resourceManager = new ResourceManager();
29-
$toolManager = new ToolManager();
27+
$promptManager = new PromptManager([
28+
new ExamplePrompt(),
29+
]);
30+
31+
$resourceManager = new ResourceManager([
32+
new ExampleResource(),
33+
]);
34+
35+
$toolManager = new ToolManager([
36+
new ExampleTool(),
37+
]);
3038

3139
return [
3240
new InitializeHandler(),

examples/cli/src/ExamplePrompt.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@
33
namespace App;
44

55
use PhpLlm\McpSdk\Capability\Prompt\MetadataInterface;
6+
use PhpLlm\McpSdk\Capability\Prompt\PromptGet;
7+
use PhpLlm\McpSdk\Capability\Prompt\PromptGetResult;
8+
use PhpLlm\McpSdk\Capability\Prompt\PromptGetResultMessages;
69

710
class ExamplePrompt implements MetadataInterface
811
{
9-
public function __invoke(?string $firstName = null): string
12+
public function __invoke(PromptGet $request): PromptGetResult
1013
{
11-
return sprintf('Hello %s', $firstName ?? 'World');
14+
$firstName = $request->arguments['first name'] ?? null;
15+
16+
return new PromptGetResult(
17+
$this->getDescription(),
18+
[new PromptGetResultMessages(
19+
'user',
20+
sprintf('Hello %s', $firstName ?? 'World')
21+
)]
22+
);
1223
}
1324

1425
public function getName(): string
@@ -25,7 +36,7 @@ public function getArguments(): array
2536
{
2637
return [
2738
[
28-
'name' => 'firstName',
39+
'name' => 'first name',
2940
'description' => 'The name of the person to greet',
3041
'required' => false,
3142
],

examples/cli/src/ExampleResource.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,19 @@
33
namespace App;
44

55
use PhpLlm\McpSdk\Capability\Resource\MetadataInterface;
6+
use PhpLlm\McpSdk\Capability\Resource\ResourceRead;
7+
use PhpLlm\McpSdk\Capability\Resource\ResourceReadResult;
68

79
class ExampleResource implements MetadataInterface
810
{
11+
public function __invoke(ResourceRead $request): ResourceReadResult
12+
{
13+
return new ResourceReadResult(
14+
'Content of '.$this->getName(),
15+
$this->getUri(),
16+
);
17+
}
18+
919
public function getUri(): string
1020
{
1121
return 'file:///project/src/main.rs';

examples/cli/src/ExampleTool.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
namespace App;
44

55
use PhpLlm\McpSdk\Capability\Tool\MetadataInterface;
6+
use PhpLlm\McpSdk\Capability\Tool\ToolCall;
7+
use PhpLlm\McpSdk\Capability\Tool\ToolCallResult;
68

79
class ExampleTool implements MetadataInterface
810
{
9-
public function __invoke(string $format = 'Y-m-d H:i:s'): string
11+
public function __invoke(ToolCall $toolCall): ToolCallResult
1012
{
11-
return (new \DateTime('now', new \DateTimeZone('UTC')))->format($format);
13+
$format = $toolCall->arguments['format'] ?? 'Y-m-d H:i:s';
14+
15+
return new ToolCallResult(
16+
(new \DateTime('now', new \DateTimeZone('UTC')))->format($format)
17+
);
1218
}
1319

1420
public function getName(): string

examples/cli/src/Manager/PromptManager.php

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

33
namespace App\Manager;
44

5-
use App\ExamplePrompt;
65
use PhpLlm\McpSdk\Capability\Prompt\CollectionInterface;
6+
use PhpLlm\McpSdk\Capability\Prompt\MetadataInterface;
77
use PhpLlm\McpSdk\Capability\Prompt\PromptGet;
88
use PhpLlm\McpSdk\Capability\Prompt\PromptGetResult;
9-
use PhpLlm\McpSdk\Capability\Prompt\PromptGetResultMessages;
109
use PhpLlm\McpSdk\Capability\Prompt\PromptGetterInterface;
1110
use PhpLlm\McpSdk\Exception\PromptGetException;
1211
use PhpLlm\McpSdk\Exception\PromptNotFoundException;
1312

1413
class PromptManager implements PromptGetterInterface, CollectionInterface
1514
{
16-
/**
17-
* @var mixed[]
18-
*/
19-
private array $items;
20-
2115
public function __construct(
16+
/**
17+
* @var (MetadataInterface | callable(PromptGet):PromptGetResult)[]
18+
*/
19+
private array $items,
2220
) {
23-
$this->items = [
24-
new ExamplePrompt(),
25-
];
2621
}
2722

2823
public function getMetadata(): array
@@ -35,13 +30,7 @@ public function get(PromptGet $request): PromptGetResult
3530
foreach ($this->items as $item) {
3631
if ($request->name === $item->getName()) {
3732
try {
38-
return new PromptGetResult(
39-
$item->getDescription(),
40-
[new PromptGetResultMessages(
41-
'user',
42-
$item->__invoke(...$request->arguments),
43-
)]
44-
);
33+
return $item($request);
4534
} catch (\Throwable $e) {
4635
throw new PromptGetException($request, $e);
4736
}

examples/cli/src/Manager/ResourceManager.php

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@
22

33
namespace App\Manager;
44

5-
use App\ExampleResource;
65
use PhpLlm\McpSdk\Capability\Resource\CollectionInterface;
6+
use PhpLlm\McpSdk\Capability\Resource\MetadataInterface;
77
use PhpLlm\McpSdk\Capability\Resource\ResourceRead;
88
use PhpLlm\McpSdk\Capability\Resource\ResourceReaderInterface;
99
use PhpLlm\McpSdk\Capability\Resource\ResourceReadResult;
1010
use PhpLlm\McpSdk\Exception\ResourceNotFoundException;
11+
use PhpLlm\McpSdk\Exception\ResourceReadException;
1112

1213
class ResourceManager implements CollectionInterface, ResourceReaderInterface
1314
{
14-
/**
15-
* @var mixed[]
16-
*/
17-
private array $items;
18-
1915
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,
2021
) {
21-
$this->items = [
22-
new ExampleResource(),
23-
];
2422
}
2523

2624
public function getMetadata(): array
@@ -30,14 +28,13 @@ public function getMetadata(): array
3028

3129
public function read(ResourceRead $request): ResourceReadResult
3230
{
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+
}
4138
}
4239
}
4340

examples/cli/src/Manager/ToolManager.php

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

33
namespace App\Manager;
44

5-
use App\ExampleTool;
65
use PhpLlm\McpSdk\Capability\Tool\CollectionInterface;
6+
use PhpLlm\McpSdk\Capability\Tool\MetadataInterface;
77
use PhpLlm\McpSdk\Capability\Tool\ToolCall;
88
use PhpLlm\McpSdk\Capability\Tool\ToolCallResult;
99
use PhpLlm\McpSdk\Capability\Tool\ToolExecutorInterface;
@@ -12,16 +12,12 @@
1212

1313
class ToolManager implements ToolExecutorInterface, CollectionInterface
1414
{
15-
/**
16-
* @var mixed[]
17-
*/
18-
private array $items;
19-
2015
public function __construct(
16+
/**
17+
* @var (MetadataInterface | callable(ToolCall):ToolCallResult)[] $items
18+
*/
19+
private array $items,
2120
) {
22-
$this->items = [
23-
new ExampleTool(),
24-
];
2521
}
2622

2723
public function getMetadata(): array
@@ -31,12 +27,10 @@ public function getMetadata(): array
3127

3228
public function execute(ToolCall $toolCall): ToolCallResult
3329
{
34-
foreach ($this->items as $tool) {
35-
if ($toolCall->name === $tool->getName()) {
30+
foreach ($this->items as $item) {
31+
if ($toolCall->name === $item->getName()) {
3632
try {
37-
return new ToolCallResult(
38-
$tool->__invoke(...$toolCall->arguments),
39-
);
33+
return $item($toolCall);
4034
} catch (\Throwable $e) {
4135
throw new ToolExecutionException($toolCall, $e);
4236
}

0 commit comments

Comments
 (0)