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

Commit ddce26c

Browse files
committed
feat: make sure we have a working demo
1 parent 562e534 commit ddce26c

16 files changed

+58
-25
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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"}}
3+
4+
{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}
5+
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "Current time"}}
6+
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "Current time","arguments": {"format": "Y-m-d",
7+
8+
{"jsonrpc": "2.0", "id": 1, "method": "prompts/list"}
9+
{"jsonrpc": "2.0", "id": 2 ,"method": "prompts/get", "params": {"name": "Greet"}}
10+
{"jsonrpc": "2.0", "id": 2 ,"method": "prompts/get", "params": {"name": "Greet", "arguments": { "firstName": "Tobias" }}}

examples/cli/foo.json

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/cli/src/ExamplePrompt.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
class ExamplePrompt implements MetadataInterface
88
{
9+
public function __invoke(?string $firstName = null)
10+
{
11+
return sprintf('Hello %s', $firstName ?? 'World');
12+
}
13+
914
public function getName(): string
1015
{
1116
return 'Greet';
@@ -20,10 +25,10 @@ public function getArguments(): array
2025
{
2126
return [
2227
[
23-
'name' => 'first name',
28+
'name' => 'firstName',
2429
'description' => 'The name of the person to greet',
2530
'required' => false,
2631
],
2732
];
2833
}
29-
}
34+
}

examples/cli/src/ExampleResource.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,4 @@ public function getSize(): ?int
3030
{
3131
return null;
3232
}
33-
34-
}
33+
}

examples/cli/src/ExampleTool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ public function getInputSchema(): array
3535
'required' => [],
3636
];
3737
}
38-
}
38+
}

examples/cli/src/PromptManager.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
use PhpLlm\McpSdk\Capability\Prompt\CollectionInterface;
66
use PhpLlm\McpSdk\Capability\Prompt\PromptGet;
77
use PhpLlm\McpSdk\Capability\Prompt\PromptGetterInterface;
8+
use PhpLlm\McpSdk\Exception\PromptGetException;
9+
use PhpLlm\McpSdk\Exception\PromptNotFoundException;
810

911
class PromptManager implements PromptGetterInterface, CollectionInterface
1012
{
1113
private array $items;
14+
1215
public function __construct(
13-
){
16+
) {
1417
$this->items = [
1518
new ExamplePrompt(),
1619
];
@@ -21,9 +24,18 @@ public function getMetadata(): array
2124
return $this->items;
2225
}
2326

24-
2527
public function get(PromptGet $request): mixed
2628
{
27-
return 'foo';
29+
foreach ($this->items as $item) {
30+
if ($request->name === $item->getName()) {
31+
try {
32+
return $item->__invoke(...$request->arguments);
33+
} catch (\Throwable $e) {
34+
throw PromptGetException::executionFailed($request, $e);
35+
}
36+
}
37+
}
38+
39+
throw PromptNotFoundException::create($request);
2840
}
2941
}

examples/cli/src/ResourceManager.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
class ResourceManager implements CollectionInterface, ResourceReaderInterface
1111
{
1212
private array $items;
13+
1314
public function __construct(
14-
){
15+
) {
1516
$this->items = [
1617
new ExampleResource(),
1718
];
@@ -28,7 +29,7 @@ public function read(ResourceRead $request): mixed
2829
if ($request->uri === $resource->getUri()) {
2930
// In a real implementation, you would read the resource from its URI.
3031
// Here we just return a dummy string for demonstration purposes.
31-
return 'Content of ' . $resource->getName();
32+
return 'Content of '.$resource->getName();
3233
}
3334
}
3435

examples/cli/src/ToolManager.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
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\ToolExecutionException;
89
use PhpLlm\McpSdk\Exception\ToolNotFoundException;
910

1011
class ToolManager implements ToolExecutorInterface, CollectionInterface
@@ -27,7 +28,11 @@ public function execute(ToolCall $toolCall): mixed
2728
{
2829
foreach ($this->items as $tool) {
2930
if ($toolCall->name === $tool->getName()) {
30-
return $tool->__invoke(...$toolCall->arguments);
31+
try {
32+
return $tool->__invoke(...$toolCall->arguments);
33+
} catch (\Throwable $e) {
34+
throw ToolExecutionException::executionFailed($toolCall, $e);
35+
}
3136
}
3237
}
3338

src/Capability/Tool/ToolExecutorInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
interface ToolExecutorInterface
99
{
1010
/**
11-
* @throws ToolExecutionException if the tool execution fails
12-
* @throws ToolNotFoundExceptionInterface if the tool is not found
11+
* @throws ToolExecutionException if the tool execution fails
12+
* @throws ToolNotFoundException if the tool is not found
1313
*/
1414
public function execute(ToolCall $toolCall): mixed;
1515
}

src/Exception/ExceptionInterface.php

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

55
namespace PhpLlm\McpSdk\Exception;
66

7-
use PhpLlm\LlmChain\Exception\ExceptionInterface as BaseExceptionInterface;
8-
9-
interface ExceptionInterface extends BaseExceptionInterface
7+
interface ExceptionInterface
108
{
119
}

0 commit comments

Comments
 (0)