Adopt server examples
This commit is contained in:
parent
da0d5a87a0
commit
2ef051a355
3 changed files with 3639 additions and 756 deletions
|
|
@ -1,87 +0,0 @@
|
|||
<?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\Prompt\MetadataInterface;
|
||||
use Mcp\Capability\PromptChain;
|
||||
use Mcp\Schema\Request\ListPromptsRequest;
|
||||
use Mcp\Schema\Result\ListPromptsResult;
|
||||
use Mcp\Server\RequestHandler\ListPromptsHandler;
|
||||
use Nyholm\NSA;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PromptListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$handler = new ListPromptsHandler(new PromptChain([]));
|
||||
$request = new ListPromptsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
|
||||
$this->assertEquals(1, $response->getId());
|
||||
$this->assertEquals([], $response->result->prompts);
|
||||
}
|
||||
|
||||
public function testHandleReturnAll()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$handler = new ListPromptsHandler(new PromptChain([$item]));
|
||||
$request = new ListPromptsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
|
||||
$this->assertCount(1, $response->result->prompts);
|
||||
$this->assertNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$handler = new ListPromptsHandler(new PromptChain([$item, $item]), 2);
|
||||
$request = new ListPromptsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListPromptsResult::class, $response->result);
|
||||
$this->assertCount(2, $response->result->prompts);
|
||||
$this->assertNotNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
private static function createMetadataItem(): MetadataInterface
|
||||
{
|
||||
return new class implements MetadataInterface {
|
||||
public function getName(): string
|
||||
{
|
||||
return 'greet';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'Greet a person with a nice message';
|
||||
}
|
||||
|
||||
public function getArguments(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
'name' => 'first name',
|
||||
'description' => 'The name of the person to greet',
|
||||
'required' => false,
|
||||
],
|
||||
];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,126 +0,0 @@
|
|||
<?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\Resource\CollectionInterface;
|
||||
use Mcp\Capability\Resource\MetadataInterface;
|
||||
use Mcp\Schema\Request\ListResourcesRequest;
|
||||
use Mcp\Schema\Result\ListResourcesResult;
|
||||
use Mcp\Server\RequestHandler\ListResourcesHandler;
|
||||
use Nyholm\NSA;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ResourceListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$handler = new ListResourcesHandler($collection);
|
||||
$request = new ListResourcesRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $response->result);
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals([], $response->result->resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<MetadataInterface> $metadataList
|
||||
*/
|
||||
#[DataProvider('metadataProvider')]
|
||||
public function testHandleReturnAll(iterable $metadataList)
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
|
||||
$handler = new ListResourcesHandler($collection);
|
||||
$request = new ListResourcesRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $response->result);
|
||||
$this->assertCount(1, $response->result->resources);
|
||||
$this->assertNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, iterable<MetadataInterface>>
|
||||
*/
|
||||
public static function metadataProvider(): array
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
|
||||
return [
|
||||
'array' => [[$item]],
|
||||
'generator' => [(function () use ($item) { yield $item; })()],
|
||||
];
|
||||
}
|
||||
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
|
||||
$handler = new ListResourcesHandler($collection, 2);
|
||||
$request = new ListResourcesRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListResourcesResult::class, $response->result);
|
||||
$this->assertCount(2, $response->result->resources);
|
||||
$this->assertNotNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
private static function createMetadataItem(): MetadataInterface
|
||||
{
|
||||
return new class implements MetadataInterface {
|
||||
public function getUri(): string
|
||||
{
|
||||
return 'file:///src/SomeFile.php';
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'SomeFile';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'File src/SomeFile.php';
|
||||
}
|
||||
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return 'text/plain';
|
||||
}
|
||||
|
||||
public function getSize(): int
|
||||
{
|
||||
return 1024;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -1,114 +0,0 @@
|
|||
<?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\Tool\CollectionInterface;
|
||||
use Mcp\Capability\Tool\MetadataInterface;
|
||||
use Mcp\Schema\Request\ListToolsRequest;
|
||||
use Mcp\Schema\Result\ListToolsResult;
|
||||
use Mcp\Server\RequestHandler\ListToolsHandler;
|
||||
use Nyholm\NSA;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ToolListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty()
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$handler = new ListToolsHandler($collection);
|
||||
$request = new ListToolsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListToolsResult::class, $response->result);
|
||||
$this->assertSame(1, $response->id);
|
||||
$this->assertSame([], $response->result->tools);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<MetadataInterface> $metadataList
|
||||
*/
|
||||
#[DataProvider('metadataProvider')]
|
||||
public function testHandleReturnAll(iterable $metadataList)
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
$handler = new ListToolsHandler($collection);
|
||||
$request = new ListToolsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListToolsResult::class, $response->result);
|
||||
$this->assertCount(1, $response->result->tools);
|
||||
$this->assertNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, iterable<MetadataInterface>>
|
||||
*/
|
||||
public static function metadataProvider(): array
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
|
||||
return [
|
||||
'array' => [[$item]],
|
||||
'generator' => [(function () use ($item) { yield $item; })()],
|
||||
];
|
||||
}
|
||||
|
||||
public function testHandlePagination()
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
$handler = new ListToolsHandler($collection, 2);
|
||||
$request = new ListToolsRequest();
|
||||
NSA::setProperty($request, 'id', 1);
|
||||
$response = $handler->handle($request);
|
||||
|
||||
$this->assertInstanceOf(ListToolsResult::class, $response->result);
|
||||
$this->assertCount(2, $response->result->tools);
|
||||
$this->assertNotNull($response->result->nextCursor);
|
||||
}
|
||||
|
||||
private static function createMetadataItem(): MetadataInterface
|
||||
{
|
||||
return new class implements MetadataInterface {
|
||||
public function getName(): string
|
||||
{
|
||||
return 'test_tool';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'A test tool';
|
||||
}
|
||||
|
||||
public function getInputSchema(): array
|
||||
{
|
||||
return ['type' => 'object'];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue