[Schema][Server] Harden sampling tools against the spec (#420)
* harden sampling tools against the spec Report tool-flow violations as -32602 instead of dropping the request, accept resource_link in tool results, gate tools on sampling.tools, and reject empty content. * normalize keyed content arrays to lists array_filter() and friends preserve keys, and a keyed array serializes as a JSON object instead of the content-block array the schema requires.
This commit is contained in:
parent
becce29c41
commit
43815e38a8
4 changed files with 1212 additions and 167 deletions
125
tests/Unit/Schema/Content/SamplingMessageTest.php
Normal file
125
tests/Unit/Schema/Content/SamplingMessageTest.php
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
<?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\Content;
|
||||
|
||||
use Mcp\Exception\InvalidArgumentException;
|
||||
use Mcp\Schema\Content\SamplingMessage;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\Content\ToolResultContent;
|
||||
use Mcp\Schema\Content\ToolUseContent;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SamplingMessageTest extends TestCase
|
||||
{
|
||||
public function testToolLoopMessagesRoundTrip(): void
|
||||
{
|
||||
$assistant = SamplingMessage::fromArray([
|
||||
'role' => 'assistant',
|
||||
'_meta' => ['provider' => 'test'],
|
||||
'content' => [
|
||||
['type' => 'text', 'text' => 'I will check.'],
|
||||
['type' => 'tool_use', 'id' => 'call-1', 'name' => 'weather', 'input' => ['city' => 'Paris']],
|
||||
],
|
||||
]);
|
||||
$user = SamplingMessage::fromArray([
|
||||
'role' => 'user',
|
||||
'content' => [[
|
||||
'type' => 'tool_result',
|
||||
'toolUseId' => 'call-1',
|
||||
'content' => [['type' => 'text', 'text' => '21 C']],
|
||||
'structuredContent' => ['temperature' => 21],
|
||||
]],
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(ToolUseContent::class, $assistant->content[1]);
|
||||
$this->assertSame(['provider' => 'test'], $assistant->meta);
|
||||
$this->assertSame(['provider' => 'test'], $assistant->jsonSerialize()['_meta']);
|
||||
$this->assertInstanceOf(ToolResultContent::class, $user->content[0]);
|
||||
$this->assertSame(['temperature' => 21], $user->content[0]->structuredContent);
|
||||
|
||||
$this->assertEquals($assistant, SamplingMessage::fromArray(json_decode(json_encode($assistant), true)));
|
||||
$this->assertEquals($user, SamplingMessage::fromArray(json_decode(json_encode($user), true)));
|
||||
}
|
||||
|
||||
public function testSingleContentBlockKeepsItsShape(): void
|
||||
{
|
||||
$message = SamplingMessage::fromArray(['role' => 'user', 'content' => ['type' => 'text', 'text' => 'hi']]);
|
||||
|
||||
$this->assertInstanceOf(TextContent::class, $message->content);
|
||||
$this->assertSame('{"role":"user","content":{"type":"text","text":"hi"}}', json_encode($message));
|
||||
$this->assertCount(1, $message->getContentBlocks());
|
||||
}
|
||||
|
||||
public function testSingleElementListKeepsItsShape(): void
|
||||
{
|
||||
$message = SamplingMessage::fromArray(['role' => 'user', 'content' => [['type' => 'text', 'text' => 'hi']]]);
|
||||
|
||||
$this->assertIsArray($message->content);
|
||||
$this->assertSame('{"role":"user","content":[{"type":"text","text":"hi"}]}', json_encode($message));
|
||||
$this->assertCount(1, $message->getContentBlocks());
|
||||
}
|
||||
|
||||
/**
|
||||
* The tool-flow rules span the whole message list, so a single message is never
|
||||
* rejected for carrying the "wrong" block — see CreateSamplingMessageRequestTest.
|
||||
*/
|
||||
public function testToolBlocksAreAcceptedRegardlessOfRole(): void
|
||||
{
|
||||
$message = new SamplingMessage(Role::User, new ToolUseContent('call-1', 'weather', []));
|
||||
|
||||
$this->assertInstanceOf(ToolUseContent::class, $message->content);
|
||||
}
|
||||
|
||||
public function testFilteredContentStillSerializesAsAnArray(): void
|
||||
{
|
||||
$blocks = [new TextContent('thinking'), new ToolUseContent('call-1', 'weather', [])];
|
||||
|
||||
// array_filter() preserves keys, so this list starts at index 1.
|
||||
$toolUses = array_filter($blocks, static fn ($block): bool => $block instanceof ToolUseContent);
|
||||
$message = new SamplingMessage(Role::Assistant, $toolUses);
|
||||
|
||||
$this->assertSame(
|
||||
'{"role":"assistant","content":[{"type":"tool_use","id":"call-1","name":"weather","input":{}}]}',
|
||||
json_encode($message),
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmptyContentListIsRejected(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
new SamplingMessage(Role::User, []);
|
||||
}
|
||||
|
||||
public function testEmptyContentListIsRejectedWhenHydrating(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
SamplingMessage::fromArray(['role' => 'user', 'content' => []]);
|
||||
}
|
||||
|
||||
public function testUnknownContentTypeIsRejected(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
SamplingMessage::fromArray(['role' => 'user', 'content' => ['type' => 'nope']]);
|
||||
}
|
||||
|
||||
public function testUnknownRoleIsRejected(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
/* @phpstan-ignore argument.type */
|
||||
SamplingMessage::fromArray(['role' => 'system', 'content' => ['type' => 'text', 'text' => 'hi']]);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
<?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\Content;
|
||||
|
||||
use Mcp\Exception\InvalidArgumentException;
|
||||
use Mcp\Schema\Content\SamplingMessage;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\Content\ToolResultContent;
|
||||
use Mcp\Schema\Content\ToolUseContent;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class SamplingToolContentTest extends TestCase
|
||||
{
|
||||
public function testToolLoopMessagesRoundTrip(): void
|
||||
{
|
||||
$assistant = SamplingMessage::fromArray([
|
||||
'role' => 'assistant',
|
||||
'_meta' => ['provider' => 'test'],
|
||||
'content' => [
|
||||
['type' => 'text', 'text' => 'I will check.'],
|
||||
['type' => 'tool_use', 'id' => 'call-1', 'name' => 'weather', 'input' => ['city' => 'Paris']],
|
||||
],
|
||||
]);
|
||||
$user = SamplingMessage::fromArray([
|
||||
'role' => 'user',
|
||||
'content' => [[
|
||||
'type' => 'tool_result',
|
||||
'toolUseId' => 'call-1',
|
||||
'content' => [['type' => 'text', 'text' => '21 C']],
|
||||
'structuredContent' => ['temperature' => 21],
|
||||
]],
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(ToolUseContent::class, $assistant->content[1]);
|
||||
$this->assertSame(['provider' => 'test'], $assistant->meta);
|
||||
$this->assertSame(['provider' => 'test'], $assistant->jsonSerialize()['_meta']);
|
||||
$this->assertInstanceOf(ToolResultContent::class, $user->content[0]);
|
||||
$this->assertSame(['temperature' => 21], $user->content[0]->structuredContent);
|
||||
$textContent = $user->content[0]->content[0];
|
||||
$this->assertInstanceOf(TextContent::class, $textContent);
|
||||
$this->assertSame('21 C', $textContent->text);
|
||||
}
|
||||
|
||||
public function testToolUseIsRejectedInUserMessage(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
new SamplingMessage(Role::User, new ToolUseContent('call-1', 'weather', []));
|
||||
}
|
||||
|
||||
public function testToolResultCannotBeMixedWithOtherContent(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
new SamplingMessage(Role::User, [
|
||||
new ToolResultContent('call-1', [new TextContent('done')]),
|
||||
new TextContent('extra'),
|
||||
]);
|
||||
}
|
||||
|
||||
public function testToolResultRejectsNonStandardContentBlocks(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
new ToolResultContent('call-1', [
|
||||
new SamplingMessage(Role::User, new TextContent('not a tool result content block')),
|
||||
]);
|
||||
}
|
||||
}
|
||||
119
tests/Unit/Schema/Content/ToolResultContentTest.php
Normal file
119
tests/Unit/Schema/Content/ToolResultContentTest.php
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
<?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\Content;
|
||||
|
||||
use Mcp\Exception\InvalidArgumentException;
|
||||
use Mcp\Schema\Content\EmbeddedResource;
|
||||
use Mcp\Schema\Content\ResourceLink;
|
||||
use Mcp\Schema\Content\SamplingMessage;
|
||||
use Mcp\Schema\Content\TextContent;
|
||||
use Mcp\Schema\Content\ToolResultContent;
|
||||
use Mcp\Schema\Enum\Role;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ToolResultContentTest extends TestCase
|
||||
{
|
||||
public function testRoundTrip(): void
|
||||
{
|
||||
$content = ToolResultContent::fromArray([
|
||||
'type' => 'tool_result',
|
||||
'toolUseId' => 'call-1',
|
||||
'content' => [['type' => 'text', 'text' => '21 C']],
|
||||
'structuredContent' => ['temperature' => 21],
|
||||
'isError' => true,
|
||||
'_meta' => ['provider' => 'test'],
|
||||
]);
|
||||
|
||||
$this->assertSame('tool_result', $content->type);
|
||||
$this->assertSame('call-1', $content->toolUseId);
|
||||
$this->assertSame(['temperature' => 21], $content->structuredContent);
|
||||
$this->assertTrue($content->isError);
|
||||
$this->assertSame(['provider' => 'test'], $content->meta);
|
||||
|
||||
$textContent = $content->content[0];
|
||||
$this->assertInstanceOf(TextContent::class, $textContent);
|
||||
$this->assertSame('21 C', $textContent->text);
|
||||
|
||||
$restored = ToolResultContent::fromArray(json_decode(json_encode($content), true));
|
||||
$this->assertEquals($content, $restored);
|
||||
}
|
||||
|
||||
public function testIsErrorIsOmittedWhenFalse(): void
|
||||
{
|
||||
$content = new ToolResultContent('call-1', [new TextContent('ok')]);
|
||||
|
||||
$serialized = $content->jsonSerialize();
|
||||
|
||||
$this->assertArrayNotHasKey('isError', $serialized);
|
||||
$this->assertArrayNotHasKey('structuredContent', $serialized);
|
||||
$this->assertArrayNotHasKey('_meta', $serialized);
|
||||
$this->assertFalse(ToolResultContent::fromArray(json_decode(json_encode($content), true))->isError);
|
||||
}
|
||||
|
||||
public function testAcceptsEveryCallToolResultContentBlock(): void
|
||||
{
|
||||
$content = ToolResultContent::fromArray([
|
||||
'toolUseId' => 'call-1',
|
||||
'content' => [
|
||||
['type' => 'text', 'text' => 'a'],
|
||||
['type' => 'image', 'data' => base64_encode('img'), 'mimeType' => 'image/png'],
|
||||
['type' => 'audio', 'data' => base64_encode('snd'), 'mimeType' => 'audio/wav'],
|
||||
['type' => 'resource_link', 'uri' => 'file:///report.txt', 'name' => 'report'],
|
||||
['type' => 'resource', 'resource' => ['uri' => 'file:///a.txt', 'mimeType' => 'text/plain', 'text' => 'a']],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertInstanceOf(ResourceLink::class, $content->content[3]);
|
||||
$this->assertInstanceOf(EmbeddedResource::class, $content->content[4]);
|
||||
}
|
||||
|
||||
public function testFilteredContentStillSerializesAsAnArray(): void
|
||||
{
|
||||
$blocks = [new TextContent('drop me'), new TextContent('keep me')];
|
||||
|
||||
// array_filter() preserves keys, so this list starts at index 1.
|
||||
$kept = array_filter($blocks, static fn (TextContent $block): bool => 'keep me' === $block->text);
|
||||
$content = new ToolResultContent('call-1', $kept);
|
||||
|
||||
$this->assertSame(
|
||||
'{"type":"tool_result","toolUseId":"call-1","content":[{"type":"text","text":"keep me"}]}',
|
||||
json_encode($content),
|
||||
);
|
||||
}
|
||||
|
||||
public function testRejectsNonStandardContentBlocks(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
/* @phpstan-ignore argument.type */
|
||||
new ToolResultContent('call-1', [
|
||||
new SamplingMessage(Role::User, new TextContent('not a tool result content block')),
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRejectsUnsupportedContentType(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
ToolResultContent::fromArray([
|
||||
'toolUseId' => 'call-1',
|
||||
'content' => [['type' => 'tool_use', 'id' => 'x', 'name' => 'y', 'input' => []]],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRejectsMissingToolUseId(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
ToolResultContent::fromArray(['content' => []]);
|
||||
}
|
||||
}
|
||||
80
tests/Unit/Schema/Content/ToolUseContentTest.php
Normal file
80
tests/Unit/Schema/Content/ToolUseContentTest.php
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?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\Content;
|
||||
|
||||
use Mcp\Exception\InvalidArgumentException;
|
||||
use Mcp\Schema\Content\ToolUseContent;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ToolUseContentTest extends TestCase
|
||||
{
|
||||
public function testRoundTrip(): void
|
||||
{
|
||||
$content = ToolUseContent::fromArray([
|
||||
'type' => 'tool_use',
|
||||
'id' => 'call-1',
|
||||
'name' => 'weather',
|
||||
'input' => ['city' => 'Paris'],
|
||||
'_meta' => ['provider' => 'test'],
|
||||
]);
|
||||
|
||||
$this->assertSame('tool_use', $content->type);
|
||||
$this->assertSame('call-1', $content->id);
|
||||
$this->assertSame('weather', $content->name);
|
||||
$this->assertSame(['city' => 'Paris'], $content->input);
|
||||
$this->assertSame(['provider' => 'test'], $content->meta);
|
||||
|
||||
$this->assertSame(
|
||||
'{"type":"tool_use","id":"call-1","name":"weather","input":{"city":"Paris"},"_meta":{"provider":"test"}}',
|
||||
json_encode($content),
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmptyInputSerializesAsObject(): void
|
||||
{
|
||||
$content = new ToolUseContent('call-1', 'ping', []);
|
||||
|
||||
$this->assertSame('{"type":"tool_use","id":"call-1","name":"ping","input":{}}', json_encode($content));
|
||||
}
|
||||
|
||||
public function testEmptyInputSurvivesRoundTrip(): void
|
||||
{
|
||||
$decoded = json_decode(json_encode(new ToolUseContent('call-1', 'ping', [])), true);
|
||||
|
||||
$this->assertSame([], ToolUseContent::fromArray($decoded)->input);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return iterable<string, array{array<string, mixed>}>
|
||||
*/
|
||||
public static function provideInvalidData(): iterable
|
||||
{
|
||||
yield 'missing id' => [['name' => 'weather', 'input' => []]];
|
||||
yield 'non-string id' => [['id' => 1, 'name' => 'weather', 'input' => []]];
|
||||
yield 'missing name' => [['id' => 'call-1', 'input' => []]];
|
||||
yield 'non-string name' => [['id' => 'call-1', 'name' => 1, 'input' => []]];
|
||||
yield 'missing input' => [['id' => 'call-1', 'name' => 'weather']];
|
||||
yield 'non-array input' => [['id' => 'call-1', 'name' => 'weather', 'input' => 'nope']];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
#[DataProvider('provideInvalidData')]
|
||||
public function testInvalidDataIsRejected(array $data): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
|
||||
ToolUseContent::fromArray($data);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue