This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
feat: remove dependency on llm-chain #13
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
], | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
interface ExceptionInterface extends BaseExceptionInterface | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) { | ||||||
} | ||||||
|
||||||
|
@@ -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'); | ||||||
} | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) { | ||||||
} | ||||||
|
||||||
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()), | ||||||
]); | ||||||
} | ||||||
|
||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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