92 lines
2.6 KiB
PHP
92 lines
2.6 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/service.php';
|
|
|
|
function erp_lager_mcp_token(array $env): string
|
|
{
|
|
$token = env_value('MARKET_MCP_TOKEN', $env);
|
|
if ($token === '') {
|
|
throw new RuntimeException('MARKET_MCP_TOKEN ist nicht konfiguriert.');
|
|
}
|
|
|
|
return $token;
|
|
}
|
|
|
|
function erp_lager_mcp_authorize(array $env): string
|
|
{
|
|
$header = $_SERVER['HTTP_AUTHORIZATION'] ?? '';
|
|
if (!preg_match('/^Bearer\s+(.+)$/i', $header, $matches)) {
|
|
throw new RuntimeException('MCP Authorization Bearer Token fehlt.');
|
|
}
|
|
|
|
$token = trim($matches[1]);
|
|
if (!hash_equals(erp_lager_mcp_token($env), $token)) {
|
|
throw new RuntimeException('MCP Authorization Bearer Token ist ungültig.');
|
|
}
|
|
|
|
return 'hermes';
|
|
}
|
|
|
|
function erp_lager_mcp_tools(): array
|
|
{
|
|
return [
|
|
[
|
|
'name' => 'erp_lager.list_products',
|
|
'description' => 'Aktive ERP-Lagerprodukte zur Ermittlung der Produkt-IDs lesen',
|
|
'inputSchema' => ['type' => 'object', 'properties' => (object) []],
|
|
],
|
|
[
|
|
'name' => 'erp_lager.get_product',
|
|
'description' => 'Produkt, aktuelle Lagermenge, Chargen und Ausverkaufsprognose lesen',
|
|
'inputSchema' => [
|
|
'type' => 'object',
|
|
'required' => ['product_id'],
|
|
'properties' => [
|
|
'product_id' => ['type' => 'integer'],
|
|
],
|
|
],
|
|
],
|
|
];
|
|
}
|
|
|
|
function erp_lager_mcp_list_products(PDO $pdo): array
|
|
{
|
|
return array_map(
|
|
static fn (array $product): array => [
|
|
'id' => $product['id'],
|
|
'name' => $product['name'],
|
|
'sku' => $product['sku'],
|
|
'uom' => $product['uom'],
|
|
],
|
|
get_otc_order_form_products($pdo)
|
|
);
|
|
}
|
|
|
|
function erp_lager_mcp_get_product(PDO $pdo, int $productId): array
|
|
{
|
|
if ($productId < 1) {
|
|
throw new InvalidArgumentException('product_id muss eine positive Ganzzahl sein.');
|
|
}
|
|
|
|
foreach (get_lager_product_overview($pdo) as $product) {
|
|
if ((int) $product['id'] === $productId) {
|
|
return $product;
|
|
}
|
|
}
|
|
|
|
throw new InvalidArgumentException('Aktives Lagerprodukt nicht gefunden.');
|
|
}
|
|
|
|
function erp_lager_mcp_call(PDO $pdo, string $tool, array $arguments): array
|
|
{
|
|
return match ($tool) {
|
|
'erp_lager.list_products' => erp_lager_mcp_list_products($pdo),
|
|
'erp_lager.get_product' => erp_lager_mcp_get_product(
|
|
$pdo,
|
|
(int) ($arguments['product_id'] ?? 0)
|
|
),
|
|
default => throw new InvalidArgumentException('Unbekanntes Lager-MCP-Tool: ' . $tool),
|
|
};
|
|
}
|