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\Core\Handler;
15:
16: use Amp\Cancellation;
17: use Nexus\Mcp\Core\Schema\Notification\ProgressNotification;
18: use Nexus\Mcp\Core\Schema\NotificationParams\ProgressNotificationParams;
19: use Nexus\Mcp\Core\Schema\ProgressToken;
20: use Nexus\Mcp\Core\Schema\RequestId;
21:
22: /**
23: * Context passed to a request handler. Carries metadata about the incoming
24: * request and exposes domain helpers for emitting out-of-band messages.
25: */
26: abstract readonly class AbstractContext
27: {
28: public function __construct(
29: public RequestId $requestId,
30: public Cancellation $cancellation,
31: public ?ProgressToken $progressToken,
32: protected SenderInterface $sender,
33: ) {
34: }
35:
36: public function reportProgress(float $progress, ?float $total = null, ?string $message = null): void
37: {
38: $token = $this->progressToken;
39:
40: if (null === $token) {
41: return;
42: }
43:
44: $this->sender->sendNotification(new ProgressNotification(
45: params: new ProgressNotificationParams(
46: progressToken: $token,
47: progress: $progress,
48: total: $total,
49: message: '' === $message ? null : $message,
50: ),
51: ));
52: }
53: }
54: