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
10 changes: 5 additions & 5 deletions examples/http-discovery-userprofile/McpElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Mcp\Capability\Attribute\McpResource;
use Mcp\Capability\Attribute\McpResourceTemplate;
use Mcp\Capability\Attribute\McpTool;
use Mcp\Exception\InvalidArgumentException;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ public function __construct(
*
* @return User user profile data
*
* @throws McpServerException if the user is not found
* @throws InvalidArgumentException if the user is not found
*/
#[McpResourceTemplate(
uriTemplate: 'user://{userId}/profile',
Expand All @@ -61,8 +62,7 @@ public function getUserProfile(
): array {
$this->logger->info('Reading resource: user profile', ['userId' => $userId]);
if (!isset($this->users[$userId])) {
// Throwing an exception that Processor can turn into an error response
throw McpServerException::invalidParams("User profile not found for ID: {$userId}");
throw new InvalidArgumentException("User profile not found for ID: {$userId}");
}

return $this->users[$userId];
Expand Down Expand Up @@ -130,7 +130,7 @@ public function testToolWithoutParams(): array
*
* @return array<string, string>[] prompt messages
*
* @throws McpServerException if user not found
* @throws InvalidArgumentException if user not found
*/
#[McpPrompt(name: 'generate_bio_prompt')]
public function generateBio(
Expand All @@ -140,7 +140,7 @@ public function generateBio(
): array {
$this->logger->info('Executing prompt: generate_bio', ['userId' => $userId, 'tone' => $tone]);
if (!isset($this->users[$userId])) {
throw McpServerException::invalidParams("User not found for bio prompt: {$userId}");
throw new InvalidArgumentException("User not found for bio prompt: {$userId}");
}
$user = $this->users[$userId];

Expand Down
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
parameters:
ignoreErrors:
-
message: '#^Call to static method invalidParams\(\) on an unknown class Mcp\\Example\\HttpDiscoveryUserProfile\\McpServerException\.$#'
identifier: class.notFound
count: 2
path: examples/http-discovery-userprofile/McpElements.php

-
message: '#^PHPDoc tag @throws with type Mcp\\Example\\HttpDiscoveryUserProfile\\McpServerException is not subtype of Throwable$#'
identifier: throws.notThrowable
count: 2
path: examples/http-discovery-userprofile/McpElements.php

-
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\>\}\.$#'
identifier: return.type
Expand Down
19 changes: 9 additions & 10 deletions src/Capability/Registry/ResourceTemplateReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,17 @@ public function getVariableNames(): array

public function matches(string $uri): bool
{
if (preg_match($this->uriTemplateRegex, $uri, $matches)) {
$variables = [];
foreach ($this->variableNames as $varName) {
if (isset($matches[$varName])) {
$variables[$varName] = $matches[$varName];
}
}
return 1 === preg_match($this->uriTemplateRegex, $uri);
}

return true;
}
/** @return array<string, mixed> */
public function extractVariables(string $uri): array
{
$matches = [];

preg_match($this->uriTemplateRegex, $uri, $matches);

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

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Server/Handler/Request/ReadResourceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,14 @@ public function handle(Request $request, SessionInterface $session): Response|Er
'_session' => $session,
];

$result = $this->referenceHandler->handle($reference, $arguments);

if ($reference instanceof ResourceTemplateReference) {
$variables = $reference->extractVariables($uri);
$arguments = array_merge($arguments, $variables);

$result = $this->referenceHandler->handle($reference, $arguments);
$formatted = $reference->formatResult($result, $uri, $reference->resourceTemplate->mimeType);
} else {
$result = $this->referenceHandler->handle($reference, $arguments);
$formatted = $reference->formatResult($result, $uri, $reference->schema->mimeType);
}

Expand Down