Skip to content

Commit 6fbe669

Browse files
committed
Make client request handler results covariant
Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>
1 parent 7155eae commit 6fbe669

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/Client/Handler/Request/RequestHandlerInterface.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* Interface for handling requests from the server.
2020
*
21-
* @template TResult
21+
* @template-covariant TResult
2222
*
2323
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
2424
*/

‎tests/Unit/Client/BuilderTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
use Mcp\Client\Client;
1515
use Mcp\Client\Configuration;
1616
use Mcp\Client\Exception\LogicException;
17+
use Mcp\Client\Handler\Request\ElicitationCallbackInterface;
18+
use Mcp\Client\Handler\Request\ElicitationRequestHandler;
19+
use Mcp\Client\Handler\Request\ListRootsRequestHandler;
20+
use Mcp\Client\Handler\Request\RootsCallbackInterface;
21+
use Mcp\Client\Handler\Request\SamplingCallbackInterface;
22+
use Mcp\Client\Handler\Request\SamplingRequestHandler;
1723
use Mcp\Schema\ClientCapabilities;
1824
use Mcp\Schema\Extension\Apps\McpApps;
1925
use PHPUnit\Framework\Attributes\TestDox;
@@ -81,6 +87,19 @@ public function testEnableExtensionRejectsDuplicate(): void
8187
Client::builder()->enableExtension(new McpApps(), new McpApps());
8288
}
8389

90+
#[TestDox('addRequestHandler() accepts handlers with concrete result types')]
91+
public function testAddRequestHandlerAcceptsConcreteResultTypes(): void
92+
{
93+
$builder = Client::builder();
94+
$elicitation = new ElicitationRequestHandler($this->createMock(ElicitationCallbackInterface::class));
95+
$sampling = new SamplingRequestHandler($this->createMock(SamplingCallbackInterface::class));
96+
$roots = new ListRootsRequestHandler($this->createMock(RootsCallbackInterface::class));
97+
98+
$this->assertSame($builder, $builder->addRequestHandler($elicitation));
99+
$this->assertSame($builder, $builder->addRequestHandler($sampling));
100+
$this->assertSame($builder, $builder->addRequestHandler($roots));
101+
}
102+
84103
private function extractConfiguration(Client $client): Configuration
85104
{
86105
$config = (new \ReflectionClass($client))->getProperty('config')->getValue($client);

0 commit comments

Comments
 (0)