Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ Set the backend to =claude-code=, =codex=, =github-copilot-cli=, =pi=, or =antig

Auto-wiring is currently enabled for =claude-code=, =codex=, =github-copilot-cli=, =open-interpreter=, =antigravity=, and =pi=.

Pi sessions load a per-launch temporary extension (=--extension=) that calls =pi.registerMcpServer= with the session token read from the child process environment. Each Pi instance gets its own token, session, and extension file, so several Pi sessions can use the Emacs tools independently. This requires a Pi build with built-in MCP support.
Pi sessions load a per-launch temporary extension (=--extension=) that calls =pi.registerMcpServer= with the session token read from the child process environment. Each Pi instance gets its own token, session, and extension file, so several Pi sessions can use the Emacs tools independently. By default, =ai-code-mcp-agent-pi-exposure= uses ="codemode"= (which automatically enables Pi's =codemode= script runner for parallel tool batching and keeps large tool schemas out of every turn) while =ai-code-mcp-agent-pi-tool-exposure= keeps =diagnostics_baseline=, =get_diagnostics=, and =get_org_heading_context= directly declared; customize either option if you prefer full ="direct"= exposure. This requires a Pi build with built-in MCP support.

Antigravity does not expand environment variables in MCP headers, so each Antigravity launch creates a private per-session temporary directory containing =.agents/mcp_config.json= (mode =0700=/=0600=) and passes it via =--add-dir=. Project workspaces stay untouched, multiple concurrent Antigravity sessions stay isolated with independent tokens and contexts, and temporary directories are removed on session exit or Emacs shutdown.

Expand Down Expand Up @@ -790,9 +790,44 @@ with =C-c a H=. Use its references to revisit the original evidence.
scroll, search, and number it; Pi's default fullscreen mode draws on the
alternate screen, leaving only the visible rows in the session buffer.
Customize =ai-code-pi-tui-mode= to =fullscreen=, or to =nil= to keep Pi's
own setting. In Pi session buffers, Shift+Enter and
Ctrl+Enter insert a newline, and Ctrl+Escape sends Escape to cancel the
current operation.
own setting. Customize =ai-code-pi-project-trust= to =approve= or
=no-approve= if you want AI Code to pass =--approve= or =--no-approve=
instead of using Pi's saved project-trust prompt.

In Pi session buffers, Shift+Enter and Ctrl+Enter insert a newline,
Alt+Enter (=M-<return>=) submits immediately when idle or queues a
follow-up while Pi is busy, and Ctrl+Escape sends Escape to cancel the
current operation. From any project buffer or Pi session, run
=M-x ai-code-pi-compose= to open Pi's external editor in an Emacs
viewport (=ai-code-editor-viewport=) without =C-g= conflicting with
=keyboard-quit= (leave =externalEditor= unset in
=~/.pi/agent/settings.json= so Pi uses AI Code's =VISUAL=/=EDITOR=
bridge). Use =M-x ai-code-pi-send-follow-up= to queue a follow-up from
Emacs (or customize =ai-code-pi-send-while-busy= to =followup= to make
=ai-code-pi-send-command= queue follow-ups instead of steering), and
=M-x ai-code-pi-slash-command= to send slash commands such as =/model=,
=/thinking=, =/tree=, or =/compact=.

*Recommended manual Gemini setup for Pi:* AI Code never overrides your Pi
model or credentials automatically. To use Gemini with Pi, export
=GEMINI_API_KEY= (Google AI Studio) or configure Vertex AI
(=GOOGLE_CLOUD_PROJECT=, =GOOGLE_CLOUD_LOCATION=, and Application Default
Credentials), and optionally configure =~/.pi/agent/settings.json= so
=Ctrl+P= cycles cleanly between Pro and Flash with Vertex-safe thinking
levels (avoiding =minimal= on Vertex endpoints):

#+begin_src json
{
"defaultProvider": "google",
"defaultModel": "gemini-3.1-pro-preview-customtools",
"defaultThinkingLevel": "high",
"enabledModels": [
"google/gemini-3.1-pro-preview-customtools:high",
"google/gemini-3-flash-preview:medium",
"google/gemini-2.5-pro:high"
]
}
#+end_src

**** Grok CLI setup
Install [[https://grokcli.io/][grok-cli]] and ensure the `grok` executable is on your PATH.
Expand Down
10 changes: 8 additions & 2 deletions ai-code-backends-infra.el
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,8 @@ session buffer.
:env-vars is an optional list of environment variable strings.
:multiline-input-sequence is an optional terminal sequence for
multiline input.
:post-start-fn is an optional function called with (BUFFER PROCESS
INSTANCE-NAME) after session creation.
:prepare-launch is an optional function called with (WORKING-DIR ARGV)
only when a new session is being created.
When :prepare-launch is present, it may return :argv, :env-vars,
Expand Down Expand Up @@ -1860,7 +1862,7 @@ A legacy :command entry is normalized to argv for compatibility."
arg
(plist-get options :env-vars)
(plist-get options :multiline-input-sequence)
nil))))
(plist-get options :post-start-fn)))))

(defun ai-code-backends-infra--last-accessed-session-buffer (session-prefix)
"Return the last accessed buffer when it belongs to SESSION-PREFIX."
Expand Down Expand Up @@ -2108,6 +2110,9 @@ When PREFIX and WORKING-DIR are provided, select from multiple sessions."
(select-window window))
(ai-code-backends-infra--display-buffer-in-side-window buffer))))

(defvar ai-code-backends-infra--submit-return-function nil
"Optional function called instead of Return when submitting a prompt.")

(defun ai-code-backends-infra--submit-prompt (buffer prompt &optional source-buffer)
"Submit PROMPT to the already chosen terminal BUFFER.
Refresh MCP context from SOURCE-BUFFER, defaulting to the current buffer,
Expand All @@ -2127,7 +2132,8 @@ Return result. This does not select a target or change window display."
(ai-code-backends-infra--terminal-send-string prompt))
;; Shorter delays can lose submissions in some CLI backends.
(sit-for 0.5)
(ai-code-backends-infra--terminal-send-return))
(funcall (or ai-code-backends-infra--submit-return-function
#'ai-code-backends-infra--terminal-send-return)))
(ai-code-session-note-prompt buffer prompt)
(setq ai-code-backends-infra--last-accessed-buffer buffer)))

Expand Down
69 changes: 67 additions & 2 deletions ai-code-mcp-agent.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ agent launch renews access."
:type 'positive-integer
:group 'ai-code-mcp-agent)

(defconst ai-code-mcp-agent--pi-valid-exposures
'("codemode" "deferred" "direct" "hidden")
"Exposure strings accepted by Pi's `registerMcpServer'.")

(defcustom ai-code-mcp-agent-pi-exposure "codemode"
"Default tool exposure passed to `pi.registerMcpServer' for Emacs MCP tools.
\"codemode\" keeps most Emacs MCP schemas out of the direct tool list while
automatically enabling Pi's `codemode' script runner for parallel calls."
:type '(choice (const :tag "Codemode (script-callable)" "codemode")
(const :tag "Deferred (tool_search)" "deferred")
(const :tag "Direct (all tools declared)" "direct")
(const :tag "Hidden" "hidden"))
:group 'ai-code-mcp-agent)

(defcustom ai-code-mcp-agent-pi-tool-exposure
'(("diagnostics_baseline" . "direct")
("get_diagnostics" . "direct")
("get_org_heading_context" . "direct"))
"Per-tool exposure overrides passed as `toolExposure' for Pi MCP sessions.
Each entry is `(TOOL-OR-PATTERN . EXPOSURE)', where EXPOSURE is one of
\"codemode\", \"deferred\", \"direct\", or \"hidden\". When nil, omit
`toolExposure' from the generated Pi extension."
:type '(alist :key-type string
:value-type (choice (const "codemode")
(const "deferred")
(const "direct")
(const "hidden")))
:group 'ai-code-mcp-agent)

(defconst ai-code-mcp-agent--server-name "emacs_tools"
"Server name used in backend MCP config overrides.")

Expand Down Expand Up @@ -403,14 +432,49 @@ SERVER-WAS-LIVE records whether this launch started the shared HTTP server."
token-env-var))))))))))))
config-file))

(defun ai-code-mcp-agent--validate-pi-exposure (exposure)
"Return EXPOSURE when Pi accepts it, or signal an error."
(unless (and (stringp exposure)
(member exposure ai-code-mcp-agent--pi-valid-exposures))
(error "Unsupported Pi MCP exposure: %S" exposure))
exposure)

(defun ai-code-mcp-agent--pi-tool-exposure-line (entries)
"Return the `toolExposure' property line for ENTRIES, or empty string."
(unless (listp entries)
(error "Invalid Pi MCP tool exposure list: %S" entries))
(if (null entries)
""
(let ((pairs
(mapcar
(lambda (entry)
(unless (and (consp entry)
(stringp (car entry))
(not (string-empty-p (car entry))))
(error "Invalid Pi MCP tool exposure entry: %S" entry))
(format "%s: %s"
(json-encode (car entry))
(json-encode
(ai-code-mcp-agent--validate-pi-exposure (cdr entry)))))
entries)))
(format " toolExposure: { %s },\n"
(mapconcat #'identity pairs ", ")))))

(defun ai-code-mcp-agent--pi-extension-source (url token-env-var)
"Return Pi extension source registering the MCP server at URL.
The extension reads the bearer token from TOKEN-ENV-VAR at runtime, so the
secret never touches disk. Each Pi process gets its own extension file and
token environment, which keeps concurrent Pi sessions independent."
(let ((server-name (json-encode ai-code-mcp-agent--server-name))
(url-json (json-encode url))
(env-json (json-encode token-env-var)))
(env-json (json-encode token-env-var))
(exposure-json
(json-encode
(ai-code-mcp-agent--validate-pi-exposure
ai-code-mcp-agent-pi-exposure)))
(tool-exposure-line
(ai-code-mcp-agent--pi-tool-exposure-line
ai-code-mcp-agent-pi-tool-exposure)))
(concat
"// Generated by ai-code-mcp-agent.el for one Pi session.\n"
"export default function (pi) {\n"
Expand All @@ -425,7 +489,8 @@ token environment, which keeps concurrent Pi sessions independent."
(format " pi.registerMcpServer(%s, {\n" server-name)
(format " url: %s,\n" url-json)
" headers: { Authorization: `Bearer ${token}` },\n"
" exposure: \"direct\",\n"
(format " exposure: %s,\n" exposure-json)
tool-exposure-line
" description: \"Live Emacs editor, project, and debugging tools\"\n"
" });\n"
"}\n")))
Expand Down
Loading
Loading