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
14 changes: 10 additions & 4 deletions src/mcp-sdk/src/Server/RequestHandler/PromptListHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\AI\McpSdk\Server\RequestHandler;

use Symfony\AI\McpSdk\Capability\Prompt\CollectionInterface;
use Symfony\AI\McpSdk\Capability\Prompt\MetadataInterface;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Message\Response;

Expand All @@ -27,7 +26,14 @@ public function __construct(
public function createResponse(Request $message): Response
{
$nextCursor = null;
$prompts = array_map(function (MetadataInterface $metadata) use (&$nextCursor) {
$prompts = [];

$metadataList = $this->collection->getMetadata(
$this->pageSize,
$message->params['cursor'] ?? null
);

foreach ($metadataList as $metadata) {
$nextCursor = $metadata->getName();
$result = [
'name' => $metadata->getName(),
Expand Down Expand Up @@ -55,8 +61,8 @@ public function createResponse(Request $message): Response
$result['arguments'] = $arguments;
}

return $result;
}, $this->collection->getMetadata($this->pageSize, $message->params['cursor'] ?? null));
$prompts[] = $result;
}

$result = [
'prompts' => $prompts,
Expand Down
14 changes: 10 additions & 4 deletions src/mcp-sdk/src/Server/RequestHandler/ResourceListHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\AI\McpSdk\Server\RequestHandler;

use Symfony\AI\McpSdk\Capability\Resource\CollectionInterface;
use Symfony\AI\McpSdk\Capability\Resource\MetadataInterface;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Message\Response;

Expand All @@ -27,7 +26,14 @@ public function __construct(
public function createResponse(Request $message): Response
{
$nextCursor = null;
$resources = array_map(function (MetadataInterface $metadata) use (&$nextCursor) {
$resources = [];

$metadataList = $this->collection->getMetadata(
$this->pageSize,
$message->params['cursor'] ?? null
);

foreach ($metadataList as $metadata) {
$nextCursor = $metadata->getUri();
$result = [
'uri' => $metadata->getUri(),
Expand All @@ -49,8 +55,8 @@ public function createResponse(Request $message): Response
$result['size'] = $size;
}

return $result;
}, $this->collection->getMetadata($this->pageSize, $message->params['cursor'] ?? null));
$resources[] = $result;
}

$result = [
'resources' => $resources,
Expand Down
15 changes: 10 additions & 5 deletions src/mcp-sdk/src/Server/RequestHandler/ToolListHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\AI\McpSdk\Server\RequestHandler;

use Symfony\AI\McpSdk\Capability\Tool\CollectionInterface;
use Symfony\AI\McpSdk\Capability\Tool\MetadataInterface;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Message\Response;

Expand All @@ -27,19 +26,25 @@ public function __construct(
public function createResponse(Request $message): Response
{
$nextCursor = null;
$tools = array_map(function (MetadataInterface $tool) use (&$nextCursor) {
$tools = [];

$metadataList = $this->collection->getMetadata(
$this->pageSize,
$message->params['cursor'] ?? null
);

foreach ($metadataList as $tool) {
$nextCursor = $tool->getName();
$inputSchema = $tool->getInputSchema();

return [
$tools[] = [
'name' => $tool->getName(),
'description' => $tool->getDescription(),
'inputSchema' => [] === $inputSchema ? [
'type' => 'object',
'$schema' => 'http://json-schema.org/draft-07/schema#',
] : $inputSchema,
];
}, $this->collection->getMetadata($this->pageSize, $message->params['cursor'] ?? null));
}

$result = [
'tools' => $tools,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\Prompt\MetadataInterface;
use Symfony\AI\McpSdk\Capability\PromptChain;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Server\RequestHandler\PromptListHandler;

#[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 changes: 117 additions & 0 deletions src/mcp-sdk/tests/Server/RequestHandler/ResourceListHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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\DataProvider;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;
use Symfony\AI\McpSdk\Capability\Resource\CollectionInterface;
use Symfony\AI\McpSdk\Capability\Resource\MetadataInterface;
use Symfony\AI\McpSdk\Message\Request;
use Symfony\AI\McpSdk\Server\RequestHandler\ResourceListHandler;

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