dolibarr 25.0.0-alpha
mcptool.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2026 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2026 Nick Fragoulis
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
32abstract class McpTool
33{
34 const RIGHTS_ENFORCED_DOWNSTREAM = 'downstream';
35
39 const RIGHTS_UNDECLARED = 'undeclared';
40
44 const NO_WRITE = '';
45
47 protected $db;
48
50 protected $user;
51
53 protected $conf;
54
62 public function __construct($db, $user, $conf)
63 {
64 $this->db = $db;
65 $this->user = $user;
66 $this->conf = $conf;
67 }
68
77 abstract public function getDefinitions(): array;
78
90 abstract public function execute(string $toolName, array $args);
91
98 public function getCategories(): array
99 {
100 return ['global']; // Default
101 }
102
117 public function isSystem()
118 {
119 return false;
120 }
121
137 public function getRequiredRights(string $toolName)
138 {
140 }
153 public function writeConfirmationPreview(string $toolName, array $args)
154 {
155 return self::NO_WRITE;
156 }
157
169 public function writePrecheck(string $toolName, array $args)
170 {
171 return '';
172 }
173
186 public static function normalizeInputSchema(array $schema): array
187 {
188 if (array_key_exists('const', $schema)) {
189 $val = $schema['const'];
190 if (!isset($schema['enum'])) {
191 $schema['enum'] = is_array($val) ? array_values($val) : array($val);
192 }
193 if (empty($schema['type'])) {
194 if (is_int($val)) {
195 $schema['type'] = 'integer';
196 } elseif (is_float($val)) {
197 $schema['type'] = 'number';
198 } elseif (is_bool($val)) {
199 $schema['type'] = 'boolean';
200 } elseif (is_string($val)) {
201 $schema['type'] = 'string';
202 } elseif (is_array($val)) {
203 $schema['type'] = (array_keys($val) === range(0, count($val) - 1)) ? 'array' : 'object';
204 }
205 }
206 unset($schema['const']);
207 }
208
209 // Drop keywords known to be rejected by strict LLM function-calling schemas
210 unset($schema['$schema'], $schema['$id']);
211
212 foreach ($schema as $key => $value) {
213 if (is_array($value)) {
214 $schema[$key] = self::normalizeInputSchema($value);
215 }
216 }
217
218 return $schema;
219 }
220}
Abstract base class for all MCP (Model Context Protocol) tools.
execute(string $toolName, array $args)
Execute a specific tool.
writeConfirmationPreview(string $toolName, array $args)
Preview of what a call would write, or NO_WRITE for a read tool.
writePrecheck(string $toolName, array $args)
Reason why a write cannot run at all, checked before any confirmation.
getDefinitions()
Return the list of tools provided by this class.
__construct($db, $user, $conf)
Constructor.
const RIGHTS_UNDECLARED
Default of getRequiredRights(): the class never declared anything, deny.
isSystem()
Return true if this is a system/infrastructure tool that must always remain visible and executable re...
getRequiredRights(string $toolName)
Rights the caller must hold to run a tool of this class.
static normalizeInputSchema(array $schema)
Normalize a tool input schema to ensure broad compatibility with LLM function-calling APIs (e....
const NO_WRITE
Returned by writeConfirmationPreview() when the tool writes nothing.
getCategories()
Return categories this tool belongs to.
conf($dolibarr_main_document_root, $realpathconf=null)
Load conf file (file must exists)
Definition inc.php:431
$conf db user
Active Directory does not allow anonymous connections.
Definition repair.php:166