1. Go to this page and download the library: Download code-wheel/mcp-events library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
code-wheel / mcp-events example snippets
use CodeWheel\McpEvents\ToolExecutionStartedEvent;
use CodeWheel\McpEvents\ToolExecutionSucceededEvent;
use CodeWheel\McpEvents\ToolExecutionFailedEvent;
// When tool execution starts
$startEvent = new ToolExecutionStartedEvent(
toolName: 'create_user',
pluginId: 'my_module.create_user',
arguments: ['username' => 'john', 'email' => '[redacted]'],
requestId: 'req-123',
timestamp: microtime(true),
);
$dispatcher->dispatch($startEvent);
// On success
$successEvent = new ToolExecutionSucceededEvent(
toolName: 'create_user',
pluginId: 'my_module.create_user',
arguments: ['username' => 'john', 'email' => '[redacted]'],
result: $callToolResult,
durationMs: 45.2,
requestId: 'req-123',
);
$dispatcher->dispatch($successEvent);
// On failure
$failEvent = new ToolExecutionFailedEvent(
toolName: 'create_user',
pluginId: 'my_module.create_user',
arguments: ['username' => 'john'],
reason: ToolExecutionFailedEvent::REASON_VALIDATION,
result: null,
exception: $validationException,
durationMs: 12.5,
requestId: 'req-123',
);
$dispatcher->dispatch($failEvent);
use CodeWheel\McpEvents\ToolExecutionStartedEvent;
use CodeWheel\McpEvents\ToolExecutionSucceededEvent;
use CodeWheel\McpEvents\ToolExecutionFailedEvent;
class ToolExecutionLogger {
public function onStart(ToolExecutionStartedEvent $event): void {
$this->logger->info('Tool started', [
'tool' => $event->toolName,
'request_id' => $event->requestId,
]);
}
public function onSuccess(ToolExecutionSucceededEvent $event): void {
$this->metrics->histogram('tool_duration_ms', $event->durationMs, [
'tool' => $event->toolName,
]);
}
public function onFailure(ToolExecutionFailedEvent $event): void {
$context = [
'tool' => $event->toolName,
'reason' => $event->reason,
'duration_ms' => $event->durationMs,
];
if ($event->hasException()) {
$context['exception'] = $event->exception->getMessage();
}
if ($event->isPolicyFailure()) {
$this->logger->warning('Tool blocked by policy', $context);
} else {
$this->logger->error('Tool execution failed', $context);
}
}
}
// Check if failure was policy-related
if ($event->isPolicyFailure()) {
// Handle policy block differently
}
// Check if exception was thrown
if ($event->hasException()) {
$exception = $event->exception;
}
// Get all valid failure reasons
$allReasons = ToolExecutionFailedEvent::allReasons();
// ['REASON_VALIDATION' => 'validation_failed', ...]
// Validate a reason string
if (ToolExecutionFailedEvent::isValidReason($reason)) {
// Valid reason
}
// Direct serialization
$json = json_encode($event);
// Or get the array
$data = $event->jsonSerialize();
// Example output for ToolExecutionFailedEvent:
// {
// "event": "tool_execution_failed",
// "tool_name": "create_user",
// "plugin_id": "my_module.create_user",
// "reason": "validation_failed",
// "duration_ms": 12.5,
// "request_id": "req-123",
// "is_policy_failure": false,
// "has_exception": true,
// "exception_class": "InvalidArgumentException",
// "exception_message": "Email is
// With mcp/sdk (optional)
use Mcp\Schema\Result\CallToolResult;
$event = new ToolExecutionSucceededEvent(
toolName: 'my_tool',
pluginId: 'my_module.my_tool',
arguments: [],
result: $callToolResult, // CallToolResult from mcp/sdk
durationMs: 10.5,
requestId: null,
);
// Access result properties
$structured = $event->result->structuredContent;
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.