| 1 |
<?php |
| 2 |
/** |
| 3 |
* FunnelMcpObservabilityHandler — observability sink for the WPFunnels MCP server. |
| 4 |
* |
| 5 |
* Wraps the adapter's error-log handler and drops the events that the adapter |
| 6 |
* emits while building the server, keeping only the events that describe |
| 7 |
* something a site owner would actually want in an audit trail. |
| 8 |
* |
| 9 |
* @package WPFunnels\MCP\Observability |
| 10 |
* @since 3.13.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace WPFunnels\MCP\Observability; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
use WP\MCP\Infrastructure\Observability\ErrorLogMcpObservabilityHandler as AdapterErrorLogHandler; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class FunnelMcpObservabilityHandler |
| 21 |
* |
| 22 |
* Logs MCP tool traffic without the per-request lifecycle noise. |
| 23 |
*/ |
| 24 |
class FunnelMcpObservabilityHandler extends AdapterErrorLogHandler { |
| 25 |
|
| 26 |
/** |
| 27 |
* Events the adapter emits as a side effect of constructing the server, |
| 28 |
* whether or not anyone is using MCP. |
| 29 |
* |
| 30 |
* The adapter builds every registered server on `rest_api_init`, so passing |
| 31 |
* these through to error_log() writes one line per REST request on every |
| 32 |
* site — hundreds of thousands of lines on a busy store, none of them |
| 33 |
* actionable. Tool traffic (`mcp.request`) is still logged. |
| 34 |
* |
| 35 |
* @var string[] |
| 36 |
*/ |
| 37 |
private const LIFECYCLE_EVENTS = array( |
| 38 |
'mcp.server.created', |
| 39 |
'mcp.component.registration', |
| 40 |
); |
| 41 |
|
| 42 |
/** |
| 43 |
* Record an event unless it is per-request lifecycle noise. |
| 44 |
* |
| 45 |
* @param string $event The event name to record. |
| 46 |
* @param array $tags Optional tags to attach to the event. |
| 47 |
* @param float|null $duration_ms Optional duration in milliseconds. |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function record_event( string $event, array $tags = array(), ?float $duration_ms = null ): void { |
| 51 |
if ( in_array( self::format_metric_name( $event ), self::LIFECYCLE_EVENTS, true ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
parent::record_event( $event, $tags, $duration_ms ); |
| 56 |
} |
| 57 |
} |
| 58 |
|