3030 :type 'boolean
3131 :group 'ai-code-mcp-debug-tools )
3232
33+ (defvar ai-code-mcp-debug-tools--session-overrides (make-hash-table :test 'equal )
34+ " Hash table of session-local debug tool overrides keyed by MCP session id." )
35+
36+ (defvar ai-code-mcp--current-session-id nil
37+ " Dynamically bound MCP session id for the current tool invocation." )
38+
3339(defvar ai-code-mcp--last-error-record nil
3440 " Most recent Emacs error snapshot recorded for MCP diagnostics tools." )
3541
107113 :optional t )))
108114 " Optional MCP eval tool specification." )
109115
116+ (defun ai-code-mcp-debug-tools--register-base-tools ()
117+ " Register the standard MCP debugging tools."
118+ (dolist (tool ai-code-mcp-debug-tools--specs)
119+ (apply #'ai-code-mcp-make-tool tool)))
120+
121+ (defun ai-code-mcp-debug-tools--register-eval-tool ()
122+ " Register the optional `eval_elisp' MCP tool."
123+ (apply #'ai-code-mcp-make-tool ai-code-mcp-debug-tools--eval-spec))
124+
125+ (defun ai-code-mcp-debug-tools--session-override (&optional session-id )
126+ " Return session-local override for SESSION-ID or the active MCP session."
127+ (gethash (or session-id ai-code-mcp--current-session-id)
128+ ai-code-mcp-debug-tools--session-overrides))
129+
130+ (defun ai-code-mcp-debug-tools--enabled-p ()
131+ " Return non-nil when debug tools are enabled for the active session."
132+ (or ai-code-mcp-debug-tools-enabled
133+ (alist-get 'enabled
134+ (ai-code-mcp-debug-tools--session-override))))
135+
136+ (defun ai-code-mcp-debug-tools--eval-enabled-p ()
137+ " Return non-nil when `eval_elisp' is enabled for the active session."
138+ (or ai-code-mcp-debug-tools-enable-eval-elisp
139+ (alist-get 'enable_eval_elisp
140+ (ai-code-mcp-debug-tools--session-override))))
141+
142+ (defun ai-code-mcp-debug-tools--require-enabled ()
143+ " Signal an error unless debug inspection tools are enabled."
144+ (unless (ai-code-mcp-debug-tools--enabled-p)
145+ (error " Emacs debug MCP tools are disabled for this session " )))
146+
147+ (defun ai-code-mcp-debug-tools--require-eval-enabled ()
148+ " Signal an error unless `eval_elisp' is enabled."
149+ (unless (ai-code-mcp-debug-tools--eval-enabled-p)
150+ (error " The eval_elisp tool is disabled for this session " )))
151+
110152(defun ai-code-mcp-debug-tools-setup ()
111153 " Register optional MCP debugging tools when enabled."
112154 (when ai-code-mcp-debug-tools-enabled
113155 (ai-code-mcp--ensure-error-capture)
114- (dolist (tool ai-code-mcp-debug-tools--specs)
115- (apply #'ai-code-mcp-make-tool tool))
156+ (ai-code-mcp-debug-tools--register-base-tools)
116157 (when ai-code-mcp-debug-tools-enable-eval-elisp
117- (apply #'ai-code-mcp-make-tool ai-code-mcp-debug-tools--eval-spec))))
158+ (ai-code-mcp-debug-tools--register-eval-tool))))
159+
160+ ;;;### autoload
161+ (defun ai-code-mcp-debug-tools-enable-for-session
162+ (session-id &optional enable-eval-elisp)
163+ " Enable MCP debug tools for SESSION-ID.
164+ When ENABLE-EVAL-ELISP is non-nil, also expose `eval_elisp' for that
165+ session."
166+ (unless (and (stringp session-id)
167+ (not (string-empty-p session-id)))
168+ (user-error " SESSION-ID is required to enable Emacs debug MCP tools" ))
169+ (puthash session-id
170+ `((enabled . t )
171+ (enable_eval_elisp . ,(and enable-eval-elisp t )))
172+ ai-code-mcp-debug-tools--session-overrides)
173+ (ai-code-mcp--ensure-error-capture)
174+ (ai-code-mcp-debug-tools--register-base-tools)
175+ (when enable-eval-elisp
176+ (ai-code-mcp-debug-tools--register-eval-tool))
177+ session-id)
118178
119179(defun ai-code-mcp--documentation-summary (documentation )
120180 " Return a trimmed summary line for DOCUMENTATION."
@@ -343,6 +403,7 @@ keeps the backtrace on failures."
343403
344404(defun ai-code-mcp-get-variable-binding-info (variable-name &optional buffer-name )
345405 " Return JSON binding details for VARIABLE-NAME in BUFFER-NAME."
406+ (ai-code-mcp-debug-tools--require-enabled)
346407 (let ((symbol (ai-code-mcp--find-existing-variable-symbol variable-name)))
347408 (if (not symbol)
348409 (json-encode
@@ -380,6 +441,7 @@ keeps the backtrace on failures."
380441 " Return the printed representation of VARIABLE-NAME.
381442Return a friendly error string when VARIABLE-NAME does not name an
382443existing bound variable."
444+ (ai-code-mcp-debug-tools--require-enabled)
383445 (let ((symbol (ai-code-mcp--find-existing-variable-symbol variable-name)))
384446 (cond
385447 ((not symbol)
@@ -429,6 +491,7 @@ existing bound variable."
429491
430492(defun ai-code-mcp-get-function-info (function-name )
431493 " Return JSON metadata describing FUNCTION-NAME."
494+ (ai-code-mcp-debug-tools--require-enabled)
432495 (let ((symbol (ai-code-mcp--find-existing-function-symbol function-name)))
433496 (if (not (and symbol (fboundp symbol)))
434497 (json-encode
@@ -480,6 +543,7 @@ existing bound variable."
480543
481544(defun ai-code-mcp-get-last-error-backtrace ()
482545 " Return a JSON snapshot of the most recently recorded Emacs error."
546+ (ai-code-mcp-debug-tools--require-enabled)
483547 (json-encode
484548 (if ai-code-mcp--last-error-record
485549 (ai-code-mcp--last-error-json-payload ai-code-mcp--last-error-record)
@@ -520,6 +584,7 @@ existing bound variable."
520584
521585(defun ai-code-mcp-get-feature-load-state (feature-name )
522586 " Return JSON load-state details for FEATURE-NAME."
587+ (ai-code-mcp-debug-tools--require-enabled)
523588 (if (not (ai-code-mcp--valid-feature-name-p feature-name))
524589 (json-encode
525590 (ai-code-mcp--invalid-feature-load-state-payload feature-name))
@@ -539,6 +604,7 @@ existing bound variable."
539604
540605(defun ai-code-mcp-get-recent-messages (&optional limit )
541606 " Return a JSON payload for recent messages using LIMIT."
607+ (ai-code-mcp-debug-tools--require-enabled)
542608 (let* ((limit (or limit 50 ))
543609 (messages (ai-code-mcp--message-lines)))
544610 (unless (and (integerp limit) (> limit 0 ))
@@ -556,6 +622,8 @@ existing bound variable."
556622Return a JSON payload. BUFFER-NAME or FILE-PATH select the evaluation
557623context. CAPTURE-MESSAGES, INCLUDE-BACKTRACE, and TIMEOUT-MS control
558624diagnostics."
625+ (ai-code-mcp-debug-tools--require-enabled)
626+ (ai-code-mcp-debug-tools--require-eval-enabled)
559627 (let* ((capture-messages (ai-code-mcp-debug-tools--bool-arg
560628 capture-messages
561629 t ))
0 commit comments