From 3d562e1fa94f4cad514aa819ebc5fbb2d5f2476d Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Sun, 25 May 2025 13:29:21 +0200 Subject: [PATCH] feat: remove dependency on llm-chain --- composer.json | 3 +- src/Capability/Tool/MetadataInterface.php | 14 +++++++ src/Capability/Tool/ToolCall.php | 40 +++++++++++++++++++ .../Tool/ToolCollectionInterface.php | 11 +++++ src/Capability/Tool/ToolExecutorInterface.php | 15 +++++++ src/Exception/ExceptionInterface.php | 11 +++++ src/Exception/ToolExecutionException.php | 20 ++++++++++ src/Exception/ToolNotFoundException.php | 26 ++++++++++++ src/Server/RequestHandler/ToolCallHandler.php | 10 ++--- src/Server/RequestHandler/ToolListHandler.php | 20 +++++----- 10 files changed, 154 insertions(+), 16 deletions(-) create mode 100644 src/Capability/Tool/MetadataInterface.php create mode 100644 src/Capability/Tool/ToolCall.php create mode 100644 src/Capability/Tool/ToolCollectionInterface.php create mode 100644 src/Capability/Tool/ToolExecutorInterface.php create mode 100644 src/Exception/ExceptionInterface.php create mode 100644 src/Exception/ToolExecutionException.php create mode 100644 src/Exception/ToolNotFoundException.php diff --git a/composer.json b/composer.json index 94ca8d4..606ca75 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,7 @@ "require": { "php": "^8.2", "psr/log": "^3.0", - "symfony/uid": "^6.4 || ^7.0", - "php-llm/llm-chain": "^0.19.0" + "symfony/uid": "^6.4 || ^7.0" }, "require-dev": { "php-cs-fixer/shim": "^3.70", diff --git a/src/Capability/Tool/MetadataInterface.php b/src/Capability/Tool/MetadataInterface.php new file mode 100644 index 0000000..1e217b6 --- /dev/null +++ b/src/Capability/Tool/MetadataInterface.php @@ -0,0 +1,14 @@ + $arguments + */ + public function __construct( + public string $id, + public string $name, + public array $arguments = [], + ) { + } + + /** + * @return array{ + * id: string, + * type: 'function', + * function: array{ + * name: string, + * arguments: string + * } + * } + */ + public function jsonSerialize(): array + { + return [ + 'id' => $this->id, + 'type' => 'function', + 'function' => [ + 'name' => $this->name, + 'arguments' => json_encode($this->arguments), + ], + ]; + } +} diff --git a/src/Capability/Tool/ToolCollectionInterface.php b/src/Capability/Tool/ToolCollectionInterface.php new file mode 100644 index 0000000..ffb378b --- /dev/null +++ b/src/Capability/Tool/ToolCollectionInterface.php @@ -0,0 +1,11 @@ +name, $previous->getMessage()), previous: $previous); + $exception->toolCall = $toolCall; + + return $exception; + } +} diff --git a/src/Exception/ToolNotFoundException.php b/src/Exception/ToolNotFoundException.php new file mode 100644 index 0000000..b31d9e6 --- /dev/null +++ b/src/Exception/ToolNotFoundException.php @@ -0,0 +1,26 @@ +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)); + } +} diff --git a/src/Server/RequestHandler/ToolCallHandler.php b/src/Server/RequestHandler/ToolCallHandler.php index a8f1088..d1036a0 100644 --- a/src/Server/RequestHandler/ToolCallHandler.php +++ b/src/Server/RequestHandler/ToolCallHandler.php @@ -4,9 +4,9 @@ namespace PhpLlm\McpSdk\Server\RequestHandler; -use PhpLlm\LlmChain\Chain\Toolbox\ToolboxInterface; -use PhpLlm\LlmChain\Exception\ExceptionInterface; -use PhpLlm\LlmChain\Model\Response\ToolCall; +use PhpLlm\McpSdk\Capability\Tool\ToolCall; +use PhpLlm\McpSdk\Capability\Tool\ToolExecutorInterface; +use PhpLlm\McpSdk\Exception\ExceptionInterface; use PhpLlm\McpSdk\Message\Error; use PhpLlm\McpSdk\Message\Request; use PhpLlm\McpSdk\Message\Response; @@ -14,7 +14,7 @@ final class ToolCallHandler extends BaseRequestHandler { public function __construct( - private readonly ToolboxInterface $toolbox, + private readonly ToolExecutorInterface $toolbox, ) { } @@ -24,7 +24,7 @@ public function createResponse(Request $message): Response|Error $arguments = $message->params['arguments'] ?? []; try { - $result = $this->toolbox->execute(new ToolCall(uniqid(), $name, $arguments)); + $result = $this->toolbox->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 461ba8f..03bb8ac 100644 --- a/src/Server/RequestHandler/ToolListHandler.php +++ b/src/Server/RequestHandler/ToolListHandler.php @@ -4,31 +4,33 @@ namespace PhpLlm\McpSdk\Server\RequestHandler; -use PhpLlm\LlmChain\Chain\Toolbox\Metadata; -use PhpLlm\LlmChain\Chain\Toolbox\ToolboxInterface; +use PhpLlm\McpSdk\Capability\Tool\MetadataInterface; +use PhpLlm\McpSdk\Capability\Tool\ToolCollectionInterface; use PhpLlm\McpSdk\Message\Request; use PhpLlm\McpSdk\Message\Response; final class ToolListHandler extends BaseRequestHandler { public function __construct( - private readonly ToolboxInterface $toolbox, + private readonly ToolCollectionInterface $toolbox, ) { } public function createResponse(Request $message): Response { return new Response($message->id, [ - 'tools' => array_map(function (Metadata $tool) { + 'tools' => array_map(function (MetadataInterface $tool) { + $inputSchema = $tool->getInputSchema(); + return [ - 'name' => $tool->name, - 'description' => $tool->description, - 'inputSchema' => $tool->parameters ?? [ + 'name' => $tool->getName(), + 'description' => $tool->getDescription(), + 'inputSchema' => [] === $inputSchema ? [ 'type' => 'object', '$schema' => 'http://json-schema.org/draft-07/schema#', - ], + ] : $inputSchema, ]; - }, $this->toolbox->getMap()), + }, $this->toolbox->getMetadata()), ]); }