| 1 |
<?php |
| 2 |
/** |
| 3 |
* JSON-RPC 2.0 envelopes and error codes (spec 044, FR-004/FR-005). |
| 4 |
* |
| 5 |
* @package Templately\Modules\McpServer\Server |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Templately\Modules\McpServer\Server; |
| 9 |
|
| 10 |
class JsonRpc { |
| 11 |
|
| 12 |
const VERSION = '2.0'; |
| 13 |
|
| 14 |
const PARSE_ERROR = -32700; |
| 15 |
const INVALID_REQUEST = -32600; |
| 16 |
const METHOD_NOT_FOUND = -32601; |
| 17 |
const INVALID_PARAMS = -32602; |
| 18 |
const UNAUTHORIZED = -32001; |
| 19 |
|
| 20 |
/** |
| 21 |
* @param mixed $id |
| 22 |
* @param mixed $result |
| 23 |
* @return array |
| 24 |
*/ |
| 25 |
public static function result( $id, $result ): array { |
| 26 |
return [ |
| 27 |
'jsonrpc' => self::VERSION, |
| 28 |
'id' => $id, |
| 29 |
'result' => $result, |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @param mixed $id |
| 35 |
* @param int $code |
| 36 |
* @param string $message |
| 37 |
* @param mixed $data |
| 38 |
* @return array |
| 39 |
*/ |
| 40 |
public static function error( $id, int $code, string $message, $data = null ): array { |
| 41 |
$error = [ |
| 42 |
'code' => $code, |
| 43 |
'message' => $message, |
| 44 |
]; |
| 45 |
|
| 46 |
if ( null !== $data ) { |
| 47 |
$error['data'] = $data; |
| 48 |
} |
| 49 |
|
| 50 |
return [ |
| 51 |
'jsonrpc' => self::VERSION, |
| 52 |
'id' => $id, |
| 53 |
'error' => $error, |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* A capability that failed INSIDE its own logic is still a successful |
| 59 |
* exchange — the agent gets a readable message it can act on, rather than a |
| 60 |
* protocol error implying it called the server wrongly (FR-005). |
| 61 |
* |
| 62 |
* @param mixed $id |
| 63 |
* @param string $message |
| 64 |
* @return array |
| 65 |
*/ |
| 66 |
public static function tool_error( $id, string $message ): array { |
| 67 |
return self::result( |
| 68 |
$id, |
| 69 |
[ |
| 70 |
'content' => [ |
| 71 |
[ |
| 72 |
'type' => 'text', |
| 73 |
'text' => $message, |
| 74 |
], |
| 75 |
], |
| 76 |
'isError' => true, |
| 77 |
] |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @param mixed $id |
| 83 |
* @param mixed $payload |
| 84 |
* @return array |
| 85 |
*/ |
| 86 |
public static function tool_result( $id, $payload ): array { |
| 87 |
return self::result( |
| 88 |
$id, |
| 89 |
[ |
| 90 |
'content' => [ |
| 91 |
[ |
| 92 |
'type' => 'text', |
| 93 |
'text' => wp_json_encode( $payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ), |
| 94 |
], |
| 95 |
], |
| 96 |
'isError' => false, |
| 97 |
] |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* A message with no `id` is a notification: no reply is ever sent (FR-002). |
| 103 |
* |
| 104 |
* @param array $message |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public static function is_notification( array $message ): bool { |
| 108 |
return ! array_key_exists( 'id', $message ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* A structurally valid single request object. |
| 113 |
* |
| 114 |
* @param mixed $message |
| 115 |
* @return bool |
| 116 |
*/ |
| 117 |
public static function is_valid_request( $message ): bool { |
| 118 |
return is_array( $message ) |
| 119 |
&& ! empty( $message['method'] ) |
| 120 |
&& is_string( $message['method'] ); |
| 121 |
} |
| 122 |
} |
| 123 |
|