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
This commit is contained in:
parent
217e32c6c1
commit
1aeec05d80
4 changed files with 80 additions and 11 deletions
|
|
@ -2,6 +2,7 @@
|
|||
/* Copyright (C) 2026 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
* Copyright (C) 2026 Nick Fragoulis
|
||||
* Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
|
||||
* Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
|
@ -131,6 +132,14 @@ if ($userId > 0) {
|
|||
|
||||
if ($result > 0) {
|
||||
$serviceUser->loadRights();
|
||||
// Promote the service user to the global $user so MCP tools that
|
||||
// legitimately rely on the `global $user` pattern (Dolibarr core
|
||||
// convention) see an authenticated user. Without this, there is
|
||||
// no PHP web session in HTTP MCP context and any tool reading
|
||||
// `global $user` would treat the request as unauthenticated even
|
||||
// though authentication via X-API-Key/Bearer succeeded above.
|
||||
global $user;
|
||||
$user = $serviceUser;
|
||||
} else {
|
||||
http_response_code(500);
|
||||
echo json_encode([
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
/* Copyright (C) 2026 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
* Copyright (C) 2026 Nick Fragoulis
|
||||
* Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
|
||||
* Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
|
@ -37,11 +38,22 @@ class ToolCrudObjects extends McpTool
|
|||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Aligned with McpHandler's instantiation contract: new $className($db, $user, $conf).
|
||||
* Accepting $user via DI allows this tool to work in HTTP MCP context where no PHP
|
||||
* web session exists. Some sibling tool classes (ToolThirdParty, ToolCategories,
|
||||
* ToolProducts) already use this signature; this aligns ToolCrudObjects with them.
|
||||
*
|
||||
* @param DoliDB $db Database handler
|
||||
* @param User|null $user Service user provided by McpHandler (from AI_MCP_USER_ID)
|
||||
* @param Conf|null $conf Dolibarr config (optional)
|
||||
*/
|
||||
public function __construct(DoliDB $db)
|
||||
public function __construct(DoliDB $db, $user = null, $conf = null)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->user = $user;
|
||||
if ($conf !== null) {
|
||||
$this->conf = $conf;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -322,13 +334,20 @@ If user says 'order' without any qualifier, they mean a SALES ORDER - use this t
|
|||
*/
|
||||
public function execute(string $name, array $args)
|
||||
{
|
||||
global $user, $langs, $conf, $mysoc;
|
||||
global $langs, $conf, $mysoc;
|
||||
|
||||
// Ensure $this->user is the authenticated global user
|
||||
$this->user = $user;
|
||||
|
||||
if (!$user->id) {
|
||||
return ["error" => "User not authenticated."];
|
||||
// Use the user injected via constructor (McpHandler). Fall back to global $user
|
||||
// when running in a web session context (e.g. AI Assistant) where DI is not used.
|
||||
// is_object() is used instead of empty() because the parent McpTool declares $user
|
||||
// with a non-nullable type hint, which makes PHPStan flag empty($this->user) as
|
||||
// unreachable code.
|
||||
if (!is_object($this->user) || empty($this->user->id)) {
|
||||
global $user;
|
||||
if (is_object($user) && !empty($user->id)) {
|
||||
$this->user = $user;
|
||||
} else {
|
||||
return ["error" => "User not authenticated."];
|
||||
}
|
||||
}
|
||||
|
||||
if (!is_object($mysoc) || empty($mysoc->id)) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
/* Copyright (C) 2026 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
* Copyright (C) 2026 Nick Fragoulis
|
||||
* Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
|
@ -40,11 +41,36 @@ class ToolInvoices extends McpTool
|
|||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Aligned with McpHandler's instantiation contract: new $className($db, $user, $conf).
|
||||
*
|
||||
* @param DoliDB $db Database handler
|
||||
* @param User|null $user Service user provided by McpHandler (from AI_MCP_USER_ID)
|
||||
* @param Conf|null $conf Dolibarr config (optional)
|
||||
*/
|
||||
public function __construct(DoliDB $db)
|
||||
public function __construct(DoliDB $db, $user = null, $conf = null)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->user = $user;
|
||||
if ($conf !== null) {
|
||||
$this->conf = $conf;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the authenticated user, preferring DI ($this->user) and falling back to global $user.
|
||||
* In HTTP MCP context there is no PHP web session — only the service user injected via DI.
|
||||
*
|
||||
* @return User|null
|
||||
*/
|
||||
private function getUser()
|
||||
{
|
||||
// is_object() instead of empty() to avoid PHPStan flagging empty() on a
|
||||
// non-nullable typed parent property as unreachable.
|
||||
if (is_object($this->user) && !empty($this->user->id)) {
|
||||
return $this->user;
|
||||
}
|
||||
global $user;
|
||||
return is_object($user) && !empty($user->id) ? $user : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -312,7 +338,10 @@ class ToolInvoices extends McpTool
|
|||
*/
|
||||
private function validateInvoice($args)
|
||||
{
|
||||
global $user;
|
||||
$user = $this->getUser();
|
||||
if ($user === null) {
|
||||
return ["error" => "User not authenticated."];
|
||||
}
|
||||
$invoice = $this->findInvoice($args['invoice']);
|
||||
if (is_array($invoice)) {
|
||||
return $invoice;
|
||||
|
|
@ -351,7 +380,10 @@ class ToolInvoices extends McpTool
|
|||
*/
|
||||
private function payInvoice($args)
|
||||
{
|
||||
global $user;
|
||||
$user = $this->getUser();
|
||||
if ($user === null) {
|
||||
return ["error" => "User not authenticated."];
|
||||
}
|
||||
$invoice = $this->findInvoice($args['invoice']);
|
||||
if (is_array($invoice)) {
|
||||
return $invoice;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<?php
|
||||
/* Copyright (C) 2026 Laurent Destailleur <eldy@users.sourceforge.net>
|
||||
* Copyright (C) 2026 Nick Fragoulis
|
||||
* Copyright (C) 2026 Jose Martinez <jose.martinez@pichinov.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
|
|
@ -40,11 +41,19 @@ class ToolReports extends McpTool
|
|||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Aligned with McpHandler's instantiation contract: new $className($db, $user, $conf).
|
||||
*
|
||||
* @param DoliDB $db Database handler
|
||||
* @param User|null $user Service user provided by McpHandler (from AI_MCP_USER_ID)
|
||||
* @param Conf|null $conf Dolibarr config (optional)
|
||||
*/
|
||||
public function __construct(DoliDB $db)
|
||||
public function __construct(DoliDB $db, $user = null, $conf = null)
|
||||
{
|
||||
$this->db = $db;
|
||||
$this->user = $user;
|
||||
if ($conf !== null) {
|
||||
$this->conf = $conf;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue