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\Schema\Error;
15:
16: use Nexus\Assert\Assert;
17: use Nexus\Mcp\Core\Schema\Enum\ProtocolErrorCode;
18: use Nexus\Mcp\Core\Schema\Error;
19:
20: /**
21: * A JSON-RPC error indicating that the requested method does not exist or is not available.
22: *
23: * In MCP, a server returns this error when a client invokes a method the server does not
24: * implement — either a genuinely unknown method, or one gated behind a server capability the
25: * server did not advertise (e.g., calling `prompts/list` when the `prompts` capability was not
26: * advertised).
27: *
28: * A request that requires a client capability the client did not declare is signalled instead
29: * by `MissingRequiredClientCapabilityError` (`-32021`).
30: *
31: * @extends Error<array{code: -32601, message: non-empty-string, data?: mixed}>
32: *
33: * @see https://modelcontextprotocol.io/specification/2026-07-28/schema#methodnotfounderror
34: */
35: final readonly class MethodNotFoundError extends Error
36: {
37: public const string DEFAULT_MESSAGE = 'Method not found';
38:
39: /**
40: * @param non-empty-string $message
41: */
42: public function __construct(string $message, mixed $data = null)
43: {
44: parent::__construct(code: ProtocolErrorCode::MethodNotFound, message: $message, data: $data);
45: }
46:
47: #[\Override]
48: public static function fromArray(array $data): static
49: {
50: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
51: Assert::that($message)->isNonEmptyString('error "message" must be a non-empty string, {type} given.');
52:
53: return new self(message: $message, data: $data['data'] ?? null);
54: }
55:
56: #[\Override]
57: public function toArray(): array
58: {
59: $result = [
60: 'code' => ProtocolErrorCode::MethodNotFound->value,
61: 'message' => $this->message,
62: ];
63:
64: $data = $this->data ?? [];
65:
66: if ([] !== $data) {
67: $result['data'] = $data;
68: }
69:
70: return $result;
71: }
72: }
73: