From 9c67345c82e7abf720f9669e198068402e91da2a Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Sun, 25 May 2025 19:00:20 +0200 Subject: [PATCH 1/2] refactor: tool var names and exceptions --- src/Exception/ExceptionInterface.php | 4 +--- src/Exception/ToolExecutionException.php | 15 ++++++-------- src/Exception/ToolNotFoundException.php | 20 +++++-------------- src/Server/RequestHandler/ToolCallHandler.php | 4 ++-- src/Server/RequestHandler/ToolListHandler.php | 4 ++-- 5 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/Exception/ExceptionInterface.php b/src/Exception/ExceptionInterface.php index f70fd4c..14b84f4 100644 --- a/src/Exception/ExceptionInterface.php +++ b/src/Exception/ExceptionInterface.php @@ -4,8 +4,6 @@ namespace PhpLlm\McpSdk\Exception; -use PhpLlm\LlmChain\Exception\ExceptionInterface as BaseExceptionInterface; - -interface ExceptionInterface extends BaseExceptionInterface +interface ExceptionInterface { } diff --git a/src/Exception/ToolExecutionException.php b/src/Exception/ToolExecutionException.php index ba58350..82a64c3 100644 --- a/src/Exception/ToolExecutionException.php +++ b/src/Exception/ToolExecutionException.php @@ -4,17 +4,14 @@ namespace PhpLlm\McpSdk\Exception; -use PhpLlm\LlmChain\Model\Response\ToolCall; +use PhpLlm\McpSdk\Capability\Tool\ToolCall; final class ToolExecutionException extends \RuntimeException implements ExceptionInterface { - public ?ToolCall $toolCall = null; - - public static function executionFailed(ToolCall $toolCall, \Throwable $previous): self - { - $exception = new self(sprintf('Execution of tool "%s" failed with error: %s', $toolCall->name, $previous->getMessage()), previous: $previous); - $exception->toolCall = $toolCall; - - return $exception; + public function __construct( + public readonly ToolCall $toolCall, + ?\Throwable $previous = null + ) { + parent::__construct(sprintf('Execution of tool "%s" failed with error: %s', $toolCall->name, $previous->getMessage()), previous: $previous); } } diff --git a/src/Exception/ToolNotFoundException.php b/src/Exception/ToolNotFoundException.php index b31d9e6..26c943d 100644 --- a/src/Exception/ToolNotFoundException.php +++ b/src/Exception/ToolNotFoundException.php @@ -4,23 +4,13 @@ namespace PhpLlm\McpSdk\Exception; -use PhpLlm\LlmChain\Chain\Toolbox\ExecutionReference; -use PhpLlm\LlmChain\Model\Response\ToolCall; +use PhpLlm\McpSdk\Capability\Tool\ToolCall; final class ToolNotFoundException extends \RuntimeException implements ExceptionInterface { - public ?ToolCall $toolCall = null; - - public static function notFoundForToolCall(ToolCall $toolCall): self - { - $exception = new self(sprintf('Tool not found for call: %s.', $toolCall->name)); - $exception->toolCall = $toolCall; - - return $exception; - } - - public static function notFoundForReference(ExecutionReference $reference): self - { - return new self(sprintf('Tool not found for reference: %s::%s.', $reference->class, $reference->method)); + public function __construct( + public readonly ToolCall $toolCall, + ) { + parent::__construct(sprintf('Tool not found for call: %s.', $toolCall->name)); } } diff --git a/src/Server/RequestHandler/ToolCallHandler.php b/src/Server/RequestHandler/ToolCallHandler.php index d1036a0..065ca48 100644 --- a/src/Server/RequestHandler/ToolCallHandler.php +++ b/src/Server/RequestHandler/ToolCallHandler.php @@ -14,7 +14,7 @@ final class ToolCallHandler extends BaseRequestHandler { public function __construct( - private readonly ToolExecutorInterface $toolbox, + private readonly ToolExecutorInterface $toolExecutor, ) { } @@ -24,7 +24,7 @@ public function createResponse(Request $message): Response|Error $arguments = $message->params['arguments'] ?? []; try { - $result = $this->toolbox->execute(new ToolCall(uniqid('', true), $name, $arguments)); + $result = $this->toolExecutor->execute(new ToolCall(uniqid('', true), $name, $arguments)); } catch (ExceptionInterface) { return Error::internalError($message->id, 'Error while executing tool'); } diff --git a/src/Server/RequestHandler/ToolListHandler.php b/src/Server/RequestHandler/ToolListHandler.php index 03bb8ac..d4c4263 100644 --- a/src/Server/RequestHandler/ToolListHandler.php +++ b/src/Server/RequestHandler/ToolListHandler.php @@ -12,7 +12,7 @@ final class ToolListHandler extends BaseRequestHandler { public function __construct( - private readonly ToolCollectionInterface $toolbox, + private readonly ToolCollectionInterface $toolCollection, ) { } @@ -30,7 +30,7 @@ public function createResponse(Request $message): Response '$schema' => 'http://json-schema.org/draft-07/schema#', ] : $inputSchema, ]; - }, $this->toolbox->getMetadata()), + }, $this->toolCollection->getMetadata()), ]); } From 1b051b70e498a745072979c331c04fd467127172 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Sun, 25 May 2025 19:06:46 +0200 Subject: [PATCH 2/2] fix: phpstan and psr/cache dependency --- composer.json | 6 ++++-- src/Capability/Tool/MetadataInterface.php | 3 +++ src/Exception/ToolExecutionException.php | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 606ca75..c3e3d5b 100644 --- a/composer.json +++ b/composer.json @@ -19,10 +19,12 @@ "phpstan/phpstan": "^2.1", "phpunit/phpunit": "^11.5", "symfony/console": "^6.4 || ^7.0", - "rector/rector": "^2.0" + "rector/rector": "^2.0", + "psr/cache": "^3.0" }, "suggest": { - "symfony/console": "To use SymfonyConsoleTransport for STDIO" + "symfony/console": "To use SymfonyConsoleTransport for STDIO", + "psr/cache": "To use CachePoolStore with SSE Transport" }, "autoload": { "psr-4": { diff --git a/src/Capability/Tool/MetadataInterface.php b/src/Capability/Tool/MetadataInterface.php index 1e217b6..141578b 100644 --- a/src/Capability/Tool/MetadataInterface.php +++ b/src/Capability/Tool/MetadataInterface.php @@ -10,5 +10,8 @@ public function getName(): string; public function getDescription(): string; + /** + * @return array + */ public function getInputSchema(): array; } diff --git a/src/Exception/ToolExecutionException.php b/src/Exception/ToolExecutionException.php index 82a64c3..5235e87 100644 --- a/src/Exception/ToolExecutionException.php +++ b/src/Exception/ToolExecutionException.php @@ -10,7 +10,7 @@ final class ToolExecutionException extends \RuntimeException implements Exceptio { public function __construct( public readonly ToolCall $toolCall, - ?\Throwable $previous = null + ?\Throwable $previous = null, ) { parent::__construct(sprintf('Execution of tool "%s" failed with error: %s', $toolCall->name, $previous->getMessage()), previous: $previous); }