Add elicitation support for server-to-client user input requests (#228)

- Add ElicitAction enum (accept/decline/cancel)
- Add schema definitions for elicitation fields (string, number, boolean, enum)
- Add ElicitationSchema wrapper for requestedSchema
- Add ElicitRequest and ElicitResult for elicitation/create method
- Update ClientCapabilities to support elicitation capability
- Add elicit() method to ClientGateway
- Store client capabilities in session during initialization
- Add comprehensive unit tests
- Add elicitation example server with book_restaurant, confirm_action, and collect_feedback tools
This commit is contained in:
Johannes Wachter 2026-02-15 23:34:19 +01:00 • committed by GitHub
commit 02e86377e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/*
* 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\Tests\Unit\Schema\Request;
use Mcp\Schema\Elicitation\ElicitationSchema;
use Mcp\Schema\Elicitation\NumberSchemaDefinition;
use Mcp\Schema\Elicitation\StringSchemaDefinition;
use Mcp\Schema\Request\ElicitRequest;
use PHPUnit\Framework\TestCase;
final class ElicitRequestTest extends TestCase
{
public function testConstructor(): void
{
$schema = new ElicitationSchema([
'name' => new StringSchemaDefinition('Name'),
]);
$request = new ElicitRequest('Please provide your name', $schema);
$this->assertSame('Please provide your name', $request->message);
$this->assertSame($schema, $request->requestedSchema);
}
public function testGetMethod(): void
{
$this->assertSame('elicitation/create', ElicitRequest::getMethod());
}
public function testJsonSerialization(): void
{
$schema = new ElicitationSchema(
[
'name' => new StringSchemaDefinition('Name'),
'age' => new NumberSchemaDefinition('Age', integerOnly: true, minimum: 0),
],
['name'],
);
$request = new ElicitRequest('Please provide your details', $schema);
$request = $request->withId(1);
$json = json_encode($request);
$this->assertIsString($json);
$decoded = json_decode($json, true);
$this->assertIsArray($decoded);
$this->assertSame('2.0', $decoded['jsonrpc']);
$this->assertSame('elicitation/create', $decoded['method']);
$this->assertArrayHasKey('params', $decoded);
$params = $decoded['params'];
$this->assertSame('Please provide your details', $params['message']);
$this->assertArrayHasKey('requestedSchema', $params);
$requestedSchema = $params['requestedSchema'];
$this->assertSame('object', $requestedSchema['type']);
$this->assertArrayHasKey('name', $requestedSchema['properties']);
$this->assertArrayHasKey('age', $requestedSchema['properties']);
$this->assertSame(['name'], $requestedSchema['required']);
}
}