Skip to content
Merged
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
64 changes: 20 additions & 44 deletions src/Capability/Registry/ResourceTemplateReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Mcp\Schema\Content\ResourceContents;
use Mcp\Schema\Content\TextResourceContents;
use Mcp\Schema\ResourceTemplate;
use Psr\Container\ContainerInterface;

/**
* @phpstan-import-type Handler from ElementReference
Expand All @@ -31,11 +30,6 @@ class ResourceTemplateReference extends ElementReference
*/
private array $variableNames;

/**
* @var array<string, string>
*/
private array $uriVariables;

private string $uriTemplateRegex;

/**
Expand All @@ -53,22 +47,6 @@ public function __construct(
$this->compileTemplate();
}

/**
* @deprecated
* Gets the resource template
*
* @return array<int, ResourceContents> array of ResourceContents objects
*/
public function read(ContainerInterface $container, string $uri): array
{
$arguments = array_merge($this->uriVariables, ['uri' => $uri]);

$referenceHandler = new ReferenceHandler($container);
$result = $referenceHandler->handle($this, $arguments);

return $this->formatResult($result, $uri, $this->resourceTemplate->mimeType);
}

/**
* @return array<int, string>
*/
Expand All @@ -87,34 +65,12 @@ public function matches(string $uri): bool
}
}

$this->uriVariables = $variables;

return true;
}

return false;
}

private function compileTemplate(): void
{
$this->variableNames = [];
$regexParts = [];

$segments = preg_split('/(\{\w+\})/', $this->resourceTemplate->uriTemplate, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);

foreach ($segments as $segment) {
if (preg_match('/^\{(\w+)\}$/', $segment, $matches)) {
$varName = $matches[1];
$this->variableNames[] = $varName;
$regexParts[] = '(?P<'.$varName.'>[^/]+)';
} else {
$regexParts[] = preg_quote($segment, '#');
}
}

$this->uriTemplateRegex = '#^'.implode('', $regexParts).'$#';
}

/**
* Formats the raw result of a resource read operation into MCP ResourceContent items.
*
Expand Down Expand Up @@ -256,6 +212,26 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
throw new RuntimeException("Cannot format resource read result for URI '{$uri}'. Handler method returned unhandled type: ".\gettype($readResult));
}

private function compileTemplate(): void
{
$this->variableNames = [];
$regexParts = [];

$segments = preg_split('/(\{\w+\})/', $this->resourceTemplate->uriTemplate, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);

foreach ($segments as $segment) {
if (preg_match('/^\{(\w+)\}$/', $segment, $matches)) {
$varName = $matches[1];
$this->variableNames[] = $varName;
$regexParts[] = '(?P<'.$varName.'>[^/]+)';
} else {
$regexParts[] = preg_quote($segment, '#');
}
}

$this->uriTemplateRegex = '#^'.implode('', $regexParts).'$#';
}

/** Guesses MIME type from string content (very basic) */
private function guessMimeTypeFromString(string $content): string
{
Expand Down