Extend elicitation enum schema compliance (#261)

This commit is contained in:
Christopher Hertel 2026-03-22 18:46:40 +01:00 • committed by GitHub
commit e8f5a8ba22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,6 +16,14 @@ use Mcp\Schema\Content\ImageContent;
use Mcp\Schema\Content\PromptMessage;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Content\TextResourceContents;
use Mcp\Schema\Elicitation\BooleanSchemaDefinition;
use Mcp\Schema\Elicitation\ElicitationSchema;
use Mcp\Schema\Elicitation\EnumSchemaDefinition;
use Mcp\Schema\Elicitation\MultiSelectEnumSchemaDefinition;
use Mcp\Schema\Elicitation\NumberSchemaDefinition;
use Mcp\Schema\Elicitation\StringSchemaDefinition;
use Mcp\Schema\Elicitation\TitledEnumSchemaDefinition;
use Mcp\Schema\Elicitation\TitledMultiSelectEnumSchemaDefinition;
use Mcp\Schema\Enum\Role;
use Mcp\Schema\Result\CallToolResult;
use Mcp\Server\Protocol;
@ -76,6 +84,65 @@ final class Elements
);
}
/**
* @param string $message The message to display to the user
*/
public function toolWithElicitation(RequestContext $context, string $message): string
{
$schema = new ElicitationSchema(
properties: [
'username' => new StringSchemaDefinition('Username'),
'email' => new StringSchemaDefinition('Email'),
],
);
$context->getClientGateway()->elicit($message, $schema);
return 'ok';
}
public function toolWithElicitationDefaults(RequestContext $context): string
{
$schema = new ElicitationSchema(
properties: [
'name' => new StringSchemaDefinition('Name', default: 'John Doe'),
'age' => new NumberSchemaDefinition('Age', integerOnly: true, default: 30),
'score' => new NumberSchemaDefinition('Score', default: 95.5),
'status' => new EnumSchemaDefinition('Status', enum: ['active', 'inactive', 'pending'], default: 'active'),
'verified' => new BooleanSchemaDefinition('Verified', default: true),
],
);
$context->getClientGateway()->elicit('Provide profile information', $schema);
return 'ok';
}
public function toolWithElicitationEnums(RequestContext $context): string
{
$schema = new ElicitationSchema(
properties: [
'untitledSingle' => new EnumSchemaDefinition('Untitled Single', enum: ['option1', 'option2', 'option3']),
'titledSingle' => new TitledEnumSchemaDefinition('Titled Single', oneOf: [
['const' => 'value1', 'title' => 'Label 1'],
['const' => 'value2', 'title' => 'Label 2'],
['const' => 'value3', 'title' => 'Label 3'],
]),
'legacyEnum' => new EnumSchemaDefinition('Legacy Enum', enum: ['opt1', 'opt2', 'opt3'], enumNames: ['Option 1', 'Option 2', 'Option 3']),
'untitledMulti' => new MultiSelectEnumSchemaDefinition('Untitled Multi', enum: ['option1', 'option2', 'option3']),
'titledMulti' => new TitledMultiSelectEnumSchemaDefinition('Titled Multi', anyOf: [
['const' => 'value1', 'title' => 'Label 1'],
['const' => 'value2', 'title' => 'Label 2'],
['const' => 'value3', 'title' => 'Label 3'],
]),
],
);
$context->getClientGateway()->elicit('Select options', $schema);
return 'ok';
}
public function resourceTemplate(string $id): TextResourceContents
{
return new TextResourceContents(