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\Exception;
15:
16: use Nexus\Mcp\Core\JsonRpc\SafeDisplay;
17: use Nexus\Mcp\Core\Schema\Enum\ProtocolErrorCode;
18: use Nexus\Mcp\Core\Schema\RequestId;
19:
20: /**
21: * Thrown when an inbound envelope's shape (request or notification) does not
22: * match the shape the method was registered under.
23: */
24: final class MethodMisroutedException extends AbstractJsonRpcProtocolException
25: {
26: /**
27: * @param non-empty-string $method
28: * @param non-empty-string $expectedShape
29: * @param non-empty-string $receivedShape
30: */
31: public function __construct(
32: string $method,
33: string $expectedShape,
34: string $receivedShape,
35: ?RequestId $requestId = null,
36: ?\Throwable $previous = null,
37: ) {
38: parent::__construct(
39: $requestId,
40: \sprintf(
41: 'Method "%s" must be sent as a %s, not a %s.',
42: SafeDisplay::sanitise($method),
43: $expectedShape,
44: $receivedShape,
45: ),
46: $previous,
47: );
48: }
49:
50: #[\Override]
51: public static function getErrorCode(): ProtocolErrorCode
52: {
53: return ProtocolErrorCode::InvalidRequest;
54: }
55: }
56: