Skip to content

Commit 5fc8dff

Browse files
fix: extract and pass URI template variables to resource handlers
1 parent f41ea86 commit 5fc8dff

File tree

4 files changed

+19
-29
lines changed

4 files changed

+19
-29
lines changed

examples/http-discovery-userprofile/McpElements.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Mcp\Capability\Attribute\McpResource;
1717
use Mcp\Capability\Attribute\McpResourceTemplate;
1818
use Mcp\Capability\Attribute\McpTool;
19+
use Mcp\Exception\InvalidArgumentException;
1920
use Psr\Log\LoggerInterface;
2021

2122
/**
@@ -47,7 +48,7 @@ public function __construct(
4748
*
4849
* @return User user profile data
4950
*
50-
* @throws McpServerException if the user is not found
51+
* @throws InvalidArgumentException if the user is not found
5152
*/
5253
#[McpResourceTemplate(
5354
uriTemplate: 'user://{userId}/profile',
@@ -61,8 +62,7 @@ public function getUserProfile(
6162
): array {
6263
$this->logger->info('Reading resource: user profile', ['userId' => $userId]);
6364
if (!isset($this->users[$userId])) {
64-
// Throwing an exception that Processor can turn into an error response
65-
throw McpServerException::invalidParams("User profile not found for ID: {$userId}");
65+
throw new InvalidArgumentException("User profile not found for ID: {$userId}");
6666
}
6767

6868
return $this->users[$userId];
@@ -130,7 +130,7 @@ public function testToolWithoutParams(): array
130130
*
131131
* @return array<string, string>[] prompt messages
132132
*
133-
* @throws McpServerException if user not found
133+
* @throws InvalidArgumentException if user not found
134134
*/
135135
#[McpPrompt(name: 'generate_bio_prompt')]
136136
public function generateBio(
@@ -140,7 +140,7 @@ public function generateBio(
140140
): array {
141141
$this->logger->info('Executing prompt: generate_bio', ['userId' => $userId, 'tone' => $tone]);
142142
if (!isset($this->users[$userId])) {
143-
throw McpServerException::invalidParams("User not found for bio prompt: {$userId}");
143+
throw new InvalidArgumentException("User not found for bio prompt: {$userId}");
144144
}
145145
$user = $this->users[$userId];
146146

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: '#^Call to static method invalidParams\(\) on an unknown class Mcp\\Example\\HttpDiscoveryUserProfile\\McpServerException\.$#'
5-
identifier: class.notFound
6-
count: 2
7-
path: examples/http-discovery-userprofile/McpElements.php
8-
9-
-
10-
message: '#^PHPDoc tag @throws with type Mcp\\Example\\HttpDiscoveryUserProfile\\McpServerException is not subtype of Throwable$#'
11-
identifier: throws.notThrowable
12-
count: 2
13-
path: examples/http-discovery-userprofile/McpElements.php
14-
153
-
164
message: '#^Method Mcp\\Schema\\Result\\ReadResourceResult\:\:jsonSerialize\(\) should return array\{contents\: array\<Mcp\\Schema\\Content\\BlobResourceContents\|Mcp\\Schema\\Content\\TextResourceContents\>\} but returns array\{contents\: array\<Mcp\\Schema\\Content\\ResourceContents\>\}\.$#'
175
identifier: return.type

src/Capability/Registry/ResourceTemplateReference.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,17 @@ public function getVariableNames(): array
5757

5858
public function matches(string $uri): bool
5959
{
60-
if (preg_match($this->uriTemplateRegex, $uri, $matches)) {
61-
$variables = [];
62-
foreach ($this->variableNames as $varName) {
63-
if (isset($matches[$varName])) {
64-
$variables[$varName] = $matches[$varName];
65-
}
66-
}
60+
return 1 === preg_match($this->uriTemplateRegex, $uri);
61+
}
6762

68-
return true;
69-
}
63+
/** @return array<string, mixed> */
64+
public function extractVariables(string $uri): array
65+
{
66+
$matches = [];
67+
68+
preg_match($this->uriTemplateRegex, $uri, $matches);
7069

71-
return false;
70+
return array_filter($matches, fn ($key) => \in_array($key, $this->variableNames), \ARRAY_FILTER_USE_KEY);
7271
}
7372

7473
/**

src/Server/Handler/Request/ReadResourceHandler.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,14 @@ public function handle(Request $request, SessionInterface $session): Response|Er
6565
'_session' => $session,
6666
];
6767

68-
$result = $this->referenceHandler->handle($reference, $arguments);
69-
7068
if ($reference instanceof ResourceTemplateReference) {
69+
$variables = $reference->extractVariables($uri);
70+
$arguments = array_merge($arguments, $variables);
71+
72+
$result = $this->referenceHandler->handle($reference, $arguments);
7173
$formatted = $reference->formatResult($result, $uri, $reference->resourceTemplate->mimeType);
7274
} else {
75+
$result = $this->referenceHandler->handle($reference, $arguments);
7376
$formatted = $reference->formatResult($result, $uri, $reference->schema->mimeType);
7477
}
7578

0 commit comments

Comments
 (0)