automator_mcp_token_event
Fires after a token event is processed, allowing customization of the cleaned token count.
add_action( 'automator_mcp_token_event', $callback, 10, 1 );
Description
Fires after expired API access tokens are cleaned up by the Model Context Protocol. Developers can hook into this action to perform custom actions based on the number of tokens cleaned, such as logging or sending notifications. The `$cleaned_count` parameter indicates how many tokens were removed.
Usage
add_action( 'automator_mcp_token_event', 'your_function_name', 10, 1 );
Parameters
-
$cleaned_count(mixed) - This parameter contains the number of tokens that have been successfully cleaned up.
Examples
<?php
/**
* Log token cleanup events to a custom log file for debugging.
*
* This function is triggered by the 'automator_mcp_token_event' action hook
* and logs the number of tokens cleaned up, along with the event type and timestamp.
*
* @param array $event_data An array containing event details, including 'cleaned_count', 'event_type', and 'timestamp'.
*/
function my_custom_token_cleanup_logger( $event_data ) {
// Ensure we have the expected data.
if ( ! is_array( $event_data ) || ! isset( $event_data['cleaned_count'] ) || ! isset( $event_data['event_type'] ) ) {
// In a real-world scenario, you might want to log an error here too.
return;
}
$cleaned_count = absint( $event_data['cleaned_count'] );
$event_type = sanitize_text_field( $event_data['event_type'] );
$timestamp = isset( $event_data['timestamp'] ) ? intval( $event_data['timestamp'] ) : time();
$log_message = sprintf(
'[%s] Token Cleanup Event: Type - %s, Tokens Cleaned - %d',
date( 'Y-m-d H:i:s', $timestamp ),
$event_type,
$cleaned_count
);
// For demonstration, we'll use WordPress's error logging.
// In a production environment, you might write to a dedicated log file.
error_log( $log_message );
}
// Add the action hook with the correct number of accepted arguments.
// The hook passes a single array argument.
add_action( 'automator_mcp_token_event', 'my_custom_token_cleanup_logger', 10, 1 );
?>
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/oauth/class-token-cleanup.php:110
src/api/transports/model-context-protocol/oauth/class-token-cleanup.php:122
src/api/transports/model-context-protocol/oauth/class-token-cleanup.php:148
src/api/transports/model-context-protocol/oauth/class-token-manager.php:527
public function run_cleanup(): void {
try {
$cleaned_count = $this->token_manager->cleanup_expired_tokens();
// Log cleanup results.
do_action(
'automator_mcp_token_event',
array(
'event_type' => 'automated_cleanup',
'cleaned_count' => $cleaned_count,
'timestamp' => time(),
)
);
} catch ( Exception $e ) {
// Log cleanup failure.
do_action(
'automator_mcp_token_event',
array(
'event_type' => 'cleanup_failed',
'error' => $e->getMessage(),
'timestamp' => time(),
)
);
}
}