Kick off the SDK based on Symfony's MCP SDK

This commit is contained in:
Christopher Hertel 2025-07-25 11:40:39 +02:00
commit ee0dffe369
No known key found for this signature in database
GPG key ID: 111F6BFF7B6B5AA3
4 changed files with 3924 additions and 0 deletions

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

View 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,
],
];
}
};
}
}

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

View 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'];
}
};
}
}