Content
1 month ago
Contracts
1 month ago
Core
1 month ago
JsonRpc
1 month ago
Lifecycle
1 month ago
Protocol
1 month ago
Tasks
1 month ago
Traits
1 month ago
AbstractDataTransferObject.php
1 month ago
AbstractEnum.php
1 month ago
McpConstants.php
1 month ago
McpConstants.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace WP\McpSchema\Common; |
| 6 | |
| 7 | /** |
| 8 | * MCP Protocol Constants. |
| 9 | * |
| 10 | * Contains all exported constants from the MCP TypeScript schema including: |
| 11 | * - Protocol version constants |
| 12 | * - JSON-RPC version |
| 13 | * - Standard JSON-RPC error codes |
| 14 | * - MCP-specific error codes |
| 15 | * |
| 16 | * @since 2025-11-25 |
| 17 | * |
| 18 | * @mcp-version 2025-11-25 |
| 19 | */ |
| 20 | final class McpConstants |
| 21 | { |
| 22 | // Protocol constants |
| 23 | public const LATEST_PROTOCOL_VERSION = '2025-11-25'; |
| 24 | public const JSONRPC_VERSION = '2.0'; |
| 25 | |
| 26 | // Error codes |
| 27 | public const PARSE_ERROR = -32700; |
| 28 | public const INVALID_REQUEST = -32600; |
| 29 | public const METHOD_NOT_FOUND = -32601; |
| 30 | public const INVALID_PARAMS = -32602; |
| 31 | public const INTERNAL_ERROR = -32603; |
| 32 | public const URL_ELICITATION_REQUIRED = -32042; |
| 33 | |
| 34 | /** |
| 35 | * Returns all error codes defined in this class. |
| 36 | * |
| 37 | * @return int[] |
| 38 | */ |
| 39 | public static function getErrorCodes(): array |
| 40 | { |
| 41 | return [ |
| 42 | self::PARSE_ERROR, |
| 43 | self::INVALID_REQUEST, |
| 44 | self::METHOD_NOT_FOUND, |
| 45 | self::INVALID_PARAMS, |
| 46 | self::INTERNAL_ERROR, |
| 47 | self::URL_ELICITATION_REQUIRED, |
| 48 | ]; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns error code names mapped to their values. |
| 53 | * |
| 54 | * @return array<string, int> |
| 55 | */ |
| 56 | public static function getErrorCodeNames(): array |
| 57 | { |
| 58 | return [ |
| 59 | 'PARSE_ERROR' => self::PARSE_ERROR, |
| 60 | 'INVALID_REQUEST' => self::INVALID_REQUEST, |
| 61 | 'METHOD_NOT_FOUND' => self::METHOD_NOT_FOUND, |
| 62 | 'INVALID_PARAMS' => self::INVALID_PARAMS, |
| 63 | 'INTERNAL_ERROR' => self::INTERNAL_ERROR, |
| 64 | 'URL_ELICITATION_REQUIRED' => self::URL_ELICITATION_REQUIRED, |
| 65 | ]; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Checks if the given error code is valid. |
| 70 | * |
| 71 | * @param int $code |
| 72 | * @return bool |
| 73 | */ |
| 74 | public static function isValidErrorCode(int $code): bool |
| 75 | { |
| 76 | return in_array($code, self::getErrorCodes(), true); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Gets the constant name for an error code. |
| 81 | * |
| 82 | * @param int $code |
| 83 | * @return string|null The constant name, or null if not found |
| 84 | */ |
| 85 | public static function getErrorCodeName(int $code): ?string |
| 86 | { |
| 87 | $flipped = array_flip(self::getErrorCodeNames()); |
| 88 | return $flipped[$code] ?? null; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Checks if an error code is a standard JSON-RPC error. |
| 93 | * |
| 94 | * Standard JSON-RPC errors are in the range -32700 to -32600. |
| 95 | * |
| 96 | * @param int $code |
| 97 | * @return bool |
| 98 | */ |
| 99 | public static function isStandardJsonRpcError(int $code): bool |
| 100 | { |
| 101 | return $code >= -32700 && $code <= -32600; |
| 102 | } |
| 103 | } |
| 104 |