Kick off the SDK based on Symfony's MCP SDK
This commit is contained in:
parent
0044ffb365
commit
ee0dffe369
9 changed files with 3924 additions and 0 deletions
50
tests/Fixtures/InMemoryTransport.php
Normal file
50
tests/Fixtures/InMemoryTransport.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?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\Fixtures;
|
||||
|
||||
use Mcp\Server\TransportInterface;
|
||||
|
||||
class InMemoryTransport implements TransportInterface
|
||||
{
|
||||
private bool $connected = true;
|
||||
|
||||
/**
|
||||
* @param list<string> $messages
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly array $messages = [],
|
||||
) {
|
||||
}
|
||||
|
||||
public function initialize(): void
|
||||
{
|
||||
}
|
||||
|
||||
public function isConnected(): bool
|
||||
{
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
public function receive(): \Generator
|
||||
{
|
||||
yield from $this->messages;
|
||||
$this->connected = false;
|
||||
}
|
||||
|
||||
public function send(string $data): void
|
||||
{
|
||||
}
|
||||
|
||||
public function close(): void
|
||||
{
|
||||
}
|
||||
}
|
||||
52
tests/Message/ErrorTest.php
Normal file
52
tests/Message/ErrorTest.php
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
<?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\Message;
|
||||
|
||||
use Mcp\Message\Error;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Error::class)]
|
||||
final class ErrorTest extends TestCase
|
||||
{
|
||||
public function testWithIntegerId(): void
|
||||
{
|
||||
$error = new Error(1, -32602, 'Another error occurred');
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 1,
|
||||
'error' => [
|
||||
'code' => -32602,
|
||||
'message' => 'Another error occurred',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $error->jsonSerialize());
|
||||
}
|
||||
|
||||
public function testWithStringId(): void
|
||||
{
|
||||
$error = new Error('abc', -32602, 'Another error occurred');
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 'abc',
|
||||
'error' => [
|
||||
'code' => -32602,
|
||||
'message' => 'Another error occurred',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $error->jsonSerialize());
|
||||
}
|
||||
}
|
||||
94
tests/Message/FactoryTest.php
Normal file
94
tests/Message/FactoryTest.php
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
<?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\Message;
|
||||
|
||||
use Mcp\Exception\InvalidInputMessageException;
|
||||
use Mcp\Message\Factory;
|
||||
use Mcp\Message\Notification;
|
||||
use Mcp\Message\Request;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Factory::class)]
|
||||
final class FactoryTest extends TestCase
|
||||
{
|
||||
private Factory $factory;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->factory = new Factory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<mixed> $items
|
||||
*/
|
||||
private function first(iterable $items): mixed
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
return $item;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function testCreateRequest(): void
|
||||
{
|
||||
$json = '{"jsonrpc": "2.0", "method": "test_method", "params": {"foo": "bar"}, "id": 123}';
|
||||
|
||||
$result = $this->first($this->factory->create($json));
|
||||
|
||||
$this->assertInstanceOf(Request::class, $result);
|
||||
$this->assertSame('test_method', $result->method);
|
||||
$this->assertSame(['foo' => 'bar'], $result->params);
|
||||
$this->assertSame(123, $result->id);
|
||||
}
|
||||
|
||||
public function testCreateNotification(): void
|
||||
{
|
||||
$json = '{"jsonrpc": "2.0", "method": "notifications/test_event", "params": {"foo": "bar"}}';
|
||||
|
||||
$result = $this->first($this->factory->create($json));
|
||||
|
||||
$this->assertInstanceOf(Notification::class, $result);
|
||||
$this->assertSame('notifications/test_event', $result->method);
|
||||
$this->assertSame(['foo' => 'bar'], $result->params);
|
||||
}
|
||||
|
||||
public function testInvalidJson(): void
|
||||
{
|
||||
$this->expectException(\JsonException::class);
|
||||
|
||||
$this->first($this->factory->create('invalid json'));
|
||||
}
|
||||
|
||||
public function testMissingMethod(): void
|
||||
{
|
||||
$result = $this->first($this->factory->create('{"jsonrpc": "2.0", "params": {}, "id": 1}'));
|
||||
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
|
||||
$this->assertEquals('Invalid JSON-RPC request, missing "method".', $result->getMessage());
|
||||
}
|
||||
|
||||
public function testBatchMissingMethod(): void
|
||||
{
|
||||
$results = $this->factory->create('[{"jsonrpc": "2.0", "params": {}, "id": 1}, {"jsonrpc": "2.0", "method": "notifications/test_event", "params": {}, "id": 2}]');
|
||||
|
||||
$results = iterator_to_array($results);
|
||||
$result = array_shift($results);
|
||||
$this->assertInstanceOf(InvalidInputMessageException::class, $result);
|
||||
$this->assertEquals('Invalid JSON-RPC request, missing "method".', $result->getMessage());
|
||||
|
||||
$result = array_shift($results);
|
||||
$this->assertInstanceOf(Notification::class, $result);
|
||||
}
|
||||
}
|
||||
46
tests/Message/ResponseTest.php
Normal file
46
tests/Message/ResponseTest.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?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\Message;
|
||||
|
||||
use Mcp\Message\Response;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Response::class)]
|
||||
final class ResponseTest extends TestCase
|
||||
{
|
||||
public function testWithIntegerId(): void
|
||||
{
|
||||
$response = new Response(1, ['foo' => 'bar']);
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 1,
|
||||
'result' => ['foo' => 'bar'],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $response->jsonSerialize());
|
||||
}
|
||||
|
||||
public function testWithStringId(): void
|
||||
{
|
||||
$response = new Response('abc', ['foo' => 'bar']);
|
||||
$expected = [
|
||||
'jsonrpc' => '2.0',
|
||||
'id' => 'abc',
|
||||
'result' => ['foo' => 'bar'],
|
||||
];
|
||||
|
||||
$this->assertSame($expected, $response->jsonSerialize());
|
||||
}
|
||||
}
|
||||
90
tests/Server/JsonRpcHandlerTest.php
Normal file
90
tests/Server/JsonRpcHandlerTest.php
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
<?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;
|
||||
|
||||
use Mcp\Message\Factory;
|
||||
use Mcp\Message\Response;
|
||||
use Mcp\Server\JsonRpcHandler;
|
||||
use Mcp\Server\NotificationHandlerInterface;
|
||||
use Mcp\Server\RequestHandlerInterface;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\Attributes\TestDox;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(JsonRpcHandler::class)]
|
||||
class JsonRpcHandlerTest extends TestCase
|
||||
{
|
||||
#[TestDox('Make sure a single notification can be handled by multiple handlers.')]
|
||||
public function testHandleMultipleNotifications(): void
|
||||
{
|
||||
$handlerA = $this->getMockBuilder(NotificationHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerA->method('supports')->willReturn(true);
|
||||
$handlerA->expects($this->once())->method('handle');
|
||||
|
||||
$handlerB = $this->getMockBuilder(NotificationHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerB->method('supports')->willReturn(false);
|
||||
$handlerB->expects($this->never())->method('handle');
|
||||
|
||||
$handlerC = $this->getMockBuilder(NotificationHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'handle'])
|
||||
->getMock();
|
||||
$handlerC->method('supports')->willReturn(true);
|
||||
$handlerC->expects($this->once())->method('handle');
|
||||
|
||||
$jsonRpc = new JsonRpcHandler(new Factory(), [], [$handlerA, $handlerB, $handlerC], new NullLogger());
|
||||
$result = $jsonRpc->process(
|
||||
'{"jsonrpc": "2.0", "id": 1, "method": "notifications/foobar"}'
|
||||
);
|
||||
iterator_to_array($result);
|
||||
}
|
||||
|
||||
#[TestDox('Make sure a single request can NOT be handled by multiple handlers.')]
|
||||
public function testHandleMultipleRequests(): void
|
||||
{
|
||||
$handlerA = $this->getMockBuilder(RequestHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'createResponse'])
|
||||
->getMock();
|
||||
$handlerA->method('supports')->willReturn(true);
|
||||
$handlerA->expects($this->once())->method('createResponse')->willReturn(new Response(1));
|
||||
|
||||
$handlerB = $this->getMockBuilder(RequestHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'createResponse'])
|
||||
->getMock();
|
||||
$handlerB->method('supports')->willReturn(false);
|
||||
$handlerB->expects($this->never())->method('createResponse');
|
||||
|
||||
$handlerC = $this->getMockBuilder(RequestHandlerInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['supports', 'createResponse'])
|
||||
->getMock();
|
||||
$handlerC->method('supports')->willReturn(true);
|
||||
$handlerC->expects($this->never())->method('createResponse');
|
||||
|
||||
$jsonRpc = new JsonRpcHandler(new Factory(), [$handlerA, $handlerB, $handlerC], [], new NullLogger());
|
||||
$result = $jsonRpc->process(
|
||||
'{"jsonrpc": "2.0", "id": 1, "method": "request/foobar"}'
|
||||
);
|
||||
iterator_to_array($result);
|
||||
}
|
||||
}
|
||||
80
tests/Server/RequestHandler/PromptListHandlerTest.php
Normal file
80
tests/Server/RequestHandler/PromptListHandlerTest.php
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?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\Message\Request;
|
||||
use Mcp\Server\RequestHandler\PromptListHandler;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(PromptListHandler::class)]
|
||||
class PromptListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty(): void
|
||||
{
|
||||
$handler = new PromptListHandler(new PromptChain([]));
|
||||
$message = new Request(1, 'prompts/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals(['prompts' => []], $response->result);
|
||||
}
|
||||
|
||||
public function testHandleReturnAll(): void
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$handler = new PromptListHandler(new PromptChain([$item]));
|
||||
$message = new Request(1, 'prompts/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(1, $response->result['prompts']);
|
||||
$this->assertArrayNotHasKey('nextCursor', $response->result);
|
||||
}
|
||||
|
||||
public function testHandlePagination(): void
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$handler = new PromptListHandler(new PromptChain([$item, $item]), 2);
|
||||
$message = new Request(1, 'prompts/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(2, $response->result['prompts']);
|
||||
$this->assertArrayHasKey('nextCursor', $response->result);
|
||||
}
|
||||
|
||||
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,
|
||||
],
|
||||
];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
117
tests/Server/RequestHandler/ResourceListHandlerTest.php
Normal file
117
tests/Server/RequestHandler/ResourceListHandlerTest.php
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
<?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\Message\Request;
|
||||
use Mcp\Server\RequestHandler\ResourceListHandler;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(ResourceListHandler::class)]
|
||||
class ResourceListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty(): void
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$handler = new ResourceListHandler($collection);
|
||||
$message = new Request(1, 'resources/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals(['resources' => []], $response->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<MetadataInterface> $metadataList
|
||||
*/
|
||||
#[DataProvider('metadataProvider')]
|
||||
public function testHandleReturnAll(iterable $metadataList): void
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
$handler = new ResourceListHandler($collection);
|
||||
$message = new Request(1, 'resources/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(1, $response->result['resources']);
|
||||
$this->assertArrayNotHasKey('nextCursor', $response->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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(): void
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
$handler = new ResourceListHandler($collection, 2);
|
||||
$message = new Request(1, 'resources/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(2, $response->result['resources']);
|
||||
$this->assertArrayHasKey('nextCursor', $response->result);
|
||||
}
|
||||
|
||||
private static function createMetadataItem(): MetadataInterface
|
||||
{
|
||||
return new class implements MetadataInterface {
|
||||
public function getUri(): string
|
||||
{
|
||||
return 'file:///src/SomeFile.php';
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'src/SomeFile.php';
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return 'File src/SomeFile.php';
|
||||
}
|
||||
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return 'text/plain';
|
||||
}
|
||||
|
||||
public function getSize(): int
|
||||
{
|
||||
return 1024;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
109
tests/Server/RequestHandler/ToolListHandlerTest.php
Normal file
109
tests/Server/RequestHandler/ToolListHandlerTest.php
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
<?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\Message\Request;
|
||||
use Mcp\Server\RequestHandler\ToolListHandler;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(ToolListHandler::class)]
|
||||
class ToolListHandlerTest extends TestCase
|
||||
{
|
||||
public function testHandleEmpty(): void
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([]);
|
||||
|
||||
$handler = new ToolListHandler($collection);
|
||||
$message = new Request(1, 'tools/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertEquals(1, $response->id);
|
||||
$this->assertEquals(['tools' => []], $response->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param iterable<MetadataInterface> $metadataList
|
||||
*/
|
||||
#[DataProvider('metadataProvider')]
|
||||
public function testHandleReturnAll(iterable $metadataList): void
|
||||
{
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn($metadataList);
|
||||
$handler = new ToolListHandler($collection);
|
||||
$message = new Request(1, 'tools/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(1, $response->result['tools']);
|
||||
$this->assertArrayNotHasKey('nextCursor', $response->result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, iterable<MetadataInterface>>
|
||||
*/
|
||||
public static function metadataProvider(): array
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
|
||||
return [
|
||||
'array' => [[$item]],
|
||||
'generator' => [(function () use ($item) { yield $item; })()],
|
||||
];
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function handlePagination(): void
|
||||
{
|
||||
$item = self::createMetadataItem();
|
||||
$collection = $this->getMockBuilder(CollectionInterface::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['getMetadata'])
|
||||
->getMock();
|
||||
$collection->expects($this->once())->method('getMetadata')->willReturn([$item, $item]);
|
||||
$handler = new ToolListHandler($collection, 2);
|
||||
$message = new Request(1, 'tools/list', []);
|
||||
$response = $handler->createResponse($message);
|
||||
$this->assertCount(2, $response->result['tools']);
|
||||
$this->assertArrayHasKey('nextCursor', $response->result);
|
||||
}
|
||||
|
||||
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'];
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
50
tests/ServerTest.php
Normal file
50
tests/ServerTest.php
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
<?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;
|
||||
|
||||
use Mcp\Server;
|
||||
use Mcp\Server\JsonRpcHandler;
|
||||
use Mcp\Tests\Fixtures\InMemoryTransport;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\Small;
|
||||
use PHPUnit\Framework\MockObject\Stub\Exception;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
#[Small]
|
||||
#[CoversClass(Server::class)]
|
||||
class ServerTest extends TestCase
|
||||
{
|
||||
public function testJsonExceptions(): void
|
||||
{
|
||||
$logger = $this->getMockBuilder(NullLogger::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['error'])
|
||||
->getMock();
|
||||
$logger->expects($this->once())->method('error');
|
||||
|
||||
$handler = $this->getMockBuilder(JsonRpcHandler::class)
|
||||
->disableOriginalConstructor()
|
||||
->onlyMethods(['process'])
|
||||
->getMock();
|
||||
$handler->expects($this->exactly(2))->method('process')->willReturnOnConsecutiveCalls(new Exception(new \JsonException('foobar')), ['success']);
|
||||
|
||||
$transport = $this->getMockBuilder(InMemoryTransport::class)
|
||||
->setConstructorArgs([['foo', 'bar']])
|
||||
->onlyMethods(['send'])
|
||||
->getMock();
|
||||
$transport->expects($this->once())->method('send')->with('success');
|
||||
|
||||
$server = new Server($handler, $logger);
|
||||
$server->connect($transport);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue