dolibarr 25.0.0-alpha
list_tools.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Nick Fragoulis
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
28if (!defined('NOTOKENRENEWAL')) {
29 define('NOTOKENRENEWAL', 1);
30}
31if (!defined('NOREQUIREMENU')) {
32 define('NOREQUIREMENU', 1);
33}
34if (!defined('NOREQUIREHTML')) {
35 define('NOREQUIREHTML', 1);
36}
37if (!defined('NOREQUIREAJAX')) {
38 define('NOREQUIREAJAX', 1);
39}
40// The token is checked explicitly below by aiCheckCsrfToken(), like the other endpoints of the chat.
41if (!defined('NOCSRFCHECK')) {
42 define('NOCSRFCHECK', 1);
43}
44
45require '../../main.inc.php';
46require_once DOL_DOCUMENT_ROOT.'/ai/class/mcp.class.php';
47require_once DOL_DOCUMENT_ROOT.'/ai/tools/api_bridge.class.php';
48require_once DOL_DOCUMENT_ROOT.'/ai/lib/ai.lib.php';
49
57// Same gates as execute_tool.php, which runs what the palette offers.
58if (!isModEnabled('ai') || !getDolGlobalString('AI_ASSISTANT_ENABLED')) {
59 accessforbidden('Module or feature not allowed');
60}
61if (!$user->hasRight('ai', 'assistant', 'use')) {
63}
64aiCheckCsrfToken('ai/assistant/list_tools.php');
65
66top_httphead('application/json');
67
68// The "/reload" command of the chat: purge the cache of the AI tool
69// definitions, so the next request rebuilds it from the current state.
70if (GETPOST('action', 'aZ09') == 'purge') {
71 try {
72 $bridge = new ToolApiBridge($db, $user, $conf);
73 $nbpurged = $bridge->purgeDefsCache();
74 dol_syslog('AI assistant /reload: '.$nbpurged.' tool definitions cache file(s) purged', LOG_INFO, 0, '_ai');
75 print json_encode(array('status' => 'ok', 'purged' => $nbpurged));
76 } catch (Throwable $e) {
77 dol_syslog('AI assistant /reload error: '.$e->getMessage().' in '.$e->getFile().' on line '.$e->getLine(), LOG_ERR, 0, '_ai');
78 print json_encode(array('error' => $e->getMessage()));
79 }
80 $db->close();
81 exit;
82}
83
84$mcp = new McpHandler($db, $user, $conf, McpHandler::CTX_ASSISTANT);
85$mcp->loadTools();
86
87$out = array();
88foreach ($mcp->getToolsSchemaForLLM() as $def) {
89 $name = (string) ($def['name'] ?? '');
90 if ($name === '') {
91 continue;
92 }
93 $schema = (isset($def['inputSchema']) && is_array($def['inputSchema'])) ? $def['inputSchema'] : array();
94 $props = array();
95 foreach (($schema['properties'] ?? array()) as $pname => $pdef) {
96 // Only what a form needs: type, help text, choices, and for a list of
97 // records (invoice lines) the fields of one record.
98 $props[$pname] = array(
99 'type' => (string) ($pdef['type'] ?? 'string'),
100 'description' => dol_trunc((string) ($pdef['description'] ?? ''), 200),
101 'enum' => array_values((array) ($pdef['enum'] ?? array())),
102 'items' => (isset($pdef['items']['properties']) && is_array($pdef['items']['properties'])) ? array_keys($pdef['items']['properties']) : array()
103 );
104 }
105 // A plain name for the person choosing, the API name shown beside it.
106 $tool = $mcp->toolsByName[$name] ?? null;
107 $route = ($tool instanceof ToolApiBridge) ? $tool->toolRoute($name) : null;
108 $out[] = array(
109 'name' => $name,
110 'title' => aiPaletteToolTitle($langs, $name, $route),
111 'description' => dol_trunc(aiPaletteToolDescription((string) ($def['description'] ?? '')), 200),
112 'properties' => $props,
113 'required' => array_values((array) ($schema['required'] ?? array()))
114 );
115}
116
117usort($out,
123 function ($a, $b) {
124 return strcmp($a['title'], $b['title']) ?: strcmp($a['name'], $b['name']);
125 }
126);
127
128print json_encode(array('tools' => $out));
129
130$db->close();
aiPaletteToolTitle($langs, $name, $route)
Plain name of a tool for the palette of the chat: "Get invoice" rather than api_invoices_get.
Definition ai.lib.php:2119
aiPaletteToolDescription($description)
Description of a tool for the palette: without the sentence every generated bridge tool repeats,...
Definition ai.lib.php:2234
aiCheckCsrfToken($context='')
Check the anti-CSRF token of a request sent to one of the AI Assistant endpoints.
Definition ai.lib.php:1260
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to handle MCP (Model Context Protocol).
Definition mcp.class.php:40
Class ToolApiBridge.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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.