Introduce schema component

This commit is contained in:
Christopher Hertel 2025-08-01 16:41:16 +02:00
commit 6025f890f9
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3

View 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);
}
}