Skip to content

Commit 72c9db4

Browse files
chr-hertelclaude
andcommitted
Add title parameter to Builder::addResource() and addResourceTemplate()
#301 added the `title` field to the `Resource`/`ResourceTemplate` schema classes and the `McpResource`/`McpResourceTemplate` attributes, but the manual registration methods on `Builder` were never updated — so manual registration could not set a resource title, unlike tools and prompts. Add a `$title` parameter between `$name` and `$description` (matching `addTool()`/`addPrompt()`) and propagate it through `ArrayLoader` when constructing the `Resource`/`ResourceTemplate` objects. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent a820497 commit 72c9db4

4 files changed

Lines changed: 84 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ All notable changes to `mcp/sdk` will be documented in this file.
1616
* [BC Break] `RegistryInterface::registerTool()`, `registerResource()`, `registerResourceTemplate()`, `registerPrompt()` lost their trailing `bool $isManual = false` parameter. Callers using positional arguments must drop the flag.
1717
* [BC Break] Removed `RegistryInterface::clear()`, `getDiscoveryState()`, `setDiscoveryState()`. Rediscovery now goes through `DiscoveryLoader::load()` directly.
1818
* `Registry::register*()` semantics changed to plain last-write-wins (overwrites silently) and the methods now return the stored `*Reference`. The previous "discovered registration is ignored when a manual one already exists" precedence rule still applies, but is now enforced by `DiscoveryLoader` via reference-identity tracking — and still emits a debug log when a discovery is skipped due to a conflicting registration.
19+
* Add optional `title` parameter to `Builder::addResource()` and `Builder::addResourceTemplate()` for MCP spec compliance
20+
* [BC Break] `Builder::addResource()` signature changed — `$title` parameter added between `$name` and `$description`. Callers using positional arguments must switch to named arguments.
21+
* [BC Break] `Builder::addResourceTemplate()` signature changed — `$title` parameter added between `$name` and `$description`. Callers using positional arguments must switch to named arguments.
1922

2023
0.5.0
2124
-----

src/Capability/Registry/Loader/ArrayLoader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ final class ArrayLoader implements LoaderInterface
5656
* handler: Handler,
5757
* uri: string,
5858
* name: ?string,
59+
* title: ?string,
5960
* description: ?string,
6061
* mimeType: ?string,
6162
* size: int|null,
@@ -67,6 +68,7 @@ final class ArrayLoader implements LoaderInterface
6768
* handler: Handler,
6869
* uriTemplate: string,
6970
* name: ?string,
71+
* title: ?string,
7072
* description: ?string,
7173
* mimeType: ?string,
7274
* annotations: ?Annotations,
@@ -157,6 +159,7 @@ public function load(RegistryInterface $registry): void
157159
$resource = new Resource(
158160
uri: $data['uri'],
159161
name: $name,
162+
title: $data['title'] ?? null,
160163
description: $description,
161164
mimeType: $data['mimeType'] ?? null,
162165
annotations: $data['annotations'] ?? null,
@@ -197,6 +200,7 @@ public function load(RegistryInterface $registry): void
197200
$template = new ResourceTemplate(
198201
uriTemplate: $data['uriTemplate'],
199202
name: $name,
203+
title: $data['title'] ?? null,
200204
description: $description,
201205
mimeType: $data['mimeType'] ?? null,
202206
annotations: $data['annotations'] ?? null,

src/Server/Builder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ final class Builder
119119
* handler: Handler,
120120
* uri: string,
121121
* name: ?string,
122+
* title: ?string,
122123
* description: ?string,
123124
* mimeType: ?string,
124125
* size: int|null,
@@ -134,6 +135,7 @@ final class Builder
134135
* handler: Handler,
135136
* uriTemplate: string,
136137
* name: ?string,
138+
* title: ?string,
137139
* description: ?string,
138140
* mimeType: ?string,
139141
* annotations: ?Annotations,
@@ -433,13 +435,15 @@ public function addTool(
433435
* Manually registers a resource handler.
434436
*
435437
* @param Handler $handler
438+
* @param ?string $title Optional human-readable title for display in UI
436439
* @param ?Icon[] $icons
437440
* @param array<string, mixed>|null $meta
438441
*/
439442
public function addResource(
440443
\Closure|array|string $handler,
441444
string $uri,
442445
?string $name = null,
446+
?string $title = null,
443447
?string $description = null,
444448
?string $mimeType = null,
445449
?int $size = null,
@@ -451,6 +455,7 @@ public function addResource(
451455
'handler',
452456
'uri',
453457
'name',
458+
'title',
454459
'description',
455460
'mimeType',
456461
'size',
@@ -466,12 +471,14 @@ public function addResource(
466471
* Manually registers a resource template handler.
467472
*
468473
* @param Handler $handler
474+
* @param ?string $title Optional human-readable title for display in UI
469475
* @param array<string, mixed>|null $meta
470476
*/
471477
public function addResourceTemplate(
472478
\Closure|array|string $handler,
473479
string $uriTemplate,
474480
?string $name = null,
481+
?string $title = null,
475482
?string $description = null,
476483
?string $mimeType = null,
477484
?Annotations $annotations = null,
@@ -481,6 +488,7 @@ public function addResourceTemplate(
481488
'handler',
482489
'uriTemplate',
483490
'name',
491+
'title',
484492
'description',
485493
'mimeType',
486494
'annotations',
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the official PHP MCP SDK.
5+
*
6+
* A collaboration between Symfony and the PHP Foundation.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Mcp\Tests\Unit\Capability\Registry\Loader;
13+
14+
use Mcp\Capability\Registry;
15+
use Mcp\Capability\Registry\Loader\ArrayLoader;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class ArrayLoaderResourceTitleTest extends TestCase
19+
{
20+
public function testLoadPropagatesResourceTitleToRegisteredResource(): void
21+
{
22+
$resources = [
23+
[
24+
'handler' => static fn (): string => 'ok',
25+
'uri' => 'config://app/settings',
26+
'name' => 'app_settings',
27+
'title' => 'Application Settings',
28+
'description' => null,
29+
'mimeType' => null,
30+
'size' => null,
31+
'annotations' => null,
32+
'icons' => null,
33+
'meta' => null,
34+
],
35+
];
36+
37+
$loader = new ArrayLoader([], $resources);
38+
$registry = new Registry();
39+
40+
$loader->load($registry);
41+
42+
$resourceRef = $registry->getResource('config://app/settings');
43+
$this->assertSame('Application Settings', $resourceRef->resource->title);
44+
}
45+
46+
public function testLoadPropagatesResourceTemplateTitleToRegisteredTemplate(): void
47+
{
48+
$resourceTemplates = [
49+
[
50+
'handler' => static fn (): string => 'ok',
51+
'uriTemplate' => 'user://{userId}/profile',
52+
'name' => 'user_profile',
53+
'title' => 'User Profile',
54+
'description' => null,
55+
'mimeType' => null,
56+
'annotations' => null,
57+
'meta' => null,
58+
],
59+
];
60+
61+
$loader = new ArrayLoader([], [], $resourceTemplates);
62+
$registry = new Registry();
63+
64+
$loader->load($registry);
65+
66+
$templateRef = $registry->getResourceTemplate('user://{userId}/profile');
67+
$this->assertSame('User Profile', $templateRef->resourceTemplate->title);
68+
}
69+
}

0 commit comments

Comments
 (0)