* feat(schema): add top-level title to Tool Mirror PR #278's Prompt change for Tool: - Add optional ?string $title to Mcp\Schema\Tool between $name and $inputSchema. fromArray() now reads $data['title']; jsonSerialize() emits 'title' right after 'name'. The @phpstan-type ToolData gains title?: string. - Add ?string $title to the #[McpTool] attribute between $name and $description. - Note ToolAnnotations::$title as deprecated-for-display in favor of Tool::$title per MCP spec 2025-06-18 (PHPDoc only, field retained for BC). Phase 1 of plan 01--tool-title-spec-compliance. Later phases update Discoverer, Builder::addTool(), ArrayLoader, and the conformance fixture to match the new signature. * feat(server): thread title through discovery - Discoverer::processMethod() now propagates McpTool::$title into every discovered Tool via named arguments. - Builder::addTool() accepts ?string $title between $name and $description. [BC Break] positional callers passing the old $description as 3rd positional argument must switch to named arguments, matching the precedent set for addPrompt() in 0.5.0. - tests/Conformance/server.php migrated to named-argument form for every addTool() call. Phase 2 of plan 01--tool-title-spec-compliance. * feat(loader): forward title in ArrayLoader ArrayLoader now reads 'title' from each tool config entry and passes it into the Tool constructor, completing the wiring from Builder::addTool() through array-driven registration. The $tools @param array-shape typedef gains title: ?string between name and description. Phase 3 of plan 01--tool-title-spec-compliance. * test(schema): cover Tool.title end-to-end - New tests/Unit/Schema/ToolTest.php: constructor round-trip, jsonSerialize key-order when title set vs null, fromArray tolerance. - New tests/Unit/Capability/Discovery/DiscovererToolTitleTest plus a TitleFixture class verifying McpTool(title: ...) propagates through discovery into the registered Tool. - New tests/Unit/Capability/Registry/Loader/ArrayLoader... test verifying array-driven title propagation. - Extend McpToolTest for title named-arg and default-null. - Extend BuilderTest with addTool(title: 't') propagation. - Update tests/Unit/.../RegistryTest, CallToolHandlerTest, ListToolsHandlerTest and examples/.../server.php new Tool(...) call sites to pass title (named args) so they match the new constructor signature. - CHANGELOG 0.6.0 section: feature bullet + [BC Break] note for addTool() signature, mirroring 0.5.0 Prompt entries. - docs/server-builder.md: addTool() row and prose now list title? between name and description. Phase 4 of plan 01--tool-title-spec-compliance. All 730 phpunit tests pass; remaining phpstan warnings are pre-existing and unrelated to Tool.title. * refactor(schema): make Tool.title non-BC-breaking Move `title` to the end of the Tool, Builder::addTool, and McpTool attribute constructors with a default of null. This preserves positional-argument compatibility and removes the need for `title: null` boilerplate at call sites. JSON serialization order (name, title, inputSchema, ...) is unchanged. Also tighten the new tests: parameterize ToolTest with data providers, fold the Discoverer title fixture into the existing DiscoverableToolHandler, and drop the reflection-heavy Builder test that overlapped with ArrayLoaderToolTitleTest. * fix: move title after name --------- Co-authored-by: DDEV User <nobody@example.com>
291 lines
10 KiB
PHP
291 lines
10 KiB
PHP
<?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\Example\Server\Elicitation;
|
|
|
|
use Mcp\Capability\Attribute\McpTool;
|
|
use Mcp\Schema\Elicitation\BooleanSchemaDefinition;
|
|
use Mcp\Schema\Elicitation\ElicitationSchema;
|
|
use Mcp\Schema\Elicitation\EnumSchemaDefinition;
|
|
use Mcp\Schema\Elicitation\NumberSchemaDefinition;
|
|
use Mcp\Schema\Elicitation\StringSchemaDefinition;
|
|
use Mcp\Server\RequestContext;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
/**
|
|
* Example handlers demonstrating the elicitation feature.
|
|
*
|
|
* Elicitation allows servers to request additional information from users
|
|
* during tool execution. The user can accept (providing data), decline,
|
|
* or cancel the request.
|
|
*/
|
|
final class ElicitationHandlers
|
|
{
|
|
public function __construct(
|
|
private readonly LoggerInterface $logger,
|
|
) {
|
|
$this->logger->info('ElicitationHandlers instantiated.');
|
|
}
|
|
|
|
/**
|
|
* Book a restaurant reservation with user elicitation.
|
|
*
|
|
* Demonstrates multi-field elicitation with different field types:
|
|
* - Number field for party size with validation
|
|
* - String field with date format for reservation date
|
|
* - Enum field for dietary restrictions with human-readable labels
|
|
*
|
|
* @return array{status: string, message: string, booking?: array{party_size: int, date: string, dietary: string}}
|
|
*/
|
|
#[McpTool(name: 'book_restaurant', description: 'Book a restaurant reservation, collecting details via elicitation.')]
|
|
public function bookRestaurant(RequestContext $context, string $restaurantName): array
|
|
{
|
|
if (!$context->getClientGateway()->supportsElicitation()) {
|
|
return [
|
|
'status' => 'error',
|
|
'message' => 'Client does not support elicitation. Please provide reservation details (party_size, date, dietary) as tool parameters instead.',
|
|
];
|
|
}
|
|
|
|
$client = $context->getClientGateway();
|
|
|
|
$this->logger->info(\sprintf('Starting reservation process for restaurant: %s', $restaurantName));
|
|
|
|
$schema = new ElicitationSchema(
|
|
properties: [
|
|
'party_size' => new NumberSchemaDefinition(
|
|
title: 'Party Size',
|
|
integerOnly: true,
|
|
description: 'Number of guests in your party',
|
|
default: 2,
|
|
minimum: 1,
|
|
maximum: 20,
|
|
),
|
|
'date' => new StringSchemaDefinition(
|
|
title: 'Reservation Date',
|
|
description: 'Preferred date for your reservation',
|
|
format: 'date',
|
|
),
|
|
'dietary' => new EnumSchemaDefinition(
|
|
title: 'Dietary Restrictions',
|
|
enum: ['none', 'vegetarian', 'vegan', 'gluten-free', 'halal', 'kosher'],
|
|
description: 'Any dietary restrictions or preferences',
|
|
default: 'none',
|
|
enumNames: ['None', 'Vegetarian', 'Vegan', 'Gluten-Free', 'Halal', 'Kosher'],
|
|
),
|
|
],
|
|
required: ['party_size', 'date'],
|
|
);
|
|
|
|
$result = $client->elicit(
|
|
message: \sprintf('Please provide your reservation details for %s:', $restaurantName),
|
|
requestedSchema: $schema,
|
|
timeout: 120,
|
|
);
|
|
|
|
if ($result->isDeclined()) {
|
|
$this->logger->info('User declined to provide reservation details.');
|
|
|
|
return [
|
|
'status' => 'declined',
|
|
'message' => 'Reservation request was declined by user.',
|
|
];
|
|
}
|
|
|
|
if ($result->isCancelled()) {
|
|
$this->logger->info('User cancelled the reservation request.');
|
|
|
|
return [
|
|
'status' => 'cancelled',
|
|
'message' => 'Reservation request was cancelled.',
|
|
];
|
|
}
|
|
|
|
$content = $result->content;
|
|
if (null === $content) {
|
|
throw new \RuntimeException('Expected content for accepted elicitation.');
|
|
}
|
|
|
|
if (!isset($content['party_size']) || !isset($content['date'])) {
|
|
throw new \RuntimeException('Missing required fields: party_size and date.');
|
|
}
|
|
|
|
$partySize = (int) $content['party_size'];
|
|
$date = (string) $content['date'];
|
|
$dietary = (string) ($content['dietary'] ?? 'none');
|
|
|
|
if ($partySize < 1 || $partySize > 20) {
|
|
throw new \RuntimeException(\sprintf('Invalid party size: %d. Must be between 1 and 20.', $partySize));
|
|
}
|
|
|
|
$this->logger->info(\sprintf(
|
|
'Booking confirmed: %d guests on %s with %s dietary requirements',
|
|
$partySize,
|
|
$date,
|
|
$dietary,
|
|
));
|
|
|
|
return [
|
|
'status' => 'confirmed',
|
|
'message' => \sprintf(
|
|
'Reservation confirmed at %s for %d guests on %s.',
|
|
$restaurantName,
|
|
$partySize,
|
|
$date,
|
|
),
|
|
'booking' => [
|
|
'party_size' => $partySize,
|
|
'date' => $date,
|
|
'dietary' => $dietary,
|
|
],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Confirm an action with a simple boolean elicitation.
|
|
*
|
|
* Demonstrates the simplest elicitation pattern - a yes/no confirmation.
|
|
*
|
|
* @return array{status: string, message: string}
|
|
*/
|
|
#[McpTool(name: 'confirm_action', description: 'Request user confirmation before proceeding with an action.')]
|
|
public function confirmAction(RequestContext $context, string $actionDescription): array
|
|
{
|
|
if (!$context->getClientGateway()->supportsElicitation()) {
|
|
return [
|
|
'status' => 'error',
|
|
'message' => 'Client does not support elicitation. Please confirm the action explicitly in your request.',
|
|
];
|
|
}
|
|
|
|
$client = $context->getClientGateway();
|
|
|
|
$schema = new ElicitationSchema(
|
|
properties: [
|
|
'confirm' => new BooleanSchemaDefinition(
|
|
title: 'Confirm',
|
|
description: 'Check to confirm you want to proceed',
|
|
default: false,
|
|
),
|
|
],
|
|
required: ['confirm'],
|
|
);
|
|
|
|
$result = $client->elicit(
|
|
message: \sprintf('Are you sure you want to: %s?', $actionDescription),
|
|
requestedSchema: $schema,
|
|
);
|
|
|
|
if (!$result->isAccepted()) {
|
|
return [
|
|
'status' => 'not_confirmed',
|
|
'message' => 'Action was not confirmed by user.',
|
|
];
|
|
}
|
|
|
|
$content = $result->content;
|
|
if (null === $content) {
|
|
throw new \RuntimeException('Expected content for accepted elicitation.');
|
|
}
|
|
|
|
if (!isset($content['confirm'])) {
|
|
throw new \RuntimeException('Missing required field: confirm.');
|
|
}
|
|
|
|
$confirmed = (bool) $content['confirm'];
|
|
|
|
if (!$confirmed) {
|
|
return [
|
|
'status' => 'not_confirmed',
|
|
'message' => 'User did not check the confirmation box.',
|
|
];
|
|
}
|
|
|
|
$this->logger->info(\sprintf('User confirmed action: %s', $actionDescription));
|
|
|
|
return [
|
|
'status' => 'confirmed',
|
|
'message' => \sprintf('Action confirmed: %s', $actionDescription),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Collect user feedback using elicitation.
|
|
*
|
|
* Demonstrates elicitation with optional fields and enum with labels.
|
|
*
|
|
* @return array{status: string, message: string, feedback?: array{rating: string, comments: string}}
|
|
*/
|
|
#[McpTool(name: 'collect_feedback', description: 'Collect user feedback via elicitation form.')]
|
|
public function collectFeedback(RequestContext $context, string $topic): array
|
|
{
|
|
if (!$context->getClientGateway()->supportsElicitation()) {
|
|
return [
|
|
'status' => 'error',
|
|
'message' => 'Client does not support elicitation. Please provide feedback (rating 1-5, comments) as tool parameters instead.',
|
|
];
|
|
}
|
|
|
|
$client = $context->getClientGateway();
|
|
|
|
$schema = new ElicitationSchema(
|
|
properties: [
|
|
'rating' => new EnumSchemaDefinition(
|
|
title: 'Rating',
|
|
enum: ['1', '2', '3', '4', '5'],
|
|
description: 'Rate your experience from 1 (poor) to 5 (excellent)',
|
|
enumNames: ['1 - Poor', '2 - Fair', '3 - Good', '4 - Very Good', '5 - Excellent'],
|
|
),
|
|
'comments' => new StringSchemaDefinition(
|
|
title: 'Comments',
|
|
description: 'Any additional comments or suggestions (optional)',
|
|
maxLength: 500,
|
|
),
|
|
],
|
|
required: ['rating'],
|
|
);
|
|
|
|
$result = $client->elicit(
|
|
message: \sprintf('Please provide your feedback about: %s', $topic),
|
|
requestedSchema: $schema,
|
|
);
|
|
|
|
if (!$result->isAccepted()) {
|
|
return [
|
|
'status' => 'skipped',
|
|
'message' => 'User chose not to provide feedback.',
|
|
];
|
|
}
|
|
|
|
$content = $result->content;
|
|
if (null === $content) {
|
|
throw new \RuntimeException('Expected content for accepted elicitation.');
|
|
}
|
|
|
|
if (!isset($content['rating'])) {
|
|
throw new \RuntimeException('Missing required field: rating.');
|
|
}
|
|
|
|
$rating = (string) $content['rating'];
|
|
$comments = (string) ($content['comments'] ?? '');
|
|
|
|
$this->logger->info(\sprintf('Feedback received: rating=%s, comments=%s', $rating, $comments));
|
|
|
|
return [
|
|
'status' => 'received',
|
|
'message' => 'Thank you for your feedback!',
|
|
'feedback' => [
|
|
'rating' => $rating,
|
|
'comments' => $comments,
|
|
],
|
|
];
|
|
}
|
|
}
|