Skip to content

Commit 2e754bf

Browse files
authored
refactor: tool var names and exceptions (#20)
* refactor: tool var names and exceptions * fix: phpstan and psr/cache dependency
1 parent 158beba commit 2e754bf

File tree

7 files changed

+23
-33
lines changed

7 files changed

+23
-33
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
"phpstan/phpstan": "^2.1",
2020
"phpunit/phpunit": "^11.5",
2121
"symfony/console": "^6.4 || ^7.0",
22-
"rector/rector": "^2.0"
22+
"rector/rector": "^2.0",
23+
"psr/cache": "^3.0"
2324
},
2425
"suggest": {
25-
"symfony/console": "To use SymfonyConsoleTransport for STDIO"
26+
"symfony/console": "To use SymfonyConsoleTransport for STDIO",
27+
"psr/cache": "To use CachePoolStore with SSE Transport"
2628
},
2729
"autoload": {
2830
"psr-4": {

src/Capability/Tool/MetadataInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,8 @@ public function getName(): string;
1010

1111
public function getDescription(): string;
1212

13+
/**
14+
* @return array<string, mixed>
15+
*/
1316
public function getInputSchema(): array;
1417
}

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
}

src/Exception/ToolExecutionException.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44

55
namespace PhpLlm\McpSdk\Exception;
66

7-
use PhpLlm\LlmChain\Model\Response\ToolCall;
7+
use PhpLlm\McpSdk\Capability\Tool\ToolCall;
88

99
final class ToolExecutionException extends \RuntimeException implements ExceptionInterface
1010
{
11-
public ?ToolCall $toolCall = null;
12-
13-
public static function executionFailed(ToolCall $toolCall, \Throwable $previous): self
14-
{
15-
$exception = new self(sprintf('Execution of tool "%s" failed with error: %s', $toolCall->name, $previous->getMessage()), previous: $previous);
16-
$exception->toolCall = $toolCall;
17-
18-
return $exception;
11+
public function __construct(
12+
public readonly ToolCall $toolCall,
13+
?\Throwable $previous = null,
14+
) {
15+
parent::__construct(sprintf('Execution of tool "%s" failed with error: %s', $toolCall->name, $previous->getMessage()), previous: $previous);
1916
}
2017
}

src/Exception/ToolNotFoundException.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,13 @@
44

55
namespace PhpLlm\McpSdk\Exception;
66

7-
use PhpLlm\LlmChain\Chain\Toolbox\ExecutionReference;
8-
use PhpLlm\LlmChain\Model\Response\ToolCall;
7+
use PhpLlm\McpSdk\Capability\Tool\ToolCall;
98

109
final class ToolNotFoundException extends \RuntimeException implements ExceptionInterface
1110
{
12-
public ?ToolCall $toolCall = null;
13-
14-
public static function notFoundForToolCall(ToolCall $toolCall): self
15-
{
16-
$exception = new self(sprintf('Tool not found for call: %s.', $toolCall->name));
17-
$exception->toolCall = $toolCall;
18-
19-
return $exception;
20-
}
21-
22-
public static function notFoundForReference(ExecutionReference $reference): self
23-
{
24-
return new self(sprintf('Tool not found for reference: %s::%s.', $reference->class, $reference->method));
11+
public function __construct(
12+
public readonly ToolCall $toolCall,
13+
) {
14+
parent::__construct(sprintf('Tool not found for call: %s.', $toolCall->name));
2515
}
2616
}

src/Server/RequestHandler/ToolCallHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
final class ToolCallHandler extends BaseRequestHandler
1515
{
1616
public function __construct(
17-
private readonly ToolExecutorInterface $toolbox,
17+
private readonly ToolExecutorInterface $toolExecutor,
1818
) {
1919
}
2020

@@ -24,7 +24,7 @@ public function createResponse(Request $message): Response|Error
2424
$arguments = $message->params['arguments'] ?? [];
2525

2626
try {
27-
$result = $this->toolbox->execute(new ToolCall(uniqid('', true), $name, $arguments));
27+
$result = $this->toolExecutor->execute(new ToolCall(uniqid('', true), $name, $arguments));
2828
} catch (ExceptionInterface) {
2929
return Error::internalError($message->id, 'Error while executing tool');
3030
}

src/Server/RequestHandler/ToolListHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
final class ToolListHandler extends BaseRequestHandler
1313
{
1414
public function __construct(
15-
private readonly ToolCollectionInterface $toolbox,
15+
private readonly ToolCollectionInterface $toolCollection,
1616
) {
1717
}
1818

@@ -30,7 +30,7 @@ public function createResponse(Request $message): Response
3030
'$schema' => 'http://json-schema.org/draft-07/schema#',
3131
] : $inputSchema,
3232
];
33-
}, $this->toolbox->getMetadata()),
33+
}, $this->toolCollection->getMetadata()),
3434
]);
3535
}
3636

0 commit comments

Comments
 (0)