dolibarr 25.0.0-alpha
ToolCrudObjects Class Reference

Tool class for CRUD operations on Dolibarr objects TODO Remove all tools in this file. More...

Inheritance diagram for ToolCrudObjects:
Collaboration diagram for ToolCrudObjects:

Public Member Functions

 __construct (DoliDB $db, $user=null, $conf=null)
 Constructor.
 
 getDefinitions ()
 Returns an array of tool definitions, including name, description, and input schema.
 
 getRequiredRights (string $toolName)
 Per-object-type rights are checked inside this class (PERM_MAP / delete map), including the two permission namespaces Dolibarr uses for the same action.
 
 writeConfirmationPreview (string $toolName, array $args)
 Preview of the record this call would write.
 
 getCategories ()
 Return categories this tool belongs to.
 
 execute (string $name, array $args)
 Executes the requested tool function based on its name.
 
- Public Member Functions inherited from McpTool
 __construct ($db, $user, $conf)
 Constructor.
 
 isSystem ()
 Return true if this is a system/infrastructure tool that must always remain visible and executable regardless of the admin allow-list.
 

Private Member Functions

 addLineItem (array $args)
 Entry point for the 'add_line_item' tool.
 
 completeCreatedProduct (Product $newprod, CommonObject $object, array $args, $price, $vat)
 Complete a product just created on the fly (create_missing_products) with the information the source document carries beyond the product card:
 
 findProduct ($identifier)
 Find a product by various identifiers (ID, Ref, Barcode, Label).
 
 deleteObject (array $args)
 Delete a document object.
 
 checkPermission (string $type, string $mode='write')
 Check if the current user has permission for the given object type.
 
 checkAccessToObject (string $type, CommonObject $object)
 Check that the current user is allowed to work on an already fetched object.
 
 instantiate (string $type)
 Factory Helper to instantiate Dolibarr objects.
 

Additional Inherited Members

- Public Attributes inherited from McpTool
const RIGHTS_UNDECLARED = 'undeclared'
 Default of getRequiredRights(): the class never declared anything, deny.
 
const NO_WRITE = ''
 Returned by writeConfirmationPreview() when the tool writes nothing.
 

Detailed Description

Tool class for CRUD operations on Dolibarr objects TODO Remove all tools in this file.

Must be into the objectname.class.php to follow the same structure than APIs.

Definition at line 37 of file crud_objects.class.php.

Constructor & Destructor Documentation

◆ __construct()

ToolCrudObjects::__construct ( DoliDB $db,
$user = null,
$conf = null )

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.

Parameters
DoliDB$dbDatabase handler
User | null$userService user provided by McpHandler (from AI_MCP_USER_ID)
Conf | null$confDolibarr config (optional)

Definition at line 51 of file crud_objects.class.php.

References conf(), and user.

Member Function Documentation

◆ addLineItem()

ToolCrudObjects::addLineItem ( array $args)
private

Entry point for the 'add_line_item' tool.

Adds line to an already existing object. Instantiates the document and calls the line processing helper.

Parameters
array{object_type:string,parent_id:int,product_id?:int,description?:string,quantity?:float|int,unit_price?:float|int,vat_rate?:float|int}$args Tool arguments for adding a line item
Returns
array{success:bool,line_id?:int,error?:string}

Definition at line 1125 of file crud_objects.class.php.

Referenced by execute().

◆ checkAccessToObject()

ToolCrudObjects::checkAccessToObject ( string $type,
CommonObject $object )
private

Check that the current user is allowed to work on an already fetched object.

fetch() selects on the rowid only and does not filter on the entity, so a bare fetch() is an IDOR: it happily returns a record of another entity or of a thirdparty the user has no access to. restrictedArea() called with $mode = 1 returns 0/1 instead of emitting an HTML error page, which is what we need in this JSON API context.

Parameters
string$typeObject type key.
CommonObject$objectObject already loaded with fetch().
Returns
array{error: string}|null Null if allowed, error array if denied.

Definition at line 1431 of file crud_objects.class.php.

References $object, dol_syslog(), restrictedArea(), and user.

◆ checkPermission()

ToolCrudObjects::checkPermission ( string $type,
string $mode = 'write' )
private

Check if the current user has permission for the given object type.

Parameters
string$typeObject type key.
string$mode'write' to check the create/update permission, 'delete' to check the dedicated delete permission.
Returns
array{error: string}|null Null if allowed, error array if denied.

Definition at line 1400 of file crud_objects.class.php.

References user.

◆ completeCreatedProduct()

ToolCrudObjects::completeCreatedProduct ( Product $newprod,
CommonObject $object,
array $args,
$price,
$vat )
private

Complete a product just created on the fly (create_missing_products) with the information the source document carries beyond the product card:

  • on supplier-side documents, the supplier price line ("Buying prices" tab): supplier = the document's thirdparty, supplier ref = the line's product_ref (the ref printed on the supplier's own document), price = the line's buying price. Product->cost_price alone does not pre-fill future supplier orders; this record does.
  • the optional 'products_category' tag (created when missing), so the freshly created products can be reviewed as a batch. Both steps are best-effort: a failure is logged and the line creation continues, since the product and its document line are already right.
Parameters
Product$newprodProduct just created
CommonObject$objectParent document the line belongs to
array<string,mixed>$args Line arguments
?float$priceLine buying price (HT), when provided
float$vatLine VAT rate
Returns
void

Definition at line 1194 of file crud_objects.class.php.

References $conf, $object, dol_syslog(), getDolGlobalInt(), isModEnabled(), and user.

◆ deleteObject()

ToolCrudObjects::deleteObject ( array $args)
private

Delete a document object.

Parameters
array{object_typestring, id: int|string} $args Arguments containing type and ID.
Returns
array{success: bool}|array{error: string} Result array.

Definition at line 1346 of file crud_objects.class.php.

References $id.

Referenced by execute().

◆ execute()

ToolCrudObjects::execute ( string $name,
array $args )

Executes the requested tool function based on its name.

Parameters
string$nameThe name of the tool to execute.
array<string,mixed>$args The arguments for the tool (key-value pairs).
Returns
mixed The result of the tool execution (usually an array) or an error array.

Reimplemented from McpTool.

Definition at line 505 of file crud_objects.class.php.

References $conf, $mysoc, addLineItem(), deleteObject(), and user.

◆ findProduct()

ToolCrudObjects::findProduct ( $identifier)
private

Find a product by various identifiers (ID, Ref, Barcode, Label).

Parameters
string | int$identifierThe search term (ID, ref, barcode, etc.).
Returns
Product|array{error: string, matches?: list<string>} Returns the Product object on success, or an error array.

Definition at line 1254 of file crud_objects.class.php.

References getEntity().

◆ getCategories()

ToolCrudObjects::getCategories ( )

Return categories this tool belongs to.

Used by the intent parser to filter available tools.

Returns
array<string> List of categories (e.g., ['billing', 'commercial'])

Reimplemented from McpTool.

Definition at line 493 of file crud_objects.class.php.

◆ getDefinitions()

ToolCrudObjects::getDefinitions ( )

Returns an array of tool definitions, including name, description, and input schema.

Returns
list<array<string, mixed>> Array of tool definitions.

Reimplemented from McpTool.

Definition at line 229 of file crud_objects.class.php.

◆ getRequiredRights()

ToolCrudObjects::getRequiredRights ( string $toolName)

Per-object-type rights are checked inside this class (PERM_MAP / delete map), including the two permission namespaces Dolibarr uses for the same action.

Parameters
string$toolNameTool being executed.
Returns
string RIGHTS_ENFORCED_DOWNSTREAM

Reimplemented from McpTool.

Definition at line 412 of file crud_objects.class.php.

◆ instantiate()

ToolCrudObjects::instantiate ( string $type)
private

Factory Helper to instantiate Dolibarr objects.

Parameters
string$typeObject type key (e.g., 'proposal', 'invoice').
Returns
CommonObject New instance of the specific Dolibarr class.
Exceptions
ExceptionIf the type is unknown or class not found.

Definition at line 1463 of file crud_objects.class.php.

◆ writeConfirmationPreview()

ToolCrudObjects::writeConfirmationPreview ( string $toolName,
array $args )

Preview of the record this call would write.

Parameters
string$toolNameTool that would run.
array<string,mixed>$args Arguments it would run with.
Returns
string Preview text, or McpTool\NO_WRITE for a read.

Reimplemented from McpTool.

Definition at line 424 of file crud_objects.class.php.

References McpTool\NO_WRITE.


The documentation for this class was generated from the following file: