Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/mcp-sdk/src/Server/JsonRpcHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
namespace Symfony\AI\McpSdk\Server;

use Psr\Log\LoggerInterface;
use Symfony\AI\McpSdk\Exception\ExceptionInterface;
use Symfony\AI\McpSdk\Exception\HandlerNotFoundException;
use Symfony\AI\McpSdk\Exception\NotFoundExceptionInterface;
use Symfony\AI\McpSdk\Message\Error;
use Symfony\AI\McpSdk\Message\Factory;
use Symfony\AI\McpSdk\Message\Notification;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Message\Response;
use Symfony\Component\String\Exception\ExceptionInterface;

/**
* @final
Expand Down Expand Up @@ -51,6 +51,7 @@ public function __construct(
}

/**
* @throws ExceptionInterface
* @throws \JsonException
*/
public function process(string $message): ?string
Expand Down Expand Up @@ -88,6 +89,9 @@ public function process(string $message): ?string
}
}

/**
* @throws \JsonException
*/
private function encodeResponse(Response|Error|null $response): ?string
{
if (null === $response) {
Expand All @@ -105,15 +109,22 @@ private function encodeResponse(Response|Error|null $response): ?string
return json_encode($response, \JSON_THROW_ON_ERROR);
}

/**
* @throws ExceptionInterface
*/
private function handleNotification(Notification $notification): null
{
$handled = false;
foreach ($this->notificationHandlers as $handler) {
if ($handler->supports($notification)) {
return $handler->handle($notification);
$handler->handle($notification);
$handled = true;
}
}

$this->logger->warning(\sprintf('No handler found for "%s".', $notification->method), ['notification' => $notification]);
if (!$handled) {
$this->logger->warning(\sprintf('No handler found for "%s".', $notification->method), ['notification' => $notification]);
}

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ protected function supportedNotification(): string
return 'initialized';
}

public function handle(Notification $notification): null
public function handle(Notification $notification): void
{
return null;
}
}
6 changes: 5 additions & 1 deletion src/mcp-sdk/src/Server/NotificationHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

namespace Symfony\AI\McpSdk\Server;

use Symfony\AI\McpSdk\Exception\ExceptionInterface;
use Symfony\AI\McpSdk\Message\Notification;

interface NotificationHandlerInterface
{
public function supports(Notification $message): bool;

public function handle(Notification $notification): null;
/**
* @throws ExceptionInterface
*/
public function handle(Notification $notification): void;
}
4 changes: 4 additions & 0 deletions src/mcp-sdk/src/Server/RequestHandlerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\AI\McpSdk\Server;

use Symfony\AI\McpSdk\Exception\ExceptionInterface;
use Symfony\AI\McpSdk\Message\Error;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Message\Response;
Expand All @@ -19,5 +20,8 @@ interface RequestHandlerInterface
{
public function supports(Request $message): bool;

/**
* @throws ExceptionInterface
*/
public function createResponse(Request $message): Response|Error;
}
4 changes: 4 additions & 0 deletions src/mcp-sdk/tests/Message/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace Symfony\AI\McpSdk\Tests\Message;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
use Symfony\AI\McpSdk\Message\Error;

#[Small]
#[CoversClass(Error::class)]
final class ErrorTest extends TestCase
{
public function testWithIntegerId(): void
Expand Down
4 changes: 4 additions & 0 deletions src/mcp-sdk/tests/Message/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@

namespace Symfony\AI\McpSdk\Tests\Message;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
use Symfony\AI\McpSdk\Message\Factory;
use Symfony\AI\McpSdk\Message\Notification;
use Symfony\AI\McpSdk\Message\Request;

#[Small]
#[CoversClass(Factory::class)]
final class FactoryTest extends TestCase
{
private Factory $factory;
Expand Down
4 changes: 4 additions & 0 deletions src/mcp-sdk/tests/Message/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace Symfony\AI\McpSdk\Tests\Message;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
use Symfony\AI\McpSdk\Message\Response;

#[Small]
#[CoversClass(Response::class)]
final class ResponseTest extends TestCase
{
public function testWithIntegerId(): void
Expand Down
88 changes: 88 additions & 0 deletions src/mcp-sdk/tests/Server/JsonRpcHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Tests\Server;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\AI\McpSdk\Message\Factory;
use Symfony\AI\McpSdk\Message\Response;
use Symfony\AI\McpSdk\Server\JsonRpcHandler;
use Symfony\AI\McpSdk\Server\NotificationHandlerInterface;
use Symfony\AI\McpSdk\Server\RequestHandlerInterface;

#[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());
$jsonRpc->process(
'{"jsonrpc": "2.0", "id": 1, "method": "notifications/foobar"}'
);
}

#[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());
$jsonRpc->process(
'{"jsonrpc": "2.0", "id": 1, "method": "request/foobar"}'
);
}
}
106 changes: 106 additions & 0 deletions src/mcp-sdk/tests/Server/RequestHandler/ToolListHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\McpSdk\Tests\Server\RequestHandler;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
use Symfony\AI\McpSdk\Capability\Tool\CollectionInterface;
use Symfony\AI\McpSdk\Capability\Tool\MetadataInterface;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Server\RequestHandler\ToolListHandler;

#[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);
}

public function testHandleReturnAll(): void
{
$item = 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',
];
}
};
$collection = $this->getMockBuilder(CollectionInterface::class)
->disableOriginalConstructor()
->onlyMethods(['getMetadata'])
->getMock();
$collection->expects($this->once())->method('getMetadata')->willReturn([$item]);

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

public function testHandlePagination(): void
{
$item = 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',
];
}
};
$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);
}
}
4 changes: 4 additions & 0 deletions src/mcp-sdk/tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@

namespace Symfony\AI\McpSdk\Tests;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\MockObject\Stub\Exception;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\AI\McpSdk\Server;
use Symfony\AI\McpSdk\Server\JsonRpcHandler;
use Symfony\AI\McpSdk\Tests\Fixtures\InMemoryTransport;

#[Small]
#[CoversClass(Server::class)]
class ServerTest extends TestCase
{
public function testJsonExceptions(): void
Expand Down