dolibarr 24.0.0-beta
execute_tool.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2026 Nick Fragoulis
4 * Copyright (C) 2026 Anthony Damhet <a.damhet@progiseize.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
26if (!defined('NOTOKENRENEWAL')) {
27 define('NOTOKENRENEWAL', 1);
28}
29if (!defined('NOREQUIREMENU')) {
30 define('NOREQUIREMENU', 1);
31}
32if (!defined('NOREQUIREHTML')) {
33 define('NOREQUIREHTML', 1);
34}
35if (!defined('NOREQUIREAJAX')) {
36 define('NOREQUIREAJAX', 1);
37}
38if (!defined('NOCSRFCHECK')) { // TODO Enable the CSRF check
39 define('NOCSRFCHECK', 1);
40}
41
42require '../../main.inc.php';
43require_once DOL_DOCUMENT_ROOT . '/ai/class/mcp.class.php';
44
45// Security check
46if (!isModEnabled('ai') || !getDolGlobalString('AI_ASSISTANT_ENABLED')) {
47 accessforbidden('Module or feature not allowed');
48}
49
50global $db, $user, $conf;
51
52// Per-user gate: same right as the assistant page and parse_intent.php
53if (!$user->hasRight('ai', 'assistant', 'use')) {
55}
56
57top_httphead('application/json');
58
59
60try {
61 $raw = file_get_contents('php://input');
62 $input = json_decode($raw, true);
63
64 if (!$input || empty($input['tool'])) {
65 throw new Exception("Invalid Request: No tool specified.");
66 }
67
68 // Initialize Handler with the private assistant context so that the correct
69 // allow-list (AI_ASSISTANT_ALLOWED_TOOLS) is enforced on both schema and execution.
70 $mcp = new McpHandler($db, $user, $conf, McpHandler::CTX_ASSISTANT);
71
72 $result = $mcp->executeTool($input['tool'], $input['arguments'] ?? []);
73
74 echo json_encode($result);
75} catch (Throwable $e) {
76 // Set HTTP response code to error (400 Bad Request)
77 http_response_code(400);
78 echo json_encode(["error" => $e->getMessage()]);
79}
Class to handle MCP (Model Context Protocol).
Definition mcp.class.php:39
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
if(!defined( 'NOREQUIREMENU')) if(!empty(GETPOST('seteventmessages', 'alpha'))) if(!function_exists("llxHeader")) top_httphead($contenttype='text/html', $forcenocache=0)
Show HTTP header.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.