[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

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);
}
}