Skip to content

Commit ed16e6b

Browse files
chr-hertelbigdevlarry
authored andcommitted
[Server] Remove dead resource template handler code (modelcontextprotocol#102)
1 parent 4f9df3d commit ed16e6b

File tree

1 file changed

+20
-44
lines changed

1 file changed

+20
-44
lines changed

src/Capability/Registry/ResourceTemplateReference.php

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Mcp\Schema\Content\ResourceContents;
1818
use Mcp\Schema\Content\TextResourceContents;
1919
use Mcp\Schema\ResourceTemplate;
20-
use Psr\Container\ContainerInterface;
2120

2221
/**
2322
* @phpstan-import-type Handler from ElementReference
@@ -31,11 +30,6 @@ class ResourceTemplateReference extends ElementReference
3130
*/
3231
private array $variableNames;
3332

34-
/**
35-
* @var array<string, string>
36-
*/
37-
private array $uriVariables;
38-
3933
private string $uriTemplateRegex;
4034

4135
/**
@@ -53,22 +47,6 @@ public function __construct(
5347
$this->compileTemplate();
5448
}
5549

56-
/**
57-
* @deprecated
58-
* Gets the resource template
59-
*
60-
* @return array<int, ResourceContents> array of ResourceContents objects
61-
*/
62-
public function read(ContainerInterface $container, string $uri): array
63-
{
64-
$arguments = array_merge($this->uriVariables, ['uri' => $uri]);
65-
66-
$referenceHandler = new ReferenceHandler($container);
67-
$result = $referenceHandler->handle($this, $arguments);
68-
69-
return $this->formatResult($result, $uri, $this->resourceTemplate->mimeType);
70-
}
71-
7250
/**
7351
* @return array<int, string>
7452
*/
@@ -87,34 +65,12 @@ public function matches(string $uri): bool
8765
}
8866
}
8967

90-
$this->uriVariables = $variables;
91-
9268
return true;
9369
}
9470

9571
return false;
9672
}
9773

98-
private function compileTemplate(): void
99-
{
100-
$this->variableNames = [];
101-
$regexParts = [];
102-
103-
$segments = preg_split('/(\{\w+\})/', $this->resourceTemplate->uriTemplate, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
104-
105-
foreach ($segments as $segment) {
106-
if (preg_match('/^\{(\w+)\}$/', $segment, $matches)) {
107-
$varName = $matches[1];
108-
$this->variableNames[] = $varName;
109-
$regexParts[] = '(?P<'.$varName.'>[^/]+)';
110-
} else {
111-
$regexParts[] = preg_quote($segment, '#');
112-
}
113-
}
114-
115-
$this->uriTemplateRegex = '#^'.implode('', $regexParts).'$#';
116-
}
117-
11874
/**
11975
* Formats the raw result of a resource read operation into MCP ResourceContent items.
12076
*
@@ -256,6 +212,26 @@ public function formatResult(mixed $readResult, string $uri, ?string $mimeType =
256212
throw new RuntimeException("Cannot format resource read result for URI '{$uri}'. Handler method returned unhandled type: ".\gettype($readResult));
257213
}
258214

215+
private function compileTemplate(): void
216+
{
217+
$this->variableNames = [];
218+
$regexParts = [];
219+
220+
$segments = preg_split('/(\{\w+\})/', $this->resourceTemplate->uriTemplate, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
221+
222+
foreach ($segments as $segment) {
223+
if (preg_match('/^\{(\w+)\}$/', $segment, $matches)) {
224+
$varName = $matches[1];
225+
$this->variableNames[] = $varName;
226+
$regexParts[] = '(?P<'.$varName.'>[^/]+)';
227+
} else {
228+
$regexParts[] = preg_quote($segment, '#');
229+
}
230+
}
231+
232+
$this->uriTemplateRegex = '#^'.implode('', $regexParts).'$#';
233+
}
234+
259235
/** Guesses MIME type from string content (very basic) */
260236
private function guessMimeTypeFromString(string $content): string
261237
{

0 commit comments

Comments
 (0)