|
4 | 4 |
|
5 | 5 | namespace Cortex\Prompts\Factories; |
6 | 6 |
|
| 7 | +use Throwable; |
| 8 | +use PhpMcp\Client\Client; |
| 9 | +use Illuminate\Support\Arr; |
| 10 | +use Illuminate\Support\Str; |
| 11 | +use Cortex\LLM\Contracts\Message; |
| 12 | +use Cortex\JsonSchema\SchemaFactory; |
| 13 | +use Cortex\Exceptions\PromptException; |
| 14 | +use Cortex\JsonSchema\Types\ObjectSchema; |
| 15 | +use Cortex\LLM\Data\Messages\UserMessage; |
7 | 16 | use Cortex\Prompts\Contracts\PromptFactory; |
8 | 17 | use Cortex\Prompts\Contracts\PromptTemplate; |
9 | | -use Cortex\Prompts\Templates\ChatPromptTemplate; |
| 18 | +use Cortex\LLM\Data\Messages\AssistantMessage; |
| 19 | +use Cortex\Prompts\Builders\ChatPromptBuilder; |
| 20 | +use PhpMcp\Client\Model\Content\PromptMessage; |
| 21 | +use Cortex\LLM\Data\Messages\Content\TextContent; |
| 22 | +use Cortex\LLM\Data\Messages\Content\AudioContent; |
| 23 | +use Cortex\LLM\Data\Messages\Content\ImageContent; |
| 24 | +use PhpMcp\Client\JsonRpc\Results\GetPromptResult; |
| 25 | +use PhpMcp\Client\Model\Definitions\PromptDefinition; |
| 26 | +use PhpMcp\Client\Model\Definitions\PromptArgumentDefinition; |
10 | 27 |
|
11 | 28 | /** |
12 | 29 | * @link https://modelcontextprotocol.io/docs/concepts/prompts |
13 | 30 | */ |
14 | 31 | class McpPromptFactory implements PromptFactory |
15 | 32 | { |
16 | 33 | public function __construct( |
17 | | - protected string $baseUri, |
| 34 | + protected Client $client, |
18 | 35 | ) {} |
19 | 36 |
|
20 | 37 | public function make(string $name, array $config = []): PromptTemplate |
21 | 38 | { |
22 | | - // TODO: Implement |
23 | | - return new ChatPromptTemplate([]); |
| 39 | + try { |
| 40 | + $this->client->initialize(); |
| 41 | + $promptDefinition = $this->getPromptDefinition($name); |
| 42 | + $compiledPrompt = $this->getCompiledPrompt($promptDefinition); |
| 43 | + } finally { |
| 44 | + $this->client->disconnect(); |
| 45 | + } |
| 46 | + |
| 47 | + $builder = new ChatPromptBuilder(); |
| 48 | + |
| 49 | + if ($promptDefinition->isTemplate()) { |
| 50 | + $builder->inputSchema($this->buildInputSchema($promptDefinition)); |
| 51 | + } |
| 52 | + |
| 53 | + return $builder->messages($this->buildMessages($compiledPrompt))->build(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Builds an input schema for the given prompt definition. |
| 58 | + */ |
| 59 | + protected function buildInputSchema(PromptDefinition $prompt): ObjectSchema |
| 60 | + { |
| 61 | + $inputSchema = new ObjectSchema($prompt->name); |
| 62 | + |
| 63 | + if ($prompt->description !== null) { |
| 64 | + $inputSchema->description($prompt->description); |
| 65 | + } |
| 66 | + |
| 67 | + $properties = []; |
| 68 | + |
| 69 | + foreach ($prompt->arguments as $argument) { |
| 70 | + $property = SchemaFactory::string($argument->name) |
| 71 | + ->description($argument->description); |
| 72 | + |
| 73 | + if ($argument->required) { |
| 74 | + $property = $property->required(); |
| 75 | + } |
| 76 | + |
| 77 | + $properties[] = $property; |
| 78 | + } |
| 79 | + |
| 80 | + return $inputSchema->properties(...$properties); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get the prompt definition for the given name. |
| 85 | + * |
| 86 | + * @throws \Cortex\Exceptions\PromptException |
| 87 | + */ |
| 88 | + protected function getPromptDefinition(string $name): PromptDefinition |
| 89 | + { |
| 90 | + try { |
| 91 | + /** @var array<array-key, \PhpMcp\Client\Model\Definitions\PromptDefinition> $prompts */ |
| 92 | + $prompts = $this->client->listPrompts(); |
| 93 | + } catch (Throwable $e) { |
| 94 | + throw new PromptException('Failed to list prompts: ' . $e->getMessage(), previous: $e); |
| 95 | + } |
| 96 | + |
| 97 | + $prompt = collect($prompts)->firstWhere('name', $name); |
| 98 | + |
| 99 | + if ($prompt === null) { |
| 100 | + throw new PromptException('Prompt not found: ' . $name); |
| 101 | + } |
| 102 | + |
| 103 | + return $prompt; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Get the compiled prompt for the given prompt definition. |
| 108 | + */ |
| 109 | + protected function getCompiledPrompt(PromptDefinition $prompt): GetPromptResult |
| 110 | + { |
| 111 | + if ($prompt->isTemplate()) { |
| 112 | + $arguments = collect($prompt->arguments)->mapWithKeys(fn(PromptArgumentDefinition $argument) => [ |
| 113 | + $argument->name => Str::of($argument->name)->prepend('{')->append('}')->toString(), |
| 114 | + ])->toArray(); |
| 115 | + |
| 116 | + return $this->client->getPrompt($prompt->name, $arguments); |
| 117 | + } |
| 118 | + |
| 119 | + return $this->client->getPrompt($prompt->name); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Build the messages from a given compiled prompt. |
| 124 | + * |
| 125 | + * @return array<array-key, \Cortex\LLM\Contracts\Message> |
| 126 | + */ |
| 127 | + protected function buildMessages(GetPromptResult $compiledPrompt): array |
| 128 | + { |
| 129 | + return array_map(function (PromptMessage $message): Message { |
| 130 | + $contentArray = $message->content->toArray(); |
| 131 | + $content = match ($message->content->getType()) { |
| 132 | + 'text' => new TextContent(Arr::get($contentArray, 'text')), |
| 133 | + 'image' => new ImageContent(Arr::get($contentArray, 'data'), Arr::get($contentArray, 'mimeType')), |
| 134 | + 'audio' => new AudioContent(Arr::get($contentArray, 'url'), Arr::get($contentArray, 'mimeType')), |
| 135 | + // "resource" type not supported for now |
| 136 | + default => throw new PromptException('Unsupported content type: ' . $message->content->getType()), |
| 137 | + }; |
| 138 | + |
| 139 | + return match ($message->role) { |
| 140 | + 'user' => new UserMessage([$content]), |
| 141 | + 'assistant' => new AssistantMessage([$content]), |
| 142 | + default => throw new PromptException('Unsupported role: ' . $message->role), |
| 143 | + }; |
| 144 | + }, $compiledPrompt->messages); |
24 | 145 | } |
25 | 146 | } |
0 commit comments