|
| 1 | +;;; ai-code-mcp-agent.el --- Agent adapters for ai-code MCP tools -*- lexical-binding: t; -*- |
| 2 | + |
| 3 | +;; Author: Kang Tu <tninja@gmail.com> |
| 4 | +;; SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +;;; Commentary: |
| 7 | +;; Backend-facing helpers that expose Emacs MCP tools to AI agents. |
| 8 | + |
| 9 | +;;; Code: |
| 10 | + |
| 11 | +(require 'json) |
| 12 | + |
| 13 | +(require 'ai-code-mcp-http-server) |
| 14 | +(require 'ai-code-mcp-server) |
| 15 | + |
| 16 | +(defgroup ai-code-mcp-agent nil |
| 17 | + "Agent adapters for AI Code MCP tools." |
| 18 | + :group 'tools |
| 19 | + :prefix "ai-code-mcp-agent-") |
| 20 | + |
| 21 | +(defcustom ai-code-mcp-agent-enabled-backends '(codex github-copilot-cli) |
| 22 | + "Backends that should receive automatic Emacs MCP integration." |
| 23 | + :type '(repeat symbol) |
| 24 | + :group 'ai-code-mcp-agent) |
| 25 | + |
| 26 | +(defconst ai-code-mcp-agent--server-name "emacs_tools" |
| 27 | + "Server name used in backend MCP config overrides.") |
| 28 | + |
| 29 | +(defconst ai-code-mcp-agent--status-buffer-name "*AI Code MCP Status*" |
| 30 | + "Buffer name used to display MCP status to users.") |
| 31 | + |
| 32 | +(defvar-local ai-code-mcp-agent--backend nil |
| 33 | + "Backend symbol attached to the current agent buffer.") |
| 34 | + |
| 35 | +(defvar-local ai-code-mcp-agent--session-id nil |
| 36 | + "Session id attached to the current agent buffer.") |
| 37 | + |
| 38 | +(defvar-local ai-code-mcp-agent--server-url nil |
| 39 | + "MCP server URL attached to the current agent buffer.") |
| 40 | + |
| 41 | +(defun ai-code-mcp-agent-buffer-status (&optional buffer) |
| 42 | + "Return MCP status for BUFFER or the current buffer." |
| 43 | + (with-current-buffer (or buffer (current-buffer)) |
| 44 | + (when ai-code-mcp-agent--session-id |
| 45 | + (list :backend ai-code-mcp-agent--backend |
| 46 | + :session-id ai-code-mcp-agent--session-id |
| 47 | + :server-url ai-code-mcp-agent--server-url)))) |
| 48 | + |
| 49 | +;;;###autoload |
| 50 | +(defun ai-code-mcp-agent-show-buffer-status (&optional buffer) |
| 51 | + "Display MCP status for BUFFER or the current buffer." |
| 52 | + (interactive) |
| 53 | + (let ((status (ai-code-mcp-agent-buffer-status buffer))) |
| 54 | + (if (not status) |
| 55 | + (message "No MCP session is attached to the current buffer.") |
| 56 | + (with-help-window ai-code-mcp-agent--status-buffer-name |
| 57 | + (princ (ai-code-mcp-agent--format-status status)))) |
| 58 | + status)) |
| 59 | + |
| 60 | +(defun ai-code-mcp-agent--format-status (status) |
| 61 | + "Return a display string for MCP STATUS." |
| 62 | + (concat |
| 63 | + "AI Code MCP Status\n\n" |
| 64 | + (format "Backend: %s\n" (plist-get status :backend)) |
| 65 | + (format "Session ID: %s\n" (plist-get status :session-id)) |
| 66 | + (format "Server URL: %s\n" (plist-get status :server-url)))) |
| 67 | + |
| 68 | +(defun ai-code-mcp-agent-prepare-launch (backend working-dir command) |
| 69 | + "Return MCP launch metadata for BACKEND, WORKING-DIR, and COMMAND." |
| 70 | + (when (memq backend ai-code-mcp-agent-enabled-backends) |
| 71 | + (ai-code-mcp-builtins-setup) |
| 72 | + (let* ((port (ai-code-mcp-http-server-ensure)) |
| 73 | + (session-id (ai-code-mcp-agent--make-session-id backend)) |
| 74 | + (url (ai-code-mcp-agent--make-server-url port session-id))) |
| 75 | + (list :command (ai-code-mcp-agent--inject-command backend command url) |
| 76 | + :cleanup-fn (lambda () |
| 77 | + (ai-code-mcp-unregister-session session-id)) |
| 78 | + :post-start-fn (lambda (buffer _process _instance-name) |
| 79 | + (ai-code-mcp-agent--record-buffer-session |
| 80 | + buffer backend session-id working-dir url)))))) |
| 81 | + |
| 82 | +(defun ai-code-mcp-agent--make-session-id (backend) |
| 83 | + "Create a fresh session id for BACKEND." |
| 84 | + (format "%s-%s-%d" |
| 85 | + (symbol-name backend) |
| 86 | + (format-time-string "%Y%m%d%H%M%S") |
| 87 | + (random 1000000))) |
| 88 | + |
| 89 | +(defun ai-code-mcp-agent--make-server-url (port session-id) |
| 90 | + "Build an MCP server URL from PORT and SESSION-ID." |
| 91 | + (format "http://127.0.0.1:%d/mcp/%s" port session-id)) |
| 92 | + |
| 93 | +(defun ai-code-mcp-agent--record-buffer-session (buffer backend session-id working-dir url) |
| 94 | + "Record session metadata for BUFFER." |
| 95 | + (ai-code-mcp-register-session session-id working-dir buffer) |
| 96 | + (with-current-buffer buffer |
| 97 | + (setq-local ai-code-mcp-agent--backend backend |
| 98 | + ai-code-mcp-agent--session-id session-id |
| 99 | + ai-code-mcp-agent--server-url url))) |
| 100 | + |
| 101 | +(defun ai-code-mcp-agent--inject-command (backend command url) |
| 102 | + "Inject backend-specific MCP config into COMMAND for URL." |
| 103 | + (pcase backend |
| 104 | + ('codex |
| 105 | + (concat command |
| 106 | + " -c " |
| 107 | + (shell-quote-argument |
| 108 | + (format "mcp_servers.%s={ url = %s }" |
| 109 | + ai-code-mcp-agent--server-name |
| 110 | + (json-encode url))))) |
| 111 | + ('github-copilot-cli |
| 112 | + (concat command |
| 113 | + " --additional-mcp-config " |
| 114 | + (shell-quote-argument |
| 115 | + (ai-code-mcp-agent--copilot-config-json url)))) |
| 116 | + (_ command))) |
| 117 | + |
| 118 | +(defun ai-code-mcp-agent--copilot-config-json (url) |
| 119 | + "Return a Copilot CLI MCP config JSON string for URL." |
| 120 | + (json-encode |
| 121 | + `((mcpServers |
| 122 | + . ((,ai-code-mcp-agent--server-name |
| 123 | + . ((type . "http") |
| 124 | + (url . ,url)))))))) |
| 125 | + |
| 126 | +(provide 'ai-code-mcp-agent) |
| 127 | + |
| 128 | +;;; ai-code-mcp-agent.el ends here |
0 commit comments