dolibarr 24.0.0-beta
server_mcp.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2022 Alice Adminson <aadminson@example.com>
4 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
5 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Coryright (C) 2024 Alexandre Spangaro <alexandre@inovea-conseil.com>
7 * Copyright (C) 2026 Nick Fragoulis
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY, without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
29require '../../main.inc.php';
38require_once DOL_DOCUMENT_ROOT . "/core/lib/admin.lib.php";
39require_once DOL_DOCUMENT_ROOT . "/core/class/html.form.class.php";
40require_once DOL_DOCUMENT_ROOT . "/core/class/doleditor.class.php";
41require_once DOL_DOCUMENT_ROOT . "/ai/lib/ai.lib.php";
42require_once DOL_DOCUMENT_ROOT . "/core/lib/functions2.lib.php";
43require_once DOL_DOCUMENT_ROOT . "/core/lib/security.lib.php";
44
45
46$langs->loadLangs(array("admin", "website", "other"));
47
48// Access control
49if (!$user->admin) {
51}
52if (!isModEnabled('ai')) {
53 accessforbidden('Module AI not activated.');
54}
55
56// Parameters
57$action = GETPOST('action', 'aZ09');
58$backtopage = GETPOST('backtopage', 'alpha');
59
60
65// Main Settings
66if ($action == 'update') {
67 $error = 0;
68
69 if (GETPOSTISSET('AI_ASK_FOR_CONFIRMATION')) {
70 $res = dolibarr_set_const($db, "AI_ASK_FOR_CONFIRMATION", GETPOSTINT("AI_ASK_FOR_CONFIRMATION"), 'int', 0, '', $conf->entity);
71 if ($res <= 0) $error++;
72 }
73
74 if (GETPOSTISSET('AI_LOG_RETENTION')) {
75 $res = dolibarr_set_const($db, "AI_LOG_RETENTION", GETPOST("AI_LOG_RETENTION"), 'int', 0, '', $conf->entity);
76 if ($res <= 0) {
77 $error++;
78 }
79 }
80
81 if (GETPOSTISSET('AI_DEFAULT_INPUT_MODE')) {
82 $res = dolibarr_set_const($db, "AI_DEFAULT_INPUT_MODE", GETPOST("AI_DEFAULT_INPUT_MODE"), 'chaine', 0, '', $conf->entity);
83 if ($res <= 0) {
84 $error++;
85 }
86 }
87
88 if (GETPOSTISSET('AI_INTENT_PROMPT')) {
89 $res = dolibarr_set_const($db, "AI_INTENT_PROMPT", GETPOST("AI_INTENT_PROMPT"), 'chaine', 0, '', $conf->entity);
90 if ($res <= 0) {
91 $error++;
92 }
93 }
94
95 if ($error) {
96 setEventMessages($langs->trans("ErrorSavingSettings"), null, 'errors');
97 } else {
98 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
99 }
100
101 header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home");
102 exit;
103}
104
105// Test connection
106if ($action == 'test_provider') {
107 $service = GETPOST('service_key', 'aZ09');
108 if ($service) {
109 $credential = getDolGlobalString('AI_API_' . strtoupper($service) . '_KEY');
110 $url = getDolGlobalString('AI_API_' . strtoupper($service) . '_URL');
111
112 // Decrypt if needed
113 if (preg_match('/^crypted:/', $credential)) {
114 $credential = dol_decode(substr($credential, 8));
115 } elseif (preg_match('/^dolcrypt:/', $credential)) {
116 $credential = dolDecrypt($credential, '');
117 }
118
119 // Only proceed if the key is valid (decrypted or not encrypted)
120 if ($credential !== null) {
121 $res = testAIConnection($service, $credential, $url);
122
123 if ($res['success']) {
124 setEventMessages($langs->trans("ConnectionSuccessful") . $res['message'], null, 'mesgs');
125 } else {
126 setEventMessages($langs->trans("ConnectionFailed") . $res['message'], null, 'errors');
127 }
128 }
129 }
130}
131
132// External Access Settings
133if ($action == 'update_external') {
134 $error = 0;
135
136 $user_id = GETPOSTINT("AI_MCP_USER_ID");
137 if (GETPOSTISSET('AI_MCP_USER_ID')) {
138 $res = dolibarr_set_const($db, "AI_MCP_USER_ID", $user_id, 'int', 0, '', $conf->entity);
139 if ($res <= 0) {
140 $error++;
141 }
142 }
143
144 if ($error) {
145 setEventMessages($langs->trans("ErrorSavingSettings"), null, 'errors');
146 } else {
147 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
148 }
149
150 header("Location: ".$_SERVER["PHP_SELF"]."?mainmenu=home");
151 exit;
152}
153
154// Generate New API Key
155if ($action == 'generate_key') {
156 $newKey = dolGetRandomBytes(64);
157
158 if (empty($newKey)) {
159 setEventMessages($langs->trans("KeyGenerationFailed"), null, 'errors');
160 } else {
161 if (dolibarr_set_const($db, 'AI_MCP_API_KEY', $newKey, 'chaine', 0, '', $conf->entity) > 0) {
162 setEventMessages($langs->trans("KeyGenerationSuccessfull"), null, 'mesgs');
163 } else {
164 setEventMessages($langs->trans("KeySaveFailed"), null, 'errors');
165 }
166 }
167}
168
169
170/*
171 * VIEW
172 */
173
175$title = "AiSetup";
176
177llxHeader('', $langs->trans($title), $help_url, '', 0, 0, '', '', '', 'mod-ai page-admin');
178
179$linkback = '<a href="'.($backtopage ? $backtopage : DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1').'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
180
181print load_fiche_titre($langs->trans($title), $linkback, 'title_setup');
182
183
184$head = aiAdminPrepareHead();
185print dol_get_fiche_head($head, 'servermcp', "MCP Server", -1, "ai");
186
187print '<span class="opacitymedium">' . $langs->trans("ConfigHelp") . '</span><br><br>';
188
189$form = new Form($db);
190
191// Load Current Values
192$apiKey = getDolGlobalString('AI_MCP_API_KEY');
193
194// Default Prompt Logic
195$defaultPromptText = " ROLE: Dolibarr ERP AI.
196GOAL: Map user intent to specific JSON commands.
197
198CONSTRAINTS:
1991. OUTPUT: Single valid JSON object ONLY. No Markdown. No text.
200 Format: {\"tool\": \"tool_name\", \"arguments\": {\"argument_name\": \"argument_value\", ...}}
2012. TOOLS: Use ONLY provided tools. If no tool or thirdparty fits, you must first use 'respond_to_user' to inform user.
2023. ARGS: strict adherence to schema. Do not invent parameters.
2034. MISSING INFO: If a required argument (like an ID) is missing, use 'ask_for_clarification'.
2045. SAFETY: For DELETE/UPDATE actions, you MUST use 'ask_for_confirmation'.";
205
206$currentPrompt = getDolGlobalString('AI_INTENT_PROMPT', $defaultPromptText);
207
208// Settings
209print '<div class="neutral">';
210//print '<table class="noborder centpercent">';
211
212// Enable/Disable
213//print '<tr class="oddeven">';
214//print '<td class="titlefield" width="30%">'
215print $form->textwithpicto($langs->trans('EnableMCPServer'), $langs->trans('DisableMCPAI'));
216//print '</td>';
217//print '<td>';
218print ' &nbsp; ';
219print ajax_constantonoff('AI_MCP_ENABLED', array(), null, 0, 0, 1);
220//print ' <span class="opacitymedium">' . $langs->trans('DisableMCPAI') . '</span>';
221//print '</td>';
222//print '</tr>';
223
224//print '</table>';
225print '</div>';
226
227print '<br>';
228print '<br>';
229
230if (getDolGlobalString('AI_MCP_ENABLED')) {
231 /*
232 print load_fiche_titre($langs->trans("PrivateModeTitle"), '', 'fas fa-lock');
233
234 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
235 print '<input type="hidden" name="token" value="'.newToken().'">';
236 print '<input type="hidden" name="action" value="update">';
237
238 print '<div class="div-table-responsive-no-min">';
239 print '<table class="noborder centpercent">';
240
241 // Input Mode
242 print '<tr class="oddeven">';
243 print '<td>';
244 print $form->textwithpicto('Default Interface', $langs->trans("InputMethodHelp"));
245 print '</td>';
246 print '<td>';
247 $input_modes = [
248 'text' => $langs->trans('OptionTextOnly'),
249 'native' => $langs->trans('OptionCloudFast') . ' - ' . $langs->trans('OptionCloudFasthelp'),
250 'whisper' => $langs->trans('OptionWhisperLocal') . ' - ' . $langs->trans('OptionWhisperLocalhelp')
251 ];
252 print $form->selectarray('AI_DEFAULT_INPUT_MODE', $input_modes, getDolGlobalString('AI_DEFAULT_INPUT_MODE'), 0, 0, 0);
253 print '</td>';
254 print '</tr>';
255
256 // Enhanced Privacy control
257 print '<tr class="oddeven">';
258 print '<td>' . $langs->trans('ObfuscatePIIData') . '</td>';
259 print '<td>';
260 print ajax_constantonoff('AI_PRIVACY_REDACTION');
261 print ' &nbsp; <span class="opacitymedium">' . $langs->trans("RedactionHelp") . '</span>';
262 print '</td>';
263 print '</tr>';
264
265 // Confirmation Level
266 print '<tr class="oddeven">';
267 print '<td>' . $form->textwithpicto($langs->trans("AskConfirmation"), $langs->trans("AskConfirmationHelp")).'</td>';
268 print '<td>';
269 $confirmation_options = [
270 '0' => $langs->trans("ConfirmNever"),
271 '1' => $langs->trans("ConfirmWriteOnly"),
272 '2' => $langs->trans("ConfirmAlways")
273 ];
274 print $form->selectarray('AI_ASK_FOR_CONFIRMATION', $confirmation_options, getDolGlobalInt('AI_ASK_FOR_CONFIRMATION', 1), 0, 0, 0);
275 print '</td>';
276 print '</tr>';
277
278 // Logging
279 print '<tr class="oddeven">';
280 print '<td>' . $langs->trans('EnableLogging') . '</td>';
281 print '<td>';
282 print ajax_constantonoff('AI_LOG_REQUESTS');
283 print ' &nbsp; <a href="' . DOL_URL_ROOT . '/ai/admin/log_viewer.php" target="_blank" style="padding-top: 4px; padding-bottom: 4px;">'.$langs->trans("ViewLogs").'</a>';
284 print '</td>';
285 print '</tr>';
286
287 print '<tr class="oddeven">';
288 print '<td>' . $langs->trans("LogRetention") . '</td>';
289 print '<td><input class="width50" type="number" name="AI_LOG_RETENTION" value="' . getDolGlobalInt('AI_LOG_RETENTION', 30) . '"> (0 = Forever)</td>';
290 print '</tr>';
291
292 // System Prompt
293 print '<tr class="oddeven">';
294 print '<td colspan="2">';
295 print '<strong>' . $langs->trans("SystemPrompt") . '</strong><br>';
296 print '<span class="opacitymedium small">' . $langs->trans("SystemPromptHelp") . '</span><br>';
297 $doleditor = new DolEditor('AI_INTENT_PROMPT', $currentPrompt, '', 250, 'dolibarr_notes', 'In', false, false, true, ROWS_8, '90%');
298 $doleditor->Create();
299 print '</td>';
300 print '</tr>';
301
302 print '</table>';
303 print '</div>';
304
305 print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Save").'"></div>';
306 print '</form>';
307
308 print '<br>';
309 print '<br>';
310
311 // AI Provider Config and Connection testing
312 $services = getListOfAIServices();
313 $currentService = getDolGlobalString('AI_API_SERVICE');
314
315 print load_fiche_titre($langs->trans("AIProviderConfigTitle"), '', 'fa fa-plug');
316
317 if ((string) $currentService == '-1') {
318 print '<div class="warning">'.$langs->trans("NoAIProviderSelected").' <a href="'.dol_buildpath('/ai/admin/setup.php', 1).'">'.$langs->trans("ConfigureHere").'</a></div>';
319 } else {
320 print '<div class="div-table-responsive-no-min">';
321 print '<table class="noborder centpercent">';
322
323 print '<tr class="oddeven"><td class="titlefield">'.$langs->trans("AIProvider").'</td><td>'.$services[$currentService]['label'].'</td></tr>';
324
325 $prefix = 'AI_API_'.strtoupper($currentService);
326 $modelVal = getDolGlobalString($prefix.'_MODEL_TEXT', $services[$currentService]['textgeneration']['default']);
327
328 print '<tr class="oddeven"><td>'.$langs->trans("AI_API_MODEL").'</td><td>'.$modelVal.'</td></tr>';
329 print '</table></div>';
330
331 print '<div class="center">';
332
333 if ($currentService && $currentService !== '-1') {
334 print ' <a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=test_provider&token='.newToken().'&service_key='.$currentService.'" class="button">Test Connection</a>';
335 }
336
337 print '</div>';
338 }
339
340 print '<br><br>';
341 */
342
343 // External Access Configuration
344 print load_fiche_titre($langs->trans("AiMcpExternalAccess"), '', 'fas fa-lock-open');
345
346 print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
347 print '<input type="hidden" name="token" value="'.newToken().'">';
348 print '<input type="hidden" name="action" value="update_external">';
349
350 print '<div class="div-table-responsive-no-min">';
351 print '<table class="noborder centpercent">';
352
353 // Service User
354 print '<tr class="oddeven">';
355 print '<td>Service User <span class="fa fa-info-circle" title="' . $langs->trans("UserPermissionsTooltip") . '"></span></td>';
356 print '<td>';
357 print '<div style="display: flex; align-items: center;">';
358 print $form->select_dolusers(getDolGlobalInt('AI_MCP_USER_ID'), 'AI_MCP_USER_ID', 1);
359 print ' <input type="submit" class="button" value="'.$langs->trans("Save").'" style="margin-left: 20px;">';
360 print '</div>';
361 print '<span class="opacitymedium small">' . $langs->trans("DedicatedUserRecommendation") . '</span>';
362 print '</td>';
363 print '</tr>';
364
365 print '<tr class="oddeven">';
366 print '<td width="30%">API Key</td>';
367 print '<td>';
368 if ($apiKey) {
369 print '<input type="text" id="apikey" value="'.$apiKey.'" readonly style="width:400px; padding:6px; background:#f4f4f4; border:1px solid #ccc; color:#555;">';
370 print ' <a class="button smallpaddingimp" href="'.$_SERVER["PHP_SELF"].'?action=generate_key&token='.newToken().'">Generate New Key</a>';
371 } else {
372 print '<span class="opacitymedium">' . $langs->trans("NoKeyWarning") . '</span>';
373 print ' <a class="button smallpaddingimp" href="' . $_SERVER["PHP_SELF"] . '?action=generate_key&token=' . newToken() . '">' . $langs->trans("GenerateKey") . '</a>';
374 }
375 print '</td>';
376 print '</tr>';
377
378 $endpoint = dol_buildpath('/ai/server/mcp_server.php', 3);
379
380 print '<tr class="oddeven">';
381 print '<td>Endpoint URL</td>';
382 print '<td>';
383 print '<input type="text" id="endpoint" value="'.$endpoint.'" readonly style="width:600px; border:none; background:transparent;">';
384 print '</td>';
385 print '</tr>';
386
387 print '</table>';
388 print '</div>';
389
390 print '</form>';
391
392 // Configuration Examples
393 print '<br>';
394 print '<div style="background:#fcfcfc; border:1px solid #eee; padding:15px; border-radius:5px;">';
395 print '<strong>' . $langs->trans("ClaudeDesktopConfig") . '</strong><br>';
396 print '<pre style="background:#333; color:#fff; padding:10px; border-radius:4px; overflow:auto; margin-top:10px;">';
397 echo htmlspecialchars('
398 {
399 "mcpServers": {
400 "dolibarr": {
401 "command": "node",
402 "args": ["/path/to/mcp-bridge.js"],
403 "env": {
404 "DOLIBARR_URL": "'.$endpoint.'",
405 "DOLIBARR_API_KEY": "'.($apiKey ? $apiKey : "YOUR_KEY_HERE").'"
406 }
407 }
408 }
409 }');
410 print '</pre>';
411 print '</div>';
412}
413
414print dol_get_fiche_end();
415
416llxFooter();
417$db->close();
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
testAIConnection(string $service, string $key, string $url)
Tests the connection to an AI service using its API key and URL by sending message "Hello".
Definition ai.lib.php:212
aiAdminPrepareHead()
Prepare admin pages header.
Definition ai.lib.php:407
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
Class to manage generation of HTML components Only common components must be here.
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dolGetRandomBytes($length)
Return a string of random bytes (hexa string) with length = $length for cryptographic purposes.
dol_decode($chain, $key='1')
Decode a base 64 encoded + specific delta change.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.
dolDecrypt($chain, $key='', $patterntotest='')
Decode a string with a symmetric encryption.
if( $action=='update') if($action=='test_provider') if( $action=='update_external') if($action=='generate_key') $help_url
ACTIONS.