diff --git a/htdocs/ai/server/mcp_server.php b/htdocs/ai/server/mcp_server.php index e70caa4810f..f5bba5964ea 100644 --- a/htdocs/ai/server/mcp_server.php +++ b/htdocs/ai/server/mcp_server.php @@ -2,6 +2,7 @@ /* Copyright (C) 2026 Laurent Destailleur * Copyright (C) 2026 Nick Fragoulis * Copyright (C) 2026 MDW + * Copyright (C) 2026 Jose Martinez * * 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([ diff --git a/htdocs/ai/tools/crud_objects.class.php b/htdocs/ai/tools/crud_objects.class.php index 657f48fd9d4..da8f5dda046 100644 --- a/htdocs/ai/tools/crud_objects.class.php +++ b/htdocs/ai/tools/crud_objects.class.php @@ -2,6 +2,7 @@ /* Copyright (C) 2026 Laurent Destailleur * Copyright (C) 2026 Nick Fragoulis * Copyright (C) 2026 MDW + * Copyright (C) 2026 Jose Martinez * * 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)) { diff --git a/htdocs/ai/tools/invoices.class.php b/htdocs/ai/tools/invoices.class.php index d4e1ec79342..62cba455307 100644 --- a/htdocs/ai/tools/invoices.class.php +++ b/htdocs/ai/tools/invoices.class.php @@ -1,6 +1,7 @@ * Copyright (C) 2026 Nick Fragoulis + * Copyright (C) 2026 Jose Martinez * * 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; diff --git a/htdocs/ai/tools/reports.class.php b/htdocs/ai/tools/reports.class.php index 3cff4cb8065..6510c86cabb 100644 --- a/htdocs/ai/tools/reports.class.php +++ b/htdocs/ai/tools/reports.class.php @@ -1,6 +1,7 @@ * Copyright (C) 2026 Nick Fragoulis + * Copyright (C) 2026 Jose Martinez * * 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; + } } /**