| 3102 | } |
| 3103 | |
| 3104 | static cbm_daemon_runtime_application_status_t application_mcp_request( |
| 3105 | cbm_daemon_application_session_t *session, const uint8_t *request, uint32_t request_length, |
| 3106 | uint8_t **response_out, uint32_t *response_length_out) { |
| 3107 | if (!session->context_set || request_length <= 1) { |
| 3108 | return CBM_DAEMON_RUNTIME_APPLICATION_REJECTED; |
| 3109 | } |
| 3110 | char *message = application_text_copy(request + 1, request_length - 1); |
| 3111 | if (!message) { |
| 3112 | return CBM_DAEMON_RUNTIME_APPLICATION_REJECTED; |
| 3113 | } |
| 3114 | cbm_jsonrpc_request_t parsed = {0}; |
| 3115 | bool parsed_ok = cbm_jsonrpc_parse(message, &parsed) == 0; |
| 3116 | bool initialize_request = |
| 3117 | parsed_ok && parsed.has_id && parsed.method && strcmp(parsed.method, "initialize") == 0; |
| 3118 | bool tool_request = |
| 3119 | parsed_ok && parsed.has_id && parsed.method && strcmp(parsed.method, "tools/call") == 0; |
| 3120 | char *response = cbm_mcp_server_handle(session->mcp, message); |
| 3121 | free(message); |
| 3122 | if (initialize_request && application_jsonrpc_success(response)) { |
| 3123 | cbm_mutex_lock(&session->application->mutex); |
| 3124 | session->pending_background_initialize = true; |
| 3125 | cbm_mutex_unlock(&session->application->mutex); |
| 3126 | } else if (tool_request && response) { |
| 3127 | application_update_notice_inject(session, &response); |
| 3128 | } |
| 3129 | if (response) { |
| 3130 | size_t response_length = strlen(response); |
| 3131 | if (response_length > UINT32_MAX) { |
| 3132 | if (parsed_ok) { |
| 3133 | cbm_jsonrpc_request_free(&parsed); |
| 3134 | } |
| 3135 | free(response); |
| 3136 | return CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR; |
| 3137 | } |
| 3138 | /* A reply larger than one frame has to become a JSON-RPC error HERE, |
| 3139 | * while the request id is still in hand. Handing it to the transport |
| 3140 | * instead is indistinguishable from a dead socket at the frontend |
| 3141 | * worker, which responds by _Exit()ing the process — right for a broken |
| 3142 | * transport, a fatal over-reaction to a large answer. Under an MCP host |
| 3143 | * that does not respawn the server, that took every tool on it out for |
| 3144 | * the rest of the session, leaving exit=1 and an empty stderr to debug |
| 3145 | * from (#1375). */ |
| 3146 | response = application_framable_response(response, parsed_ok ? &parsed : NULL); |
| 3147 | if (!response) { |
| 3148 | if (parsed_ok) { |
| 3149 | cbm_jsonrpc_request_free(&parsed); |
| 3150 | } |
| 3151 | return CBM_DAEMON_RUNTIME_APPLICATION_HANDLER_ERROR; |
| 3152 | } |
| 3153 | response_length = strlen(response); |
| 3154 | *response_out = (uint8_t *)response; |
| 3155 | *response_length_out = (uint32_t)response_length; |
| 3156 | } |
| 3157 | if (parsed_ok) { |
| 3158 | cbm_jsonrpc_request_free(&parsed); |
| 3159 | } |
| 3160 | application_refresh_watch(session); |
| 3161 | return CBM_DAEMON_RUNTIME_APPLICATION_OK; |
no test coverage detected