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
20 changes: 8 additions & 12 deletions src/Capability/Discovery/Discoverer.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ private function processFile(SplFileInfo $file, array &$discoveredCount, array &
private function processMethod(\ReflectionMethod $method, array &$discoveredCount, \ReflectionAttribute $attribute, array &$tools, array &$resources, array &$prompts, array &$resourceTemplates): void
{
$className = $method->getDeclaringClass()->getName();
$classShortName = $method->getDeclaringClass()->getShortName();
$methodName = $method->getName();
$attributeClassName = $attribute->getName();

Expand All @@ -226,9 +225,8 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun

switch ($attributeClassName) {
case McpTool::class:
$docBlock = $this->docBlockParser->parseDocBlock($method->getDocComment() ?? null);
$name = $instance->name ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $instance->description ?? $this->docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($method, $instance->name);
$description = ElementMetadataResolver::resolveDescription($method, $instance->description, $this->docBlockParser);
$inputSchema = $this->schemaGenerator->generate($method);
$outputSchema = $this->schemaGenerator->generateOutputSchema($method);
$tool = new Tool(
Expand All @@ -246,9 +244,8 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
break;

case McpResource::class:
$docBlock = $this->docBlockParser->parseDocBlock($method->getDocComment() ?? null);
$name = $instance->name ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $instance->description ?? $this->docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($method, $instance->name);
$description = ElementMetadataResolver::resolveDescription($method, $instance->description, $this->docBlockParser);
$resource = new ResourceDefinition(
$instance->uri,
$name,
Expand All @@ -267,8 +264,8 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun

case McpPrompt::class:
$docBlock = $this->docBlockParser->parseDocBlock($method->getDocComment() ?? null);
$name = $instance->name ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $instance->description ?? $this->docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($method, $instance->name);
$description = ElementMetadataResolver::resolveDescription($method, $instance->description, $this->docBlockParser);
$arguments = [];
$paramTags = $this->docBlockParser->getParamTags($docBlock);
foreach ($method->getParameters() as $param) {
Expand All @@ -286,9 +283,8 @@ private function processMethod(\ReflectionMethod $method, array &$discoveredCoun
break;

case McpResourceTemplate::class:
$docBlock = $this->docBlockParser->parseDocBlock($method->getDocComment() ?? null);
$name = $instance->name ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $instance->description ?? $this->docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($method, $instance->name);
$description = ElementMetadataResolver::resolveDescription($method, $instance->description, $this->docBlockParser);
$mimeType = $instance->mimeType;
$annotations = $instance->annotations;
$meta = $instance->meta ?? null;
Expand Down
44 changes: 44 additions & 0 deletions src/Capability/Discovery/ElementMetadataResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Mcp\Capability\Discovery;

/**
* Derives an element's name and description from its handler method, used by
* both attribute discovery and reflection-based manual registration.
*
* @author Christopher Hertel <mail@christopher-hertel.de>
*/
final class ElementMetadataResolver
{
private function __construct()
{
}

/**
* Explicit names win; otherwise invokable classes are named after the class,
* regular handlers after the method.
*/
public static function resolveName(\ReflectionMethod $method, ?string $name): string
{
$methodName = $method->getName();

return $name ?? ('__invoke' === $methodName ? $method->getDeclaringClass()->getShortName() : $methodName);
}

/**
* Explicit descriptions win; otherwise the summary of the method's doc block is used.
*/
public static function resolveDescription(\ReflectionMethod $method, ?string $description, DocBlockParser $docBlockParser): ?string
{
return $description ?? $docBlockParser->getDescription($docBlockParser->parseDocBlock($method->getDocComment() ?? null));
}
}
33 changes: 9 additions & 24 deletions src/Capability/Registry/Loader/ReflectedElementLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Mcp\Capability\Completion\ListCompletionProvider;
use Mcp\Capability\Completion\ProviderInterface;
use Mcp\Capability\Discovery\DocBlockParser;
use Mcp\Capability\Discovery\ElementMetadataResolver;
use Mcp\Capability\Discovery\HandlerResolver;
use Mcp\Capability\Discovery\SchemaGenerator;
use Mcp\Capability\Discovery\SchemaGeneratorInterface;
Expand Down Expand Up @@ -106,12 +107,8 @@ public function load(RegistryInterface $registry): void
$name = $data['name'] ?? 'closure_tool_'.spl_object_id($data['handler']);
$description = $data['description'] ?? null;
} else {
$classShortName = $reflection->getDeclaringClass()->getShortName();
$methodName = $reflection->getName();
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);

$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $data['description'] ?? $docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($reflection, $data['name'] ?? null);
$description = ElementMetadataResolver::resolveDescription($reflection, $data['description'] ?? null, $docBlockParser);
}

$inputSchema = $data['inputSchema'] ?? $schemaGenerator->generate($reflection);
Expand Down Expand Up @@ -148,12 +145,8 @@ public function load(RegistryInterface $registry): void
$name = $data['name'] ?? 'closure_resource_'.spl_object_id($data['handler']);
$description = $data['description'] ?? null;
} else {
$classShortName = $reflection->getDeclaringClass()->getShortName();
$methodName = $reflection->getName();
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);

$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $data['description'] ?? $docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($reflection, $data['name'] ?? null);
$description = ElementMetadataResolver::resolveDescription($reflection, $data['description'] ?? null, $docBlockParser);
}

$resource = new ResourceDefinition(
Expand Down Expand Up @@ -189,12 +182,8 @@ public function load(RegistryInterface $registry): void
$name = $data['name'] ?? 'closure_template_'.spl_object_id($data['handler']);
$description = $data['description'] ?? null;
} else {
$classShortName = $reflection->getDeclaringClass()->getShortName();
$methodName = $reflection->getName();
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);

$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $data['description'] ?? $docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($reflection, $data['name'] ?? null);
$description = ElementMetadataResolver::resolveDescription($reflection, $data['description'] ?? null, $docBlockParser);
}

$template = new ResourceTemplate(
Expand Down Expand Up @@ -229,12 +218,8 @@ public function load(RegistryInterface $registry): void
$name = $data['name'] ?? 'closure_prompt_'.spl_object_id($data['handler']);
$description = $data['description'] ?? null;
} else {
$classShortName = $reflection->getDeclaringClass()->getShortName();
$methodName = $reflection->getName();
$docBlock = $docBlockParser->parseDocBlock($reflection->getDocComment() ?? null);

$name = $data['name'] ?? ('__invoke' === $methodName ? $classShortName : $methodName);
$description = $data['description'] ?? $docBlockParser->getDescription($docBlock) ?? null;
$name = ElementMetadataResolver::resolveName($reflection, $data['name'] ?? null);
$description = ElementMetadataResolver::resolveDescription($reflection, $data['description'] ?? null, $docBlockParser);
}

$arguments = [];
Expand Down