Function: mcp--send-initial-message

mcp--send-initial-message is a natively compiled function defined in mcp.el.

Signature

(mcp--send-initial-message CONNECTION &optional CHECK-SSE SYNCP)

Documentation

Send initialization message to MCP server CONNECTION.

This function sends the initial handshake message to establish communication with the MCP server. It is called internally during server connection setup.

CONNECTION is the MCP connection object representing the server connection. CHECK-SSE is an optional boolean flag indicating whether to verify is SSE mcp server before sending. SYNCP specifies if the operation should be synchronous or asynchronous.

Source Code

;; Defined in /nix/store/5m747x13pb1ddrvbq9raj91c4q1h1kl0-emacs-packages-deps/share/emacs/site-lisp/elpa/mcp-20260615.940/mcp.el
(defun mcp--send-initial-message (connection &optional check-sse syncp)
  "Send initialization message to MCP server CONNECTION.

This function sends the initial handshake message to establish communication
with the MCP server. It is called internally during server connection setup.

CONNECTION is the MCP connection object representing the server connection.
CHECK-SSE is an optional boolean flag indicating whether to verify is SSE
mcp server before sending.
SYNCP specifies if the operation should be synchronous or asynchronous."
  (let* ((init-fn (if syncp #'mcp-sync-initialize-message #'mcp-async-initialize-message))
         (callback-fn (lambda (protocolVersion serverInfo capabilities)
                        (if (cl-find protocolVersion mcp--support-versions :test #'string=)
                            (progn
                              (message "[mcp] Connected! Server `MCP (%s)' now managing." (jsonrpc-name connection))
                              (setf (mcp--capabilities connection) capabilities
                                    (mcp--server-info connection) serverInfo)
                              ;; Notify server initialized
                              (mcp-notify connection
                                          :notifications/initialized)
                              (when (mcp--initial-callback connection)
                                (funcall (mcp--initial-callback connection) connection))
                              (let* ((capability-handlers
                                      (lambda ()
                                        ;; Handle logging
                                        (when (plist-member capabilities :logging)
                                          (funcall (if syncp #'mcp-sync-set-log-level #'mcp-async-set-log-level) connection mcp-log-level))
                                        ;; Get prompts
                                        (when (plist-member capabilities :prompts)
                                          (funcall (if syncp #'mcp-sync-list-prompts #'mcp-async-list-prompts) connection (mcp--prompts-callback connection)))
                                        ;; Get tools
                                        (when (plist-member capabilities :tools)
                                          (funcall (if syncp #'mcp-sync-list-tools #'mcp-async-list-tools) connection (mcp--tools-callback connection)))
                                        ;; Get resources
                                        (when (plist-member capabilities :resources)
                                          (funcall (if syncp #'mcp-sync-list-resources #'mcp-async-list-resources) connection (mcp--resources-callback connection)))
                                        ;; Get templace resources
                                        (when (plist-member capabilities :resources)
                                          (funcall (if syncp #'mcp-sync-list-resource-templates #'mcp-async-list-resource-templates) connection (mcp--resources-templates-callback connection))))))
                                (if (not syncp)
                                    (run-with-idle-timer mcp-server-wait-initial-time
                                                         nil
                                                         capability-handlers)
                                  (sit-for mcp-server-wait-initial-time)
                                  ;; REVIEW 2025-07-17: This needs to happen here,
                                  ;;   because the following callbacks might rely
                                  ;;   on the connection status.
                                  (setf (mcp--status connection) 'connected)
                                  (funcall capability-handlers)))

                              (setf (mcp--status connection) 'connected))
                          (progn
                            (message "[mcp] Error %s server protocolVersion(%s) not support, client Version: %s."
                                     (jsonrpc-name connection)
                                     protocolVersion
                                     mcp--support-versions)
                            (mcp-stop-server (jsonrpc-name connection))))))
         (error-callback-fn (lambda (code message)
                              (mcp-stop-server (jsonrpc-name connection))
                              (setf (mcp--status connection) 'error)
                              (when (mcp--error-callback connection)
                                (funcall (mcp--error-callback connection) code message))
                              (message "Sadly, %s mpc server reports %s: %s"
                                       (jsonrpc-name connection) code message)))
         (args `(,connection ,check-sse ,callback-fn ,error-callback-fn)))
    (apply init-fn args)))