|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Capability\Completion; |
| 13 | + |
| 14 | +use Mcp\Capability\Registry\ReferenceProviderInterface; |
| 15 | +use Mcp\Exception\RuntimeException; |
| 16 | +use Mcp\Schema\Request\CompletionCompleteRequest; |
| 17 | +use Mcp\Schema\Result\CompletionCompleteResult; |
| 18 | +use Psr\Container\ContainerInterface; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Kyrian Obikwelu <[email protected]> |
| 22 | + */ |
| 23 | +final class Completer implements CompleterInterface |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + private readonly ReferenceProviderInterface $referenceProvider, |
| 27 | + private readonly ?ContainerInterface $container = null, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function complete(CompletionCompleteRequest $request): CompletionCompleteResult |
| 32 | + { |
| 33 | + $argumentName = $request->argument['name'] ?? ''; |
| 34 | + $currentValue = $request->argument['value'] ?? ''; |
| 35 | + |
| 36 | + $reference = match (true) { |
| 37 | + 'ref/prompt' === $request->ref->type => $this->referenceProvider->getPrompt($request->ref->name), |
| 38 | + 'ref/resource' === $request->ref->type => $this->referenceProvider->getResourceTemplate($request->ref->uri), |
| 39 | + default => null, |
| 40 | + }; |
| 41 | + |
| 42 | + if (null === $reference) { |
| 43 | + return new CompletionCompleteResult([]); |
| 44 | + } |
| 45 | + |
| 46 | + $providerClassOrInstance = $reference->completionProviders[$argumentName] ?? null; |
| 47 | + if (null === $providerClassOrInstance) { |
| 48 | + return new CompletionCompleteResult([]); |
| 49 | + } |
| 50 | + |
| 51 | + if (\is_string($providerClassOrInstance)) { |
| 52 | + if (!class_exists($providerClassOrInstance)) { |
| 53 | + throw new RuntimeException(\sprintf('Completion provider class "%s" does not exist.', $providerClassOrInstance)); |
| 54 | + } |
| 55 | + |
| 56 | + $provider = $this->container?->has($providerClassOrInstance) |
| 57 | + ? $this->container->get($providerClassOrInstance) |
| 58 | + : new $providerClassOrInstance(); |
| 59 | + } else { |
| 60 | + $provider = $providerClassOrInstance; |
| 61 | + } |
| 62 | + |
| 63 | + if (!$provider instanceof ProviderInterface) { |
| 64 | + throw new RuntimeException('Completion provider must implement ProviderInterface.'); |
| 65 | + } |
| 66 | + |
| 67 | + $completions = $provider->getCompletions($currentValue); |
| 68 | + |
| 69 | + $total = \count($completions); |
| 70 | + $hasMore = $total > 100; |
| 71 | + $pagedCompletions = \array_slice($completions, 0, 100); |
| 72 | + |
| 73 | + return new CompletionCompleteResult($pagedCompletions, $total, $hasMore); |
| 74 | + } |
| 75 | +} |
0 commit comments