Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
5fa0e4c
Add MCP server config writers for coding agents
simonfaltum Nov 12, 2026
f367c1f
Edit agent MCP configs in place
simonfaltum Nov 12, 2026
0d8f9ed
Keep user keys and fix JSON layout in agent MCP writers
simonfaltum Nov 12, 2026
f580b77
Fix agent MCP config writer review findings
simonfaltum Nov 12, 2026
ad58694
Fix agent MCP config writer review findings (pass 4)
simonfaltum Nov 12, 2026
f178f46
Drop an internal spec reference from a code comment
simonfaltum Nov 12, 2026
9057947
Drop containers emptied by removing the last agent MCP server
simonfaltum Nov 16, 2026
5f1e1fe
Refuse to write agent MCP configs that the user made read-only
simonfaltum Nov 16, 2026
0869ad3
Keep env tables when a pin is removed and check env writes
simonfaltum Nov 16, 2026
4e9655d
Ignore array elements when finding TOML table headers
simonfaltum Nov 16, 2026
fe50ff0
Wrap the underlying errors in the agent MCP writer
simonfaltum Nov 16, 2026
920f030
Report the first unexpected env key in a fixed order
simonfaltum Nov 16, 2026
d19a0d2
Define the MCP gateway path prefix in mcpconfig
simonfaltum Nov 17, 2026
3f8cf21
Check JSON agent config edits after writing them
simonfaltum Nov 17, 2026
7101f11
Name the server in JSON edit refusals and wrap the read-only error
simonfaltum Nov 17, 2026
7c4d5c3
Refuse a read-only agent config with the ErrReadOnly sentinel
simonfaltum Nov 17, 2026
1f2656d
Check only the edited part of a JSON agent config edit
simonfaltum Nov 17, 2026
2cf24e6
Hold Claude Code's config lock while editing its MCP entry
simonfaltum Nov 19, 2026
221de51
Refuse an OpenCode config that still holds V1 servers
simonfaltum Nov 19, 2026
6ef0082
Write Claude MCP servers to the legacy config Claude Code reads
simonfaltum Nov 19, 2026
244c03f
Name Claude Code's config lock after the path as given, and retry a l…
simonfaltum Nov 19, 2026
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
256 changes: 256 additions & 0 deletions libs/aitools/agents/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package agents

import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"

"github.com/databricks/cli/libs/aitools/mcpconfig"
"github.com/databricks/cli/libs/env"
)

Expand Down Expand Up @@ -56,6 +60,9 @@ type Agent struct {
// Plugin describes the databricks plugin for this agent, or nil when the
// agent has no plugin and skills files are its native delivery.
Plugin *PluginSpec
// MCP describes where the agent keeps user-scope MCP server entries, or nil
// when this CLI does not write them (Pi, Goose, Kiro).
MCP *mcpconfig.Spec
// pluginInstalls reads the databricks plugin's install records from the
// agent's own plugin manifest. Each agent's manifest format differs, so it is
// set per agent (only for formats we have verified); when nil,
Expand Down Expand Up @@ -137,6 +144,16 @@ const (
NameKiro = "kiro"
)

// MCP entry keys shared by the agents' MCP config specs.
const (
// mcpServersKey is the key of the server table in the JSON MCP config files
// that use it (every agent but Codex and OpenCode).
mcpServersKey = "mcpServers"
// localServerType is the "type" of a local stdio server in Copilot and
// OpenCode. Claude Code writes "stdio" instead.
localServerType = "local"
)

// Databricks plugin identity, shared across the agents that ship a plugin.
// The verified install commands are e.g.
//
Expand Down Expand Up @@ -182,6 +199,13 @@ var Registry = []*Agent{
Binary: "claude",
Plugin: claudePlugin(),
pluginInstalls: claudePluginInstalls,
MCP: &mcpconfig.Spec{
Path: claudeMCPPath,
Format: mcpconfig.FormatJSON,
Root: mcpServersKey,
Entry: claudeMCPEntry,
Lock: true,
},
},
{
Name: NameCursor,
Expand All @@ -193,13 +217,26 @@ var Registry = []*Agent{
// Cursor has a databricks plugin, but it can't be installed headlessly, so
// the CLI treats Cursor as a skills-only agent (Plugin nil) rather than
// referencing a plugin it can't act on.
MCP: &mcpconfig.Spec{
// Cursor's global MCP file. See https://cursor.com/docs/context/mcp.
Path: homeSubdir(".cursor", "mcp.json"),
Format: mcpconfig.FormatJSON,
Root: mcpServersKey,
Entry: stdioEntry,
},
},
{
Name: NameCodex,
DisplayName: "Codex CLI",
ConfigDir: homeSubdir(".codex"),
Binary: "codex",
Plugin: databricksPlugin(),
MCP: &mcpconfig.Spec{
Path: codexMCPPath,
Format: mcpconfig.FormatTOML,
Root: "mcp_servers",
Entry: stdioEntry,
},
},
{
Name: NameOpenCode,
Expand All @@ -209,20 +246,41 @@ var Registry = []*Agent{
// OpenCode exposes an `opencode plugin <module>` command, but that's an
// npm-module system, not the agent-skills marketplace, so OpenCode stays
// skills-only (Plugin nil).
MCP: &mcpconfig.Spec{
Path: openCodeMCPPath,
Format: mcpconfig.FormatJSON,
Root: "mcp.servers",
Entry: openCodeMCPEntry,
// OpenCode's V1 layout kept servers directly under mcp. See
// https://opencode.ai/docs/migrate-v1/.
Legacy: true,
},
},
{
Name: NameCopilot,
DisplayName: "GitHub Copilot",
ConfigDir: homeSubdir(".copilot"),
Binary: "copilot",
Plugin: databricksPlugin(),
MCP: &mcpconfig.Spec{
Path: copilotMCPPath,
Format: mcpconfig.FormatJSON,
Root: mcpServersKey,
Entry: copilotMCPEntry,
},
},
{
Name: NameAntigravity,
DisplayName: "Antigravity",
ConfigDir: homeSubdir(".gemini", "antigravity"),
SkillsSubdir: "global_skills",
// Antigravity is IDE-only with no CLI binary, so it has no plugin path.
MCP: &mcpconfig.Spec{
Path: antigravityMCPPath,
Format: mcpconfig.FormatJSON,
Root: mcpServersKey,
Entry: stdioEntry,
},
},
{
Name: NamePi,
Expand All @@ -246,6 +304,12 @@ var Registry = []*Agent{
// Gemini writes projects.json after real use. Antigravity shares ~/.gemini,
// and installation_id is not reliable, so detection uses this Gemini-only file.
MandatoryFile: "projects.json",
MCP: &mcpconfig.Spec{
Path: geminiMCPPath,
Format: mcpconfig.FormatJSON,
Root: mcpServersKey,
Entry: stdioEntry,
},
},
{
Name: NameGoose,
Expand Down Expand Up @@ -374,6 +438,198 @@ func gooseConfigDir(ctx context.Context) (string, error) {
return filepath.Join(xdg, "goose"), nil
}

// claudeMCPPath returns the file that Claude Code reads its user-scope mcpServers
// from. That is the global config, which holds auth state next to them: it sits
// in the home directory, not in ~/.claude, and CLAUDE_CONFIG_DIR moves it to
// <dir>/.claude.json. Claude Code reads the legacy <config dir>/.config.json
// instead when claudeLegacyWins says so.
// See https://code.claude.com/docs/en/env-vars.
func claudeMCPPath(ctx context.Context) (string, error) {
global, err := claudeGlobalConfig(ctx)
if err != nil {
return "", err
}
dir, err := claudeConfigDir(ctx)
if err != nil {
return "", err
}
legacy := filepath.Join(dir, ".config.json")
if claudeLegacyWins(legacy, global) {
return legacy, nil
}
return global, nil
}

// claudeGlobalConfig returns Claude Code's global config file: .claude.json in
// CLAUDE_CONFIG_DIR when that is set, and in the home directory otherwise.
func claudeGlobalConfig(ctx context.Context) (string, error) {
if dir := env.Get(ctx, "CLAUDE_CONFIG_DIR"); dir != "" {
return filepath.Join(dir, ".claude.json"), nil
}
return homeSubdir(".claude.json")(ctx)
}

// claudeLegacyWins reports whether Claude Code reads the legacy config file
// instead of the global one, following its config path resolution. A non-empty
// legacy file always wins. An empty one wins only if some write bit is set and
// the global file is missing. A legacy file that cannot be inspected does not
// win. Claude Code also treats an empty, read-only global file as a placeholder
// on Linux and WSL, which this does not model.
func claudeLegacyWins(legacy, global string) bool {
info, err := os.Stat(legacy)
if err != nil || !info.Mode().IsRegular() {
return false
}
if info.Size() > 0 {
return true
}
if info.Mode().Perm()&0o222 == 0 {
return false
}
_, err = os.Stat(global)
return err != nil
}

// claudeMCPEntry matches `claude mcp add`, which always writes an env map, even an empty one.
// See https://code.claude.com/docs/en/mcp.
func claudeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any {
if len(vars) == 0 {
vars = map[string]string{}
}
return map[string]any{"type": "stdio", "command": cmd, "args": args, "env": vars}
}

// codexMCPPath returns Codex's config.toml, which honors CODEX_HOME.
// See https://learn.chatgpt.com/docs/config-file/config-advanced.
func codexMCPPath(ctx context.Context) (string, error) {
if home := env.Get(ctx, "CODEX_HOME"); home != "" {
return filepath.Join(home, "config.toml"), nil
}
return homeSubdir(".codex", "config.toml")(ctx)
}

// openCodeMCPPath returns OpenCode's config file: opencode.jsonc when it exists,
// and opencode.json otherwise. A symbolic link counts as existing, so a dangling
// link is chosen and the write refuses it. When both files exist, the CLI refuses
// to choose, since it cannot tell which one OpenCode reads. The file may contain
// comments; a file we cannot parse is refused, never rewritten.
// See https://opencode.ai/docs/config/.
func openCodeMCPPath(ctx context.Context) (string, error) {
dir, err := openCodeConfigDir(ctx)
if err != nil {
return "", err
}
jsonPath := filepath.Join(dir, "opencode.json")
jsoncPath := filepath.Join(dir, "opencode.jsonc")
hasJSON, err := linkExists(jsonPath)
if err != nil {
return "", err
}
hasJSONC, err := linkExists(jsoncPath)
if err != nil {
return "", err
}
switch {
case hasJSON && hasJSONC:
return "", fmt.Errorf("%s and %s both exist; remove one, since this CLI cannot tell which one OpenCode reads",
filepath.ToSlash(jsonPath), filepath.ToSlash(jsoncPath))
case hasJSONC:
return jsoncPath, nil
default:
return jsonPath, nil
}
}

// linkExists reports whether path exists. It uses Lstat, so a symbolic link counts
// even when its target is missing.
func linkExists(path string) (bool, error) {
_, err := os.Lstat(path)
switch {
case err == nil:
return true, nil
case errors.Is(err, fs.ErrNotExist):
return false, nil
default:
return false, fmt.Errorf("cannot read %s: %w", filepath.ToSlash(path), err)
}
}

// openCodeMCPEntry follows the current OpenCode schema: servers live under
// mcp.servers, the env map is "environment", and there is no "enabled" key
// (the schema forbids extra keys, and an absent key means enabled).
// See https://opencode.ai/docs/mcp-servers/.
func openCodeMCPEntry(cmd string, args []string, vars map[string]string) map[string]any {
entry := map[string]any{"type": localServerType, "command": append([]string{cmd}, args...)}
if len(vars) > 0 {
entry["environment"] = vars
}
return entry
}

// copilotMCPPath returns Copilot CLI's user config. COPILOT_HOME replaces ~/.copilot.
// See https://docs.github.com/en/copilot/reference/copilot-cli-reference/cli-config-dir-reference.
func copilotMCPPath(ctx context.Context) (string, error) {
if home := env.Get(ctx, "COPILOT_HOME"); home != "" {
return filepath.Join(home, "mcp-config.json"), nil
}
return homeSubdir(".copilot", "mcp-config.json")(ctx)
}

// copilotMCPEntry is the stdio entry Copilot CLI documents, with all tools enabled.
// See https://docs.github.com/en/copilot/how-tos/copilot-cli/customize-copilot/add-mcp-servers.
func copilotMCPEntry(cmd string, args []string, vars map[string]string) map[string]any {
entry := stdioEntry(cmd, args, vars)
entry["type"] = localServerType
entry["tools"] = []string{"*"}
return entry
}

// geminiMCPPath returns Gemini CLI's user settings, which hold mcpServers. The
// settings directory follows geminiConfigDir, which honors GEMINI_CLI_HOME.
// See https://geminicli.com/docs/tools/mcp-server/.
func geminiMCPPath(ctx context.Context) (string, error) {
dir, err := geminiConfigDir(ctx)
if err != nil {
return "", err
}
return filepath.Join(dir, "settings.json"), nil
}

// antigravityMCPPath is the global MCP file in Antigravity's docs, for both the
// CLI and the IDE. It is not the skills directory (~/.gemini/antigravity).
// See https://antigravity.google/docs/mcp.
func antigravityMCPPath(ctx context.Context) (string, error) {
return homeSubdir(".gemini", "config", "mcp_config.json")(ctx)
}

// stdioEntry is the command/args entry used by Cursor, Codex, Gemini and
// Antigravity. env is omitted when empty. Antigravity's "disabled" key is
// optional and defaults to false, so it is not written.
func stdioEntry(cmd string, args []string, vars map[string]string) map[string]any {
entry := map[string]any{"command": cmd, "args": args}
if len(vars) > 0 {
entry["env"] = vars
}
return entry
}

// SupportsMCP reports whether this CLI writes the agent's MCP server entries.
func (a *Agent) SupportsMCP() bool {
return a.MCP != nil
}

// MCPAgents returns the agents whose MCP server entries this CLI writes, in
// registry order.
func MCPAgents() []*Agent {
var agents []*Agent
for _, a := range Registry {
if a.SupportsMCP() {
agents = append(agents, a)
}
}
return agents
}

// ByName returns the registry agent with the given name, or nil if not found.
func ByName(name string) *Agent {
for _, a := range Registry {
Expand Down
Loading
Loading