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

Commit 562e534

Browse files
committed
feat: add more examples
1 parent 48fdefd commit 562e534

File tree

12 files changed

+165
-11
lines changed

12 files changed

+165
-11
lines changed

examples/cli/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
}
2222
}
2323
}
24+

examples/cli/foo.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"jsonrpc": "2.0", "id": 1, "method": "resources/list", "params": []}
2+
{"jsonrpc": "2.0", "id": 2, "method": "resources/read", "params": {"uri": "file:///project/src/main.rs"}}

examples/cli/index.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

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

5+
use Symfony\Component\Console\Output\OutputInterface;
6+
57
// Setup input, output and logger
68
$input = new Symfony\Component\Console\Input\ArgvInput($argv);
7-
$output = new Symfony\Component\Console\Output\ConsoleOutput();
9+
$output = new Symfony\Component\Console\Output\ConsoleOutput(OutputInterface::VERBOSITY_VERY_VERBOSE);
810
$logger = new Symfony\Component\Console\Logger\ConsoleLogger($output);
911

1012
// Configure the JsonRpcHandler

examples/cli/src/ExamplePrompt.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use PhpLlm\McpSdk\Capability\Prompt\MetadataInterface;
6+
7+
class ExamplePrompt implements MetadataInterface
8+
{
9+
public function getName(): string
10+
{
11+
return 'Greet';
12+
}
13+
14+
public function getDescription(): ?string
15+
{
16+
return 'Greet a person with a nice message';
17+
}
18+
19+
public function getArguments(): array
20+
{
21+
return [
22+
[
23+
'name' => 'first name',
24+
'description' => 'The name of the person to greet',
25+
'required' => false,
26+
],
27+
];
28+
}
29+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use PhpLlm\McpSdk\Capability\Resource\MetadataInterface;
6+
7+
class ExampleResource implements MetadataInterface
8+
{
9+
public function getUri(): string
10+
{
11+
return 'file:///project/src/main.rs';
12+
}
13+
14+
public function getName(): string
15+
{
16+
return 'My resource';
17+
}
18+
19+
public function getDescription(): ?string
20+
{
21+
return 'This is just an example';
22+
}
23+
24+
public function getMimeType(): ?string
25+
{
26+
return null;
27+
}
28+
29+
public function getSize(): ?int
30+
{
31+
return null;
32+
}
33+
34+
}

examples/cli/src/ExampleTool.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use PhpLlm\McpSdk\Capability\Tool\MetadataInterface;
6+
7+
class ExampleTool implements MetadataInterface
8+
{
9+
public function __invoke(string $format = 'Y-m-d H:i:s')
10+
{
11+
return (new \DateTime('now', new \DateTimeZone('UTC')))->format($format);
12+
}
13+
14+
public function getName(): string
15+
{
16+
return 'Current time';
17+
}
18+
19+
public function getDescription(): string
20+
{
21+
return 'Returns the current time in UTC';
22+
}
23+
24+
public function getInputSchema(): array
25+
{
26+
return [
27+
'type' => 'object',
28+
'properties' => [
29+
'format' => [
30+
'type' => 'string',
31+
'description' => 'The format of the time, e.g. "Y-m-d H:i:s"',
32+
'default' => 'Y-m-d H:i:s',
33+
],
34+
],
35+
'required' => [],
36+
];
37+
}
38+
}

examples/cli/src/PromptManager.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,20 @@
88

99
class PromptManager implements PromptGetterInterface, CollectionInterface
1010
{
11+
private array $items;
12+
public function __construct(
13+
){
14+
$this->items = [
15+
new ExamplePrompt(),
16+
];
17+
}
18+
1119
public function getMetadata(): array
1220
{
13-
return [];
21+
return $this->items;
1422
}
1523

24+
1625
public function get(PromptGet $request): mixed
1726
{
1827
return 'foo';

examples/cli/src/ResourceManager.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,33 @@
55
use PhpLlm\McpSdk\Capability\Resource\CollectionInterface;
66
use PhpLlm\McpSdk\Capability\Resource\ResourceRead;
77
use PhpLlm\McpSdk\Capability\Resource\ResourceReaderInterface;
8+
use PhpLlm\McpSdk\Exception\ResourceNotFoundException;
89

910
class ResourceManager implements CollectionInterface, ResourceReaderInterface
1011
{
12+
private array $items;
13+
public function __construct(
14+
){
15+
$this->items = [
16+
new ExampleResource(),
17+
];
18+
}
19+
1120
public function getMetadata(): array
1221
{
13-
return [];
22+
return $this->items;
1423
}
1524

1625
public function read(ResourceRead $request): mixed
1726
{
18-
return 'foo';
27+
foreach ($this->items as $resource) {
28+
if ($request->uri === $resource->getUri()) {
29+
// In a real implementation, you would read the resource from its URI.
30+
// Here we just return a dummy string for demonstration purposes.
31+
return 'Content of ' . $resource->getName();
32+
}
33+
}
34+
35+
throw new ResourceNotFoundException();
1936
}
2037
}

examples/cli/src/ToolManager.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,32 @@
55
use PhpLlm\McpSdk\Capability\Tool\CollectionInterface;
66
use PhpLlm\McpSdk\Capability\Tool\ToolCall;
77
use PhpLlm\McpSdk\Capability\Tool\ToolExecutorInterface;
8+
use PhpLlm\McpSdk\Exception\ToolNotFoundException;
89

910
class ToolManager implements ToolExecutorInterface, CollectionInterface
1011
{
12+
private array $items;
13+
14+
public function __construct(
15+
) {
16+
$this->items = [
17+
new ExampleTool(),
18+
];
19+
}
20+
1121
public function getMetadata(): array
1222
{
13-
return [];
23+
return $this->items;
1424
}
1525

1626
public function execute(ToolCall $toolCall): mixed
1727
{
18-
return 'bar';
28+
foreach ($this->items as $tool) {
29+
if ($toolCall->name === $tool->getName()) {
30+
return $tool->__invoke(...$toolCall->arguments);
31+
}
32+
}
33+
34+
throw ToolNotFoundException::create($toolCall);
1935
}
2036
}

src/Capability/Tool/ToolExecutorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace PhpLlm\McpSdk\Capability\Tool;
44

55
use PhpLlm\McpSdk\Exception\ToolExecutionException;
6-
use PhpLlm\McpSdk\Exception\ToolNotFoundExceptionInterface;
6+
use PhpLlm\McpSdk\Exception\ToolNotFoundException;
77

88
interface ToolExecutorInterface
99
{

0 commit comments

Comments
 (0)