|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ecourty\McpServerBundle\Command; |
| 6 | + |
| 7 | +use Ecourty\McpServerBundle\Resource\AbstractResourceDefinition; |
| 8 | +use Ecourty\McpServerBundle\Service\ResourceRegistry; |
| 9 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 10 | +use Symfony\Component\Console\Command\Command; |
| 11 | +use Symfony\Component\Console\Input\InputArgument; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Output\OutputInterface; |
| 14 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 15 | + |
| 16 | +/** |
| 17 | + * Command to display information about MCP resources. |
| 18 | + * |
| 19 | + * This command allows users to view details about specific MCP resources or all available resources. |
| 20 | + * It provides a table format for easy readability of resource attributes such as name, uri, description, |
| 21 | + * mimeType... |
| 22 | + */ |
| 23 | +#[AsCommand( |
| 24 | + name: 'debug:mcp-resources', |
| 25 | + description: 'Display current MCP resources', |
| 26 | +)] |
| 27 | +class DebugResourceCommand extends Command |
| 28 | +{ |
| 29 | + public function __construct( |
| 30 | + private readonly ResourceRegistry $resourceRegistry, |
| 31 | + ) { |
| 32 | + parent::__construct(); |
| 33 | + } |
| 34 | + |
| 35 | + protected function configure(): void |
| 36 | + { |
| 37 | + $this |
| 38 | + ->addArgument('resource', InputArgument::OPTIONAL, 'Resource name'); |
| 39 | + } |
| 40 | + |
| 41 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 42 | + { |
| 43 | + $io = new SymfonyStyle($input, $output); |
| 44 | + |
| 45 | + $resourceName = $input->getArgument('resource'); |
| 46 | + |
| 47 | + if ($resourceName !== null) { |
| 48 | + return $this->displaySingleResourceInformation($io, $resourceName); |
| 49 | + } |
| 50 | + |
| 51 | + return $this->displayAllResourcesInformation($io); |
| 52 | + } |
| 53 | + |
| 54 | + private function displaySingleResourceInformation(SymfonyStyle $io, string $resourceName): int |
| 55 | + { |
| 56 | + $resourceDefinition = $this->resourceRegistry->getResourceDefinition($resourceName); |
| 57 | + |
| 58 | + if ($resourceDefinition === null) { |
| 59 | + $io->error(\sprintf('Resource "%s" not found.', $resourceName)); |
| 60 | + |
| 61 | + return self::FAILURE; |
| 62 | + } |
| 63 | + |
| 64 | + $io->table( |
| 65 | + ['Name', 'URI', 'Title', 'Description', 'MimeType', 'Size'], |
| 66 | + [ |
| 67 | + [ |
| 68 | + $resourceDefinition->name, |
| 69 | + $resourceDefinition->uri, |
| 70 | + $resourceDefinition->title, |
| 71 | + $resourceDefinition->description, |
| 72 | + $resourceDefinition->mimeType, |
| 73 | + $resourceDefinition->size ?? null, |
| 74 | + ], |
| 75 | + ], |
| 76 | + ); |
| 77 | + |
| 78 | + return self::SUCCESS; |
| 79 | + } |
| 80 | + |
| 81 | + private function displayAllResourcesInformation(SymfonyStyle $io): int |
| 82 | + { |
| 83 | + $io->title('MCP Resources Debug Information'); |
| 84 | + |
| 85 | + $resourceDefinitions = $this->resourceRegistry->getResourceDefinitions(); |
| 86 | + |
| 87 | + if (empty($resourceDefinitions) === true) { |
| 88 | + $io->warning('No resources found.'); |
| 89 | + |
| 90 | + return self::SUCCESS; |
| 91 | + } |
| 92 | + |
| 93 | + $io->table( |
| 94 | + ['Name', 'URI', 'Title', 'Description', 'MimeType', 'Size'], |
| 95 | + array_map(static function (AbstractResourceDefinition $resourceDefinition) { |
| 96 | + return [ |
| 97 | + $resourceDefinition->name, |
| 98 | + $resourceDefinition->uri, |
| 99 | + $resourceDefinition->title, |
| 100 | + $resourceDefinition->description, |
| 101 | + $resourceDefinition->mimeType, |
| 102 | + $resourceDefinition->size ?? null, |
| 103 | + ]; |
| 104 | + }, $resourceDefinitions), |
| 105 | + ); |
| 106 | + |
| 107 | + return self::SUCCESS; |
| 108 | + } |
| 109 | +} |
0 commit comments