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\Dispatch;
15:
16: use Nexus\Mcp\Core\Exception\OutboundRequestsNotSupportedException;
17: use Nexus\Mcp\Core\Handler\SenderInterface;
18: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcNotification;
19: use Nexus\Mcp\Core\Schema\JsonRpc\JsonRpcRequest;
20: use Nexus\Mcp\Core\Schema\RequestId;
21: use Nexus\Mcp\Core\Transport\SendContext;
22: use Nexus\Mcp\Core\Transport\TransportInterface;
23:
24: /**
25: * `SenderInterface` implementation scoped to a single inbound request. Tags
26: * outbound notifications with `relatedRequestId` so the transport can route
27: * them back to the originating session (used by streamable HTTP).
28: */
29: final readonly class RequestBoundSender implements SenderInterface
30: {
31: private SendContext $outboundContext;
32:
33: public function __construct(private TransportInterface $transport, private RequestId $requestId)
34: {
35: $this->outboundContext = new SendContext(relatedRequestId: $this->requestId);
36: }
37:
38: #[\Override]
39: public function sendNotification(JsonRpcNotification $notification): void
40: {
41: $this->transport->send($notification, $this->outboundContext);
42: }
43:
44: /**
45: * The spec defines no server-initiated request, so this always rejects.
46: */
47: #[\Override]
48: public function sendRequest(JsonRpcRequest $request): never
49: {
50: throw new OutboundRequestsNotSupportedException($this->requestId);
51: }
52: }
53: