dolibarr/htdocs/ai
Pichinov-Jose 1aeec05d80
Fix: MCP server propagates authenticated service user to tools (#38294)
The MCP server (htdocs/ai/server/mcp_server.php) loads a service user
from AI_MCP_USER_ID but does not expose it as the PHP global $user.
Several tool classes call 'global $user' to identify the caller --
they therefore see no authenticated user in HTTP MCP context (no PHP
web session exists) and return 'User not authenticated' even though
authentication via X-API-Key/Bearer has already succeeded.

This breaks roughly 40% of the MCP tools shipped by the module when
called from external clients (Claude Desktop Custom Connectors,
Claude Code CLI, MCP Inspector, custom scripts).

Two complementary fixes applied:

1. mcp_server.php: after successfully loading the service user,
   assign it to $GLOBALS['user'] and the local $user variable. This
   acts as a safety net for any tool (current or third-party) that
   relies on the legacy 'global $user' pattern.

2. Three tool classes harmonize their constructor signatures with
   the McpHandler contract -- new $className($db, $user, $conf):
   - ToolCrudObjects (htdocs/ai/tools/crud_objects.class.php)
   - ToolInvoices (htdocs/ai/tools/invoices.class.php)
   - ToolReports (htdocs/ai/tools/reports.class.php)

   These now accept $user via constructor (matching the pattern
   already used in ToolThirdParty, ToolCategories, ToolProducts)
   and store it as $this->user. Methods that previously called
   'global $user' (execute, validateInvoice, payInvoice) now use
   the injected $this->user, with a fallback to the global for
   web-session contexts.

Tested with:
- create_other_document (proposal, supplier_invoice): works
- search_invoice, validate_invoice, pay_invoice: works
- get_sales_report, get_purchase_report: works
- AI Assistant web UI: unchanged, no regression
2026-07-13 06:57:42 +02:00
..
admin New: granular right for AI Assistant usage (#38312) 2026-06-11 09:33:14 +02:00
ajax FIX: Translate French code comments to English in accountancy, admin and ai modules 2026-06-28 09:51:09 +02:00
assistant UI|UX: AI design (#38789) 2026-06-14 01:05:01 +02:00
class Qual: Avoid variable scope clash (param $conf vs. global $conf) (#38969) 2026-06-23 20:16:24 +02:00
css Debug v24 2026-06-19 14:57:16 +02:00
js Fix: Using the Microphone (#38828) 2026-06-15 14:56:35 +02:00
lib Debug v24 2026-06-19 14:57:16 +02:00
server Fix: MCP server propagates authenticated service user to tools (#38294) 2026-07-13 06:57:42 +02:00
tools Fix: MCP server propagates authenticated service user to tools (#38294) 2026-07-13 06:57:42 +02:00
README.md

AI FOR DOLIBARR ERP CRM

Features

Provides AI (Artificial Intelligence) features in different part of the application. Need external AI API.

Licenses

Main code

GPLv3 or (at your option) any later version. See file COPYING for more information.

Documentation

All texts and readmes are licensed under GFDL.