Add title parameter to Builder::addResource() and addResourceTemplate() (#310)

#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>
This commit is contained in:
Christopher Hertel 2026-05-18 20:02:01 +02:00 • committed by GitHub
commit 1300d2ee7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,69 @@
<?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\Tests\Unit\Capability\Registry\Loader;
use Mcp\Capability\Registry;
use Mcp\Capability\Registry\Loader\ArrayLoader;
use PHPUnit\Framework\TestCase;
class ArrayLoaderResourceTitleTest extends TestCase
{
public function testLoadPropagatesResourceTitleToRegisteredResource(): void
{
$resources = [
[
'handler' => static fn (): string => 'ok',
'uri' => 'config://app/settings',
'name' => 'app_settings',
'title' => 'Application Settings',
'description' => null,
'mimeType' => null,
'size' => null,
'annotations' => null,
'icons' => null,
'meta' => null,
],
];
$loader = new ArrayLoader([], $resources);
$registry = new Registry();
$loader->load($registry);
$resourceRef = $registry->getResource('config://app/settings');
$this->assertSame('Application Settings', $resourceRef->resource->title);
}
public function testLoadPropagatesResourceTemplateTitleToRegisteredTemplate(): void
{
$resourceTemplates = [
[
'handler' => static fn (): string => 'ok',
'uriTemplate' => 'user://{userId}/profile',
'name' => 'user_profile',
'title' => 'User Profile',
'description' => null,
'mimeType' => null,
'annotations' => null,
'meta' => null,
],
];
$loader = new ArrayLoader([], [], $resourceTemplates);
$registry = new Registry();
$loader->load($registry);
$templateRef = $registry->getResourceTemplate('user://{userId}/profile');
$this->assertSame('User Profile', $templateRef->resourceTemplate->title);
}
}