PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / mcp-server / Server / JsonRpc.php

JsonRpc.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/mcp-server/Server/JsonRpc.php

123 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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