| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "mcp-jsonrpc.h" |
| 4 | |
| 5 | #include <string.h> |
| 6 | |
| 7 | static const size_t MCP_JSONRPC_RESPONSE_MAX_BYTES = 16 * 1024 * 1024; |
| 8 | |
| 9 | static void buffer_append_json_id(BUFFER *out, struct json_object *id_obj) { |
| 10 | if (!id_obj) { |
| 11 | buffer_strcat(out, "null"); |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | const char *id_text = json_object_to_json_string_ext(id_obj, JSON_C_TO_STRING_PLAIN); |
| 16 | if (!id_text) |
| 17 | id_text = "null"; |
| 18 | buffer_fast_strcat(out, id_text, strlen(id_text)); |
| 19 | } |
| 20 | |
| 21 | static void buffer_append_json_string_value(BUFFER *out, const char *text) { |
| 22 | struct json_object *tmp = json_object_new_string(text ? text : ""); |
| 23 | const char *payload = json_object_to_json_string_ext(tmp, JSON_C_TO_STRING_PLAIN); |
| 24 | if (payload) |
| 25 | buffer_fast_strcat(out, payload, strlen(payload)); |
| 26 | json_object_put(tmp); |
| 27 | } |
| 28 | |
| 29 | int mcp_jsonrpc_error_code(MCP_RETURN_CODE rc) { |
| 30 | switch (rc) { |
| 31 | case MCP_RC_INVALID_PARAMS: |
| 32 | return -32602; |
| 33 | case MCP_RC_NOT_FOUND: |
| 34 | case MCP_RC_NOT_IMPLEMENTED: |
| 35 | return -32601; |
| 36 | case MCP_RC_BAD_REQUEST: |
| 37 | return -32600; |
| 38 | case MCP_RC_INTERNAL_ERROR: |
| 39 | return -32603; |
| 40 | case MCP_RC_OK: |
| 41 | return 0; |
| 42 | case MCP_RC_ERROR: |
| 43 | default: |
| 44 | return -32000; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | BUFFER *mcp_jsonrpc_build_error_payload(struct json_object *id_obj, int code, const char *message, |
| 49 | const struct mcp_response_chunk *chunks, size_t chunk_count) { |
| 50 | BUFFER *out = buffer_create(512, NULL); |
| 51 | buffer_strcat(out, "{\"jsonrpc\":\"2.0\",\"id\":"); |
| 52 | buffer_append_json_id(out, id_obj); |
| 53 | buffer_strcat(out, ",\"error\":{\"code\":"); |
| 54 | buffer_sprintf(out, "%d", code); |
| 55 | buffer_strcat(out, ",\"message\":"); |
| 56 | buffer_append_json_string_value(out, message ? message : ""); |
| 57 | |
| 58 | if (chunk_count >= 1 && chunks && chunks[0].buffer && buffer_strlen(chunks[0].buffer)) { |
| 59 | buffer_strcat(out, ",\"data\":"); |
| 60 | if (chunks[0].type == MCP_RESPONSE_CHUNK_JSON) |
| 61 | buffer_fast_strcat(out, buffer_tostring(chunks[0].buffer), buffer_strlen(chunks[0].buffer)); |
| 62 | else |
| 63 | buffer_append_json_string_value(out, buffer_tostring(chunks[0].buffer)); |
| 64 | } |
| 65 | |
| 66 | buffer_strcat(out, "}}"); |
| 67 | return out; |
| 68 | } |
| 69 | |
| 70 | BUFFER *mcp_jsonrpc_build_success_payload(struct json_object *id_obj, const struct mcp_response_chunk *chunk) { |
| 71 | const char *chunk_text = chunk && chunk->buffer ? buffer_tostring(chunk->buffer) : NULL; |
| 72 | size_t chunk_len = chunk_text ? buffer_strlen(chunk->buffer) : 0; |
| 73 | |
| 74 | BUFFER *out = buffer_create(64 + chunk_len, NULL); |
| 75 | buffer_strcat(out, "{\"jsonrpc\":\"2.0\",\"id\":"); |
| 76 | buffer_append_json_id(out, id_obj); |
| 77 | buffer_strcat(out, ",\"result\":"); |
| 78 | if (chunk_text && chunk_len) |
| 79 | buffer_fast_strcat(out, chunk_text, chunk_len); |
| 80 | else |
| 81 | buffer_strcat(out, "{}"); |
| 82 | buffer_strcat(out, "}"); |
| 83 | return out; |
| 84 | } |
| 85 | |
| 86 | BUFFER *mcp_jsonrpc_process_single_request(MCP_CLIENT *mcpc, struct json_object *request, bool *had_error) { |
| 87 | if (had_error) |
| 88 | *had_error = false; |
| 89 | |
| 90 | if (!mcpc || !request) |
| 91 | return NULL; |
| 92 | |
| 93 | struct json_object *id_obj = NULL; |
| 94 | bool has_id = json_object_is_type(request, json_type_object) && json_object_object_get_ex(request, "id", &id_obj); |
| 95 | |
| 96 | if (!json_object_is_type(request, json_type_object)) |
| 97 | return mcp_jsonrpc_build_error_payload(has_id ? id_obj : NULL, -32600, "Invalid request", NULL, 0); |
| 98 | |
| 99 | struct json_object *jsonrpc_obj = NULL; |
| 100 | if (!json_object_object_get_ex(request, "jsonrpc", &jsonrpc_obj) || |
| 101 | !json_object_is_type(jsonrpc_obj, json_type_string) || |
| 102 | strcmp(json_object_get_string(jsonrpc_obj), "2.0") != 0) { |
| 103 | return mcp_jsonrpc_build_error_payload(has_id ? id_obj : NULL, -32600, "Invalid or missing jsonrpc version", NULL, 0); |
| 104 | } |
| 105 | |
| 106 | struct json_object *method_obj = NULL; |
| 107 | if (!json_object_object_get_ex(request, "method", &method_obj) || |
| 108 | !json_object_is_type(method_obj, json_type_string)) { |
| 109 | return mcp_jsonrpc_build_error_payload(has_id ? id_obj : NULL, -32600, "Missing or invalid method", NULL, 0); |
| 110 | } |
| 111 | const char *method = json_object_get_string(method_obj); |
| 112 | |
| 113 | struct json_object *params_obj = NULL; |
| 114 | bool params_created = false; |
| 115 | if (json_object_object_get_ex(request, "params", ¶ms_obj)) { |
| 116 | if (!json_object_is_type(params_obj, json_type_object)) { |
| 117 | return mcp_jsonrpc_build_error_payload(has_id ? id_obj : NULL, -32602, "Params must be an object", NULL, 0); |
| 118 | } |
| 119 | } else { |
| 120 | params_obj = json_object_new_object(); |
| 121 | params_created = true; |
| 122 | } |
| 123 | |
| 124 | MCP_RETURN_CODE rc = mcp_dispatch_method(mcpc, method, params_obj, has_id ? 1 : 0); |
| 125 | |
| 126 | if (params_created) |
| 127 | json_object_put(params_obj); |
| 128 | |
| 129 | size_t total_bytes = mcp_client_response_size(mcpc); |
| 130 | if (total_bytes > MCP_JSONRPC_RESPONSE_MAX_BYTES) { |
| 131 | BUFFER *payload = mcp_jsonrpc_build_error_payload(has_id ? id_obj : NULL, |
| 132 | -32001, |
| 133 | "Response too large for transport", |
| 134 | NULL, 0); |
| 135 | mcp_client_release_response(mcpc); |
| 136 | mcp_client_clear_error(mcpc); |
| 137 | if (had_error) |
| 138 | *had_error = true; |
| 139 | return payload; |
| 140 | } |
| 141 | |
| 142 | if (!has_id) { |
| 143 | mcp_client_release_response(mcpc); |
| 144 | mcp_client_clear_error(mcpc); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
| 148 | const struct mcp_response_chunk *chunks = mcp_client_response_chunks(mcpc); |
| 149 | size_t chunk_count = mcp_client_response_chunk_count(mcpc); |
| 150 | |
| 151 | BUFFER *payload = NULL; |
| 152 | |
| 153 | if (rc == MCP_RC_OK && !mcpc->last_response_error) { |
| 154 | if (!chunks || chunk_count == 0) { |
| 155 | payload = mcp_jsonrpc_build_error_payload(id_obj, -32603, "Empty response", NULL, 0); |
| 156 | if (had_error) |
| 157 | *had_error = true; |
| 158 | } |
| 159 | else if (chunk_count > 1 || chunks[0].type != MCP_RESPONSE_CHUNK_JSON) { |
| 160 | payload = mcp_jsonrpc_build_error_payload(id_obj, -32002, "Streaming responses not supported on this transport", NULL, 0); |
| 161 | if (had_error) |
| 162 | *had_error = true; |
| 163 | } |
| 164 | else { |
| 165 | payload = mcp_jsonrpc_build_success_payload(id_obj, &chunks[0]); |
| 166 | } |
| 167 | } else { |
| 168 | const char *message = mcp_client_error_message(mcpc); |
| 169 | if (!message) |
| 170 | message = MCP_RETURN_CODE_2str(rc); |
| 171 | payload = mcp_jsonrpc_build_error_payload(id_obj, mcp_jsonrpc_error_code(rc), message, chunks, chunk_count); |
| 172 | if (had_error) |
| 173 | *had_error = true; |
| 174 | } |
| 175 | |
| 176 | mcp_client_release_response(mcpc); |
| 177 | mcp_client_clear_error(mcpc); |
| 178 | return payload; |
| 179 | } |
| 180 | |
| 181 | BUFFER *mcp_jsonrpc_build_batch_response(BUFFER **responses, size_t count) { |
| 182 | if (!responses || count == 0) |
| 183 | return NULL; |
| 184 | |
| 185 | size_t total_len = 2; // [] |
| 186 | for (size_t i = 0; i < count; i++) { |
| 187 | if (!responses[i]) |
| 188 | continue; |
| 189 | total_len += buffer_strlen(responses[i]); |
| 190 | if (i) |
| 191 | total_len += 1; |
| 192 | } |
| 193 | |
| 194 | BUFFER *batch = buffer_create(total_len + 32, NULL); |
| 195 | buffer_strcat(batch, "["); |
| 196 | bool first = true; |
| 197 | for (size_t i = 0; i < count; i++) { |
| 198 | if (!responses[i]) |
| 199 | continue; |
| 200 | if (!first) |
| 201 | buffer_strcat(batch, ","); |
| 202 | first = false; |
| 203 | const char *resp_text = buffer_tostring(responses[i]); |
| 204 | size_t resp_len = buffer_strlen(responses[i]); |
| 205 | buffer_fast_strcat(batch, resp_text, resp_len); |
| 206 | } |
| 207 | buffer_strcat(batch, "]"); |
| 208 | return batch; |
| 209 | } |