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

Commit ed0854a

Browse files
committed
feat: add result classes
1 parent 4fbc7e7 commit ed0854a

File tree

11 files changed

+117
-18
lines changed

11 files changed

+117
-18
lines changed

examples/cli/src/Manager/PromptManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public function get(PromptGet $request): mixed
3737
}
3838
}
3939

40-
throw PromptNotFoundException::create($request);
40+
throw new PromptNotFoundException($request);
4141
}
4242
}

examples/cli/src/Manager/ResourceManager.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@ public function read(ResourceRead $request): ResourceReadResult
3232
// In a real implementation, you would read the resource from its URI.
3333
// Here we just return a dummy string for demonstration purposes.
3434
return new ResourceReadResult(
35-
$request->id,
3635
'Content of '.$resource->getName(),
3736
$resource->getUri(),
3837
);
3938
}
4039
}
4140

42-
throw new ResourceNotFoundException();
41+
throw new ResourceNotFoundException($request);
4342
}
4443
}

examples/cli/src/Manager/ToolManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public function execute(ToolCall $toolCall): mixed
3737
}
3838
}
3939

40-
throw ToolNotFoundException::create($toolCall);
40+
throw new ToolNotFoundException($toolCall);
4141
}
4242
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\McpSdk\Capability\Prompt;
6+
7+
final readonly class PromptGetResult
8+
{
9+
/**
10+
* @param list<PromptGetResultMessages> $messages
11+
*/
12+
public function __construct(
13+
public string $description,
14+
public array $messages = [],
15+
) {
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\McpSdk\Capability\Prompt;
6+
7+
final readonly class PromptGetResultMessages
8+
{
9+
public function __construct(
10+
public string $role,
11+
public string $result,
12+
public string $mimeType,
13+
/**
14+
* @var "text"|"image"|"audio"|"resource"
15+
*/
16+
public string $type = 'text',
17+
public ?string $uri = null,
18+
) {
19+
}
20+
}

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): mixed;
14+
public function get(PromptGet $request): PromptGetResult;
1515
}

src/Capability/Resource/ResourceReadResult.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
final readonly class ResourceReadResult
88
{
99
public function __construct(
10-
public string $id,
1110
public string $result,
1211
public string $uri,
1312

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\McpSdk\Capability\Tool;
6+
7+
final readonly class ToolCallResult
8+
{
9+
public function __construct(
10+
public string $result,
11+
public string $mimeType,
12+
/**
13+
* @var "text"|"image"|"audio"|"resource"
14+
*/
15+
public string $type = 'text',
16+
public bool $isError = false,
17+
public ?string $uri = null,
18+
) {
19+
}
20+
}

src/Capability/Tool/ToolExecutorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ interface ToolExecutorInterface
1111
* @throws ToolExecutionException if the tool execution fails
1212
* @throws ToolNotFoundException if the tool is not found
1313
*/
14-
public function execute(ToolCall $toolCall): mixed;
14+
public function execute(ToolCall $toolCall): ToolCallResult;
1515
}

src/Server/RequestHandler/PromptGetHandler.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,39 @@ public function createResponse(Request $message): Response|Error
2929
return Error::internalError($message->id, 'Error while handling prompt');
3030
}
3131

32+
$messages = [];
33+
foreach ($result->messages as $resultMessage) {
34+
$content = match ($result->type) {
35+
'text' => [
36+
'type' => 'text',
37+
'text' => $result->result,
38+
],
39+
'image', 'audio' => [
40+
'type' => $result->type,
41+
'data' => $result->result,
42+
'mimeType' => $result->mimeType,
43+
],
44+
'resource' => [
45+
'type' => 'resource',
46+
'resource' => [
47+
'uri' => $result->uri,
48+
'mimeType' => $result->mimeType,
49+
'text' => $result->result,
50+
],
51+
],
52+
// TODO better exception
53+
default => throw new \InvalidArgumentException('Unsupported PromptGet result type: ' . $result->type),
54+
};
55+
56+
$messages[] = [
57+
'role' => $resultMessage->role,
58+
'content' => $content,
59+
];
60+
}
61+
3262
return new Response($message->id, [
33-
'content' => [
34-
['type' => 'text', 'text' => $result],
35-
],
63+
'description' => $result->description,
64+
'messages' => $messages,
3665
]);
3766
}
3867

0 commit comments

Comments
 (0)