|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the official PHP MCP SDK. |
| 5 | + * |
| 6 | + * A collaboration between Symfony and the PHP Foundation. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Mcp\Capability\Logger; |
| 13 | + |
| 14 | +use Mcp\Schema\Enum\LoggingLevel; |
| 15 | +use Mcp\Server\NotificationSender; |
| 16 | +use Psr\Log\AbstractLogger; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +/** |
| 20 | + * MCP-aware PSR-3 logger that sends log messages as MCP notifications. |
| 21 | + * |
| 22 | + * This logger implements the standard PSR-3 LoggerInterface and forwards |
| 23 | + * log messages to the NotificationSender. The NotificationHandler will |
| 24 | + * decide whether to actually send the notification based on capabilities. |
| 25 | + * |
| 26 | + * @author Adam Jamiu <[email protected]> |
| 27 | + */ |
| 28 | +final class ClientLogger extends AbstractLogger |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private readonly NotificationSender $notificationSender, |
| 32 | + private readonly ?LoggerInterface $fallbackLogger = null, |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Logs with an arbitrary level. |
| 38 | + * |
| 39 | + * @param string|\Stringable $message |
| 40 | + * @param array<string, mixed> $context |
| 41 | + */ |
| 42 | + public function log($level, $message, array $context = []): void |
| 43 | + { |
| 44 | + // Always log to fallback logger if provided (for local debugging) |
| 45 | + $this->fallbackLogger?->log($level, $message, $context); |
| 46 | + |
| 47 | + // Convert PSR-3 level to MCP LoggingLevel |
| 48 | + $mcpLevel = $this->convertToMcpLevel($level); |
| 49 | + if (null === $mcpLevel) { |
| 50 | + return; // Unknown level, skip MCP notification |
| 51 | + } |
| 52 | + |
| 53 | + // Send MCP logging notification - let NotificationHandler decide if it should be sent |
| 54 | + try { |
| 55 | + $this->notificationSender->send('notifications/message', [ |
| 56 | + 'level' => $mcpLevel->value, |
| 57 | + 'data' => (string) $message, |
| 58 | + 'logger' => $context['logger'] ?? null, |
| 59 | + ]); |
| 60 | + } catch (\Throwable $e) { |
| 61 | + // If MCP notification fails, at least log to fallback |
| 62 | + $this->fallbackLogger?->error('Failed to send MCP log notification', [ |
| 63 | + 'original_level' => $level, |
| 64 | + 'original_message' => $message, |
| 65 | + 'error' => $e->getMessage(), |
| 66 | + ]); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Converts PSR-3 log level to MCP LoggingLevel. |
| 72 | + * |
| 73 | + * @param mixed $level PSR-3 level |
| 74 | + * |
| 75 | + * @return LoggingLevel|null MCP level or null if unknown |
| 76 | + */ |
| 77 | + private function convertToMcpLevel($level): ?LoggingLevel |
| 78 | + { |
| 79 | + return match (strtolower((string) $level)) { |
| 80 | + 'emergency' => LoggingLevel::Emergency, |
| 81 | + 'alert' => LoggingLevel::Alert, |
| 82 | + 'critical' => LoggingLevel::Critical, |
| 83 | + 'error' => LoggingLevel::Error, |
| 84 | + 'warning' => LoggingLevel::Warning, |
| 85 | + 'notice' => LoggingLevel::Notice, |
| 86 | + 'info' => LoggingLevel::Info, |
| 87 | + 'debug' => LoggingLevel::Debug, |
| 88 | + default => null, |
| 89 | + }; |
| 90 | + } |
| 91 | +} |
0 commit comments