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

View file

@ -0,0 +1,77 @@
<?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\Capability;
use Mcp\Capability\Resource\CollectionInterface;
use Mcp\Capability\Resource\IdentifierInterface;
use Mcp\Capability\Resource\MetadataInterface;
use Mcp\Capability\Resource\ResourceRead;
use Mcp\Capability\Resource\ResourceReaderInterface;
use Mcp\Capability\Resource\ResourceReadResult;
use Mcp\Exception\InvalidCursorException;
use Mcp\Exception\ResourceNotFoundException;
use Mcp\Exception\ResourceReadException;
/**
* A collection of resources. All resources need to implement IdentifierInterface.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ResourceChain implements CollectionInterface, ResourceReaderInterface
{
public function __construct(
/**
* @var IdentifierInterface[]
*/
private readonly array $items,
) {
}
public function getMetadata(int $count, ?string $lastIdentifier = null): iterable
{
$found = null === $lastIdentifier;
foreach ($this->items as $item) {
if (!$item instanceof MetadataInterface) {
continue;
}
if (false === $found) {
$found = $item->getUri() === $lastIdentifier;
continue;
}
yield $item;
if (--$count <= 0) {
break;
}
}
if (!$found) {
throw new InvalidCursorException($lastIdentifier);
}
}
public function read(ResourceRead $input): ResourceReadResult
{
foreach ($this->items as $item) {
if ($item instanceof ResourceReaderInterface && $input->uri === $item->getUri()) {
try {
return $item->read($input);
} catch (\Throwable $e) {
throw new ResourceReadException($input, $e);
}
}
}
throw new ResourceNotFoundException($input);
}
}