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/draft/schema#methodnotfounderror
34: */
35: final readonly class MethodNotFoundError extends Error
36: {
37: public const string DEFAULT_MESSAGE = 'Method not found';
38:
39: public function __construct(string $message, mixed $data = null)
40: {
41: parent::__construct(code: ProtocolErrorCode::MethodNotFound, message: $message, data: $data);
42: }
43:
44: #[\Override]
45: public static function fromArray(array $data): static
46: {
47: $message = $data['message'] ?? self::DEFAULT_MESSAGE;
48: Assert::that($message)->isString('error "message" must be a string, {type} given.');
49:
50: return new self(message: $message, data: $data['data'] ?? null);
51: }
52:
53: #[\Override]
54: public function toArray(): array
55: {
56: $result = [
57: 'code' => ProtocolErrorCode::MethodNotFound->value,
58: 'message' => $this->message,
59: ];
60:
61: $data = $this->data ?? [];
62:
63: if ([] !== $data) {
64: $result['data'] = $data;
65: }
66:
67: return $result;
68: }
69: }
70: