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