* 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.
80 lines
2.6 KiB
PHP
80 lines
2.6 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\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);
|
|
}
|
|
}
|