1: <?php
2:
3: declare(strict_types=1);
4:
5: /**
6: * This file is part of the Nexus MCP SDK package.
7: *
8: * (c) 2026 John Paul E. Balandan, CPA <paulbalandan@gmail.com>
9: *
10: * For the full copyright and license information, please view
11: * the LICENSE file that was distributed with this source code.
12: */
13:
14: namespace Nexus\Mcp\Server;
15:
16: use Amp\Cancellation;
17: use Nexus\Mcp\Core\Handler\AbstractContext;
18: use Nexus\Mcp\Core\Handler\SenderInterface;
19: use Nexus\Mcp\Core\Schema\Enum\LoggingLevel;
20: use Nexus\Mcp\Core\Schema\Notification\LoggingMessageNotification;
21: use Nexus\Mcp\Core\Schema\NotificationParams\LoggingMessageNotificationParams;
22: use Nexus\Mcp\Core\Schema\RequestId;
23: use Nexus\Mcp\Core\Schema\RequestMetaObject;
24: use Nexus\Mcp\Server\Logging\LoggingLevelGate;
25:
26: /**
27: * Context passed to server-side request handlers.
28: */
29: final readonly class ServerContext extends AbstractContext
30: {
31: public function __construct(
32: RequestId $requestId,
33: Cancellation $cancellation,
34: RequestMetaObject $meta,
35: ?string $sessionId,
36: SenderInterface $sender,
37: private LoggingLevelGate $gate = new LoggingLevelGate(),
38: ) {
39: parent::__construct($requestId, $cancellation, $meta, $sessionId, $sender);
40: }
41:
42: public function log(LoggingLevel $level, mixed $data, ?string $logger = null): void
43: {
44: if (! $this->gate->shouldEmit($level)) {
45: return;
46: }
47:
48: $this->sender->sendNotification(new LoggingMessageNotification(
49: new LoggingMessageNotificationParams($level, $data, $logger),
50: ));
51: }
52: }
53: