Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ All notable changes to `mcp/sdk` will be documented in this file.
### Server

* Fix const validation errors always reporting the expected value as null.
* Add `icons` to `#[McpResourceTemplate]` and `Builder::addResourceTemplate()`.
* Add `ContextAwareProviderInterface`, a completion provider that receives the arguments the client sent in `context.arguments` of `completion/complete`.

### Client

* Add an optional `$context` argument to `Client::complete()`, sent as `context` of `completion/complete`.

### Schema

* Add `icons` to `ResourceTemplate`, so `resources/templates/list` carries them.
* Add `_meta` to `TextContent`, `ImageContent`, `AudioContent` and `EmbeddedResource`, as `$meta` constructor argument and property.
* Add `lastModified` to `Annotations`, the ISO 8601 moment a resource or content block was last modified.
* Add `context` to `CompletionCompleteRequest`. A `context` or `context.arguments` that is not an object, or an argument value that is not a string, is rejected with `-32602`.

0.10.0
------
Expand Down
11 changes: 11 additions & 0 deletions docs/client/capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,14 @@ foreach ($completionResult->values as $value) {
echo "Suggestion: {$value}\n";
}
```

When the prompt or URI template has several arguments, pass the ones already filled in as `context`, so the server can
narrow its suggestions:

```php
$completionResult = $client->complete(
ref: new PromptReference('code_review'),
argument: ['name' => 'framework', 'value' => 'fla'],
context: ['arguments' => ['language' => 'python']],
);
```
2 changes: 1 addition & 1 deletion docs/run/server-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ $server = Server::builder()
| `addNotificationHandlers()` | handlers | Prepend multiple custom notification handlers |
| `addTool()` | handler, name?, title?, description?, annotations?, inputSchema?, icons?, meta?, outputSchema? | Register tool |
| `addResource()` | handler, uri, name?, title?, description?, mimeType?, size?, annotations?, icons?, meta? | Register resource |
| `addResourceTemplate()` | handler, uriTemplate, name?, title?, description?, mimeType?, annotations?, meta? | Register resource template |
| `addResourceTemplate()` | handler, uriTemplate, name?, title?, description?, mimeType?, annotations?, meta?, icons? | Register resource template |
| `addPrompt()` | handler, name?, title?, description?, icons?, meta? | Register prompt |
| `add()` | definition, handler, completionProviders? | Register an element from a schema VO + handler pair, see [Explicit element registration](../servers/registration.md#explicit-element-registration) |
| `build()` | - | Create the server instance |
Expand Down
29 changes: 29 additions & 0 deletions docs/servers/completions.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,35 @@ public function getUserProfile(
}
```

A provider whose suggestions depend on the other arguments implements `ContextAwareProviderInterface` instead. It
receives the arguments the client already resolved, from the request's `context.arguments`:

```php
use Mcp\Server\Capability\Completion\ContextAwareProviderInterface;

class FrameworkCompletionProvider implements ContextAwareProviderInterface
{
private const FRAMEWORKS = [
'python' => ['flask', 'django', 'fastapi'],
'php' => ['symfony', 'laravel'],
];

public function getCompletions(string $currentValue): array
{
return $this->getCompletionsWithContext($currentValue, []);
}

public function getCompletionsWithContext(string $currentValue, array $arguments): array
{
$frameworks = self::FRAMEWORKS[$arguments['language'] ?? ''] ?? array_merge(...array_values(self::FRAMEWORKS));

return array_values(array_filter($frameworks, static fn (string $f): bool => str_starts_with($f, $currentValue)));
}
}
```

The context is optional, so the arguments can be empty or miss the ones you look for.

**Provider Resolution:**

- **Class strings** (`Provider::class`) → Resolved from the PSR-11 container, or created with `new Provider()` if the container doesn't have it
Expand Down
1 change: 1 addition & 0 deletions docs/servers/registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ $server = Server::builder()
- `mimeType` (string|null): Optional MIME type of the resource
- `annotations` (Annotations|null): Optional annotations for the resource template
- `meta` (array|null): Optional metadata for the resource template
- `icons` (Icon[]|null): Optional array of icons for the resource template

### Manual Prompt Registration

Expand Down
1 change: 1 addition & 0 deletions docs/servers/resource-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class UserProvider
- **`mimeType`** (optional): MIME type of the resource content.
- **`annotations`** (optional): Additional metadata.
- **`meta`** (optional): Arbitrary key-value pairs for custom metadata.
- **`icons`** (optional): Array of `Icon` objects for visual representation.

## Variable Rules

Expand Down
3 changes: 2 additions & 1 deletion docs/servers/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class ConfigProvider
- **`description`** (optional): Resource description. Falls back to the docblock (summary plus long description).
- **`mimeType`** (optional): MIME type of the resource content.
- **`size`** (optional): Size in bytes if known.
- **`annotations`** (optional): Additional metadata.
- **`annotations`** (optional): An `Annotations` object with hints for the client: `audience`, `priority` and
`lastModified` (an ISO 8601 timestamp such as `2025-01-12T15:00:58Z`).
- **`icons`** (optional): Array of `Icon` objects for visual representation.
- **`meta`** (optional): Arbitrary key-value pairs for custom metadata.

Expand Down
6 changes: 6 additions & 0 deletions docs/servers/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public function getResourceLink(): ResourceLink
}
```

Every content type also takes optional `annotations` and `meta`; the latter is sent as the block's `_meta`:

```php
new TextContent('Found 3 matches', meta: ['com.example/query-id' => 'q-42']);
```

### Multiple Content Items

Return an array of content items:
Expand Down
9 changes: 5 additions & 4 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,15 @@ public function getPrompt(string $name, array $arguments = [], ?callable $onProg
/**
* Request completion suggestions for a prompt or resource argument.
*
* @param PromptReference|ResourceReference $ref The prompt or resource reference
* @param array{name: string, value: string} $argument The argument to complete
* @param PromptReference|ResourceReference $ref The prompt or resource reference
* @param array{name: string, value: string} $argument The argument to complete
* @param array{arguments?: array<string, string>}|null $context Previously resolved arguments, sent as `context`
*
* @throws AuthorizationException|RequestException|ConnectionException|InvalidArgumentException
*/
public function complete(PromptReference|ResourceReference $ref, array $argument): CompletionCompleteResult
public function complete(PromptReference|ResourceReference $ref, array $argument, ?array $context = null): CompletionCompleteResult
{
$request = new CompletionCompleteRequest($ref, $argument);
$request = new CompletionCompleteRequest($ref, $argument, $context);

$response = $this->sendRequest($request);

Expand Down
14 changes: 12 additions & 2 deletions src/Schema/Annotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*
* @phpstan-type AnnotationsData array{
* audience?: string[],
* priority?: float
* priority?: float,
* lastModified?: string
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
Expand All @@ -36,10 +37,12 @@ class Annotations implements \JsonSerializable
* A value of 1 means "most important," and indicates that the data is
* effectively required, while 0 means "least important," and indicates that
* the data is entirely optional.
* @param string|null $lastModified the moment the resource was last modified, as an ISO 8601 formatted string (e.g. "2025-01-12T15:00:58Z")
*/
public function __construct(
public readonly ?array $audience = null,
public readonly ?float $priority = null,
public readonly ?string $lastModified = null,
) {
if (null !== $this->priority && ($this->priority < 0 || $this->priority > 1)) {
throw new InvalidArgumentException('Annotation priority must be between 0 and 1.');
Expand Down Expand Up @@ -75,10 +78,14 @@ static function (mixed $role): Role {
if (isset($data['priority']) && !\is_float($data['priority']) && !\is_int($data['priority'])) {
throw new InvalidArgumentException('Invalid "priority" in Annotations data; expected a number.');
}
if (isset($data['lastModified']) && !\is_string($data['lastModified'])) {
throw new InvalidArgumentException('Invalid "lastModified" in Annotations data; expected a string.');
}

return new self(
$audience,
isset($data['priority']) ? (float) $data['priority'] : null
isset($data['priority']) ? (float) $data['priority'] : null,
$data['lastModified'] ?? null,
);
}

Expand Down Expand Up @@ -112,6 +119,9 @@ public function jsonSerialize(): array
if (null !== $this->priority) {
$data['priority'] = $this->priority;
}
if (null !== $this->lastModified) {
$data['lastModified'] = $this->lastModified;
}

return $data;
}
Expand Down
18 changes: 17 additions & 1 deletion src/Schema/Content/AudioContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@
* data: string,
* mimeType: string,
* annotations?: AnnotationsData,
* _meta?: array<string, mixed>,
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class AudioContent extends Content
{
/**
* @param string $data Base64-encoded audio data
* @param string $mimeType The MIME type of the audio
* @param ?Annotations $annotations Optional annotations describing the content
* @param ?array<string, mixed> $meta Optional metadata
*/
public function __construct(
public readonly string $data,
public readonly string $mimeType,
public readonly ?Annotations $annotations = null,
public readonly ?array $meta = null,
) {
parent::__construct('audio');
}
Expand All @@ -50,11 +58,15 @@ public static function fromArray(array $data): self
if (!isset($data['mimeType']) || !\is_string($data['mimeType'])) {
throw new InvalidArgumentException('Missing or invalid "mimeType" in AudioContent data.');
}
if (isset($data['_meta']) && !\is_array($data['_meta'])) {
throw new InvalidArgumentException('Invalid "_meta" in AudioContent data.');
}

return new self(
$data['data'],
$data['mimeType'],
Annotations::tryFromArray($data['annotations'] ?? null, 'AudioContent')
Annotations::tryFromArray($data['annotations'] ?? null, 'AudioContent'),
$data['_meta'] ?? null,
);
}

Expand Down Expand Up @@ -101,6 +113,7 @@ public static function fromString(string $data, string $mimeType, ?Annotations $
* data: string,
* mimeType: string,
* annotations?: Annotations,
* _meta?: array<string, mixed>,
* }
*/
public function jsonSerialize(): array
Expand All @@ -114,6 +127,9 @@ public function jsonSerialize(): array
if (null !== $this->annotations) {
$result['annotations'] = $this->annotations;
}
if (null !== $this->meta) {
$result['_meta'] = $this->meta;
}

return $result;
}
Expand Down
15 changes: 14 additions & 1 deletion src/Schema/Content/EmbeddedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,21 @@
* @phpstan-type EmbeddedResourceData = array{
* type: 'resource',
* resource: TextResourceContentsData|BlobResourceContentsData,
* annotations?: AnnotationsData
* annotations?: AnnotationsData,
* _meta?: array<string, mixed>
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class EmbeddedResource extends Content
{
/**
* @param ?array<string, mixed> $meta optional metadata of the content block, unlike the `_meta` of the resource contents
*/
public function __construct(
public readonly TextResourceContents|BlobResourceContents $resource,
public readonly ?Annotations $annotations = null,
public readonly ?array $meta = null,
) {
parent::__construct('resource');
}
Expand All @@ -50,6 +55,9 @@ public static function fromArray(array $data): self
if (!isset($data['resource']) || !\is_array($data['resource'])) {
throw new InvalidArgumentException('Missing or invalid "resource" field for EmbeddedResource.');
}
if (isset($data['_meta']) && !\is_array($data['_meta'])) {
throw new InvalidArgumentException('Invalid "_meta" field for EmbeddedResource.');
}

$resourceData = $data['resource'];
if (isset($resourceData['text'])) {
Expand All @@ -63,6 +71,7 @@ public static function fromArray(array $data): self
return new self(
$resourceInstance,
Annotations::tryFromArray($data['annotations'] ?? null, 'EmbeddedResource'),
$data['_meta'] ?? null,
);
}

Expand Down Expand Up @@ -129,6 +138,7 @@ public static function fromSplFileInfo(string $uri, \SplFileInfo $file, ?string
* type: 'resource',
* resource: TextResourceContents|BlobResourceContents,
* annotations?: Annotations,
* _meta?: array<string, mixed>,
* }
*/
public function jsonSerialize(): array
Expand All @@ -140,6 +150,9 @@ public function jsonSerialize(): array
if (null !== $this->annotations) {
$data['annotations'] = $this->annotations;
}
if (null !== $this->meta) {
$data['_meta'] = $this->meta;
}

return $data;
}
Expand Down
19 changes: 15 additions & 4 deletions src/Schema/Content/ImageContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* data: string,
* mimeType: string,
* annotations?: AnnotationsData,
* _meta?: array<string, mixed>,
* }
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
Expand All @@ -31,14 +32,16 @@ class ImageContent extends Content
/**
* Create a new ImageContent instance.
*
* @param string $data Base64-encoded image data
* @param string $mimeType The MIME type of the image
* @param ?Annotations $annotations Optional annotations describing the content
* @param string $data Base64-encoded image data
* @param string $mimeType The MIME type of the image
* @param ?Annotations $annotations Optional annotations describing the content
* @param ?array<string, mixed> $meta Optional metadata
*/
public function __construct(
public readonly string $data,
public readonly string $mimeType,
public readonly ?Annotations $annotations = null,
public readonly ?array $meta = null,
) {
parent::__construct('image');
}
Expand All @@ -54,11 +57,15 @@ public static function fromArray(array $data): self
if (!isset($data['mimeType']) || !\is_string($data['mimeType'])) {
throw new InvalidArgumentException('Missing or invalid "mimeType" in ImageContent data.');
}
if (isset($data['_meta']) && !\is_array($data['_meta'])) {
throw new InvalidArgumentException('Invalid "_meta" in ImageContent data.');
}

return new self(
$data['data'],
$data['mimeType'],
isset($data['annotations']) ? Annotations::fromArray($data['annotations']) : null
isset($data['annotations']) ? Annotations::fromArray($data['annotations']) : null,
$data['_meta'] ?? null,
);
}

Expand Down Expand Up @@ -101,6 +108,7 @@ public static function fromString(string $data, string $mimeType, ?Annotations $
* data: string,
* mimeType: string,
* annotations?: Annotations,
* _meta?: array<string, mixed>,
* }
*/
public function jsonSerialize(): array
Expand All @@ -114,6 +122,9 @@ public function jsonSerialize(): array
if (null !== $this->annotations) {
$result['annotations'] = $this->annotations;
}
if (null !== $this->meta) {
$result['_meta'] = $this->meta;
}

return $result;
}
Expand Down
Loading