Action uncanny-automator

automator_mcp_tools_handle

Fires when the API handles tool requests, allowing customization of the process based on the provided ID.

add_action( 'automator_mcp_tools_handle', $callback, 10, 1 );

Description

Fires after a tool has been successfully executed by the Model Context Protocol. Developers can use this action to log tool executions, perform post-tool actions based on the result, or even dynamically modify the `$id` parameter before it's passed on. This hook provides a crucial extension point for customizing tool integration workflows.


Usage

add_action( 'automator_mcp_tools_handle', 'your_function_name', 10, 1 );

Parameters

$id (mixed)
This parameter represents the unique identifier for the tool call being handled.

Examples

<?php
/**
 * Example of how to hook into the 'automator_mcp_tools_handle' action hook.
 * This example demonstrates how to log the tool ID and parameters when a tool is executed.
 */
add_action(
	'automator_mcp_tools_handle',
	function ( $tool_data ) {
		// Ensure we have the expected data structure.
		if ( ! is_array( $tool_data ) || ! isset( $tool_data['id'] ) || ! isset( $tool_data['params'] ) ) {
			return;
		}

		$tool_id   = $tool_data['id'];
		$tool_params = $tool_data['params'];

		// Log the tool execution details for debugging or auditing purposes.
		// In a real-world scenario, you might want to save this to a custom database table
		// or send it to an external logging service.
		error_log(
			sprintf(
				'Automator tool executed. ID: %s, Parameters: %s',
				esc_html( $tool_id ),
				wp_json_encode( $tool_params ) // Encode parameters for better readability in logs.
			)
		);

		// If you needed to modify the outcome or prevent further processing,
		// you would do it here. For this example, we're just logging.
	},
	10, // Priority: Default priority is 10.
	1   // Accepted args: The hook passes one argument: $tool_data (an array).
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/api/transports/model-context-protocol/class-message-router.php:188

private function handle_tools_call( $id, $params ) {

		$tool_name = $params['name'] ?? '';
		$arguments = $params['arguments'] ?? array();

		// Get tool from registry
		$registry_tool = $this->tool_registry->get_tool( $tool_name );
		if ( ! $registry_tool ) {
			return Json_Rpc_Envelope::create_error_response(
				$id,
				-32602,
				'Unknown tool: ' . $tool_name
			);
		}

		$result = $this->tool_registry->execute_tool( $tool_name, $arguments );

		// Give capability for 3rd party to extend our toolset.
		do_action(
			'automator_mcp_tools_handle',
			array(
				'id'     => $id,
				'params' => $params,
			)
		);

		if ( is_wp_error( $result ) ) {
			return Json_Rpc_Envelope::create_error_response(
				$id,
				$result->get_error_code(),
				$result->get_error_message()
			);
		}

		return Json_Rpc_Envelope::create_success_response( $id, $result );
	}

Scroll to Top