Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions src/Capability/Tool/MetadataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpLlm\McpSdk\Capability\Tool;

interface MetadataInterface
{
public function getName(): string;

public function getDescription(): string;

public function getInputSchema(): array;
}
40 changes: 40 additions & 0 deletions src/Capability/Tool/ToolCall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace PhpLlm\McpSdk\Capability\Tool;

final readonly class ToolCall implements \JsonSerializable
{
/**
* @param array<string, mixed> $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),
],
];
}
}
11 changes: 11 additions & 0 deletions src/Capability/Tool/ToolCollectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PhpLlm\McpSdk\Capability\Tool;

interface ToolCollectionInterface
{
/**
* @return MetadataInterface[]
*/
public function getMetadata(): array;
}
15 changes: 15 additions & 0 deletions src/Capability/Tool/ToolExecutorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace PhpLlm\McpSdk\Capability\Tool;

use PhpLlm\McpSdk\Exception\ToolExecutionException;
use PhpLlm\McpSdk\Exception\ToolNotFoundException;

interface ToolExecutorInterface
{
/**
* @throws ToolExecutionException if the tool execution fails
* @throws ToolNotFoundException if the tool is not found
*/
public function execute(ToolCall $toolCall): mixed;
}
11 changes: 11 additions & 0 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace PhpLlm\McpSdk\Exception;

use PhpLlm\LlmChain\Exception\ExceptionInterface as BaseExceptionInterface;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it still intended to have the dependency here? looks wrong to me


interface ExceptionInterface extends BaseExceptionInterface
{
}
20 changes: 20 additions & 0 deletions src/Exception/ToolExecutionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PhpLlm\McpSdk\Exception;

use PhpLlm\LlmChain\Model\Response\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;
}
}
26 changes: 26 additions & 0 deletions src/Exception/ToolNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PhpLlm\McpSdk\Exception;

use PhpLlm\LlmChain\Chain\Toolbox\ExecutionReference;
use PhpLlm\LlmChain\Model\Response\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));
}
}
10 changes: 5 additions & 5 deletions src/Server/RequestHandler/ToolCallHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

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;

final class ToolCallHandler extends BaseRequestHandler
{
public function __construct(
private readonly ToolboxInterface $toolbox,
private readonly ToolExecutorInterface $toolbox,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private readonly ToolExecutorInterface $toolbox,
private readonly ToolExecutorInterface $toolExecutor,

) {
}

Expand All @@ -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');
}
Expand Down
20 changes: 11 additions & 9 deletions src/Server/RequestHandler/ToolListHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private readonly ToolCollectionInterface $toolbox,
private readonly ToolCollectionInterface $toolCollection,

) {
}

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()),
]);
}

Expand Down
Loading