dolibarr 24.0.0-beta
MCPServer Class Reference

MCPServer Class. More...

Public Member Functions

 __construct ($db, $conf, $user)
 Constructor.
 
 handleRequest (array $request)
 JSON-RPC 2.0 Router.
 

Private Member Functions

 handleInitialize (array $params)
 Handles the 'initialize' request.
 
 handleToolsList ()
 Handles the 'tools/list' request by delegating to McpHandler.
 
 handleToolCall (array $params)
 Handles the 'tools/call' request by delegating to McpHandler.
 
 handleResourcesList ()
 Handles the 'resources/list' request.
 
 handleResourceRead (array $params)
 Handles the 'resources/read' request.
 
 handlePromptsList ()
 Handles the 'prompts/list' request.
 
 handlePromptGet (array $params)
 Handles the 'prompts/get' request.
 
 successResponse ($result)
 Creates a successful JSON-RPC response.
 
 errorResponse (int $code, string $message, $data=null)
 Creates an error JSON-RPC response.
 

Detailed Description

MCPServer Class.

This class acts as a thin protocol layer for the Model Context Protocol. It handles JSON-RPC 2.0 requests and delegates all tool-related operations (discovery, loading, execution) to McpHandler engine.

Instantiates McpHandler with CTX_MCP_SERVER so that the public allow-list (AI_MCP_SERVER_ALLOWED_TOOLS) is applied — tools disabled by the admin are invisible in tools/list responses and blocked at tools/call execution.

Definition at line 41 of file mcp_protocol.class.php.

Constructor & Destructor Documentation

◆ __construct()

MCPServer::__construct ( $db,
$conf,
$user )

Constructor.

Parameters
DoliDB$dbDatabase handler object
Conf$confConfiguration object
User$userUser object

Definition at line 68 of file mcp_protocol.class.php.

References conf(), and user.

Member Function Documentation

◆ errorResponse()

MCPServer::errorResponse ( int $code,
string $message,
$data = null )
private

Creates an error JSON-RPC response.

Parameters
int$codeThe error code.
string$messageThe error message.
mixed$dataOptional error data.
Returns
array{jsonrpc: string, id: int|string, error: array{code: int, message: string, data?: mixed}}|null The formatted JSON-RPC error response, or null for notifications.

Definition at line 390 of file mcp_protocol.class.php.

Referenced by handleRequest().

◆ handleInitialize()

MCPServer::handleInitialize ( array $params)
private

Handles the 'initialize' request.

Parameters
array<string,mixed>$params Initialization parameters from the client.
Returns
array{protocolVersion: string, capabilities: array{tools: array{listChanged: bool}, resources: array{subscribe: bool, listChanged: bool}, prompts: array{listChanged: bool}, logging: object}, serverInfo: array{name: string, version: string}} Server capabilities and info.

Definition at line 149 of file mcp_protocol.class.php.

Referenced by handleRequest().

◆ handlePromptGet()

MCPServer::handlePromptGet ( array $params)
private

Handles the 'prompts/get' request.

Parameters
array{name?string, arguments?: array<string, mixed>} $params Parameters containing the prompt name and arguments.
Returns
array{messages: array<int, array{role: string, content: array{type: string, text: string}}>} A list of messages forming the prompt.
Exceptions
ExceptionIf the prompt name is not found.

Definition at line 310 of file mcp_protocol.class.php.

References $prod.

Referenced by handleRequest().

◆ handlePromptsList()

MCPServer::handlePromptsList ( )
private

Handles the 'prompts/list' request.

Returns
array{prompts: array<int, array{name: string, description: string, arguments: array<int, array{name: string, description: string, required: bool}>}>} A list of available prompt templates.

Definition at line 290 of file mcp_protocol.class.php.

Referenced by handleRequest().

◆ handleRequest()

MCPServer::handleRequest ( array $request)

JSON-RPC 2.0 Router.

Routes incoming requests to the appropriate handler method.

Parameters
array{jsonrpc?string, method?: string, params?: array<mixed>, id?: string|int|null} $request The decoded JSON-RPC request array.
Returns
array{jsonrpc: string, id: string|int|null, result?: mixed, error?: array<string, mixed>}|null A JSON-RPC response array, or null for notifications.
Exceptions
ExceptionOn processing errors.

Definition at line 89 of file mcp_protocol.class.php.

References dol_syslog(), errorResponse(), handleInitialize(), handlePromptGet(), handlePromptsList(), handleResourceRead(), handleResourcesList(), handleToolCall(), handleToolsList(), and successResponse().

◆ handleResourceRead()

MCPServer::handleResourceRead ( array $params)
private

Handles the 'resources/read' request.

Parameters
array{uri?string} $params Parameters containing the URI of the resource to read.
Returns
array{contents: array<int, array{uri: string, mimeType: string, text: string}>} The content of the requested resource.
Exceptions
ExceptionIf the resource URI is not found.

Definition at line 255 of file mcp_protocol.class.php.

References conf(), and user.

Referenced by handleRequest().

◆ handleResourcesList()

MCPServer::handleResourcesList ( )
private

Handles the 'resources/list' request.

Returns
array{resources: array<int, array{uri: string, name: string, description: string, mimeType: string}>} A list of available static resources.

Definition at line 230 of file mcp_protocol.class.php.

Referenced by handleRequest().

◆ handleToolCall()

MCPServer::handleToolCall ( array $params)
private

Handles the 'tools/call' request by delegating to McpHandler.

Execution is blocked for any tool not in AI_MCP_SERVER_ALLOWED_TOOLS, even if the client sends the request directly without consulting tools/list.

Parameters
array{name?string, arguments?: array<string, mixed>} $params Parameters containing the tool name and arguments.
Returns
array{content: array<int, array<string, mixed>>, isError: bool} The result of the tool execution.
Exceptions
ExceptionIf the tool is blocked, not found or execution fails.

Definition at line 192 of file mcp_protocol.class.php.

Referenced by handleRequest().

◆ handleToolsList()

MCPServer::handleToolsList ( )
private

Handles the 'tools/list' request by delegating to McpHandler.

Only tools permitted by AI_MCP_SERVER_ALLOWED_TOOLS are returned.

Returns
array{tools: array<int, array<string, mixed>>} An array containing the list of available tools.

Definition at line 173 of file mcp_protocol.class.php.

Referenced by handleRequest().

◆ successResponse()

MCPServer::successResponse ( $result)
private

Creates a successful JSON-RPC response.

Parameters
mixed$resultThe result data to include in the response.
Returns
array{jsonrpc: string, id: int|string, result: mixed}|null The formatted JSON-RPC response, or null for notifications.

Definition at line 369 of file mcp_protocol.class.php.

Referenced by handleRequest().


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