Add support for pagination on list requests (#55)

10 - Added the list resources handler with test class

10 - Update the ListPromptsHandler to use annotations

10 - Add the list resource handler, and it's test as well

10 - Added the registry logic

10 - Added the list tools handler logic

10 - Fix all php-cs-fixer related issues

10 - Resolved phpstan issues for ListPromptsHandlerTest

10 - Resolved phpstan issues for ListToolsHandlerTest

10 - Resolved phpstan issues for ListResourcesHandlerTest

10 - Resolved phpstan issues in registry class

10 - Resolved phpstan issues for the handler class

10 - Resolved php cs fixer issues

10 - Resolved all phpstan issues by generating the baseline again

10 - Remove the two loop operation and use foreach for optimization

10 - Removed unused code and resolved namespace

10 - Add signatures to interface and registry

10 - Add updated phpstan rule set

10 - Arranged according to property type

10 - Introduce reference page DTO, fix all failing test

10 - Resolve cs-fixer issues

10 - Resolve phpstan issues

10 - Resolve php cs fixer issues

10 - Created a reference directory for the Page DTO

10 - Remove manual loop for clean code readability

Co-authored-by: larry.sulebalogun <larry.sulebalogun@tillo.io>
This commit is contained in:
Sule-Balogun Olanrewaju 2025-09-24 20:44:32 +01:00 • committed by GitHub
commit 325b4aaf9c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 1016 additions and 131 deletions

View file

@ -135,7 +135,8 @@ class DiscoveryTest extends TestCase
public function testHandlesEmptyDirectoriesOrDirectoriesWithNoPhpFiles()
{
$this->discoverer->discover(__DIR__, ['EmptyDir']);
$this->assertEmpty($this->registry->getTools());
$tools = $this->registry->getTools();
$this->assertEmpty($tools->references);
}
public function testCorrectlyInfersNamesAndDescriptionsFromMethodsOrClassesIfNotSetInAttribute()

View file

@ -161,10 +161,10 @@ class RegistryProviderTest extends TestCase
$tools = $this->registry->getTools();
$this->assertCount(2, $tools);
$this->assertArrayHasKey('tool1', $tools);
$this->assertArrayHasKey('tool2', $tools);
$this->assertInstanceOf(Tool::class, $tools['tool1']);
$this->assertInstanceOf(Tool::class, $tools['tool2']);
$this->assertArrayHasKey('tool1', $tools->references);
$this->assertArrayHasKey('tool2', $tools->references);
$this->assertInstanceOf(Tool::class, $tools->references['tool1']);
$this->assertInstanceOf(Tool::class, $tools->references['tool2']);
}
public function testGetResourcesReturnsAllRegisteredResources(): void
@ -177,10 +177,10 @@ class RegistryProviderTest extends TestCase
$resources = $this->registry->getResources();
$this->assertCount(2, $resources);
$this->assertArrayHasKey('test://resource1', $resources);
$this->assertArrayHasKey('test://resource2', $resources);
$this->assertInstanceOf(Resource::class, $resources['test://resource1']);
$this->assertInstanceOf(Resource::class, $resources['test://resource2']);
$this->assertArrayHasKey('test://resource1', $resources->references);
$this->assertArrayHasKey('test://resource2', $resources->references);
$this->assertInstanceOf(Resource::class, $resources->references['test://resource1']);
$this->assertInstanceOf(Resource::class, $resources->references['test://resource2']);
}
public function testGetPromptsReturnsAllRegisteredPrompts(): void
@ -193,10 +193,10 @@ class RegistryProviderTest extends TestCase
$prompts = $this->registry->getPrompts();
$this->assertCount(2, $prompts);
$this->assertArrayHasKey('prompt1', $prompts);
$this->assertArrayHasKey('prompt2', $prompts);
$this->assertInstanceOf(Prompt::class, $prompts['prompt1']);
$this->assertInstanceOf(Prompt::class, $prompts['prompt2']);
$this->assertArrayHasKey('prompt1', $prompts->references);
$this->assertArrayHasKey('prompt2', $prompts->references);
$this->assertInstanceOf(Prompt::class, $prompts->references['prompt1']);
$this->assertInstanceOf(Prompt::class, $prompts->references['prompt2']);
}
public function testGetResourceTemplatesReturnsAllRegisteredTemplates(): void
@ -209,10 +209,10 @@ class RegistryProviderTest extends TestCase
$templates = $this->registry->getResourceTemplates();
$this->assertCount(2, $templates);
$this->assertArrayHasKey('test1://{id}', $templates);
$this->assertArrayHasKey('test2://{category}', $templates);
$this->assertInstanceOf(ResourceTemplate::class, $templates['test1://{id}']);
$this->assertInstanceOf(ResourceTemplate::class, $templates['test2://{category}']);
$this->assertArrayHasKey('test1://{id}', $templates->references);
$this->assertArrayHasKey('test2://{category}', $templates->references);
$this->assertInstanceOf(ResourceTemplate::class, $templates->references['test1://{id}']);
$this->assertInstanceOf(ResourceTemplate::class, $templates->references['test2://{category}']);
}
public function testHasElementsReturnsFalseForEmptyRegistry(): void

View file

@ -0,0 +1,247 @@
<?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\Server\RequestHandler;
use Mcp\Capability\Registry;
use Mcp\Exception\InvalidCursorException;
use Mcp\Schema\Prompt;
use Mcp\Schema\Request\ListPromptsRequest;
use Mcp\Schema\Result\ListPromptsResult;
use Mcp\Server\RequestHandler\ListPromptsHandler;
use Mcp\Server\Session\InMemorySessionStore;
use Mcp\Server\Session\Session;
use Mcp\Server\Session\SessionInterface;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
class ListPromptsHandlerTest extends TestCase
{
private Registry $registry;
private ListPromptsHandler $handler;
private SessionInterface $session;
protected function setUp(): void
{
$this->registry = new Registry();
$this->handler = new ListPromptsHandler($this->registry, pageSize: 3); // Use small page size for testing
$this->session = new Session(new InMemorySessionStore());
}
#[TestDox('Returns first page when no cursor provided')]
public function testReturnsFirstPageWhenNoCursorProvided(): void
{
// Arrange
$this->addPromptsToRegistry(5);
$request = $this->createListPromptsRequest();
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListPromptsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListPromptsResult::class, $result);
$this->assertCount(3, $result->prompts);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('prompt_0', $result->prompts[0]->name);
$this->assertEquals('prompt_1', $result->prompts[1]->name);
$this->assertEquals('prompt_2', $result->prompts[2]->name);
}
#[TestDox('Returns paginated prompts with cursor')]
public function testReturnsPaginatedPromptsWithCursor(): void
{
// Arrange
$this->addPromptsToRegistry(10);
$request = $this->createListPromptsRequest(cursor: null);
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListPromptsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListPromptsResult::class, $result);
$this->assertCount(3, $result->prompts);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('prompt_0', $result->prompts[0]->name);
$this->assertEquals('prompt_1', $result->prompts[1]->name);
$this->assertEquals('prompt_2', $result->prompts[2]->name);
}
#[TestDox('Returns second page with cursor')]
public function testReturnsSecondPageWithCursor(): void
{
// Arrange
$this->addPromptsToRegistry(10);
$firstPageRequest = $this->createListPromptsRequest();
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
/** @var ListPromptsResult $firstPageResult */
$firstPageResult = $firstPageResponse->result;
$secondPageRequest = $this->createListPromptsRequest(cursor: $firstPageResult->nextCursor);
// Act
$response = $this->handler->handle($secondPageRequest, $this->session);
// Assert
/** @var ListPromptsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListPromptsResult::class, $result);
$this->assertCount(3, $result->prompts);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('prompt_3', $result->prompts[0]->name);
$this->assertEquals('prompt_4', $result->prompts[1]->name);
$this->assertEquals('prompt_5', $result->prompts[2]->name);
}
#[TestDox('Returns last page with null cursor')]
public function testReturnsLastPageWithNullCursor(): void
{
// Arrange
$this->addPromptsToRegistry(5);
$firstPageRequest = $this->createListPromptsRequest();
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
/** @var ListPromptsResult $firstPageResult */
$firstPageResult = $firstPageResponse->result;
$secondPageRequest = $this->createListPromptsRequest(cursor: $firstPageResult->nextCursor);
// Act
$response = $this->handler->handle($secondPageRequest, $this->session);
// Assert
/** @var ListPromptsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListPromptsResult::class, $result);
$this->assertCount(2, $result->prompts);
$this->assertNull($result->nextCursor);
$this->assertEquals('prompt_3', $result->prompts[0]->name);
$this->assertEquals('prompt_4', $result->prompts[1]->name);
}
#[TestDox('Handles empty registry')]
public function testHandlesEmptyRegistry(): void
{
// Arrange
$request = $this->createListPromptsRequest();
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListPromptsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListPromptsResult::class, $result);
$this->assertCount(0, $result->prompts);
$this->assertNull($result->nextCursor);
}
#[TestDox('Throws exception for invalid cursor')]
public function testThrowsExceptionForInvalidCursor(): void
{
// Arrange
$this->addPromptsToRegistry(5);
$request = $this->createListPromptsRequest(cursor: 'invalid-cursor');
// Assert
$this->expectException(InvalidCursorException::class);
// Act
$this->handler->handle($request, $this->session);
}
#[TestDox('Throws exception for cursor beyond bounds')]
public function testThrowsExceptionForCursorBeyondBounds(): void
{
// Arrange
$this->addPromptsToRegistry(5);
$outOfBoundsCursor = base64_encode('1000');
$request = $this->createListPromptsRequest(cursor: $outOfBoundsCursor);
// Assert
$this->expectException(InvalidCursorException::class);
// Act
$this->handler->handle($request, $this->session);
}
#[TestDox('Handles cursor at exact boundary')]
public function testHandlesCursorAtExactBoundary(): void
{
// Arrange
$this->addPromptsToRegistry(6);
$exactBoundaryCursor = base64_encode('6'); // Exactly at the end
$request = $this->createListPromptsRequest(cursor: $exactBoundaryCursor);
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListPromptsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListPromptsResult::class, $result);
$this->assertCount(0, $result->prompts);
$this->assertNull($result->nextCursor);
}
#[TestDox('Maintains stable cursors across calls')]
public function testMaintainsStableCursorsAcrossCalls(): void
{
// Arrange
$this->addPromptsToRegistry(10);
// Act
$request = $this->createListPromptsRequest();
$response1 = $this->handler->handle($request, $this->session);
$response2 = $this->handler->handle($request, $this->session);
// Assert
/** @var ListPromptsResult $result1 */
$result1 = $response1->result;
/** @var ListPromptsResult $result2 */
$result2 = $response2->result;
$this->assertEquals($result1->nextCursor, $result2->nextCursor);
$this->assertEquals($result1->prompts, $result2->prompts);
}
private function addPromptsToRegistry(int $count): void
{
for ($i = 0; $i < $count; ++$i) {
$prompt = new Prompt(
name: "prompt_$i",
description: "Test prompt $i"
);
$this->registry->registerPrompt($prompt, fn () => null);
}
}
private function createListPromptsRequest(?string $cursor = null): ListPromptsRequest
{
$data = [
'jsonrpc' => '2.0',
'id' => 'test-request-id',
'method' => 'prompts/list',
];
if (null !== $cursor) {
$data['params'] = ['cursor' => $cursor];
}
return ListPromptsRequest::fromArray($data);
}
}

View file

@ -0,0 +1,248 @@
<?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\Server\RequestHandler;
use Mcp\Capability\Registry;
use Mcp\Exception\InvalidCursorException;
use Mcp\Schema\Request\ListResourcesRequest;
use Mcp\Schema\Resource;
use Mcp\Schema\Result\ListResourcesResult;
use Mcp\Server\RequestHandler\ListResourcesHandler;
use Mcp\Server\Session\InMemorySessionStore;
use Mcp\Server\Session\Session;
use Mcp\Server\Session\SessionInterface;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
class ListResourcesHandlerTest extends TestCase
{
private Registry $registry;
private ListResourcesHandler $handler;
private SessionInterface $session;
protected function setUp(): void
{
$this->registry = new Registry();
$this->handler = new ListResourcesHandler($this->registry, pageSize: 3); // Use small page size for testing
$this->session = new Session(new InMemorySessionStore());
}
#[TestDox('Returns first page when no cursor provided')]
public function testReturnsFirstPageWhenNoCursorProvided(): void
{
// Arrange
$this->addResourcesToRegistry(5);
$request = $this->createListResourcesRequest();
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListResourcesResult $result */
$result = $response->result;
$this->assertInstanceOf(ListResourcesResult::class, $result);
$this->assertCount(3, $result->resources);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('resource://test/resource_0', $result->resources[0]->uri);
$this->assertEquals('resource://test/resource_1', $result->resources[1]->uri);
$this->assertEquals('resource://test/resource_2', $result->resources[2]->uri);
}
#[TestDox('Returns paginated resources with cursor')]
public function testReturnsPaginatedResourcesWithCursor(): void
{
// Arrange
$this->addResourcesToRegistry(10);
$request = $this->createListResourcesRequest(cursor: null);
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListResourcesResult $result */
$result = $response->result;
$this->assertInstanceOf(ListResourcesResult::class, $result);
$this->assertCount(3, $result->resources);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('resource://test/resource_0', $result->resources[0]->uri);
$this->assertEquals('resource://test/resource_1', $result->resources[1]->uri);
$this->assertEquals('resource://test/resource_2', $result->resources[2]->uri);
}
#[TestDox('Returns second page with cursor')]
public function testReturnsSecondPageWithCursor(): void
{
// Arrange
$this->addResourcesToRegistry(10);
$firstPageRequest = $this->createListResourcesRequest();
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
/** @var ListResourcesResult $firstPageResult */
$firstPageResult = $firstPageResponse->result;
$secondPageRequest = $this->createListResourcesRequest(cursor: $firstPageResult->nextCursor);
// Act
$response = $this->handler->handle($secondPageRequest, $this->session);
// Assert
/** @var ListResourcesResult $result */
$result = $response->result;
$this->assertInstanceOf(ListResourcesResult::class, $result);
$this->assertCount(3, $result->resources);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('resource://test/resource_3', $result->resources[0]->uri);
$this->assertEquals('resource://test/resource_4', $result->resources[1]->uri);
$this->assertEquals('resource://test/resource_5', $result->resources[2]->uri);
}
#[TestDox('Returns last page with null cursor')]
public function testReturnsLastPageWithNullCursor(): void
{
// Arrange
$this->addResourcesToRegistry(5);
$firstPageRequest = $this->createListResourcesRequest();
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
/** @var ListResourcesResult $firstPageResult */
$firstPageResult = $firstPageResponse->result;
$secondPageRequest = $this->createListResourcesRequest(cursor: $firstPageResult->nextCursor);
// Act
$response = $this->handler->handle($secondPageRequest, $this->session);
// Assert
/** @var ListResourcesResult $result */
$result = $response->result;
$this->assertInstanceOf(ListResourcesResult::class, $result);
$this->assertCount(2, $result->resources);
$this->assertNull($result->nextCursor);
$this->assertEquals('resource://test/resource_3', $result->resources[0]->uri);
$this->assertEquals('resource://test/resource_4', $result->resources[1]->uri);
}
#[TestDox('Handles empty registry')]
public function testHandlesEmptyRegistry(): void
{
// Arrange
$request = $this->createListResourcesRequest();
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListResourcesResult $result */
$result = $response->result;
$this->assertInstanceOf(ListResourcesResult::class, $result);
$this->assertCount(0, $result->resources);
$this->assertNull($result->nextCursor);
}
#[TestDox('Throws exception for invalid cursor')]
public function testThrowsExceptionForInvalidCursor(): void
{
// Arrange
$this->addResourcesToRegistry(5);
$request = $this->createListResourcesRequest(cursor: 'invalid-cursor');
// Assert
$this->expectException(InvalidCursorException::class);
// Act
$this->handler->handle($request, $this->session);
}
#[TestDox('Throws exception for cursor beyond bounds')]
public function testThrowsExceptionForCursorBeyondBounds(): void
{
// Arrange
$this->addResourcesToRegistry(5);
$outOfBoundsCursor = base64_encode('100');
$request = $this->createListResourcesRequest(cursor: $outOfBoundsCursor);
// Assert
$this->expectException(InvalidCursorException::class);
// Act
$this->handler->handle($request, $this->session);
}
#[TestDox('Handles cursor at exact boundary')]
public function testHandlesCursorAtExactBoundary(): void
{
// Arrange
$this->addResourcesToRegistry(6);
$exactBoundaryCursor = base64_encode('6');
$request = $this->createListResourcesRequest(cursor: $exactBoundaryCursor);
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListResourcesResult $result */
$result = $response->result;
$this->assertInstanceOf(ListResourcesResult::class, $result);
$this->assertCount(0, $result->resources);
$this->assertNull($result->nextCursor);
}
#[TestDox('Maintains stable cursors across calls')]
public function testMaintainsStableCursorsAcrossCalls(): void
{
// Arrange
$this->addResourcesToRegistry(10);
// Act
$request = $this->createListResourcesRequest();
$response1 = $this->handler->handle($request, $this->session);
$response2 = $this->handler->handle($request, $this->session);
// Assert
/** @var ListResourcesResult $result1 */
$result1 = $response1->result;
/** @var ListResourcesResult $result2 */
$result2 = $response2->result;
$this->assertEquals($result1->nextCursor, $result2->nextCursor);
$this->assertEquals($result1->resources, $result2->resources);
}
private function addResourcesToRegistry(int $count): void
{
for ($i = 0; $i < $count; ++$i) {
$resource = new Resource(
uri: "resource://test/resource_$i",
name: "resource_$i",
description: "Test resource $i"
);
// Use a simple callable as handler
$this->registry->registerResource($resource, fn () => null);
}
}
private function createListResourcesRequest(?string $cursor = null): ListResourcesRequest
{
$data = [
'jsonrpc' => '2.0',
'id' => 'test-request-id',
'method' => 'resources/list',
];
if (null !== $cursor) {
$data['params'] = ['cursor' => $cursor];
}
return ListResourcesRequest::fromArray($data);
}
}

View file

@ -0,0 +1,274 @@
<?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\Server\RequestHandler;
use Mcp\Capability\Registry;
use Mcp\Exception\InvalidCursorException;
use Mcp\Schema\Request\ListToolsRequest;
use Mcp\Schema\Result\ListToolsResult;
use Mcp\Schema\Tool;
use Mcp\Server\RequestHandler\ListToolsHandler;
use Mcp\Server\Session\InMemorySessionStore;
use Mcp\Server\Session\Session;
use Mcp\Server\Session\SessionInterface;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
class ListToolsHandlerTest extends TestCase
{
private Registry $registry;
private ListToolsHandler $handler;
private SessionInterface $session;
protected function setUp(): void
{
$this->registry = new Registry();
$this->handler = new ListToolsHandler($this->registry, pageSize: 3); // Use small page size for testing
$this->session = new Session(new InMemorySessionStore());
}
#[TestDox('Returns first page when no cursor provided')]
public function testReturnsFirstPageWhenNoCursorProvided(): void
{
// Arrange
$this->addToolsToRegistry(5);
$request = $this->createListToolsRequest();
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListToolsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListToolsResult::class, $result);
$this->assertCount(3, $result->tools);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('tool_0', $result->tools[0]->name);
$this->assertEquals('tool_1', $result->tools[1]->name);
$this->assertEquals('tool_2', $result->tools[2]->name);
}
#[TestDox('Returns paginated tools with cursor')]
public function testReturnsPaginatedToolsWithCursor(): void
{
// Arrange
$this->addToolsToRegistry(10);
$request = $this->createListToolsRequest(cursor: null);
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListToolsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListToolsResult::class, $result);
$this->assertCount(3, $result->tools);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('tool_0', $result->tools[0]->name);
$this->assertEquals('tool_1', $result->tools[1]->name);
$this->assertEquals('tool_2', $result->tools[2]->name);
}
#[TestDox('Returns second page with cursor')]
public function testReturnsSecondPageWithCursor(): void
{
// Arrange
$this->addToolsToRegistry(10);
$firstPageRequest = $this->createListToolsRequest();
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
/** @var ListToolsResult $firstPageResult */
$firstPageResult = $firstPageResponse->result;
$secondPageRequest = $this->createListToolsRequest(cursor: $firstPageResult->nextCursor);
// Act
$response = $this->handler->handle($secondPageRequest, $this->session);
// Assert
/** @var ListToolsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListToolsResult::class, $result);
$this->assertCount(3, $result->tools);
$this->assertNotNull($result->nextCursor);
$this->assertEquals('tool_3', $result->tools[0]->name);
$this->assertEquals('tool_4', $result->tools[1]->name);
$this->assertEquals('tool_5', $result->tools[2]->name);
}
#[TestDox('Returns last page with null cursor')]
public function testReturnsLastPageWithNullCursor(): void
{
// Arrange
$this->addToolsToRegistry(5);
$firstPageRequest = $this->createListToolsRequest();
$firstPageResponse = $this->handler->handle($firstPageRequest, $this->session);
/** @var ListToolsResult $firstPageResult */
$firstPageResult = $firstPageResponse->result;
$secondPageRequest = $this->createListToolsRequest(cursor: $firstPageResult->nextCursor);
// Act
$response = $this->handler->handle($secondPageRequest, $this->session);
// Assert
/** @var ListToolsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListToolsResult::class, $result);
$this->assertCount(2, $result->tools);
$this->assertNull($result->nextCursor);
$this->assertEquals('tool_3', $result->tools[0]->name);
$this->assertEquals('tool_4', $result->tools[1]->name);
}
#[TestDox('Returns all tools when count is less than page size')]
public function testReturnsAllToolsWhenCountIsLessThanPageSize(): void
{
// Arrange
$this->addToolsToRegistry(2); // Less than page size (3)
$request = $this->createListToolsRequest();
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListToolsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListToolsResult::class, $result);
$this->assertCount(2, $result->tools);
$this->assertNull($result->nextCursor);
$this->assertEquals('tool_0', $result->tools[0]->name);
$this->assertEquals('tool_1', $result->tools[1]->name);
}
#[TestDox('Handles empty registry')]
public function testHandlesEmptyRegistry(): void
{
// Arrange
$request = $this->createListToolsRequest();
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListToolsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListToolsResult::class, $result);
$this->assertCount(0, $result->tools);
$this->assertNull($result->nextCursor);
}
#[TestDox('Throws exception for invalid cursor')]
public function testThrowsExceptionForInvalidCursor(): void
{
// Arrange
$this->addToolsToRegistry(5);
$request = $this->createListToolsRequest(cursor: 'invalid-cursor');
// Assert
$this->expectException(InvalidCursorException::class);
// Act
$this->handler->handle($request, $this->session);
}
#[TestDox('Throws exception for cursor beyond bounds')]
public function testThrowsExceptionForCursorBeyondBounds(): void
{
// Arrange
$this->addToolsToRegistry(5);
$outOfBoundsCursor = base64_encode('100');
$request = $this->createListToolsRequest(cursor: $outOfBoundsCursor);
// Assert
$this->expectException(InvalidCursorException::class);
// Act
$this->handler->handle($request, $this->session);
}
#[TestDox('Handles cursor at exact boundary')]
public function testHandlesCursorAtExactBoundary(): void
{
// Arrange
$this->addToolsToRegistry(6);
$exactBoundaryCursor = base64_encode('6');
$request = $this->createListToolsRequest(cursor: $exactBoundaryCursor);
// Act
$response = $this->handler->handle($request, $this->session);
// Assert
/** @var ListToolsResult $result */
$result = $response->result;
$this->assertInstanceOf(ListToolsResult::class, $result);
$this->assertCount(0, $result->tools);
$this->assertNull($result->nextCursor);
}
#[TestDox('Maintains stable cursors across calls')]
public function testMaintainsStableCursorsAcrossCalls(): void
{
// Arrange
$this->addToolsToRegistry(10);
// Act
$request = $this->createListToolsRequest();
$response1 = $this->handler->handle($request, $this->session);
$response2 = $this->handler->handle($request, $this->session);
// Assert
/** @var ListToolsResult $result1 */
$result1 = $response1->result;
/** @var ListToolsResult $result2 */
$result2 = $response2->result;
$this->assertEquals($result1->nextCursor, $result2->nextCursor);
$this->assertEquals($result1->tools, $result2->tools);
}
private function addToolsToRegistry(int $count): void
{
for ($i = 0; $i < $count; ++$i) {
$tool = new Tool(
name: "tool_$i",
inputSchema: [
'type' => 'object',
'properties' => [],
'required' => [],
],
description: "Test tool $i",
annotations: null
);
$this->registry->registerTool($tool, fn () => null);
}
}
private function createListToolsRequest(?string $cursor = null): ListToolsRequest
{
$data = [
'jsonrpc' => '2.0',
'id' => 'test-request-id',
'method' => 'tools/list',
];
if (null !== $cursor) {
$data['params'] = ['cursor' => $cursor];
}
return ListToolsRequest::fromArray($data);
}
}