Function: claude-code-ide-mcp--handle-message

claude-code-ide-mcp--handle-message is a natively compiled function defined in claude-code-ide-mcp.el.

Signature

(claude-code-ide-mcp--handle-message MESSAGE &optional SESSION WS)

Documentation

Handle incoming JSON-RPC MESSAGE from SESSION.

WS is the websocket the message arrived on; a handler can yield (e.g. sit-for inside executeCode), letting a CLI reconnect swap the session's client mid-request, so the response must go back to WS — the socket that sent the request — or be dropped, never to its successor.

Source Code

;; Defined in /nix/store/yjvbhwl7p6zrbw9xlnni7y4hra8sa3pl-emacs-packages-deps/share/emacs/site-lisp/claude-code-ide-mcp.el
(defun claude-code-ide-mcp--handle-message (message &optional session ws)
  "Handle incoming JSON-RPC MESSAGE from SESSION.
WS is the websocket the message arrived on; a handler can yield (e.g.
`sit-for' inside executeCode), letting a CLI reconnect swap the
session's client mid-request, so the response must go back to WS — the
socket that sent the request — or be dropped, never to its successor."
  (when message
    (claude-code-ide-debug "Processing message with method: %s, id: %s"
                           (alist-get 'method message)
                           (alist-get 'id message))
    (let* ((method (alist-get 'method message))
           (id (alist-get 'id message))
           (params (alist-get 'params message))
           (response
            (cond
             ;; Request handlers
             ((string= method "initialize")
              (claude-code-ide-debug "Handling initialize request")
              (claude-code-ide-mcp--handle-initialize id params session))
             ((string= method "tools/list")
              (claude-code-ide-debug "Handling tools/list request")
              (claude-code-ide-mcp--handle-tools-list id params))
             ((string= method "tools/call")
              (claude-code-ide-debug "Handling tools/call request")
              (claude-code-ide-mcp--handle-tools-call id params session ws))
             ((string= method "prompts/list")
              (claude-code-ide-debug "Handling prompts/list request")
              (claude-code-ide-mcp--handle-prompts-list id params))
             ;; Unknown method
             (id
              (claude-code-ide-debug "Unknown method: %s (sending error response)" method)
              (claude-code-ide-mcp--make-error-response
               id -32601 (format "Method not found: %s" method)))
             ;; Notifications (no id)
             ((string= method "ide_connected")
              (when-let* ((pid (alist-get 'pid params)))
                (claude-code-ide-debug "CLI connected with PID: %s" pid)
                (when session
                  (setf (claude-code-ide-mcp-session-cli-pid session) pid)))
              nil)
             (t
              (claude-code-ide-debug "Received notification (no response needed): %s" method)
              nil))))
      ;; Send response if we have one
      (cond
       ;; We have a response to send
       (response
        (let* ((current-client (and session
                                    (claude-code-ide-mcp-session-client session)))
               ;; If the session's client changed while the handler ran,
               ;; the requester is gone — drop the response
               (client (if (and ws (not (eq ws current-client)))
                           (progn
                             (claude-code-ide-debug
                              "Dropping response for %s (id %s): client replaced mid-request"
                              method id)
                             nil)
                         current-client)))
          (if client
              (let ((response-text (json-encode response)))
                (claude-code-ide-debug "Sending response for method %s (id %s): %s" method id response-text)
                (claude-code-ide-debug "MCP sending response for %s: %s" method response-text)
                (condition-case err
                    (websocket-send-text client response-text)
                  (error
                   (claude-code-ide-debug "Error sending response: %s" err)
                   (claude-code-ide-debug "Error sending MCP response: %s" err))))
            (claude-code-ide-debug "No client connected, cannot send response"))))
       ;; No response but we have an ID (deferred response)
       ((and id (not response))
        (claude-code-ide-debug "No response generated for method %s (id %s) - likely deferred" method id)
        ;; Check if it's stored as deferred in the requesting session
        ;; (ids are keyed as TOOL-NAME or TOOL-NAME-UNIQUE-KEY)
        (let ((tool-name (alist-get 'name params))
              (found nil))
          (when (and tool-name session)
            (maphash (lambda (key _stored-id)
                       (when (or (equal key tool-name)
                                 (string-prefix-p (concat tool-name "-") key))
                         (setq found t)))
                     (claude-code-ide-mcp-session-deferred session))
            (when found
              (claude-code-ide-debug "Confirmed: %s is waiting for deferred response" tool-name)))))
       ;; No response and no ID (notification)
       (t
        (claude-code-ide-debug "No response needed for notification: %s" method))))))