Skip to content

Commit 158beba

Browse files
authored
feat: remove dependency on llm-chain (#13)
1 parent b4f98ac commit 158beba

File tree

10 files changed

+154
-16
lines changed

10 files changed

+154
-16
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
"require": {
1313
"php": "^8.2",
1414
"psr/log": "^3.0",
15-
"symfony/uid": "^6.4 || ^7.0",
16-
"php-llm/llm-chain": "^0.19.0"
15+
"symfony/uid": "^6.4 || ^7.0"
1716
},
1817
"require-dev": {
1918
"php-cs-fixer/shim": "^3.70",
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\McpSdk\Capability\Tool;
6+
7+
interface MetadataInterface
8+
{
9+
public function getName(): string;
10+
11+
public function getDescription(): string;
12+
13+
public function getInputSchema(): array;
14+
}

src/Capability/Tool/ToolCall.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\McpSdk\Capability\Tool;
6+
7+
final readonly class ToolCall implements \JsonSerializable
8+
{
9+
/**
10+
* @param array<string, mixed> $arguments
11+
*/
12+
public function __construct(
13+
public string $id,
14+
public string $name,
15+
public array $arguments = [],
16+
) {
17+
}
18+
19+
/**
20+
* @return array{
21+
* id: string,
22+
* type: 'function',
23+
* function: array{
24+
* name: string,
25+
* arguments: string
26+
* }
27+
* }
28+
*/
29+
public function jsonSerialize(): array
30+
{
31+
return [
32+
'id' => $this->id,
33+
'type' => 'function',
34+
'function' => [
35+
'name' => $this->name,
36+
'arguments' => json_encode($this->arguments),
37+
],
38+
];
39+
}
40+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PhpLlm\McpSdk\Capability\Tool;
4+
5+
interface ToolCollectionInterface
6+
{
7+
/**
8+
* @return MetadataInterface[]
9+
*/
10+
public function getMetadata(): array;
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace PhpLlm\McpSdk\Capability\Tool;
4+
5+
use PhpLlm\McpSdk\Exception\ToolExecutionException;
6+
use PhpLlm\McpSdk\Exception\ToolNotFoundException;
7+
8+
interface ToolExecutorInterface
9+
{
10+
/**
11+
* @throws ToolExecutionException if the tool execution fails
12+
* @throws ToolNotFoundException if the tool is not found
13+
*/
14+
public function execute(ToolCall $toolCall): mixed;
15+
}

src/Exception/ExceptionInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\McpSdk\Exception;
6+
7+
use PhpLlm\LlmChain\Exception\ExceptionInterface as BaseExceptionInterface;
8+
9+
interface ExceptionInterface extends BaseExceptionInterface
10+
{
11+
}
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\Exception;
6+
7+
use PhpLlm\LlmChain\Model\Response\ToolCall;
8+
9+
final class ToolExecutionException extends \RuntimeException implements ExceptionInterface
10+
{
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;
19+
}
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\McpSdk\Exception;
6+
7+
use PhpLlm\LlmChain\Chain\Toolbox\ExecutionReference;
8+
use PhpLlm\LlmChain\Model\Response\ToolCall;
9+
10+
final class ToolNotFoundException extends \RuntimeException implements ExceptionInterface
11+
{
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));
25+
}
26+
}

src/Server/RequestHandler/ToolCallHandler.php

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

55
namespace PhpLlm\McpSdk\Server\RequestHandler;
66

7-
use PhpLlm\LlmChain\Chain\Toolbox\ToolboxInterface;
8-
use PhpLlm\LlmChain\Exception\ExceptionInterface;
9-
use PhpLlm\LlmChain\Model\Response\ToolCall;
7+
use PhpLlm\McpSdk\Capability\Tool\ToolCall;
8+
use PhpLlm\McpSdk\Capability\Tool\ToolExecutorInterface;
9+
use PhpLlm\McpSdk\Exception\ExceptionInterface;
1010
use PhpLlm\McpSdk\Message\Error;
1111
use PhpLlm\McpSdk\Message\Request;
1212
use PhpLlm\McpSdk\Message\Response;
1313

1414
final class ToolCallHandler extends BaseRequestHandler
1515
{
1616
public function __construct(
17-
private readonly ToolboxInterface $toolbox,
17+
private readonly ToolExecutorInterface $toolbox,
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(), $name, $arguments));
27+
$result = $this->toolbox->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: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,33 @@
44

55
namespace PhpLlm\McpSdk\Server\RequestHandler;
66

7-
use PhpLlm\LlmChain\Chain\Toolbox\Metadata;
8-
use PhpLlm\LlmChain\Chain\Toolbox\ToolboxInterface;
7+
use PhpLlm\McpSdk\Capability\Tool\MetadataInterface;
8+
use PhpLlm\McpSdk\Capability\Tool\ToolCollectionInterface;
99
use PhpLlm\McpSdk\Message\Request;
1010
use PhpLlm\McpSdk\Message\Response;
1111

1212
final class ToolListHandler extends BaseRequestHandler
1313
{
1414
public function __construct(
15-
private readonly ToolboxInterface $toolbox,
15+
private readonly ToolCollectionInterface $toolbox,
1616
) {
1717
}
1818

1919
public function createResponse(Request $message): Response
2020
{
2121
return new Response($message->id, [
22-
'tools' => array_map(function (Metadata $tool) {
22+
'tools' => array_map(function (MetadataInterface $tool) {
23+
$inputSchema = $tool->getInputSchema();
24+
2325
return [
24-
'name' => $tool->name,
25-
'description' => $tool->description,
26-
'inputSchema' => $tool->parameters ?? [
26+
'name' => $tool->getName(),
27+
'description' => $tool->getDescription(),
28+
'inputSchema' => [] === $inputSchema ? [
2729
'type' => 'object',
2830
'$schema' => 'http://json-schema.org/draft-07/schema#',
29-
],
31+
] : $inputSchema,
3032
];
31-
}, $this->toolbox->getMap()),
33+
}, $this->toolbox->getMetadata()),
3234
]);
3335
}
3436

0 commit comments

Comments
 (0)