diff --git a/examples/http-discovery-userprofile/McpElements.php b/examples/http-discovery-userprofile/McpElements.php index 7120a5f..e763891 100644 --- a/examples/http-discovery-userprofile/McpElements.php +++ b/examples/http-discovery-userprofile/McpElements.php @@ -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; /** @@ -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', @@ -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]; @@ -130,7 +130,7 @@ public function testToolWithoutParams(): array * * @return array[] prompt messages * - * @throws McpServerException if user not found + * @throws InvalidArgumentException if user not found */ #[McpPrompt(name: 'generate_bio_prompt')] public function generateBio( @@ -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]; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index f78906a..f5901b2 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -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\\} but returns array\{contents\: array\\}\.$#' identifier: return.type diff --git a/src/Capability/Registry/ResourceTemplateReference.php b/src/Capability/Registry/ResourceTemplateReference.php index 951d0af..b71a943 100644 --- a/src/Capability/Registry/ResourceTemplateReference.php +++ b/src/Capability/Registry/ResourceTemplateReference.php @@ -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 */ + 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); } /** diff --git a/src/Server/Handler/Request/ReadResourceHandler.php b/src/Server/Handler/Request/ReadResourceHandler.php index 83f0d65..19e426a 100644 --- a/src/Server/Handler/Request/ReadResourceHandler.php +++ b/src/Server/Handler/Request/ReadResourceHandler.php @@ -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); }