php-sdk/tests/Unit/Schema/Content/SamplingToolContentTest.php
wWzZb 82aac972c1
[Schema][Server] feat: support sampling with tools (#409)
* feat: support sampling with tools

* address sampling tools review feedback

* tighten sampling tools spec compliance

* drop SamplingStopReason enum in favor of open string

The spec leaves stopReason open for provider-specific values, so upcasting
the four known ones to enum cases makes every future enum addition a silent
BC break for string comparisons.

---------

Co-authored-by: Christopher Hertel <mail@christopher-hertel.de>
2026-08-15 03:28:11 +02:00

76 lines
2.7 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\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')),
]);
}
}