[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>
This commit is contained in:
wWzZb 2026-08-15 09:28:11 +08:00 • committed by GitHub
commit 82aac972c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 682 additions and 27 deletions

View file

@ -0,0 +1,45 @@
<?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\Schema\ClientCapabilities;
use PHPUnit\Framework\TestCase;
final class ClientCapabilitiesSamplingTest extends TestCase
{
public function testSamplingSubCapabilitiesRoundTrip(): void
{
$capabilities = new ClientCapabilities(sampling: true, samplingContext: true, samplingTools: true);
$serialized = $capabilities->jsonSerialize();
$this->assertObjectHasProperty('context', $serialized['sampling']);
$this->assertObjectHasProperty('tools', $serialized['sampling']);
$hydrated = ClientCapabilities::fromArray(json_decode(json_encode($serialized, \JSON_THROW_ON_ERROR), true, flags: \JSON_THROW_ON_ERROR));
$this->assertTrue($hydrated->sampling);
$this->assertTrue($hydrated->samplingContext);
$this->assertTrue($hydrated->samplingTools);
}
public function testSamplingSubCapabilitiesAreHydratedFromObject(): void
{
$sampling = new \stdClass();
$sampling->context = new \stdClass();
$sampling->tools = new \stdClass();
$capabilities = ClientCapabilities::fromArray(['sampling' => $sampling]);
$this->assertTrue($capabilities->sampling);
$this->assertTrue($capabilities->samplingContext);
$this->assertTrue($capabilities->samplingTools);
}
}

View file

@ -0,0 +1,76 @@
<?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')),
]);
}
}

View file

@ -15,7 +15,10 @@ use Mcp\Exception\InvalidArgumentException;
use Mcp\Schema\Content\SamplingMessage;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Enum\Role;
use Mcp\Schema\Enum\ToolChoiceMode;
use Mcp\Schema\Request\CreateSamplingMessageRequest;
use Mcp\Schema\Tool;
use Mcp\Schema\ToolChoice;
use PHPUnit\Framework\TestCase;
final class CreateSamplingMessageRequestTest extends TestCase
@ -48,4 +51,23 @@ final class CreateSamplingMessageRequestTest extends TestCase
/* @phpstan-ignore argument.type */
new CreateSamplingMessageRequest($messages, 150);
}
public function testToolsAndToolChoiceRoundTrip(): void
{
$tool = new Tool('weather', null, ['type' => 'object', 'properties' => [], 'required' => null], 'Get weather', null);
$request = new CreateSamplingMessageRequest(
[new SamplingMessage(Role::User, new TextContent('Weather in Paris?'))],
150,
tools: [$tool],
toolChoice: new ToolChoice(ToolChoiceMode::Required),
);
$payload = $request->withId(1)->jsonSerialize();
$this->assertSame('weather', $payload['params']['tools'][0]->name);
$this->assertSame(ToolChoiceMode::Required, $payload['params']['toolChoice']->mode);
$hydrated = CreateSamplingMessageRequest::fromArray(json_decode(json_encode($payload, \JSON_THROW_ON_ERROR), true, flags: \JSON_THROW_ON_ERROR));
$this->assertSame('weather', $hydrated->tools[0]->name);
$this->assertSame(ToolChoiceMode::Required, $hydrated->toolChoice->mode);
}
}

View file

@ -0,0 +1,53 @@
<?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\Result;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Content\ToolUseContent;
use Mcp\Schema\Result\CreateSamplingMessageResult;
use PHPUnit\Framework\TestCase;
final class CreateSamplingMessageResultTest extends TestCase
{
public function testArrayContentAndKnownStopReasonAreHydrated(): void
{
$result = CreateSamplingMessageResult::fromArray([
'role' => 'assistant',
'content' => [
['type' => 'text', 'text' => 'Checking weather.'],
['type' => 'tool_use', 'id' => 'call-1', 'name' => 'weather', 'input' => ['city' => 'Paris']],
],
'model' => 'test-model',
'stopReason' => 'toolUse',
'_meta' => ['traceId' => 'trace-1'],
]);
$this->assertInstanceOf(TextContent::class, $result->content[0]);
$this->assertInstanceOf(ToolUseContent::class, $result->content[1]);
$this->assertSame('toolUse', $result->stopReason);
$this->assertSame('toolUse', $result->jsonSerialize()['stopReason']);
$this->assertSame(['traceId' => 'trace-1'], $result->jsonSerialize()['_meta']);
}
public function testProviderSpecificStopReasonIsPreserved(): void
{
$result = CreateSamplingMessageResult::fromArray([
'role' => 'assistant',
'content' => ['type' => 'text', 'text' => 'Done'],
'model' => 'test-model',
'stopReason' => 'provider-specific',
]);
$this->assertSame('provider-specific', $result->stopReason);
$this->assertSame('provider-specific', $result->jsonSerialize()['stopReason']);
}
}