|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Cortex\Tools; |
| 6 | + |
| 7 | +use PhpMcp\Client\Client; |
| 8 | +use Cortex\Facades\McpServer; |
| 9 | +use Cortex\LLM\Data\ToolCall; |
| 10 | +use Cortex\JsonSchema\SchemaFactory; |
| 11 | +use Cortex\Exceptions\GenericException; |
| 12 | +use Cortex\JsonSchema\Types\ObjectSchema; |
| 13 | +use PhpMcp\Client\Model\Content\TextContent; |
| 14 | +use PhpMcp\Client\Contracts\ContentInterface; |
| 15 | +use PhpMcp\Client\Model\Definitions\ToolDefinition; |
| 16 | + |
| 17 | +class McpTool extends AbstractTool |
| 18 | +{ |
| 19 | + protected Client $client; |
| 20 | + |
| 21 | + protected ObjectSchema $schema; |
| 22 | + |
| 23 | + protected string $description; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + protected string $name, |
| 27 | + protected ?string $mcpServer = null, |
| 28 | + ) { |
| 29 | + $this->client = McpServer::driver($this->mcpServer); |
| 30 | + $toolDefinition = $this->getToolDefinition(); |
| 31 | + $this->description = $toolDefinition->description ?? ''; |
| 32 | + $schema = SchemaFactory::fromJson($toolDefinition->inputSchema); |
| 33 | + |
| 34 | + if (! $schema instanceof ObjectSchema) { |
| 35 | + throw new GenericException(sprintf('Schema for tool %s is not an object', $this->name)); |
| 36 | + } |
| 37 | + |
| 38 | + if ($toolDefinition->description !== null) { |
| 39 | + $schema->description($toolDefinition->description); |
| 40 | + } |
| 41 | + |
| 42 | + $this->schema = $schema; |
| 43 | + } |
| 44 | + |
| 45 | + public function name(): string |
| 46 | + { |
| 47 | + return $this->name; |
| 48 | + } |
| 49 | + |
| 50 | + public function description(): string |
| 51 | + { |
| 52 | + return $this->description; |
| 53 | + } |
| 54 | + |
| 55 | + public function schema(): ObjectSchema |
| 56 | + { |
| 57 | + return $this->schema; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param ToolCall|array<string, mixed> $toolCall |
| 62 | + */ |
| 63 | + public function invoke(ToolCall|array $toolCall = []): mixed |
| 64 | + { |
| 65 | + try { |
| 66 | + $this->client->initialize(); |
| 67 | + |
| 68 | + // Get the arguments from the given tool call. |
| 69 | + $arguments = $this->getArguments($toolCall); |
| 70 | + |
| 71 | + // Ensure arguments are valid as per the tool's schema. |
| 72 | + if ($this->schema->getPropertyKeys() !== []) { |
| 73 | + $this->schema->validate($arguments); |
| 74 | + } |
| 75 | + |
| 76 | + $result = $this->client->callTool($this->name, $arguments); |
| 77 | + } finally { |
| 78 | + $this->client->disconnect(); |
| 79 | + } |
| 80 | + |
| 81 | + // Only support text content for now. |
| 82 | + $textContent = collect($result->content) |
| 83 | + ->filter(fn(ContentInterface $item): bool => $item instanceof TextContent) |
| 84 | + ->first(); |
| 85 | + |
| 86 | + return $textContent?->text; |
| 87 | + } |
| 88 | + |
| 89 | + protected function getToolDefinition(): ToolDefinition |
| 90 | + { |
| 91 | + $this->client->initialize(); |
| 92 | + $tools = $this->client->listTools(); |
| 93 | + |
| 94 | + $tool = collect($tools)->firstWhere('name', $this->name); |
| 95 | + |
| 96 | + if ($tool === null) { |
| 97 | + throw new GenericException(sprintf('Tool %s not found in MCP server %s', $this->name, $this->mcpServer)); |
| 98 | + } |
| 99 | + |
| 100 | + return $tool; |
| 101 | + } |
| 102 | + |
| 103 | + public function __destruct() |
| 104 | + { |
| 105 | + $this->client->disconnect(); |
| 106 | + } |
| 107 | +} |
0 commit comments