Expose InfoProvider capabilities via REST API
Some checks failed
Build assets artifact / Build assets artifact (push) Has been cancelled
Docker Image Build / build (linux/amd64, amd64, ubuntu-latest) (push) Has been cancelled
Docker Image Build / build (linux/arm/v7, armv7, ubuntu-24.04-arm) (push) Has been cancelled
Docker Image Build / build (linux/arm64, arm64, ubuntu-24.04-arm) (push) Has been cancelled
Docker Image Build (FrankenPHP) / build (linux/amd64, amd64, ubuntu-latest) (push) Has been cancelled
Docker Image Build (FrankenPHP) / build (linux/arm/v7, armv7, ubuntu-24.04-arm) (push) Has been cancelled
Docker Image Build (FrankenPHP) / build (linux/arm64, arm64, ubuntu-24.04-arm) (push) Has been cancelled
Static analysis / Static analysis (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, mysql) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, mysql) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, mysql) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, mysql) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, postgres) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, postgres) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, postgres) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, postgres) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.2, sqlite) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.3, sqlite) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.4, sqlite) (push) Has been cancelled
PHPUnit Tests / PHPUnit and coverage Test (PHP 8.5, sqlite) (push) Has been cancelled
Docker Image Build / merge (push) Has been cancelled
Docker Image Build (FrankenPHP) / merge (push) Has been cancelled

This commit is contained in:
Jan Böhmer 2026-07-21 00:07:52 +02:00
parent 7e53c7ae9a
commit 77724a522b
5 changed files with 205 additions and 4 deletions

View file

@ -23,7 +23,9 @@ declare(strict_types=1);
namespace App\Services\InfoProviderSystem\DTOs; namespace App\Services\InfoProviderSystem\DTOs;
use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\McpToolCollection; use ApiPlatform\Metadata\McpToolCollection;
use ApiPlatform\OpenApi\Model\Operation;
use App\Mcp\DTO\ListInfoProvidersInput; use App\Mcp\DTO\ListInfoProvidersInput;
use App\State\Mcp\ListInfoProvidersProcessor; use App\State\Mcp\ListInfoProvidersProcessor;
@ -32,8 +34,16 @@ use App\State\Mcp\ListInfoProvidersProcessor;
* searched via search_info_providers / get_info_provider_part_details). * searched via search_info_providers / get_info_provider_part_details).
*/ */
#[ApiResource( #[ApiResource(
uriTemplate: '/info_providers',
description: 'An info provider which can be used to search for parts and retrieve part details.', 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: [ mcp: [
'list_info_providers' => new McpToolCollection( 'list_info_providers' => new McpToolCollection(
title: 'List available info providers', title: 'List available info providers',

View file

@ -25,6 +25,8 @@ namespace App\Services\InfoProviderSystem\DTOs;
use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\McpTool; use ApiPlatform\Metadata\McpTool;
use ApiPlatform\Metadata\Post;
use ApiPlatform\OpenApi\Model\Operation;
use App\Entity\Parts\ManufacturingStatus; use App\Entity\Parts\ManufacturingStatus;
use App\Mcp\DTO\InfoProviderPartDetailsInput; use App\Mcp\DTO\InfoProviderPartDetailsInput;
use App\State\Mcp\GetInfoProviderPartDetailsProcessor; use App\State\Mcp\GetInfoProviderPartDetailsProcessor;
@ -34,7 +36,16 @@ use App\State\Mcp\GetInfoProviderPartDetailsProcessor;
*/ */
#[ApiResource( #[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.', 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: [ mcp: [
'get_info_provider_part_details' => new McpTool( 'get_info_provider_part_details' => new McpTool(
title: 'Get part details from an info provider', title: 'Get part details from an info provider',

View file

@ -25,6 +25,8 @@ namespace App\Services\InfoProviderSystem\DTOs;
use ApiPlatform\Metadata\ApiResource; use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\McpToolCollection; use ApiPlatform\Metadata\McpToolCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\OpenApi\Model\Operation;
use App\Entity\Parts\ManufacturingStatus; use App\Entity\Parts\ManufacturingStatus;
use App\Mcp\DTO\InfoProviderSearchInput; use App\Mcp\DTO\InfoProviderSearchInput;
use App\State\Mcp\SearchInfoProvidersProcessor; use App\State\Mcp\SearchInfoProvidersProcessor;
@ -35,7 +37,16 @@ use App\State\Mcp\SearchInfoProvidersProcessor;
*/ */
#[ApiResource( #[ApiResource(
description: 'A search result for a part from an external info provider (e.g. a distributor or manufacturer catalog).', 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: [ mcp: [
'search_info_providers' => new McpToolCollection( 'search_info_providers' => new McpToolCollection(
title: 'Search external info providers', title: 'Search external info providers',

View file

@ -24,20 +24,41 @@ namespace App\State\Mcp;
use ApiPlatform\Metadata\Operation; use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface; use ApiPlatform\State\ProcessorInterface;
use ApiPlatform\State\ProviderInterface;
use App\Services\InfoProviderSystem\DTOs\InfoProviderDTO; use App\Services\InfoProviderSystem\DTOs\InfoProviderDTO;
use App\Services\InfoProviderSystem\ProviderRegistry; 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( public function __construct(
private readonly ProviderRegistry $providerRegistry, private readonly ProviderRegistry $providerRegistry,
) { ) {
} }
/**
* @return InfoProviderDTO[]
*/
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array
{
return $this->listActiveProviders();
}
/** /**
* @return InfoProviderDTO[] * @return InfoProviderDTO[]
*/ */
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): array public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): array
{
return $this->listActiveProviders();
}
/**
* @return InfoProviderDTO[]
*/
private function listActiveProviders(): array
{ {
$result = []; $result = [];

View file

@ -0,0 +1,148 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2026 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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);
}
}