diff --git a/src/Services/InfoProviderSystem/DTOs/InfoProviderDTO.php b/src/Services/InfoProviderSystem/DTOs/InfoProviderDTO.php index 00b98436..5773bc49 100644 --- a/src/Services/InfoProviderSystem/DTOs/InfoProviderDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/InfoProviderDTO.php @@ -23,7 +23,9 @@ declare(strict_types=1); namespace App\Services\InfoProviderSystem\DTOs; use ApiPlatform\Metadata\ApiResource; +use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\McpToolCollection; +use ApiPlatform\OpenApi\Model\Operation; use App\Mcp\DTO\ListInfoProvidersInput; use App\State\Mcp\ListInfoProvidersProcessor; @@ -32,8 +34,16 @@ use App\State\Mcp\ListInfoProvidersProcessor; * searched via search_info_providers / get_info_provider_part_details). */ #[ApiResource( + uriTemplate: '/info_providers', description: 'An info provider which can be used to search for parts and retrieve part details.', - operations: [], + operations: [ + new GetCollection( + security: 'is_granted("@info_providers.create_parts")', + provider: ListInfoProvidersProcessor::class, + openapi: new Operation(summary: 'List the info providers which are currently active and can be used for searching parts.'), + ), + ], + paginationEnabled: false, mcp: [ 'list_info_providers' => new McpToolCollection( title: 'List available info providers', diff --git a/src/Services/InfoProviderSystem/DTOs/PartDetailDTO.php b/src/Services/InfoProviderSystem/DTOs/PartDetailDTO.php index f9bb442c..829b8805 100644 --- a/src/Services/InfoProviderSystem/DTOs/PartDetailDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/PartDetailDTO.php @@ -25,6 +25,8 @@ namespace App\Services\InfoProviderSystem\DTOs; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\McpTool; +use ApiPlatform\Metadata\Post; +use ApiPlatform\OpenApi\Model\Operation; use App\Entity\Parts\ManufacturingStatus; use App\Mcp\DTO\InfoProviderPartDetailsInput; use App\State\Mcp\GetInfoProviderPartDetailsProcessor; @@ -34,7 +36,16 @@ use App\State\Mcp\GetInfoProviderPartDetailsProcessor; */ #[ApiResource( description: 'Detailed information about a part from an external info provider (e.g. a distributor or manufacturer catalog), including datasheets, images, parameters and purchase information.', - operations: [], + operations: [ + new Post( + uriTemplate: '/info_providers/details', + security: 'is_granted("@info_providers.create_parts")', + input: InfoProviderPartDetailsInput::class, + validate: true, + processor: GetInfoProviderPartDetailsProcessor::class, + openapi: new Operation(summary: 'Get full detailed information about a specific part from an external info provider.'), + ), + ], mcp: [ 'get_info_provider_part_details' => new McpTool( title: 'Get part details from an info provider', diff --git a/src/Services/InfoProviderSystem/DTOs/SearchResultDTO.php b/src/Services/InfoProviderSystem/DTOs/SearchResultDTO.php index ec9f4bbc..0fc64533 100644 --- a/src/Services/InfoProviderSystem/DTOs/SearchResultDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/SearchResultDTO.php @@ -25,6 +25,8 @@ namespace App\Services\InfoProviderSystem\DTOs; use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\McpToolCollection; +use ApiPlatform\Metadata\Post; +use ApiPlatform\OpenApi\Model\Operation; use App\Entity\Parts\ManufacturingStatus; use App\Mcp\DTO\InfoProviderSearchInput; use App\State\Mcp\SearchInfoProvidersProcessor; @@ -35,7 +37,16 @@ use App\State\Mcp\SearchInfoProvidersProcessor; */ #[ApiResource( description: 'A search result for a part from an external info provider (e.g. a distributor or manufacturer catalog).', - operations: [], + operations: [ + new Post( + uriTemplate: '/info_providers/search', + security: 'is_granted("@info_providers.create_parts")', + input: InfoProviderSearchInput::class, + validate: true, + processor: SearchInfoProvidersProcessor::class, + openapi: new Operation(summary: 'Search external info providers (e.g. distributors like Digikey, Mouser, LCSC) for parts matching a keyword.'), + ), + ], mcp: [ 'search_info_providers' => new McpToolCollection( title: 'Search external info providers', diff --git a/src/State/Mcp/ListInfoProvidersProcessor.php b/src/State/Mcp/ListInfoProvidersProcessor.php index 7b46a80a..774ad4ce 100644 --- a/src/State/Mcp/ListInfoProvidersProcessor.php +++ b/src/State/Mcp/ListInfoProvidersProcessor.php @@ -24,20 +24,41 @@ namespace App\State\Mcp; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\ProcessorInterface; +use ApiPlatform\State\ProviderInterface; use App\Services\InfoProviderSystem\DTOs\InfoProviderDTO; use App\Services\InfoProviderSystem\ProviderRegistry; -class ListInfoProvidersProcessor implements ProcessorInterface +/** + * Used both as the state processor for the MCP list_info_providers tool and as the state provider for the + * REST GET /api/info_providers collection endpoint. + */ +class ListInfoProvidersProcessor implements ProcessorInterface, ProviderInterface { public function __construct( private readonly ProviderRegistry $providerRegistry, ) { } + /** + * @return InfoProviderDTO[] + */ + public function provide(Operation $operation, array $uriVariables = [], array $context = []): array + { + return $this->listActiveProviders(); + } + /** * @return InfoProviderDTO[] */ public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): array + { + return $this->listActiveProviders(); + } + + /** + * @return InfoProviderDTO[] + */ + private function listActiveProviders(): array { $result = []; diff --git a/tests/API/Endpoints/InfoProviderEndpointTest.php b/tests/API/Endpoints/InfoProviderEndpointTest.php new file mode 100644 index 00000000..7203b15c --- /dev/null +++ b/tests/API/Endpoints/InfoProviderEndpointTest.php @@ -0,0 +1,148 @@ +. + */ + +declare(strict_types=1); + +namespace App\Tests\API\Endpoints; + +use App\DataFixtures\APITokenFixtures; +use App\Tests\API\AuthenticatedApiTestCase; + +class InfoProviderEndpointTest extends AuthenticatedApiTestCase +{ + public function testListInfoProviders(): void + { + $response = self::createAuthenticatedClient()->request('GET', '/api/info_providers'); + + self::assertResponseIsSuccessful(); + + $json = $response->toArray(); + self::assertIsArray($json['hydra:member']); + self::assertNotEmpty($json['hydra:member']); + + $keys = array_column($json['hydra:member'], 'key'); + self::assertContains('test', $keys); + } + + public function testListInfoProvidersRequiresAuthentication(): void + { + self::createClient()->request('GET', '/api/info_providers'); + self::assertResponseStatusCodeSame(401); + } + + public function testListInfoProvidersRequiresPermission(): void + { + self::createAuthenticatedClient(APITokenFixtures::TOKEN_READONLY)->request('GET', '/api/info_providers'); + self::assertResponseStatusCodeSame(403); + } + + public function testSearchInfoProviders(): void + { + $response = self::createAuthenticatedClient()->request('POST', '/api/info_providers/search', [ + 'json' => [ + 'keyword' => 'foo', + 'providers' => ['test'], + ], + ]); + + self::assertResponseIsSuccessful(); + + $json = $response->toArray(); + self::assertIsArray($json['hydra:member']); + self::assertNotEmpty($json['hydra:member']); + self::assertSame('test', $json['hydra:member'][0]['provider_key']); + } + + public function testSearchInfoProvidersWithUnknownProviderReturnsBadRequest(): void + { + self::createAuthenticatedClient()->request('POST', '/api/info_providers/search', [ + 'json' => [ + 'keyword' => 'foo', + 'providers' => ['unknown'], + ], + ]); + + self::assertResponseStatusCodeSame(400); + } + + public function testSearchInfoProvidersRequiresAuthentication(): void + { + self::createClient()->request('POST', '/api/info_providers/search', [ + 'json' => ['keyword' => 'foo', 'providers' => ['test']], + ]); + + self::assertResponseStatusCodeSame(401); + } + + public function testSearchInfoProvidersRequiresPermission(): void + { + self::createAuthenticatedClient(APITokenFixtures::TOKEN_READONLY)->request('POST', '/api/info_providers/search', [ + 'json' => ['keyword' => 'foo', 'providers' => ['test']], + ]); + + self::assertResponseStatusCodeSame(403); + } + + public function testGetInfoProviderPartDetails(): void + { + $response = self::createAuthenticatedClient()->request('POST', '/api/info_providers/details', [ + 'json' => [ + 'provider_key' => 'test', + 'provider_id' => 'element1', + ], + ]); + + self::assertResponseIsSuccessful(); + self::assertJsonContains([ + 'provider_key' => 'test', + 'provider_id' => 'element1', + ]); + } + + public function testGetInfoProviderPartDetailsWithUnknownProviderReturnsBadRequest(): void + { + self::createAuthenticatedClient()->request('POST', '/api/info_providers/details', [ + 'json' => [ + 'provider_key' => 'unknown', + 'provider_id' => 'element1', + ], + ]); + + self::assertResponseStatusCodeSame(400); + } + + public function testGetInfoProviderPartDetailsRequiresAuthentication(): void + { + self::createClient()->request('POST', '/api/info_providers/details', [ + 'json' => ['provider_key' => 'test', 'provider_id' => 'element1'], + ]); + + self::assertResponseStatusCodeSame(401); + } + + public function testGetInfoProviderPartDetailsRequiresPermission(): void + { + self::createAuthenticatedClient(APITokenFixtures::TOKEN_READONLY)->request('POST', '/api/info_providers/details', [ + 'json' => ['provider_key' => 'test', 'provider_id' => 'element1'], + ]); + + self::assertResponseStatusCodeSame(403); + } +}