Introduce schema component
This commit is contained in:
parent
ee0dffe369
commit
6025f890f9
1 changed files with 5419 additions and 0 deletions
73
src/Schema/Request/CompletionCompleteRequest.php
Normal file
73
src/Schema/Request/CompletionCompleteRequest.php
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the official PHP MCP SDK.
|
||||
*
|
||||
* A collaboration between Symfony and the PHP Foundation.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Mcp\Schema\Request;
|
||||
|
||||
use Mcp\Exception\InvalidArgumentException;
|
||||
use Mcp\Schema\JsonRpc\Request;
|
||||
use Mcp\Schema\PromptReference;
|
||||
use Mcp\Schema\ResourceReference;
|
||||
|
||||
/**
|
||||
* A request from the client to the server, to ask for completion options.
|
||||
*
|
||||
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
|
||||
*/
|
||||
class CompletionCompleteRequest extends Request
|
||||
{
|
||||
/**
|
||||
* @param PromptReference|ResourceReference $ref the prompt or resource to complete
|
||||
* @param array{ name: string, value: string } $argument the argument to complete
|
||||
* @param ?array<string, mixed> $_meta optional metadata to include in the request
|
||||
*/
|
||||
public function __construct(
|
||||
string|int $id,
|
||||
public readonly PromptReference|ResourceReference $ref,
|
||||
public readonly array $argument,
|
||||
public readonly ?array $_meta = null,
|
||||
) {
|
||||
$params = [
|
||||
'ref' => $this->ref,
|
||||
'argument' => $this->argument,
|
||||
];
|
||||
|
||||
if (null !== $this->_meta) {
|
||||
$params['_meta'] = $this->_meta;
|
||||
}
|
||||
|
||||
parent::__construct($id, 'completion/complete', $params);
|
||||
}
|
||||
|
||||
public static function fromRequest(Request $request): self
|
||||
{
|
||||
if ('completion/complete' !== $request->method) {
|
||||
throw new InvalidArgumentException('Request is not a completion/complete request');
|
||||
}
|
||||
|
||||
$params = $request->params;
|
||||
|
||||
if (!isset($params['ref']) || !\is_array($params['ref'])) {
|
||||
throw new InvalidArgumentException('Missing or invalid "ref" parameter for completion/complete.');
|
||||
}
|
||||
|
||||
$ref = match ($params['ref']['type'] ?? null) {
|
||||
'ref/prompt' => new PromptReference($params['ref']['name']),
|
||||
'ref/resource' => new ResourceReference($params['ref']['uri']),
|
||||
default => throw new InvalidArgumentException('Invalid "ref" parameter for completion/complete.'),
|
||||
};
|
||||
|
||||
if (!isset($params['argument']) || !\is_array($params['argument'])) {
|
||||
throw new InvalidArgumentException('Missing or invalid "argument" parameter for completion/complete.');
|
||||
}
|
||||
|
||||
return new self($request->id, $ref, $params['argument'], $params['_meta'] ?? null);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue