mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-06-06 18:51:35 +00:00
Allow to enable/disable MCP endpoint
This commit is contained in:
parent
a9d75554d7
commit
bcf439ba22
5 changed files with 145 additions and 1 deletions
60
src/EventSubscriber/McpAccessSubscriber.php
Normal file
60
src/EventSubscriber/McpAccessSubscriber.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?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\EventSubscriber;
|
||||
|
||||
use App\Settings\AISettings\McpSettings;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
|
||||
readonly class McpAccessSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
public function __construct(private McpSettings $mcpSettings)
|
||||
{
|
||||
}
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
KernelEvents::REQUEST => ['onKernelRequest', 10],
|
||||
];
|
||||
}
|
||||
|
||||
public function onKernelRequest(RequestEvent $event): void
|
||||
{
|
||||
if (!$event->isMainRequest()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$path = $event->getRequest()->getPathInfo();
|
||||
|
||||
if (!str_starts_with($path, '/mcp')) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->mcpSettings->enabled) {
|
||||
throw new ServiceUnavailableHttpException(null, 'The MCP endpoint is disabled. Enable it in the system settings.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue