Add title field to Prompt for MCP spec compliance (#278)

The MCP 2025-11-25 specification defines an optional `title` property on
Prompt for human-readable display in UI contexts, distinct from the
programmatic `name` identifier. This was missing from the SDK.

BC Break: Builder::addPrompt() signature changed — $title parameter added
between $name and $description. Callers using positional arguments for
$description must switch to named arguments.

Closes #276
This commit is contained in:
Christopher Hertel 2026-04-14 10:04:35 +02:00 • committed by GitHub
commit 7af5a345c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 21 additions and 6 deletions

View file

@ -24,12 +24,14 @@ class McpPrompt
{
/**
* @param ?string $name overrides the prompt name (defaults to method name)
* @param ?string $title Optional human-readable title for display in UI
* @param ?string $description Optional description of the prompt. Defaults to method DocBlock summary.
* @param ?Icon[] $icons Optional list of icon URLs representing the prompt
* @param ?array<string, mixed> $meta Optional metadata
*/
public function __construct(
public ?string $name = null,
public ?string $title = null,
public ?string $description = null,
public ?array $icons = null,
public ?array $meta = null,

View file

@ -276,7 +276,7 @@ final class Discoverer implements DiscovererInterface
$paramTag = $paramTags['$'.$param->getName()] ?? null;
$arguments[] = new PromptArgument($param->getName(), $paramTag ? trim((string) $paramTag->getDescription()) : null, !$param->isOptional() && !$param->isDefaultValueAvailable());
}
$prompt = new Prompt($name, $description, $arguments, $instance->icons, $instance->meta);
$prompt = new Prompt($name, $instance->title, $description, $arguments, $instance->icons, $instance->meta);
$completionProviders = $this->getCompletionProviders($method);
$prompts[$name] = new PromptReference($prompt, [$className, $methodName], false, $completionProviders);
++$discoveredCount['prompts'];

View file

@ -252,6 +252,7 @@ final class ArrayLoader implements LoaderInterface
}
$prompt = new Prompt(
name: $name,
title: $data['title'] ?? null,
description: $description,
arguments: $arguments,
icons: $data['icons'] ?? null,

View file

@ -21,6 +21,7 @@ use Mcp\Exception\InvalidArgumentException;
*
* @phpstan-type PromptData array{
* name: string,
* title?: string,
* description?: string,
* arguments?: PromptArgumentData[],
* icons?: IconData[],
@ -33,6 +34,7 @@ class Prompt implements \JsonSerializable
{
/**
* @param string $name the name of the prompt or prompt template
* @param ?string $title Optional human-readable title for display in UI
* @param ?string $description an optional description of what this prompt provides
* @param ?PromptArgument[] $arguments A list of arguments for templating. Null if not a template.
* @param ?Icon[] $icons optional icons representing the prompt
@ -40,6 +42,7 @@ class Prompt implements \JsonSerializable
*/
public function __construct(
public readonly string $name,
public readonly ?string $title = null,
public readonly ?string $description = null,
public readonly ?array $arguments = null,
public readonly ?array $icons = null,
@ -73,6 +76,7 @@ class Prompt implements \JsonSerializable
return new self(
name: $data['name'],
title: $data['title'] ?? null,
description: $data['description'] ?? null,
arguments: $arguments,
icons: isset($data['icons']) && \is_array($data['icons']) ? array_map(Icon::fromArray(...), $data['icons']) : null,
@ -83,6 +87,7 @@ class Prompt implements \JsonSerializable
/**
* @return array{
* name: string,
* title?: string,
* description?: string,
* arguments?: array<PromptArgument>,
* icons?: Icon[],
@ -92,6 +97,9 @@ class Prompt implements \JsonSerializable
public function jsonSerialize(): array
{
$data = ['name' => $this->name];
if (null !== $this->title) {
$data['title'] = $this->title;
}
if (null !== $this->description) {
$data['description'] = $this->description;
}

View file

@ -473,11 +473,12 @@ final class Builder
public function addPrompt(
\Closure|array|string $handler,
?string $name = null,
?string $title = null,
?string $description = null,
?array $icons = null,
?array $meta = null,
): self {
$this->prompts[] = compact('handler', 'name', 'description', 'icons', 'meta');
$this->prompts[] = compact('handler', 'name', 'title', 'description', 'icons', 'meta');
return $this;
}