The MCP specification (2025-11-25) defines an optional title field for Resource and ResourceTemplate as a human-readable display label, distinct from name (a short identifier). Tool and Prompt already implement this field; Resource and ResourceTemplate were missing it. Changes: - Add title parameter to Resource and ResourceTemplate constructors - Add title to fromArray(), jsonSerialize(), and PHPStan type arrays - Add title to McpResource and McpResourceTemplate attributes - Pass title through Discoverer when constructing schema objects - Add tests for deserialization, serialization, and null omission Fixes #296
144 lines
4.1 KiB
PHP
144 lines
4.1 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\Tests\Unit\Schema;
|
|
|
|
use Mcp\Exception\InvalidArgumentException;
|
|
use Mcp\Schema\Resource;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ResourceTest extends TestCase
|
|
{
|
|
private const VALID_URI = 'https://example.com/list-books';
|
|
|
|
public function testConstructorValid(): void
|
|
{
|
|
$uri = self::VALID_URI;
|
|
|
|
$resource = new Resource(
|
|
uri: $uri,
|
|
name: 'list-books',
|
|
);
|
|
|
|
$this->assertInstanceOf(Resource::class, $resource);
|
|
$this->assertSame($uri, $resource->uri);
|
|
}
|
|
|
|
public function testConstructorInvalid(): void
|
|
{
|
|
$uri = '/list-books';
|
|
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage('Invalid resource URI: "/list-books" must be a valid URI with a scheme and optional path.');
|
|
|
|
$resource = new Resource(
|
|
uri: $uri,
|
|
name: 'list-books',
|
|
);
|
|
}
|
|
|
|
#[DataProvider('provideValidUris')]
|
|
public function testConstructorAcceptsUris(string $uri): void
|
|
{
|
|
$resource = new Resource(
|
|
uri: $uri,
|
|
name: 'test-resource',
|
|
);
|
|
|
|
$this->assertInstanceOf(Resource::class, $resource);
|
|
$this->assertSame($uri, $resource->uri);
|
|
}
|
|
|
|
public static function provideValidUris(): iterable
|
|
{
|
|
yield 'urn' => ['urn:isbn:0451450523'];
|
|
yield 'mailto' => ['mailto:user@example.com'];
|
|
yield 'data' => ['data:text/plain;base64,SGVsbG8='];
|
|
yield 'custom scheme without slashes' => ['config:myapp/settings'];
|
|
yield 'custom scheme with slashes' => ['config://myapp/settings'];
|
|
}
|
|
|
|
public function testFromArrayValid(): void
|
|
{
|
|
$resource = Resource::fromArray([
|
|
'uri' => self::VALID_URI,
|
|
'name' => 'list-books',
|
|
]);
|
|
|
|
$this->assertInstanceOf(Resource::class, $resource);
|
|
$this->assertSame(self::VALID_URI, $resource->uri);
|
|
$this->assertSame('list-books', $resource->name);
|
|
$this->assertNull($resource->title);
|
|
$this->assertNull($resource->description);
|
|
$this->assertNull($resource->meta);
|
|
}
|
|
|
|
public function testTitleFromArray(): void
|
|
{
|
|
$resource = Resource::fromArray([
|
|
'uri' => self::VALID_URI,
|
|
'name' => 'list-books',
|
|
'title' => 'Book Listing',
|
|
]);
|
|
|
|
$this->assertSame('Book Listing', $resource->title);
|
|
}
|
|
|
|
public function testTitleSerialization(): void
|
|
{
|
|
$resource = new Resource(
|
|
uri: self::VALID_URI,
|
|
name: 'list-books',
|
|
title: 'Book Listing',
|
|
);
|
|
|
|
$data = $resource->jsonSerialize();
|
|
$this->assertSame('Book Listing', $data['title']);
|
|
}
|
|
|
|
public function testTitleOmittedWhenNull(): void
|
|
{
|
|
$resource = new Resource(
|
|
uri: self::VALID_URI,
|
|
name: 'list-books',
|
|
);
|
|
|
|
$data = $resource->jsonSerialize();
|
|
$this->assertArrayNotHasKey('title', $data);
|
|
}
|
|
|
|
#[DataProvider('provideInvalidResources')]
|
|
public function testFromArrayInvalid(array $input, string $expectedExceptionMessage): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
$this->expectExceptionMessage($expectedExceptionMessage);
|
|
|
|
Resource::fromArray($input);
|
|
}
|
|
|
|
public static function provideInvalidResources(): iterable
|
|
{
|
|
yield 'missing uri' => [[], 'Invalid or missing "uri" in Resource data.'];
|
|
yield 'missing name' => [
|
|
['uri' => self::VALID_URI],
|
|
'Invalid or missing "name" in Resource data.',
|
|
];
|
|
yield 'meta' => [
|
|
[
|
|
'uri' => self::VALID_URI,
|
|
'name' => 'list-books',
|
|
'_meta' => 'foo',
|
|
],
|
|
'Invalid "_meta" in Resource data.',
|
|
];
|
|
}
|
|
}
|