diff --git a/acceptance/cmd/aitools/mcp/hidden/output.txt b/acceptance/cmd/aitools/mcp/hidden/output.txt index 1d58c9b50c8..8eaca19f672 100644 --- a/acceptance/cmd/aitools/mcp/hidden/output.txt +++ b/acceptance/cmd/aitools/mcp/hidden/output.txt @@ -12,10 +12,12 @@ Each server runs through 'databricks aitools mcp serve' with that profile's cred Servers come from --names: full names such as system.ai.github, or leaf names such as github. Leaf names resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share. -Agents come from --agents. Without it, every detected MCP-capable agent is used. +Agents come from --agents. -Interactive selection is not available yet: in a terminal, pass both --names and --agents. -With --output json, both are required. +In a terminal, the command asks for whatever is not given: the profile, the servers and the agents (detected +agents are pre-checked). It confirms the plan when it asked for any of them, and it asks before each sign-in unless +--login or --no-login is given. Without a terminal, --names is required and the agents default to every detected +MCP-capable agent. With --output json, --names and --agents are required. Supported agents: claude-code, cursor, codex, opencode, copilot, antigravity, gemini @@ -23,14 +25,14 @@ Usage: databricks aitools mcp add [flags] Flags: - --agents string Agents to install for, comma-separated (default: every detected MCP-capable agent) + --agents string Agents to install for, comma-separated (default: a picker in a terminal; otherwise every detected MCP-capable agent) --force Overwrite an existing server with the same name that this CLI did not write -h, --help help for add --location string Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share) --login Sign in to each sign-in-required server that was installed, after the installs --name string Server name to use instead of the generated one; only with exactly one service --names string Services to install: full names (system.ai.github) or leaf names (github), comma-separated - --no-login Do not sign in; print the login commands for sign-in-required servers (default) + --no-login Do not sign in; print the login commands for sign-in-required servers (default without a terminal; in a terminal, the default asks for each one) Global Flags: --debug enable debug logging diff --git a/acceptance/cmd/aitools/mcp/repair-and-warnings/output.txt b/acceptance/cmd/aitools/mcp/repair-and-warnings/output.txt index b57da8bcbf0..d7adc1bd530 100644 --- a/acceptance/cmd/aitools/mcp/repair-and-warnings/output.txt +++ b/acceptance/cmd/aitools/mcp/repair-and-warnings/output.txt @@ -54,4 +54,4 @@ Error: no installed MCP server named orphan Error: no installed MCP servers on profile typo >>> musterr [CLI] aitools mcp remove --names genie_one_mcp-logfood --agents cursor -Error: no selected MCP server is installed for Cursor +Error: no MCP server is installed for Cursor diff --git a/cmd/aitools/install.go b/cmd/aitools/install.go index 2e792a82b9c..74521eeff64 100644 --- a/cmd/aitools/install.go +++ b/cmd/aitools/install.go @@ -20,10 +20,19 @@ import ( "github.com/spf13/cobra" ) +// Prompt text shared by install, uninstall and the mcp commands. +const ( + // multiSelectHelp is the description of the multi-select prompts of install and mcp add and remove. + multiSelectHelp = "space to toggle, enter to confirm" + // cancelledMessage is printed when the user declines at a prompt, and the command ends without a change. + cancelledMessage = "Cancelled." +) + // Package-level seams for testability. Tests override these via helpers in // install_test.go. var ( promptAgentSelection = defaultPromptAgentSelection + promptProceed = defaultPromptProceed installSkillsForAgentsFn = installer.InstallSkillsForAgents installPluginForAgentFn = installer.InstallPluginForAgent recordPluginInstallsFn = installer.RecordPluginInstalls @@ -509,6 +518,18 @@ func agentStateLabel(s agents.DisplayState) string { } } +func defaultPromptProceed() (bool, error) { + proceed := true + err := huh.NewConfirm(). + Title("Proceed?"). + Value(&proceed). + Run() + if err != nil { + return false, err + } + return proceed, nil +} + func defaultPromptAgentSelection(_ context.Context, scope string, choices []agentChoice) ([]*agents.Agent, error) { options := make([]huh.Option[string], 0, len(choices)) byName := make(map[string]*agents.Agent, len(choices)) diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index c829a1298c5..1f81d5fb0d0 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -111,7 +111,7 @@ func resolveProfile(ctx context.Context, flagValue string) (string, error) { case 1: return profiles[0].Name, nil } - return "", failure(categoryProfileRequired, nil, "several workspace profiles exist; pass --profile. Found: %s", strings.Join(profiles.Names(), ", ")) + return "", failure(categoryProfileRequired, &profileChoice{profiles: profiles}, "several workspace profiles exist; pass --profile. Found: %s", strings.Join(profiles.Names(), ", ")) } // profileFlag returns the root --profile value, or "" when it is not set. diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index c37b20a7314..b97c9e3eaca 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -25,6 +25,7 @@ import ( "github.com/databricks/databricks-sdk-go" "github.com/databricks/databricks-sdk-go/apierr" "github.com/databricks/databricks-sdk-go/config" + "github.com/databricks/databricks-sdk-go/service/catalog" "github.com/spf13/cobra" ) @@ -90,8 +91,24 @@ type addPlan struct { service mcp.Service name string failed map[*agents.Agent]error + // reasserted holds the agents whose config already has this server as the write would leave it. + reasserted map[*agents.Agent]bool + // updated holds the agents whose config has a Databricks entry for this server under its name that the write changes. + updated map[*agents.Agent]bool + // replaced holds the agents whose config has an entry under this name that this CLI did not write. Under --force + // the write replaces it. + replaced map[*agents.Agent]bool } +// agentSource says where the agents of an add came from. It is shown in the header. +type agentSource string + +const ( + agentsNamed agentSource = "named with --agents" + agentsDetected agentSource = "detected" + agentsPicked agentSource = "selected" +) + // addRequest is what one `mcp add` run applies to every server. type addRequest struct { // jsonMode suppresses the text output; the JSON document is the only output. @@ -100,7 +117,7 @@ type addRequest struct { host string workspaceID string agents []*agents.Agent - explicit bool + agentSource agentSource force bool binary string // configFile is the resolved config file that the servers bind to. @@ -109,11 +126,14 @@ type addRequest struct { pinned string // env is the entry env: DATABRICKS_CONFIG_FILE when it was set. env map[string]string - // refused holds the agents named with --agents that are not installed, with the reason. Their - // config is not written. + // refused holds the agents named with --agents that are not installed, with the reason. Their config is not written. refused map[*agents.Agent]error + // unreadable holds the agents whose config could not be read, with the reason. Their writes fail. + unreadable map[*agents.Agent]error // login signs in to the sign-in-required servers after they are installed. login bool + // askLogin asks, per sign-in-required server, whether to sign in after it is installed. + askLogin bool } // slashPaths rewrites the agent config paths of the document to forward slashes, as the text output @@ -138,10 +158,12 @@ Each server runs through 'databricks aitools mcp serve' with that profile's cred Servers come from --names: full names such as system.ai.github, or leaf names such as github. Leaf names resolve in --location, which defaults to system.ai, or to the one schema that the full names in --names share. -Agents come from --agents. Without it, every detected MCP-capable agent is used. +Agents come from --agents. -Interactive selection is not available yet: in a terminal, pass both --names and --agents. -With --output json, both are required. +In a terminal, the command asks for whatever is not given: the profile, the servers and the agents (detected +agents are pre-checked). It confirms the plan when it asked for any of them, and it asks before each sign-in unless +--login or --no-login is given. Without a terminal, --names is required and the agents default to every detected +MCP-capable agent. With --output json, --names and --agents are required. Supported agents: ` + strings.Join(mcpAgentNames(), ", "), Args: cobra.NoArgs, @@ -177,25 +199,29 @@ Supported agents: ` + strings.Join(mcpAgentNames(), ", "), cmd.Flags().StringVar(&f.names, "names", "", "Services to install: full names (system.ai.github) or leaf names (github), comma-separated") cmd.Flags().StringVar(&f.location, "location", "", "Schema to resolve leaf names in, as . (default: system.ai, or the one schema that the full names in --names share)") - cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to install for, comma-separated (default: every detected MCP-capable agent)") + cmd.Flags().StringVar(&f.agents, "agents", "", "Agents to install for, comma-separated (default: a picker in a terminal; otherwise every detected MCP-capable agent)") cmd.Flags().StringVar(&f.name, "name", "", "Server name to use instead of the generated one; only with exactly one service") cmd.Flags().BoolVar(&f.force, "force", false, "Overwrite an existing server with the same name that this CLI did not write") cmd.Flags().BoolVar(&f.login, "login", false, "Sign in to each sign-in-required server that was installed, after the installs") - cmd.Flags().BoolVar(&f.noLogin, "no-login", false, "Do not sign in; print the login commands for sign-in-required servers (default)") + cmd.Flags().BoolVar(&f.noLogin, "no-login", false, "Do not sign in; print the login commands for sign-in-required servers (default without a terminal; in a terminal, the default asks for each one)") return cmd } // runMcpAdd installs the requested servers and fills out. Every refusal that needs no request comes before -// the first one: the flags, the agents, the binary name, the binding of each service that a recorded entry -// holds, and the server names. A server that no agent can take is refused the same way, unless a detected agent -// can take it. A failure of an agent named with --agents is returned as an agentErrors; detected agents only report -// theirs. A server that reaches no agent fails the run. +// the first one: the flags, the agents named with --agents, the binary name, the binding of each service that a +// recorded entry holds, and the server names. A server that no agent can take is refused the same way, unless a +// detected or picked agent can take it. A failure of an agent named with --agents is returned as an agentErrors; +// detected and picked agents only report theirs. A server that reaches no agent fails the run. +// +// Without --names, in a terminal, the services are picked from the workspace, so the workspace is read before the +// plan. The services named with --names are planned first, and the workspace is read after that. func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode bool, out *addOutput) error { names := splitAndTrim(f.names) agentNames := splitAndTrim(f.agents) if err := validateMCPAddFlags(ctx, f, names, agentNames, jsonMode); err != nil { return err } + interactive := !jsonMode && cmdio.IsPromptSupported(ctx) location, fullNames, err := resolveServices(f.location, names) if err != nil { return err @@ -203,21 +229,32 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if f.name != "" && len(fullNames) != 1 { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service in --names") } - targets, explicit, err := targetAgents(ctx, agentNames) - if err != nil { - return err + var named []*agents.Agent + if len(agentNames) > 0 { + if named, err = resolveMCPAgents(agentNames); err != nil { + return err + } + } + // Without a terminal the agents are chosen before the binary and the profile are checked, so that a run with no + // agent reports that first. A terminal chooses them once the profile is known. + var targets []*agents.Agent + var source agentSource + if !interactive { + if targets, source, err = chooseAgents(ctx, named, false); err != nil { + return err + } } binary, absolute := mcpBinaryPath() if !mcpconfig.IsDatabricksBinary(binary) { return failure(protos.AitoolsErrorCategoryUncategorized, nil, "%s is not the databricks binary; coding agents start MCP servers with databricks or databricks.exe", filepath.ToSlash(binary)) } var refused map[*agents.Agent]error - if explicit { - refused = refusedAgents(ctx, targets) + if len(named) > 0 { + refused = refusedAgents(ctx, named) // Nothing can be written when every agent is refused, so the refusals end the run before any request. - if len(refused) == len(targets) { + if len(refused) == len(named) { var errs []error - for _, a := range targets { + for _, a := range named { errs = append(errs, refused[a]) } return errors.Join(errs...) @@ -231,7 +268,7 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode return err } ctx = env.Set(ctx, mcpconfig.ConfigFileEnv, configFile) - profileName, err := resolveProfile(ctx, profileFlag(cmd)) + profileName, profilePicked, err := chooseProfile(ctx, profileFlag(cmd), interactive) if err != nil { return err } @@ -241,18 +278,48 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode if err != nil { return err } - for _, name := range fullNames { - if _, err := mcp.CheckBinding(ctx, state, name, profileName, configFile); err != nil { + // Until the services are known, nothing reaches the workspace. A run that picks its services checks the profile here, + // and the agents are chosen before the first request. + pickingServices := len(names) == 0 + var w *databricks.WorkspaceClient + var workspaceID string + var picked []mcp.Service + if pickingServices { + if err := mcp.CheckProfile(profileName, configFile); err != nil { + return err + } + } else if err := checkBindings(ctx, state, fullNames, profileName, configFile); err != nil { + return err + } + if interactive { + if targets, source, err = chooseAgents(ctx, named, true); err != nil { return err } } + // Without --names the services are picked from the workspace, and then checked like the named ones. + if pickingServices { + w, workspaceID, err = connectWorkspace(ctx, out, profileName, configFile, pinned) + if err != nil { + return err + } + if picked, err = chooseServices(ctx, w, addBinding{profile: profileName, configFile: configFile}, pinned, location, state); err != nil { + return err + } + fullNames = serviceFullNames(picked) + if err := checkBindings(ctx, state, fullNames, profileName, configFile); err != nil { + return err + } + } + held, unreadable := readAgentEntries(ctx, targets) in := planInput{ binding: addBinding{profile: profileName, configFile: configFile}, nameFlag: f.name, force: f.force, - explicit: explicit, + explicit: source == agentsNamed, targets: targets, - held: readAgentEntries(ctx, targets), + held: held, + binary: binary, + env: pinnedEnv(pinned), } plans, err := planAdds(ctx, in, state, fullNames) if err != nil { @@ -262,23 +329,18 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode return err } - w, err := profileClient(ctx, profileName, configFile, pinned) - if err != nil { - return err - } - out.Host = w.Config.Host - workspaceID, err := auth.ResolveWorkspaceID(ctx, w) - if err != nil { - return describeRequestError(profileName, pinned, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) - } - - services, err := mcp.Discover(ctx, w, location) - if err != nil { - return describeRequestError(profileName, pinned, err) - } - picked, err := pickServices(location, fullNames, services) - if err != nil { - return err + if !pickingServices { + w, workspaceID, err = connectWorkspace(ctx, out, profileName, configFile, pinned) + if err != nil { + return err + } + services, err := mcp.Discover(ctx, w, location) + if err != nil { + return describeRequestError(profileName, pinned, err) + } + if picked, err = pickServices(location, fullNames, services); err != nil { + return err + } } for i := range plans { plans[i].service = picked[i] @@ -290,22 +352,35 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode host: w.Config.Host, workspaceID: workspaceID, agents: targets, - explicit: explicit, + agentSource: source, force: f.force, binary: binary, configFile: configFile, pinned: pinned, refused: refused, + unreadable: unreadable, login: f.login, - } - if pinned != "" { - req.env = map[string]string{mcpconfig.ConfigFileEnv: pinned} + askLogin: interactive && !f.login && !f.noLogin, + env: pinnedEnv(pinned), } if !absolute && !jsonMode { cmdio.LogString(ctx, "Note: databricks is not an absolute path; coding agents must find it on PATH to start the MCP servers.") } printAddHeader(ctx, req) + // A run that names everything asks no question, except the sign-in question of a sign-in-required server. + // The plan is confirmed only when a picker made one of the choices. + if interactive && (profilePicked || pickingServices || source == agentsPicked) { + printAddPlan(ctx, req, plans) + proceed, err := promptProceed() + if err != nil { + return err + } + if !proceed { + cmdio.LogString(ctx, cancelledMessage) + return nil + } + } servers := make([]addServerOutput, 0, len(plans)) for _, plan := range plans { servers = append(servers, installServer(ctx, req, plan)) @@ -317,12 +392,58 @@ func runMcpAdd(ctx context.Context, cmd *cobra.Command, f mcpAddFlags, jsonMode recordErr := recordInstalls(ctx, req, plans, servers) printAddResults(ctx, req, servers) var signErr error - if req.login { + if req.login || req.askLogin { signErr = signInInstalled(ctx, cmd.ErrOrStderr(), w, req, plans, servers) } return errors.Join(recordErr, signErr, agentFailureError(req, servers)) } +// serviceFullNames returns the full names of services, in order. +func serviceFullNames(services []mcp.Service) []string { + names := make([]string, 0, len(services)) + for _, s := range services { + names = append(names, s.FullName) + } + return names +} + +// connectWorkspace returns a client for the profile and the ID of its workspace, and sets the host in out. +func connectWorkspace(ctx context.Context, out *addOutput, profileName, configFile, pinned string) (*databricks.WorkspaceClient, string, error) { + w, err := profileClient(ctx, profileName, configFile, pinned) + if err != nil { + return nil, "", err + } + out.Host = w.Config.Host + workspaceID, err := auth.ResolveWorkspaceID(ctx, w) + if err != nil { + return nil, "", describeRequestError(profileName, pinned, fmt.Errorf("cannot read the workspace ID of %s: %w", w.Config.Host, err)) + } + return w, workspaceID, nil +} + +// checkBindings returns the first failure of CheckBinding for the services, which makes no request. +func checkBindings(ctx context.Context, state *mcp.State, fullNames []string, profileName, configFile string) error { + for _, name := range fullNames { + if _, err := mcp.CheckBinding(ctx, state, name, profileName, configFile); err != nil { + return err + } + } + return nil +} + +// chooseServices returns the services the user checks in the picker, from the services of location. The services +// the profile already has are rows marked installed, unchecked, so that they can be added to another agent. +func chooseServices(ctx context.Context, w *databricks.WorkspaceClient, b addBinding, pinned, location string, state *mcp.State) ([]mcp.Service, error) { + list := func(ctx context.Context, location string) ([]mcp.Service, error) { + services, err := mcp.Discover(ctx, w, location) + if err != nil { + return nil, describeRequestError(b.profile, pinned, err) + } + return services, nil + } + return pickServicesInteractively(ctx, b, location, state, list) +} + // validateMCPAddFlags rejects flag combinations that cannot run, before anything is read or written. func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames []string, jsonMode bool) error { if f.login && f.noLogin { @@ -349,15 +470,13 @@ func validateMCPAddFlags(ctx context.Context, f mcpAddFlags, names, agentNames [ } return nil } - if cmdio.IsPromptSupported(ctx) { - if len(names) == 0 || len(agentNames) == 0 { - return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "interactive selection is not available yet; pass --names and --agents") - } - return nil - } - if len(names) == 0 { + if len(names) == 0 && !cmdio.IsPromptSupported(ctx) { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--names is required so the command runs without interactive prompts") } + // The picker chooses services in a terminal, and --name needs exactly one of them, which only --names can give. + if f.name != "" && len(names) == 0 { + return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--name needs exactly one service; leave out --name to pick services in the picker") + } return nil } @@ -434,12 +553,23 @@ func describeListError(profileName string, err error) error { return failure(category, listErr, "profile %s: %s", profileName, listErr.Error()) } -// targetAgents returns the agents named by --agents, or else every detected MCP-capable agent. -// explicit reports whether the names were given. -func targetAgents(ctx context.Context, names []string) ([]*agents.Agent, bool, error) { - if len(names) > 0 { - list, err := resolveMCPAgents(names) - return list, true, err +// chooseAgents returns the agents to install for: the ones named with --agents, else the ones the user checks in the +// picker when the session is interactive, else every detected MCP-capable agent. In a terminal with no agent installed +// for MCP, the run is refused here, with the agents that were not found, before any request. +func chooseAgents(ctx context.Context, named []*agents.Agent, interactive bool) ([]*agents.Agent, agentSource, error) { + if len(named) > 0 { + return named, agentsNamed, nil + } + if interactive { + choices, notFound := mcpAgentChoices(ctx) + if len(notFound) > 0 { + cmdio.LogString(ctx, "Not found: "+strings.Join(notFound, ", ")) + } + if len(choices) == 0 { + return nil, "", noAgentsError() + } + picked, err := promptAgentSelection(ctx, installer.ScopeGlobal, choices) + return picked, agentsPicked, err } var detected []*agents.Agent for _, a := range agents.MCPAgents() { @@ -450,9 +580,14 @@ func targetAgents(ctx context.Context, names []string) ([]*agents.Agent, bool, e } } if len(detected) == 0 { - return nil, false, failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "no MCP-capable coding agents detected; pass --agents with one of: %s", strings.Join(mcpAgentNames(), ", ")) + return nil, "", noAgentsError() } - return detected, false, nil + return detected, agentsDetected, nil +} + +// noAgentsError is the refusal of a run that finds no MCP-capable agent to install for. +func noAgentsError() error { + return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "no MCP-capable coding agents detected; pass --agents with one of: %s", strings.Join(mcpAgentNames(), ", ")) } // resolveMCPAgents maps --agents names to MCP-capable agents. Unknown and non-MCP agents are errors. @@ -593,15 +728,34 @@ type addBinding struct { // config cannot be read has none here: its write reports the failure. type agentEntries map[*agents.Agent]map[string]map[string]any -func readAgentEntries(ctx context.Context, targets []*agents.Agent) agentEntries { +// readAgentEntries returns the server entries of each target agent, and the agents whose config could not be read, with +// the reason. +func readAgentEntries(ctx context.Context, targets []*agents.Agent) (agentEntries, map[*agents.Agent]error) { held := make(agentEntries, len(targets)) + unreadable := map[*agents.Agent]error{} files := newAgentFiles() for _, a := range targets { - if cfg := files.current(ctx, a); cfg.err == nil { - held[a] = cfg.entries + cfg := files.current(ctx, a) + if cfg.err != nil { + unreadable[a] = cfg.err + continue } + held[a] = cfg.entries + } + return held, unreadable +} + +// pinnedEnv returns the entry env that pins the config file, or nil when it is not pinned. +func pinnedEnv(pinned string) map[string]string { + if pinned == "" { + return nil } - return held + return map[string]string{mcpconfig.ConfigFileEnv: pinned} +} + +// serveArgs returns the arguments of the entry that runs service for agent through profile. +func serveArgs(profile, service, agent string) []string { + return []string{"aitools", "mcp", "serve", mcpconfig.ProfileFlag, profile, mcpconfig.ServiceFlag, service, mcpconfig.AgentFlag, agent} } // planInput is what planAdds plans the servers of one run against. @@ -614,6 +768,9 @@ type planInput struct { explicit bool targets []*agents.Agent held agentEntries + // binary and env are the command and env of the entries the write makes. + binary string + env map[string]string } // planAdds chooses the server name of each service before anything is written, and refuses what would overwrite @@ -637,7 +794,36 @@ func planAdds(ctx context.Context, in planInput, state *mcp.State, fullNames []s return nil, err } taken[name] = true - plans = append(plans, addPlan{service: mcp.Service{FullName: fullName}, name: name, failed: failed}) + plan := addPlan{ + service: mcp.Service{FullName: fullName}, + name: name, + failed: failed, + reasserted: map[*agents.Agent]bool{}, + updated: map[*agents.Agent]bool{}, + replaced: map[*agents.Agent]bool{}, + } + for _, a := range in.targets { + entry, held := in.held[a][name] + if !held || failed[a] != nil { + continue + } + // A foreign entry is replaced. A Databricks entry is re-asserted when the write leaves it as it is, and updated + // when the write changes it. + if !mcpconfig.IsDatabricksEntry(entry) { + plan.replaced[a] = true + continue + } + same, err := agentconfig.Unchanged(a, in.binary, serveArgs(in.binding.profile, fullName, a.Name), in.env, entry) + if err != nil { + return nil, err + } + if same { + plan.reasserted[a] = true + } else { + plan.updated[a] = true + } + } + plans = append(plans, plan) } return plans, nil } @@ -816,7 +1002,7 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO err = plan.failed[a] } if err == nil { - args := []string{"aitools", "mcp", "serve", mcpconfig.ProfileFlag, req.profile, mcpconfig.ServiceFlag, plan.service.FullName, mcpconfig.AgentFlag, a.Name} + args := serveArgs(req.profile, plan.service.FullName, a.Name) path, status, err = agentconfig.Upsert(ctx, a, plan.name, req.binary, args, req.env, req.force) } @@ -839,11 +1025,11 @@ func installServer(ctx context.Context, req addRequest, plan addPlan) addServerO return out } -// refusedAgents returns the agents named with --agents that are not installed, with the reason for each. -// It runs once, before any request, so each service reports the same refusal without another check. -func refusedAgents(ctx context.Context, targets []*agents.Agent) map[*agents.Agent]error { +// refusedAgents returns the agents of named, the ones given with --agents, that are not installed, with the reason for +// each. It runs once, before any request, so each service reports the same refusal without another check. +func refusedAgents(ctx context.Context, named []*agents.Agent) map[*agents.Agent]error { refused := map[*agents.Agent]error{} - for _, a := range targets { + for _, a := range named { if err := requireInstalled(ctx, a); err != nil { refused[a] = err } @@ -851,8 +1037,8 @@ func refusedAgents(ctx context.Context, targets []*agents.Agent) map[*agents.Age return refused } -// requireInstalled fails when an agent named with --agents is not installed. Writing its config would create -// the agent's directory, so the agent is refused instead. +// requireInstalled fails when an agent is not installed. Writing its config would create the agent's +// directory, so the agent is refused instead. func requireInstalled(ctx context.Context, a *agents.Agent) error { installed, err := a.InstalledForMCP(ctx) if err != nil { @@ -861,13 +1047,19 @@ func requireInstalled(ctx context.Context, a *agents.Agent) error { if installed { return nil } - return failure(protos.AitoolsErrorCategoryUnknownAgent, nil, "%s is not installed; install it first or leave it out of --agents", a.DisplayName) + return failure(protos.AitoolsErrorCategoryUnknownAgent, errNotInstalled, "%s is not installed; install it first or leave it out of --agents", a.DisplayName) } +// errNotInstalled is the cause of the refusal of an agent that is not installed. A failed check has another cause. +var errNotInstalled = errors.New("agent is not installed") + // signInInstalled signs in to each sign-in-required server that reached an agent, in order, and records the sign-in of -// each server in servers. A failure does not stop the others, and the failures are joined. The servers are installed -// either way. A failure names the command to run next when a sign-in can fix it, as the timeout and failed errors do. -// Other failures get no hint, since logging in again would not fix them. +// each server in servers. A sign-in failure does not stop the others: the failures are joined, and the servers are +// installed either way. An abort at a question (Ctrl+C) stops the rest. The servers from the one asked about on get +// their login hint, which the results did not print, and the abort is returned. +// +// In a terminal without --login, each server that is not already signed in is asked about first; a declined one gets +// the login command. func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient, req addRequest, plans []addPlan, servers []addServerOutput) error { var errs []error for i := range servers { @@ -879,6 +1071,24 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp if !installedAnywhere(*server) { continue } + if req.askLogin { + // A caller already signed in is recorded as such, with no question and no second read of the credential. + if signedIn(ctx, w, plans[i].service.FullName) { + server.SignIn = &addSignInOutput{State: signInStateActive} + reportSignedIn(errOut, plans[i].service, req.profile, true) + continue + } + yes, err := promptSignInNow(server.Service) + if err != nil { + errs = append(errs, err) + printSignInHints(ctx, req, servers[i:]) + break + } + if !yes { + cmdio.LogString(ctx, signInHint(server.Service, req.profile, req.pinned)) + continue + } + } url := mcp.SignInURL(w.Config.Host, plans[i].service.FullName, req.workspaceID) state, _, _, err := signIn(ctx, errOut, w, req.pinned, url, plans[i].service, signInOptions{Profile: req.profile, Open: browser.OpenOutputToStderr}) server.SignIn = &addSignInOutput{State: state} @@ -891,6 +1101,27 @@ func signInInstalled(ctx context.Context, errOut io.Writer, w *databricks.Worksp return errors.Join(errs...) } +// signedIn reports whether the caller's sign-in to service is ACTIVE, so that no question is asked about it. The read is +// bounded by profileCheckTimeout, as the --check reads are. A read that fails or times out does not count as signed in; +// the sign-in reads it again and reports the failure. +func signedIn(ctx context.Context, w *databricks.WorkspaceClient, service string) bool { + ctx, cancel := context.WithTimeout(ctx, profileCheckTimeout) + defer cancel() + state, err := mcp.CredentialState(ctx, w, service) + return err == nil && state == catalog.ProvisioningInfoStateActive +} + +// printSignInHints prints the login hint of each server from the given one on that needs sign-in and reached an agent. +// It runs when an abort ends the sign-ins. It makes no request, since a credential read can wait out the SDK's retries +// and hold up the abort. So a server that is already signed in can get the hint too. +func printSignInHints(ctx context.Context, req addRequest, servers []addServerOutput) { + for i := range servers { + if servers[i].SignInRequired && installedAnywhere(servers[i]) { + cmdio.LogString(ctx, signInHint(servers[i].Service, req.profile, req.pinned)) + } + } +} + // signInHint returns the sentence that says a sign-in-required service needs sign-in, with the command that signs the // profile in. func signInHint(service, profileName, pinnedFile string) string { @@ -979,7 +1210,7 @@ func recordServer(ctx context.Context, state *mcp.State, req addRequest, plan ad // service is reported once. func agentFailureError(req addRequest, servers []addServerOutput) error { var errs []error - fail := req.explicit + fail := req.agentSource == agentsNamed for _, server := range servers { for _, a := range server.Agents { if a.err != nil { @@ -1005,11 +1236,70 @@ func printAddHeader(ctx context.Context, req addRequest) { for _, a := range req.agents { names = append(names, a.Name) } - source := "named with --agents" - if !req.explicit { - source = "detected" + cmdio.LogString(ctx, fmt.Sprintf("Agents (%s): %s", req.agentSource, strings.Join(names, ", "))) +} + +// printAddPlan lists what each agent gets, before the confirm of an interactive add. +func printAddPlan(ctx context.Context, req addRequest, plans []addPlan) { + cmdio.LogString(ctx, "") + for _, line := range addPlanLines(req, plans) { + cmdio.LogString(ctx, line) + } + cmdio.LogString(ctx, "") +} + +// addPlanLines returns the plan: one line per agent, with the servers it gets as new entries, the servers it already +// has as the write would leave them (re-asserted), the Databricks entries the write changes (updated), the foreign +// entries it replaces, and the servers it cannot take. An agent whose config cannot be read gets one line saying so. +func addPlanLines(req addRequest, plans []addPlan) []string { + lines := []string{"Plan (profile " + req.profile + "):"} + for _, a := range req.agents { + if err := req.refused[a]; err != nil { + reason := "not installed" + if !errors.Is(err, errNotInstalled) { + reason = "cannot be checked: " + err.Error() + } + lines = append(lines, fmt.Sprintf(" %-14s %s; skipped", a.DisplayName, reason)) + continue + } + if err := req.unreadable[a]; err != nil { + lines = append(lines, fmt.Sprintf(" %-14s cannot be read: %s; the write will fail", a.DisplayName, err)) + continue + } + var added, reasserted, updated, replaced, skipped []string + for _, p := range plans { + switch { + case p.failed[a] != nil: + skipped = append(skipped, p.name) + case p.reasserted[a]: + reasserted = append(reasserted, p.name) + case p.updated[a]: + updated = append(updated, p.name) + case p.replaced[a]: + replaced = append(replaced, p.name) + default: + added = append(added, p.name) + } + } + var parts []string + if len(added) > 0 { + parts = append(parts, "add "+strings.Join(added, ", ")) + } + if len(reasserted) > 0 { + parts = append(parts, "re-assert "+strings.Join(reasserted, ", ")) + } + if len(updated) > 0 { + parts = append(parts, "update "+strings.Join(updated, ", ")) + } + if len(replaced) > 0 { + parts = append(parts, "replace "+strings.Join(replaced, ", ")) + } + if len(skipped) > 0 { + parts = append(parts, "skip "+strings.Join(skipped, ", ")) + } + lines = append(lines, fmt.Sprintf(" %-14s %s", a.DisplayName, strings.Join(parts, "; "))) } - cmdio.LogString(ctx, fmt.Sprintf("Agents (%s): %s", source, strings.Join(names, ", "))) + return lines } // printAddResults prints each server, its agents, and the follow-up steps the server needs. @@ -1027,7 +1317,7 @@ func printAddResults(ctx context.Context, req addRequest, servers []addServerOut cursor = true } } - if server.SignInRequired && installedAnywhere(server) && !req.login { + if server.SignInRequired && installedAnywhere(server) && !req.login && !req.askLogin { cmdio.LogString(ctx, signInHint(server.Service, req.profile, req.pinned)) } if cursor { diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 0d364d9d64a..05c89ca7509 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -15,16 +15,21 @@ import ( "runtime" "strconv" "strings" + "sync/atomic" "testing" + "time" + "github.com/charmbracelet/huh" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/agents" "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/aitools/mcp/agentconfig" "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/flags" "github.com/databricks/cli/libs/telemetry/protos" + "github.com/databricks/databricks-sdk-go" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -348,10 +353,10 @@ func TestInstallServerRefusesUndetectedExplicitAgent(t *testing.T) { home := withHome(t) require.NoError(t, os.MkdirAll(filepath.Join(home, ".claude"), 0o700)) req := addRequest{ - profile: "logfood", - explicit: true, - agents: []*agents.Agent{agents.ByName("codex"), agents.ByName("claude-code")}, - binary: "databricks", + profile: "logfood", + agentSource: agentsNamed, + agents: []*agents.Agent{agents.ByName("codex"), agents.ByName("claude-code")}, + binary: "databricks", } req.refused = refusedAgents(ctx, req.agents) plan := addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"} @@ -378,9 +383,15 @@ func defaultBinding(t *testing.T, profile string) addBinding { } // planNames runs planAdds for the services with the agent entries read as the command reads them. +// readHeld returns the server entries of the targets that readAgentEntries reads, for tests that do not check unreadable configs. +func readHeld(ctx context.Context, targets []*agents.Agent) agentEntries { + held, _ := readAgentEntries(ctx, targets) + return held +} + func planNames(t *testing.T, nameFlag string, b addBinding, state *mcp.State, fullNames []string, targets []*agents.Agent) ([]addPlan, error) { t.Helper() - in := planInput{binding: b, nameFlag: nameFlag, targets: targets, held: readAgentEntries(t.Context(), targets)} + in := planInput{binding: b, nameFlag: nameFlag, targets: targets, held: readHeld(t.Context(), targets)} return planAdds(t.Context(), in, state, fullNames) } @@ -749,7 +760,7 @@ func TestInstallServerReassertsFromAgentFile(t *testing.T) { home := withHome(t) require.NoError(t, os.MkdirAll(filepath.Join(home, ".claude"), 0o700)) - req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName("claude-code")}, binary: "databricks"} + req := addRequest{profile: "logfood", agentSource: agentsNamed, agents: []*agents.Agent{agents.ByName("claude-code")}, binary: "databricks"} plan := addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github-logfood"} first := installServer(t.Context(), req, plan) @@ -778,8 +789,8 @@ func TestAgentFailureErrorWhenAServiceReachedNoAgent(t *testing.T) { assert.ErrorContains(t, err, "bad config") // Agents named with --agents fail the command on any failure. - assert.Error(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{installed, mixed})) - assert.NoError(t, agentFailureError(addRequest{explicit: true}, []addServerOutput{installed})) + assert.Error(t, agentFailureError(addRequest{agentSource: agentsNamed}, []addServerOutput{installed, mixed})) + assert.NoError(t, agentFailureError(addRequest{agentSource: agentsNamed}, []addServerOutput{installed})) } func TestErrorCategoryMapsTypedFailures(t *testing.T) { @@ -1232,7 +1243,7 @@ func TestAgentFailureErrorKeepsEveryDistinctFailure(t *testing.T) { {Agents: []addAgentOutput{{Name: "claude-code", Status: statusFailed, err: refusal}}}, } - err := agentFailureError(addRequest{explicit: true}, servers) + err := agentFailureError(addRequest{agentSource: agentsNamed}, servers) require.Error(t, err) assert.ErrorContains(t, err, "write ~/.claude.json") assert.Equal(t, 1, strings.Count(err.Error(), "Claude Code is not installed"), err.Error()) @@ -1262,7 +1273,7 @@ func TestPlanAddsDetectedAgentConflictFailsOnlyThatAgent(t *testing.T) { require.NoError(t, os.WriteFile(filepath.Join(home, ".claude.json"), []byte(`{"mcpServers":{"github-logfood":`+claudeEntry("system.ai.other", "logfood", "")+`}}`), 0o600)) state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test"}}} targets := []*agents.Agent{claude, codex} - in := planInput{binding: defaultBinding(t, "logfood"), targets: targets, held: readAgentEntries(ctx, targets)} + in := planInput{binding: defaultBinding(t, "logfood"), targets: targets, held: readHeld(ctx, targets)} plans, err := planAdds(ctx, in, state, []string{"system.ai.github"}) require.NoError(t, err) @@ -1279,7 +1290,7 @@ func TestPlanAddsDetectedAgentConflictFailsOnlyThatAgent(t *testing.T) { assert.Equal(t, categoryNameConflict, errorCategory(err)) onlyClaude := []*agents.Agent{claude} - in = planInput{binding: defaultBinding(t, "logfood"), targets: onlyClaude, held: readAgentEntries(ctx, onlyClaude)} + in = planInput{binding: defaultBinding(t, "logfood"), targets: onlyClaude, held: readHeld(ctx, onlyClaude)} plans, err = planAdds(ctx, in, state, []string{"system.ai.github"}) require.NoError(t, err) refusal := planningRefusals(plans, onlyClaude) @@ -1356,7 +1367,7 @@ func TestInstallServerWritesOnlyAgentsInstalledForMCP(t *testing.T) { writeFakeCLI(t, agents.ByName(tt.agent).Binary) } - req := addRequest{profile: "logfood", explicit: true, agents: []*agents.Agent{agents.ByName(tt.agent)}, binary: "databricks"} + req := addRequest{profile: "logfood", agentSource: agentsNamed, agents: []*agents.Agent{agents.ByName(tt.agent)}, binary: "databricks"} req.refused = refusedAgents(ctx, req.agents) out := installServer(ctx, req, addPlan{service: mcp.Service{FullName: "system.ai.github"}, name: "github"}) require.Len(t, out.Agents, 1) @@ -1388,3 +1399,633 @@ func writeFakeCLI(t *testing.T, binary string) { require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte("#!/bin/sh\n"), 0o755)) t.Setenv("PATH", dir) } + +// runInteractiveAdd runs `aitools mcp add` as a terminal does. The prompts are the seams, which the test overrides. +func runInteractiveAdd(t *testing.T, args ...string) error { + t.Helper() + return runInteractive(t, append([]string{"mcp", "add"}, args...)...) +} + +// runInteractive runs the aitools command with args as a terminal does, under the mcp group. +func runInteractive(t *testing.T, args ...string) error { + t.Helper() + withDatabricksBinary(t) + ctx, test := cmdio.SetupTest(t.Context(), cmdio.TestOptions{PromptSupported: true}) + t.Cleanup(test.Done) + go drainReader(test.Stdout) + go drainReader(test.Stderr) + parent := newMCPTestRoot() + parent.SetOut(&bytes.Buffer{}) + parent.SetErr(&bytes.Buffer{}) + parent.SetArgs(args) + return parent.ExecuteContext(ctx) +} + +// TestMcpAddNameOnThePickerPathIsRefusedBeforeConnecting checks that --name without --names is refused before the +// profile is read, so no request reaches the workspace. +func TestMcpAddNameOnThePickerPathIsRefusedBeforeConnecting(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + failOnPrompt(t) + + err := runInteractiveAdd(t, "--profile", "logfood", "--name", "github") + require.Error(t, err) + assert.Equal(t, "--name needs exactly one service; leave out --name to pick services in the picker", err.Error()) + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// TestMcpAddPickerRefusesRepointedHostOfPickedServiceBeforeWriting checks that a run that picks its services lists the +// workspace, then refuses a picked service whose recorded entry names another host, before anything is written. +func TestMcpAddPickerRefusesRepointedHostOfPickedServiceBeforeWriting(t *testing.T) { + home := withHome(t) + server := listingServer(t, "system.ai.github") + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test", + }}})) + failOnPrompt(t) + promptServiceSelection = func(string, []serviceRow) (servicePick, error) { + return servicePick{services: []mcp.Service{{FullName: "system.ai.github", Leaf: "github"}}}, nil + } + + err := runInteractiveAdd(t, "--profile", "logfood", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err)) + assert.Contains(t, err.Error(), "was set up for https://adb-123.test, but profile \"logfood\" now points at "+server.URL) + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) +} + +// schemaListingServer returns a workspace whose schemas list the services named for them, by the parent of the listing. +// Each service is a full name, none sign-in-required. +func schemaListingServer(t *testing.T, schemas map[string][]string) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + if r.URL.Path != "/api/2.1/unity-catalog/mcp-services" { + t.Errorf("unexpected request: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + return + } + var services []string + for _, name := range schemas[strings.TrimPrefix(r.URL.Query().Get("parent"), "schemas/")] { + services = append(services, `{"name":"mcp-services/`+name+`"}`) + } + _, _ = w.Write([]byte(`{"mcp_services":[` + strings.Join(services, ",") + `]}`)) + })) + t.Cleanup(server.Close) + return server +} + +// TestMcpAddPickerInstallsAcrossSchemas checks that a terminal run with a profile and no --names installs the services it +// picks in two schemas, reached through "Another schema...": both entries reach the agent file and the state. +func TestMcpAddPickerInstallsAcrossSchemas(t *testing.T) { + home := withHome(t) + server := schemaListingServer(t, map[string][]string{ + "system.ai": {"system.ai.github"}, + "main.tools": {"main.tools.slack"}, + }) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + failOnPrompt(t) + promptServiceSelection = func(location string, _ []serviceRow) (servicePick, error) { + if location == "system.ai" { + return servicePick{services: []mcp.Service{{FullName: "system.ai.github", Leaf: "github"}}, another: true}, nil + } + return servicePick{services: []mcp.Service{{FullName: "main.tools.slack", Leaf: "slack"}}}, nil + } + promptLocation = func() (string, error) { return "main.tools", nil } + promptAgentSelection = func(_ context.Context, _ string, choices []agentChoice) ([]*agents.Agent, error) { + return []*agents.Agent{choices[0].agent}, nil + } + proceeds := 0 + promptProceed = func() (bool, error) { + proceeds++ + return true, nil + } + + require.NoError(t, runInteractiveAdd(t, "--profile", "logfood")) + assert.Equal(t, 1, proceeds, "the picked plan is confirmed once") + + agentFile, err := os.ReadFile(filepath.Join(home, ".claude.json")) + require.NoError(t, err) + assert.Contains(t, string(agentFile), `"github-logfood"`) + assert.Contains(t, string(agentFile), `"main-tools-slack-logfood"`) + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + var recorded []string + for _, e := range state.Servers { + recorded = append(recorded, e.Service) + } + assert.ElementsMatch(t, []string{"system.ai.github", "main.tools.slack"}, recorded) +} + +// TestMcpAddPickerRefusesMissingProfileBeforeAnyRequest checks that a run that picks its services reports a profile the +// config file does not have, or has with no host, as PROFILE_NOT_FOUND, before any request. +func TestMcpAddPickerRefusesMissingProfileBeforeAnyRequest(t *testing.T) { + home := withHome(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + failOnPrompt(t) + + err := runInteractiveAdd(t, "--profile", "logfdood", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + assert.Contains(t, err.Error(), "Databricks profile \"logfdood\", which does not exist in ") + assert.Contains(t, err.Error(), "Set it up with: databricks auth login --profile logfdood") + + writeHomeConfig(t, home, "[logfood]\nhost =\nauth_type = pat\ntoken = dapi-logfood\n") + err = runInteractiveAdd(t, "--profile", "logfood", "--agents", "claude-code") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileNotFound), errorCategory(err)) + assert.Contains(t, err.Error(), "Databricks profile \"logfood\", which has no host in ") +} + +// TestMcpAddPickerWithNoAgentMakesNoRequest checks that a run that picks its services ends when no agent is detected, +// before the workspace is contacted. +func TestMcpAddPickerWithNoAgentMakesNoRequest(t *testing.T) { + home := withHome(t) + require.NoError(t, os.RemoveAll(filepath.Join(home, ".claude"))) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + failOnPrompt(t) + + err := runInteractiveAdd(t, "--profile", "logfood") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(err)) + assert.Contains(t, err.Error(), "no MCP-capable coding agents detected") +} + +// TestMcpAddWithoutTerminalReportsNoAgentBeforeProfile checks that a run without a terminal and with no agent reports +// that before the profile, which here would be ambiguous with two profiles. +func TestMcpAddWithoutTerminalReportsNoAgentBeforeProfile(t *testing.T) { + home := withHome(t) + require.NoError(t, os.RemoveAll(filepath.Join(home, ".claude"))) + writeHomeConfig(t, home, "[logfood]\nhost = https://logfood.test\nauth_type = pat\ntoken = dapi-logfood\n"+ + "[dogfood]\nhost = https://dogfood.test\nauth_type = pat\ntoken = dapi-dogfood\n") + + err := runAdd(t, "--names", "github") + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(err)) + assert.Contains(t, err.Error(), "no MCP-capable coding agents detected") +} + +// TestMcpAddInTerminalRefusesWhenAgentCheckFails checks that a terminal with no agent installed for MCP, and an agent +// whose check fails, is refused in chooseAgents: the failure is named, nothing is picked and the check is not repeated. +func TestMcpAddInTerminalRefusesWhenAgentCheckFails(t *testing.T) { + withHome(t) + t.Setenv(env.HomeEnvVar(), "") + for _, name := range agentHomeEnv { + t.Setenv(name, "") + } + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + origPrompt := promptAgentSelection + t.Cleanup(func() { promptAgentSelection = origPrompt }) + promptAgentSelection = func(context.Context, string, []agentChoice) ([]*agents.Agent, error) { + t.Error("the run asked for agents with none installed") + return nil, nil + } + + claude := agents.ByName(agents.NameClaudeCode) + _, checkErr := claude.InstalledForMCP(ctx) + require.Error(t, checkErr, "the check needs a home directory") + + _, _, err := chooseAgents(ctx, nil, true) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategoryUnknownAgent, errorCategory(err)) + assert.Contains(t, err.Error(), "no MCP-capable coding agents detected") + assert.Contains(t, stderr.String(), fmt.Sprintf("Not found: %s (%s)", claude.DisplayName, checkErr)) +} + +// failOnPrompt makes every picker and question of an interactive run fail the test, so a test can show what a +// run asks. It restores the seams when the test ends. +func failOnPrompt(t *testing.T) { + t.Helper() + origProceed, origAgents, origServices, origProfile := promptProceed, promptAgentSelection, promptServiceSelection, selectProfile + origSignIn := promptSignInNow + t.Cleanup(func() { + promptProceed, promptAgentSelection, promptServiceSelection, selectProfile = origProceed, origAgents, origServices, origProfile + promptSignInNow = origSignIn + }) + promptProceed = func() (bool, error) { t.Error("the run asked to confirm the plan"); return false, nil } + promptAgentSelection = func(context.Context, string, []agentChoice) ([]*agents.Agent, error) { + t.Error("the run asked for agents") + return nil, nil + } + promptServiceSelection = func(string, []serviceRow) (servicePick, error) { + t.Error("the run asked for services") + return servicePick{}, nil + } + selectProfile = func(context.Context, profile.SelectConfig) (string, error) { + t.Error("the run asked for a profile") + return "", nil + } + promptSignInNow = func(string) (bool, error) { + t.Error("the run asked to sign in") + return false, nil + } +} + +// listingServer returns a workspace whose schema lists the MCP services in names, none of them sign-in-required. +// A run that binds a profile with a workspace_id lists the services and makes no other call. +func listingServer(t *testing.T, names ...string) *httptest.Server { + t.Helper() + services := make([]string, 0, len(names)) + for _, name := range names { + services = append(services, `{"name":"mcp-services/`+name+`"}`) + } + return listingServerJSON(t, services...) +} + +// signInListingServer returns a workspace whose schema lists the sign-in-required service name, whose source connection +// has an OAuth provider. The service is not signed in, so its credential read answers NOT_FOUND. +func signInListingServer(t *testing.T, name string) *httptest.Server { + t.Helper() + return listingServerJSON(t, `{"name":"mcp-services/`+name+`","config":{"source_connection":{"name":"github","options":{"oauth_provider":"github"}}}}`) +} + +// listingServerJSON returns a workspace whose schema lists services, each a JSON MCP service. A credential read answers +// NOT_FOUND, as for a caller not signed in; any other request is a failure. +func listingServerJSON(t *testing.T, services ...string) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if !strings.HasPrefix(r.URL.Path, "/api/") { + // The SDK reads host metadata before it calls the API; there is none here. + w.WriteHeader(http.StatusNotFound) + return + } + if strings.HasSuffix(r.URL.Path, "/user-credentials") { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"error_code":"NOT_FOUND","message":"not found"}`)) + return + } + if r.URL.Path != "/api/2.1/unity-catalog/mcp-services" { + t.Errorf("unexpected request: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + return + } + _, _ = w.Write([]byte(`{"mcp_services":[` + strings.Join(services, ",") + `]}`)) + })) + t.Cleanup(server.Close) + return server +} + +// TestMcpAddFullySpecifiedRunAsksNothing checks that a terminal run with the profile, the services and the agents +// given asks for nothing and installs. +func TestMcpAddFullySpecifiedRunAsksNothing(t *testing.T) { + home := withHome(t) + server := listingServer(t, "system.ai.dbsql") + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + failOnPrompt(t) + + require.NoError(t, runInteractiveAdd(t, "--profile", "logfood", "--names", "dbsql", "--agents", "claude-code")) + claude, err := os.ReadFile(filepath.Join(home, ".claude.json")) + require.NoError(t, err) + assert.Contains(t, string(claude), "dbsql-logfood") +} + +// TestMcpAddFullySpecifiedRunAsksOnlyTheSignInQuestion checks that a terminal run with the profile, the services and the +// agents given asks the sign-in question of a sign-in-required server, and nothing else. +func TestMcpAddFullySpecifiedRunAsksOnlyTheSignInQuestion(t *testing.T) { + home := withHome(t) + server := signInListingServer(t, "system.ai.github") + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + failOnPrompt(t) + var asked []string + promptSignInNow = func(service string) (bool, error) { + asked = append(asked, service) + return false, nil + } + + require.NoError(t, runInteractiveAdd(t, "--profile", "logfood", "--names", "github", "--agents", "claude-code")) + assert.Equal(t, []string{"system.ai.github"}, asked) + claude, err := os.ReadFile(filepath.Join(home, ".claude.json")) + require.NoError(t, err) + assert.Contains(t, string(claude), "github-logfood") +} + +// TestMcpAddDeclinedPlanWritesNothing checks that declining the confirm of a picked agent leaves the agent's +// config unwritten. +func TestMcpAddDeclinedPlanWritesNothing(t *testing.T) { + home := withHome(t) + server := listingServer(t, "system.ai.dbsql") + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nworkspace_id = 222\nauth_type = pat\ntoken = dapi-logfood\n") + failOnPrompt(t) + origAgents, origProceed := promptAgentSelection, promptProceed + t.Cleanup(func() { promptAgentSelection, promptProceed = origAgents, origProceed }) + promptAgentSelection = func(_ context.Context, _ string, choices []agentChoice) ([]*agents.Agent, error) { + return []*agents.Agent{agents.ByName(agents.NameClaudeCode)}, nil + } + var confirmed bool + promptProceed = func() (bool, error) { + confirmed = true + return false, nil + } + + require.NoError(t, runInteractiveAdd(t, "--profile", "logfood", "--names", "dbsql")) + assert.True(t, confirmed) + assert.NoFileExists(t, filepath.Join(home, ".claude.json")) + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Empty(t, state.Servers) +} + +// credentialServer returns a workspace whose credential reads of the services in active answer ACTIVE, and that counts +// its API reads in reads. The others answer NOT_FOUND, as the API does until the caller signs in. +func credentialServer(t *testing.T, reads *atomic.Int32, active ...string) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + if !strings.HasPrefix(r.URL.Path, "/api/") { + w.WriteHeader(http.StatusNotFound) + return + } + reads.Add(1) + for _, service := range active { + if r.URL.Path == "/api/2.1/unity-catalog/mcp-services/"+service+"/user-credentials" { + _, _ = w.Write([]byte(`{"provisioning_info":{"state":"ACTIVE"}}`)) + return + } + } + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"error_code":"NOT_FOUND","message":"not found"}`)) + })) + t.Cleanup(server.Close) + return server +} + +// signInRun returns the requests and plans of two sign-in-required servers that reached claude-code, and the workspace +// they sign in to. The servers are github and genie_one_mcp. +func signInRun(t *testing.T, reads *atomic.Int32, active ...string) (*databricks.WorkspaceClient, addRequest, []addPlan, []addServerOutput) { + t.Helper() + server := credentialServer(t, reads, active...) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "test-token"}) + require.NoError(t, err) + req := addRequest{profile: "logfood", workspaceID: "123", agents: []*agents.Agent{agents.ByName(agents.NameClaudeCode)}} + plans := []addPlan{ + {service: mcp.Service{FullName: "system.ai.github", SignInRequired: true}}, + {service: mcp.Service{FullName: "system.ai.genie_one_mcp", SignInRequired: true}}, + } + installed := []addAgentOutput{{Name: agents.NameClaudeCode, Status: statusInstalled}} + servers := []addServerOutput{ + {Name: "github-logfood", Service: "system.ai.github", SignInRequired: true, Agents: installed}, + {Name: "genie-logfood", Service: "system.ai.genie_one_mcp", SignInRequired: true, Agents: installed}, + } + return w, req, plans, servers +} + +// askSignIn overrides the sign-in question for a test, and returns the services it was asked about. +func askSignIn(t *testing.T, answer func(service string) (bool, error)) *[]string { + t.Helper() + orig := promptSignInNow + t.Cleanup(func() { promptSignInNow = orig }) + var asked []string + promptSignInNow = func(service string) (bool, error) { + asked = append(asked, service) + return answer(service) + } + return &asked +} + +// TestSignInInstalledAsksEachServer checks that each server not signed in is asked about in order, and that a declined +// one keeps its server installed and prints the login command. +func TestSignInInstalledAsksEachServer(t *testing.T) { + w, req, plans, servers := signInRun(t, new(atomic.Int32)) + req.askLogin = true + asked := askSignIn(t, func(string) (bool, error) { return false, nil }) + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + require.NoError(t, signInInstalled(ctx, io.Discard, w, req, plans, servers)) + assert.Equal(t, []string{"system.ai.github", "system.ai.genie_one_mcp"}, *asked) + assert.Contains(t, stderr.String(), "system.ai.github needs sign-in. Run: ") + assert.Contains(t, stderr.String(), "system.ai.genie_one_mcp needs sign-in. Run: ") +} + +// TestSignInInstalledSkipsTheQuestionWhenSignedIn checks that a server already signed in is not asked about, and +// that its sign-in is recorded as ACTIVE. +func TestSignInInstalledSkipsTheQuestionWhenSignedIn(t *testing.T) { + var reads atomic.Int32 + w, req, plans, servers := signInRun(t, &reads, "system.ai.github") + req.askLogin = true + asked := askSignIn(t, func(string) (bool, error) { return false, nil }) + + ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) + var errOut bytes.Buffer + require.NoError(t, signInInstalled(ctx, &errOut, w, req, plans, servers)) + assert.Equal(t, []string{"system.ai.genie_one_mcp"}, *asked) + require.NotNil(t, servers[0].SignIn) + assert.Equal(t, signInStateActive, servers[0].SignIn.State) + assert.Equal(t, "Already signed in to system.ai.github on profile logfood.\n", errOut.String()) + // Each server is read once, for its question. The sign-in of the signed-in server is not started, so it is not read again. + assert.Equal(t, int32(2), reads.Load()) +} + +// TestSignInInstalledAbortPrintsTheRestOfTheHints checks that an abort at the question ends the sign-ins, prints the +// login hint of each server not signed in, and returns the abort. +// TestSignedInGivesUpOnAHungCredentialRead checks that a credential read that never answers ends at profileCheckTimeout, +// so the sign-in question is asked, instead of holding it for the SDK's whole retry budget. +func TestSignedInGivesUpOnAHungCredentialRead(t *testing.T) { + origTimeout := profileCheckTimeout + profileCheckTimeout = 100 * time.Millisecond + t.Cleanup(func() { profileCheckTimeout = origTimeout }) + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Only the credential read hangs; the SDK's host metadata read at setup answers at once. + if !strings.HasSuffix(r.URL.Path, "/user-credentials") { + w.WriteHeader(http.StatusNotFound) + return + } + <-r.Context().Done() + })) + t.Cleanup(server.Close) + w, err := databricks.NewWorkspaceClient(&databricks.Config{Host: server.URL, Token: "dapi-test"}) + require.NoError(t, err) + + start := time.Now() + assert.False(t, signedIn(t.Context(), w, "system.ai.github")) + assert.Less(t, time.Since(start), 10*time.Second) +} + +func TestSignInInstalledAbortPrintsTheRestOfTheHints(t *testing.T) { + var reads atomic.Int32 + w, req, plans, servers := signInRun(t, &reads) + req.askLogin = true + asked := askSignIn(t, func(string) (bool, error) { return false, huh.ErrUserAborted }) + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + err := signInInstalled(ctx, io.Discard, w, req, plans, servers) + require.ErrorIs(t, err, huh.ErrUserAborted) + assert.Equal(t, protos.AitoolsErrorCategoryUserAborted, errorCategory(err)) + assert.Equal(t, []string{"system.ai.github"}, *asked, "the sign-ins stop at the abort") + // The only read is the one that asked about github: the hints of the rest are printed without a request. + assert.Equal(t, int32(1), reads.Load()) + assert.Contains(t, stderr.String(), "system.ai.github needs sign-in. Run: ") + assert.Contains(t, stderr.String(), "system.ai.genie_one_mcp needs sign-in. Run: ") +} + +// TestPlanAddsReplacesAForeignEntryAndReassertsOwnEntry checks that a plan names what the write does to an entry the +// agent already holds: a replace for an entry this CLI did not write, a re-assert for one of this binding. +func TestPlanAddsReplacesAForeignEntryAndReassertsOwnEntry(t *testing.T) { + home := withHome(t) + claude := agents.ByName(agents.NameClaudeCode) + targets := []*agents.Agent{claude} + config := filepath.Join(home, ".claude.json") + b := defaultBinding(t, "logfood") + + // A foreign entry under the name, replaced under --force. + require.NoError(t, os.WriteFile(config, []byte(`{"mcpServers":{"github-logfood":{"type":"stdio","command":"other"}}}`), 0o600)) + in := planInput{binding: b, nameFlag: "github-logfood", force: true, targets: targets, held: readHeld(t.Context(), targets)} + plans, err := planAdds(t.Context(), in, &mcp.State{}, []string{"system.ai.github"}) + require.NoError(t, err) + require.Len(t, plans, 1) + assert.True(t, plans[0].replaced[claude]) + assert.False(t, plans[0].reasserted[claude]) + req := addRequest{profile: "logfood", agents: targets} + assert.Equal(t, []string{"Plan (profile logfood):", fmt.Sprintf(" %-14s %s", claude.DisplayName, "replace github-logfood")}, addPlanLines(req, plans)) + + // An entry of this binding under the name is re-asserted. + require.NoError(t, os.WriteFile(config, []byte(`{"mcpServers":{"github-logfood":`+claudeEntry("system.ai.github", "logfood", "")+`}}`), 0o600)) + in.held = readHeld(t.Context(), targets) + in.nameFlag = "" + in.force = false + in.binary = "databricks" + plans, err = planAdds(t.Context(), in, &mcp.State{}, []string{"system.ai.github"}) + require.NoError(t, err) + require.Len(t, plans, 1) + assert.True(t, plans[0].reasserted[claude]) + assert.False(t, plans[0].replaced[claude]) + assert.False(t, plans[0].updated[claude]) + + // The same entry under a binary that moved is updated, not re-asserted: the write changes its command. + in.binary = "/opt/databricks/bin/databricks" + plans, err = planAdds(t.Context(), in, &mcp.State{}, []string{"system.ai.github"}) + require.NoError(t, err) + require.Len(t, plans, 1) + assert.True(t, plans[0].updated[claude]) + assert.False(t, plans[0].reasserted[claude]) + req = addRequest{profile: "logfood", agents: targets} + assert.Equal(t, []string{"Plan (profile logfood):", fmt.Sprintf(" %-14s %s", claude.DisplayName, "update github-logfood")}, addPlanLines(req, plans)) +} + +// TestValidateMCPAddFlagsNameNeedsServicesFromNames checks that --name without --names is refused before the +// workspace is read, in a terminal too, where the picker would otherwise choose the services. +func TestValidateMCPAddFlagsNameNeedsServicesFromNames(t *testing.T) { + ctx, test := cmdio.SetupTest(t.Context(), cmdio.TestOptions{PromptSupported: true}) + defer test.Done() + err := validateMCPAddFlags(ctx, mcpAddFlags{name: "github"}, nil, nil, false) + require.Error(t, err) + assert.Equal(t, "--name needs exactly one service; leave out --name to pick services in the picker", err.Error()) + assert.NoError(t, validateMCPAddFlags(ctx, mcpAddFlags{name: "github"}, []string{"github"}, nil, false)) +} + +func TestChooseAgentsSources(t *testing.T) { + withHome(t) + writeFakeCLI(t, agents.ByName(agents.NameCodex).Binary) + ctx := cmdio.MockDiscard(t.Context()) + origPrompt := promptAgentSelection + t.Cleanup(func() { promptAgentSelection = origPrompt }) + + var offered []agentChoice + promptAgentSelection = func(_ context.Context, _ string, choices []agentChoice) ([]*agents.Agent, error) { + offered = choices + return []*agents.Agent{choices[0].agent}, nil + } + + named := []*agents.Agent{agents.ByName(agents.NameCodex)} + got, source, err := chooseAgents(ctx, named, true) + require.NoError(t, err) + assert.Equal(t, agentsNamed, source) + assert.Equal(t, named, got) + assert.Empty(t, offered, "named agents are not asked for") + + got, source, err = chooseAgents(ctx, nil, true) + require.NoError(t, err) + assert.Equal(t, agentsPicked, source) + require.Len(t, got, 1) + // The picker offers installed agents only. + require.NotEmpty(t, offered) + for _, c := range offered { + installed, err := c.agent.InstalledForMCP(ctx) + require.NoError(t, err) + assert.True(t, installed, c.agent.Name) + } +} + +// TestChooseAgentsNamesTheAgentsNotFound checks that the picker lists the installed agents and names the others above it. +func TestChooseAgentsNamesTheAgentsNotFound(t *testing.T) { + withHome(t) + writeFakeCLI(t, agents.ByName(agents.NameCodex).Binary) + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + origPrompt := promptAgentSelection + t.Cleanup(func() { promptAgentSelection = origPrompt }) + promptAgentSelection = func(_ context.Context, _ string, choices []agentChoice) ([]*agents.Agent, error) { + return []*agents.Agent{choices[0].agent}, nil + } + + _, _, err := chooseAgents(ctx, nil, true) + require.NoError(t, err) + assert.Contains(t, stderr.String(), "Not found: ") + assert.Contains(t, stderr.String(), agents.ByName(agents.NameCursor).DisplayName) +} + +// TestAddPlanLinesSayWhenConfigCannotBeRead checks that an agent whose config cannot be read is shown as such, with the +// write that will fail, not as an add. +func TestAddPlanLinesSayWhenConfigCannotBeRead(t *testing.T) { + claude := agents.ByName(agents.NameClaudeCode) + req := addRequest{ + profile: "logfood", + agents: []*agents.Agent{claude}, + unreadable: map[*agents.Agent]error{claude: errors.New("parse error")}, + } + plans := []addPlan{{service: mcp.Service{FullName: "system.ai.github"}, name: "github-logfood"}} + assert.Equal(t, []string{"Plan (profile logfood):", fmt.Sprintf(" %-14s cannot be read: parse error; the write will fail", claude.DisplayName)}, addPlanLines(req, plans)) +} + +func TestAddPlanLines(t *testing.T) { + claude := agents.ByName(agents.NameClaudeCode) + codex := agents.ByName(agents.NameCodex) + req := addRequest{profile: "logfood", agents: []*agents.Agent{claude, codex}} + plans := []addPlan{ + {service: mcp.Service{FullName: "system.ai.github"}, name: "github-logfood"}, + { + service: mcp.Service{FullName: "system.ai.genie_one_mcp"}, + name: "genie_one_mcp-logfood", + reasserted: map[*agents.Agent]bool{claude: true}, + }, + } + want := []string{ + "Plan (profile logfood):", + fmt.Sprintf(" %-14s %s", claude.DisplayName, "add github-logfood; re-assert genie_one_mcp-logfood"), + fmt.Sprintf(" %-14s %s", codex.DisplayName, "add github-logfood, genie_one_mcp-logfood"), + } + assert.Equal(t, want, addPlanLines(req, plans)) + + // A server that an agent cannot take is skipped for that agent, and a refused agent is not written. + req.refused = map[*agents.Agent]error{codex: errNotInstalled} + plans[0].failed = map[*agents.Agent]error{claude: errors.New("conflict")} + want = []string{ + "Plan (profile logfood):", + fmt.Sprintf(" %-14s %s", claude.DisplayName, "re-assert genie_one_mcp-logfood; skip github-logfood"), + fmt.Sprintf(" %-14s %s", codex.DisplayName, "not installed; skipped"), + } + assert.Equal(t, want, addPlanLines(req, plans)) + + // An agent whose check failed is refused with the reason, not as not installed. + req.refused = map[*agents.Agent]error{codex: errors.New("no home")} + want = []string{ + "Plan (profile logfood):", + fmt.Sprintf(" %-14s %s", claude.DisplayName, "re-assert genie_one_mcp-logfood; skip github-logfood"), + fmt.Sprintf(" %-14s %s", codex.DisplayName, "cannot be checked: no home; skipped"), + } + assert.Equal(t, want, addPlanLines(req, plans)) +} + +func TestValidateMCPAddFlagsInTerminalAsksForMissingChoices(t *testing.T) { + ctx, test := cmdio.SetupTest(t.Context(), cmdio.TestOptions{PromptSupported: true}) + defer test.Done() + require.NoError(t, validateMCPAddFlags(ctx, mcpAddFlags{}, nil, nil, false)) +} diff --git a/cmd/aitools/mcp_login.go b/cmd/aitools/mcp_login.go index 4459f93b77e..26b222a4479 100644 --- a/cmd/aitools/mcp_login.go +++ b/cmd/aitools/mcp_login.go @@ -275,6 +275,12 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient return "", false, urlShown, describeRequestError(profileName, pinnedFile, err) } + reportSignedIn(errOut, svc, profileName, alreadySignedIn) + return signInStateActive, alreadySignedIn, urlShown, nil +} + +// reportSignedIn prints that the profile is signed in to svc, and the note that the service has for its users. +func reportSignedIn(errOut io.Writer, svc mcp.Service, profileName string, alreadySignedIn bool) { if alreadySignedIn { fmt.Fprintf(errOut, "Already signed in to %s on profile %s.\n", svc.FullName, profileName) } else { @@ -283,5 +289,4 @@ func signIn(ctx context.Context, errOut io.Writer, w *databricks.WorkspaceClient if svc.Known != nil && svc.Known.Note != "" { fmt.Fprintln(errOut, svc.Known.Note) } - return signInStateActive, alreadySignedIn, urlShown, nil } diff --git a/cmd/aitools/mcp_pickers.go b/cmd/aitools/mcp_pickers.go new file mode 100644 index 00000000000..a66c4739ba5 --- /dev/null +++ b/cmd/aitools/mcp_pickers.go @@ -0,0 +1,346 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "slices" + "strings" + + "github.com/charmbracelet/huh" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/telemetry/protos" +) + +// Package-level seams for the interactive pickers of `mcp add` and `mcp remove`. Tests override +// them. The agent picker and the plan confirm are shared with `install` (promptAgentSelection and +// promptProceed in install.go). +var ( + selectProfile = profile.SelectProfile + promptServiceSelection = defaultPromptServiceSelection + promptLocation = defaultPromptLocation + promptRemoveSelection = defaultPromptRemoveSelection + promptSignInNow = defaultPromptSignInNow +) + +// anotherSchemaValue is the value of the "Another schema..." row. It has no dots, so it cannot be +// a service full name. +const anotherSchemaValue = "(another schema)" + +// serviceLister lists the MCP services of a schema. +type serviceLister func(ctx context.Context, location string) ([]mcp.Service, error) + +// serviceRow is one row of the service picker: a service of the schema, whether the profile already has it +// installed, and whether it starts checked. +type serviceRow struct { + service mcp.Service + installed bool + checked bool +} + +// servicePick is what the service picker returns: the rows checked, and whether "Another schema..." +// was checked. +type servicePick struct { + services []mcp.Service + another bool +} + +// profileChoice is the cause of the failure that resolveProfile returns when several workspace profiles exist and none +// is the default. It holds those profiles, so that the picker offers the same ones. +type profileChoice struct { + profiles profile.Profiles +} + +func (e *profileChoice) Error() string { return "several workspace profiles" } + +// promptProfile asks which of the profiles the servers bind to. Ctrl+C reads as the abort of the other pickers. +func promptProfile(ctx context.Context, profiles profile.Profiles) (string, error) { + name, err := selectProfile(ctx, profile.SelectConfig{ + Label: "Which Databricks profile should these MCP servers use?", + Profiles: profiles, + }) + if errors.Is(err, cmdio.ErrCtrlC) { + return "", huh.ErrUserAborted + } + return name, err +} + +// chooseProfile returns the profile for an add, and whether a picker chose it. A terminal resolves the profile as a +// non-interactive run does: --profile, DATABRICKS_CONFIG_PROFILE, the default profile, then the only workspace profile. +// The picker runs only where that stops with several profiles. +func chooseProfile(ctx context.Context, flagValue string, interactive bool) (string, bool, error) { + name, err := resolveProfile(ctx, flagValue) + if err == nil || !interactive { + return name, false, err + } + choice, ok := errors.AsType[*profileChoice](err) + if !ok { + return name, false, err + } + name, err = promptProfile(ctx, choice.profiles) + return name, true, err +} + +// mcpAgentChoices returns the agent picker rows: the MCP-capable agents installed for MCP, all checked. It also returns +// the MCP-capable agents the picker does not list, each with the reason when its check failed, as a named agent reports it. +func mcpAgentChoices(ctx context.Context) (choices []agentChoice, notFound []string) { + for _, a := range agents.MCPAgents() { + installed, err := a.InstalledForMCP(ctx) + switch { + case err != nil: + notFound = append(notFound, fmt.Sprintf("%s (%s)", a.DisplayName, err)) + case !installed: + notFound = append(notFound, a.DisplayName) + default: + choices = append(choices, agentChoice{agent: a, label: a.DisplayName, preselect: true}) + } + } + return choices, notFound +} + +// pickServicesInteractively lists the services of location and returns the ones the user checks. +// "Another schema..." asks for a catalog and schema and lists that one next; an empty answer stops browsing and keeps +// the picks. A schema is listed once per session, and a schema shown again starts with its checks, which the new page +// replaces. A schema that cannot be listed after the first one is reported and asked for again, with the checks kept. +// Services already installed for the profile are rows too, unchecked and marked installed, so that they can be added to +// another agent; they are also named above the picker. +func pickServicesInteractively(ctx context.Context, b addBinding, location string, state *mcp.State, list serviceLister) ([]mcp.Service, error) { + // checks holds the services checked on each schema page, listings the services of each schema listed, and schemas + // the pages in the order shown. + checks := map[string][]mcp.Service{} + listings := map[string][]mcp.Service{} + var schemas []string + for { + services, listed := listings[location] + if !listed { + var err error + if services, err = list(ctx, location); err != nil { + if len(schemas) == 0 { + return nil, err + } + cmdio.LogString(ctx, fmt.Sprintf("Cannot list MCP servers in %s: %s", location, err)) + if location, err = promptLocation(); err != nil { + return nil, err + } + if location == "" { + break + } + continue + } + listings[location] = services + cmdio.LogString(ctx, fmt.Sprintf("Discovering MCP servers in %s on profile %s... %d found.", location, b.profile, len(services))) + } + // A page shown first starts with the recommended services that are not installed checked; a page shown + // again starts with its checks. + firstVisit := !slices.Contains(schemas, location) + var installed []string + rows := make([]serviceRow, 0, len(services)) + for _, s := range services { + // An entry this profile cannot bind through, as one recorded under another config file, is not shown as + // installed. The check of the picked services reports it, and one service's entry does not stop the list. + entry, err := mcp.FindEntry(ctx, state, s.FullName, b.profile, b.configFile) + isInstalled := err == nil && entry != nil + if isInstalled { + installed = append(installed, s.Leaf) + } + // A row whose entry cannot be read is refused by the run, so it is not checked. + checked := err == nil && !isInstalled && s.Known != nil && s.Known.Recommended + if !firstVisit { + checked = slices.ContainsFunc(checks[location], func(p mcp.Service) bool { return p.FullName == s.FullName }) + } + rows = append(rows, serviceRow{service: s, installed: isInstalled, checked: checked}) + } + if len(installed) > 0 { + cmdio.LogString(ctx, fmt.Sprintf("Already installed for profile %s: %s", b.profile, strings.Join(installed, ", "))) + } + pick, err := promptServiceSelection(location, rows) + if err != nil { + return nil, err + } + if firstVisit { + schemas = append(schemas, location) + } + checks[location] = pick.services + if !pick.another { + break + } + if location, err = promptLocation(); err != nil { + return nil, err + } + if location == "" { + break + } + } + + var picked []mcp.Service + for _, schema := range schemas { + picked = append(picked, checks[schema]...) + } + if len(picked) == 0 { + return nil, failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "select at least one MCP service") + } + return picked, nil +} + +// typedLocation returns a catalog and schema typed by the user in the form --location takes: trimmed and +// in lowercase. +func typedLocation(s string) string { + return strings.ToLower(strings.TrimSpace(s)) +} + +// serviceChoiceLabel is a service picker row: the leaf name, its description and its tags. The +// CLI's own description wins for a known system.ai service; otherwise the API comment is shown. +func serviceChoiceLabel(s mcp.Service) string { + desc := s.Comment + var tags []string + if k := s.Known; k != nil { + desc = k.Summary + if k.Recommended { + tags = append(tags, "recommended") + } + if k.Beta { + tags = append(tags, "beta") + } + } + if s.SignInRequired { + tags = append(tags, "sign-in required") + } + label := s.Leaf + if desc != "" { + label += " " + desc + } + if len(tags) > 0 { + label += " (" + strings.Join(tags, ", ") + ")" + } + return label +} + +// serviceRowLabel is the label of a service picker row. A service the profile has installed is marked so. +func serviceRowLabel(r serviceRow) string { + if r.installed { + return serviceChoiceLabel(r.service) + " (installed)" + } + return serviceChoiceLabel(r.service) +} + +// defaultPromptServiceSelection is the huh multi-select over the rows of a schema. +func defaultPromptServiceSelection(location string, rows []serviceRow) (servicePick, error) { + options := make([]huh.Option[string], 0, len(rows)+1) + for _, r := range rows { + options = append(options, huh.NewOption(serviceRowLabel(r), r.service.FullName).Selected(r.checked)) + } + options = append(options, huh.NewOption("Another schema...", anotherSchemaValue)) + + var selected []string + err := huh.NewMultiSelect[string](). + Title("Select MCP servers in " + location). + Description(multiSelectHelp). + Options(options...). + Value(&selected). + Run() + if err != nil { + return servicePick{}, err + } + + var pick servicePick + for _, name := range selected { + if name == anotherSchemaValue { + pick.another = true + continue + } + i := slices.IndexFunc(rows, func(r serviceRow) bool { return r.service.FullName == name }) + pick.services = append(pick.services, rows[i].service) + } + return pick, nil +} + +// defaultPromptLocation asks for the catalog and schema to list next. +func defaultPromptLocation() (string, error) { + var location string + err := huh.NewInput(). + Title("Catalog and schema to list MCP servers from (leave empty to stop)"). + Value(&location). + Validate(func(s string) error { + if typed := typedLocation(s); typed != "" && validateLocation(typed) != nil { + return errors.New("enter .") + } + return nil + }). + Run() + return typedLocation(location), err +} + +// chooseRemovals runs the remove picker over the installed servers for targets: only the servers installed for the +// named agents, or every server when targets is nil. Only the agents named with --agents lose a pick, so the picks +// need no agent check. offered is false when no server is offered, and then nothing is asked. +func chooseRemovals(state *mcp.State, targets []*agents.Agent) (picks []mcp.Entry, offered bool, err error) { + var entries []mcp.Entry + for _, e := range state.Servers { + if targets == nil || slices.ContainsFunc(targets, func(a *agents.Agent) bool { + _, ok := e.Agents[a.Name] + return ok + }) { + entries = append(entries, e) + } + } + if len(entries) == 0 { + return nil, false, nil + } + picks, err = promptRemoveSelection(entries, targets) + return picks, true, err +} + +// removeChoiceLabel is a remove picker row: server name, service, profile and the agents the pick is removed from, +// which are the named agents when --agents is set. +func removeChoiceLabel(e mcp.Entry, targets []*agents.Agent) string { + var names []string + for name := range e.Agents { + if targets == nil || slices.ContainsFunc(targets, func(a *agents.Agent) bool { return a.Name == name }) { + names = append(names, name) + } + } + slices.Sort(names) + return fmt.Sprintf("%s %s %s %s", e.Name, e.Service, e.Profile, mcpAgentList(names)) +} + +// defaultPromptRemoveSelection is the huh multi-select over the installed servers. +func defaultPromptRemoveSelection(entries []mcp.Entry, targets []*agents.Agent) ([]mcp.Entry, error) { + options := make([]huh.Option[int], 0, len(entries)) + for i, e := range entries { + options = append(options, huh.NewOption(removeChoiceLabel(e, targets), i)) + } + + var selected []int + err := huh.NewMultiSelect[int](). + Title("Select MCP servers to remove"). + Description(multiSelectHelp). + Options(options...). + Value(&selected). + Run() + if err != nil { + return nil, err + } + + chosen := make([]mcp.Entry, 0, len(selected)) + for _, i := range selected { + chosen = append(chosen, entries[i]) + } + return chosen, nil +} + +// defaultPromptSignInNow asks whether to start the sign-in of a service now. The default is yes. The sign-in names the +// profile once it starts, so the question does not. +func defaultPromptSignInNow(service string) (bool, error) { + yes := true + err := huh.NewConfirm(). + Title(service + " needs a one-time sign-in. Sign in now?"). + Value(&yes). + Run() + if err != nil { + return false, err + } + return yes, nil +} diff --git a/cmd/aitools/mcp_pickers_test.go b/cmd/aitools/mcp_pickers_test.go new file mode 100644 index 00000000000..7063b20f517 --- /dev/null +++ b/cmd/aitools/mcp_pickers_test.go @@ -0,0 +1,584 @@ +package aitools + +import ( + "context" + "errors" + "fmt" + "path/filepath" + "slices" + "strings" + "testing" + + "github.com/charmbracelet/huh" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/telemetry/protos" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestPickServicesInteractivelyKeepsChecksAcrossSchemas(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + state := &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood"}}} + schemas := map[string][]mcp.Service{ + "system.ai": {{FullName: "system.ai.github", Leaf: "github"}, {FullName: "system.ai.dbsql", Leaf: "dbsql"}}, + "analytics.tools": {{FullName: "analytics.tools.search", Leaf: "search"}}, + } + var listed []string + list := func(_ context.Context, location string) ([]mcp.Service, error) { + listed = append(listed, location) + return schemas[location], nil + } + + var rowsPerPage [][]string + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + promptServiceSelection = func(location string, rows []serviceRow) (servicePick, error) { + var names []string + for _, r := range rows { + names = append(names, r.service.FullName) + // The installed github row is shown, marked installed and unchecked. + if r.service.FullName == "system.ai.github" { + assert.True(t, r.installed) + assert.False(t, r.checked) + } + } + rowsPerPage = append(rowsPerPage, names) + if location == "system.ai" { + // Checks dbsql, then asks for another schema. + return servicePick{services: []mcp.Service{rows[1].service}, another: true}, nil + } + return servicePick{services: []mcp.Service{rows[0].service}}, nil + } + origLocation := promptLocation + t.Cleanup(func() { promptLocation = origLocation }) + promptLocation = func() (string, error) { return "analytics.tools", nil } + + picked, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", state, list) + require.NoError(t, err) + assert.Equal(t, []string{"system.ai", "analytics.tools"}, listed) + assert.Equal(t, [][]string{{"system.ai.github", "system.ai.dbsql"}, {"analytics.tools.search"}}, rowsPerPage) + var names []string + for _, s := range picked { + names = append(names, s.FullName) + } + assert.Equal(t, []string{"system.ai.dbsql", "analytics.tools.search"}, names) +} + +// TestPickServicesInteractivelyRevisitShowsAndReplacesChecks checks that a schema shown again starts with the +// services checked on it before, and that its new page replaces them, so an unchecked service is dropped. +func TestPickServicesInteractivelyRevisitShowsAndReplacesChecks(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + schemas := map[string][]mcp.Service{ + "system.ai": {{FullName: "system.ai.github", Leaf: "github"}, {FullName: "system.ai.dbsql", Leaf: "dbsql"}}, + "analytics.tools": {{FullName: "analytics.tools.search", Leaf: "search"}}, + } + listed := map[string]int{} + list := func(_ context.Context, location string) ([]mcp.Service, error) { + listed[location]++ + return schemas[location], nil + } + + // Each page answers with the rows it is given, checked or not, and the schema sequence the user typed. + type page struct { + location string + checked map[string]bool + pick []string + another bool + } + pages := []page{ + {location: "system.ai", checked: map[string]bool{"system.ai.github": false, "system.ai.dbsql": false}, pick: []string{"system.ai.dbsql"}, another: true}, + {location: "analytics.tools", checked: map[string]bool{"analytics.tools.search": false}, pick: []string{"analytics.tools.search"}, another: true}, + // Back on system.ai, dbsql is shown checked; the user unchecks it and checks github. + {location: "system.ai", checked: map[string]bool{"system.ai.github": false, "system.ai.dbsql": true}, pick: []string{"system.ai.github"}, another: true}, + // Back on analytics.tools, search is shown checked and the user finishes there. + {location: "analytics.tools", checked: map[string]bool{"analytics.tools.search": true}, pick: []string{"analytics.tools.search"}}, + } + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + promptServiceSelection = func(location string, rows []serviceRow) (servicePick, error) { + require.NotEmpty(t, pages, "more pages than expected") + want := pages[0] + pages = pages[1:] + require.Equal(t, want.location, location) + for _, r := range rows { + assert.Equal(t, want.checked[r.service.FullName], r.checked, r.service.FullName) + } + var picked []mcp.Service + for _, name := range want.pick { + for _, s := range schemas[location] { + if s.FullName == name { + picked = append(picked, s) + } + } + } + return servicePick{services: picked, another: want.another}, nil + } + locations := []string{"analytics.tools", "system.ai", "analytics.tools"} + origLocation := promptLocation + t.Cleanup(func() { promptLocation = origLocation }) + promptLocation = func() (string, error) { + next := locations[0] + locations = locations[1:] + return next, nil + } + + picked, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", &mcp.State{}, list) + require.NoError(t, err) + require.Empty(t, pages, "every page was shown") + var names []string + for _, s := range picked { + names = append(names, s.FullName) + } + assert.Equal(t, []string{"system.ai.github", "analytics.tools.search"}, names) + // A schema shown again is not listed again. + assert.Equal(t, map[string]int{"system.ai": 1, "analytics.tools": 1}, listed) +} + +// TestPickServicesInteractivelyEmptyLocationStopsAndKeepsPicks checks that an empty answer to "Another schema" stops +// browsing: the picks of every page so far are kept, and no other schema is listed. +func TestPickServicesInteractivelyEmptyLocationStopsAndKeepsPicks(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + list := func(_ context.Context, location string) ([]mcp.Service, error) { + require.Equal(t, "system.ai", location, "no schema is listed after the empty answer") + return []mcp.Service{{FullName: "system.ai.github", Leaf: "github"}, {FullName: "system.ai.dbsql", Leaf: "dbsql"}}, nil + } + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + promptServiceSelection = func(_ string, rows []serviceRow) (servicePick, error) { + return servicePick{services: []mcp.Service{rows[0].service}, another: true}, nil + } + origLocation := promptLocation + t.Cleanup(func() { promptLocation = origLocation }) + asked := 0 + promptLocation = func() (string, error) { + asked++ + return "", nil + } + + picked, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", &mcp.State{}, list) + require.NoError(t, err) + assert.Equal(t, 1, asked) + require.Len(t, picked, 1) + assert.Equal(t, "system.ai.github", picked[0].FullName) +} + +// TestPickServicesInteractivelyKeepsPicksWhenAnotherSchemaFails checks that a schema that cannot be listed is +// reported and asked for again, and that the services picked before are kept. A failure on the first schema +// ends the run, since nothing is picked yet. +func TestPickServicesInteractivelyKeepsPicksWhenAnotherSchemaFails(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + list := func(_ context.Context, location string) ([]mcp.Service, error) { + switch location { + case "system.ai": + return []mcp.Service{{FullName: "system.ai.github", Leaf: "github"}}, nil + case "analytics.tools": + return []mcp.Service{{FullName: "analytics.tools.search", Leaf: "search"}}, nil + } + return nil, errors.New("schema not found") + } + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + promptServiceSelection = func(_ string, rows []serviceRow) (servicePick, error) { + return servicePick{services: []mcp.Service{rows[0].service}, another: rows[0].service.FullName == "system.ai.github"}, nil + } + // The user types a typo first, then the schema that exists. The prompt returns the location trimmed and in lowercase. + typed := []string{"analytcs.tools", "analytics.tools"} + origLocation := promptLocation + t.Cleanup(func() { promptLocation = origLocation }) + promptLocation = func() (string, error) { + next := typed[0] + typed = typed[1:] + return next, nil + } + + picked, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", &mcp.State{}, list) + require.NoError(t, err) + var names []string + for _, s := range picked { + names = append(names, s.FullName) + } + assert.Equal(t, []string{"system.ai.github", "analytics.tools.search"}, names) + + // A failure on the first schema is returned, since nothing is picked yet. + _, err = pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "missing.schema", &mcp.State{}, list) + assert.EqualError(t, err, "schema not found") +} + +// TestPickServicesInteractivelyEntryUnderOtherConfigFile checks that a service recorded under another config file does +// not stop the picker for the other services: its row is not marked installed, and the check of the picked services +// reports the binding. +func TestPickServicesInteractivelyEntryUnderOtherConfigFile(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + state := &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", ConfigFile: filepath.Join(t.TempDir(), "other.cfg"), + }}} + list := func(context.Context, string) ([]mcp.Service, error) { + return []mcp.Service{{FullName: "system.ai.github", Leaf: "github"}, {FullName: "system.ai.dbsql", Leaf: "dbsql"}}, nil + } + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + promptServiceSelection = func(_ string, rows []serviceRow) (servicePick, error) { + require.Len(t, rows, 2) + assert.False(t, rows[0].installed) + assert.False(t, rows[1].installed) + return servicePick{services: []mcp.Service{rows[1].service}}, nil + } + + picked, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", state, list) + require.NoError(t, err) + require.Len(t, picked, 1) + assert.Equal(t, "system.ai.dbsql", picked[0].FullName) +} + +// TestPickServicesInteractivelyUncheckableEntryIsNotChecked checks that a recommended service whose entry cannot be read +// is not checked: the run refuses it, so the picker must not offer it as a pick. +func TestPickServicesInteractivelyUncheckableEntryIsNotChecked(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + state := &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", ConfigFile: filepath.Join(t.TempDir(), "other.cfg"), + }}} + list := func(context.Context, string) ([]mcp.Service, error) { + return []mcp.Service{ + {FullName: "system.ai.github", Leaf: "github", Known: &mcp.KnownService{Recommended: true}}, + {FullName: "system.ai.dbsql", Leaf: "dbsql", Known: &mcp.KnownService{Recommended: true}}, + }, nil + } + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + promptServiceSelection = func(_ string, rows []serviceRow) (servicePick, error) { + require.Len(t, rows, 2) + assert.False(t, rows[0].checked, "the entry under another config file must not start checked") + assert.True(t, rows[1].checked) + return servicePick{services: []mcp.Service{rows[1].service}}, nil + } + + _, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", state, list) + require.NoError(t, err) +} + +// TestPickServicesInteractivelyAnnouncesEachListingOnce checks that the discovery line is printed for each listing that +// runs, and not again when a schema is shown again. +func TestPickServicesInteractivelyAnnouncesEachListingOnce(t *testing.T) { + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + schemas := map[string][]mcp.Service{ + "system.ai": {{FullName: "system.ai.github", Leaf: "github"}}, + "analytics.tools": {{FullName: "analytics.tools.search", Leaf: "search"}}, + } + list := func(_ context.Context, location string) ([]mcp.Service, error) { + return schemas[location], nil + } + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + pages := []struct { + another bool + }{{another: true}, {another: true}, {}} + promptServiceSelection = func(_ string, rows []serviceRow) (servicePick, error) { + page := pages[0] + pages = pages[1:] + return servicePick{services: []mcp.Service{rows[0].service}, another: page.another}, nil + } + locations := []string{"analytics.tools", "system.ai"} + origLocation := promptLocation + t.Cleanup(func() { promptLocation = origLocation }) + promptLocation = func() (string, error) { + next := locations[0] + locations = locations[1:] + return next, nil + } + + _, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", &mcp.State{}, list) + require.NoError(t, err) + assert.Empty(t, pages) + assert.Equal(t, 2, strings.Count(stderr.String(), "Discovering MCP servers in "), stderr.String()) +} + +func TestPickServicesInteractivelyNeedsOneService(t *testing.T) { + ctx := cmdio.MockDiscard(t.Context()) + list := func(context.Context, string) ([]mcp.Service, error) { + return []mcp.Service{{FullName: "system.ai.dbsql", Leaf: "dbsql"}}, nil + } + origPick := promptServiceSelection + t.Cleanup(func() { promptServiceSelection = origPick }) + promptServiceSelection = func(string, []serviceRow) (servicePick, error) { + return servicePick{}, nil + } + + _, err := pickServicesInteractively(ctx, defaultBinding(t, "logfood"), "system.ai", &mcp.State{}, list) + require.Error(t, err) + assert.Equal(t, "select at least one MCP service", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) +} + +func TestServiceChoiceLabel(t *testing.T) { + cases := []struct { + name string + service mcp.Service + want string + }{ + { + name: "known and recommended", + service: mcp.Service{Leaf: "genie_one_mcp", Comment: "api text", Known: &mcp.KnownService{ + Summary: "Ask questions about your data", Recommended: true, + }}, + want: "genie_one_mcp Ask questions about your data (recommended)", + }, + { + name: "sign-in required uses the API comment", + service: mcp.Service{Leaf: "search", Comment: "Search docs", SignInRequired: true}, + want: "search Search docs (sign-in required)", + }, + { + name: "no description", + service: mcp.Service{Leaf: "bare"}, + want: "bare", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, serviceChoiceLabel(tc.service)) + }) + } +} + +func TestChooseProfileDefaultIsNotAsked(t *testing.T) { + writeDatabricksCfg(t, "[__settings__]\ndefault_profile = dogfood\n\n[logfood]\nhost = https://logfood.test\n\n[dogfood]\nhost = https://dogfood.test\n") + + origSelect := selectProfile + t.Cleanup(func() { selectProfile = origSelect }) + selectProfile = func(context.Context, profile.SelectConfig) (string, error) { + t.Fatal("the picker must not run when the default profile is configured") + return "", nil + } + + name, picked, err := chooseProfile(t.Context(), "", true) + require.NoError(t, err) + assert.Equal(t, "dogfood", name) + assert.False(t, picked) +} + +func TestChooseProfilePickerListsEveryProfile(t *testing.T) { + writeDatabricksCfg(t, "[logfood]\nhost = https://logfood.test\n\n[dogfood]\nhost = https://dogfood.test\n") + + origSelect := selectProfile + t.Cleanup(func() { selectProfile = origSelect }) + var got profile.SelectConfig + selectProfile = func(_ context.Context, cfg profile.SelectConfig) (string, error) { + got = cfg + return "logfood", nil + } + + name, picked, err := chooseProfile(t.Context(), "", true) + require.NoError(t, err) + assert.Equal(t, "logfood", name) + assert.True(t, picked) + assert.Len(t, got.Profiles, 2) +} + +// TestChooseProfileCtrlCIsTheOtherPickersAbort checks that Ctrl+C at the profile picker aborts as the other pickers do: +// the same error, categorized as USER_ABORTED. +func TestChooseProfileCtrlCIsTheOtherPickersAbort(t *testing.T) { + writeDatabricksCfg(t, "[logfood]\nhost = https://logfood.test\n\n[dogfood]\nhost = https://dogfood.test\n") + + origSelect := selectProfile + t.Cleanup(func() { selectProfile = origSelect }) + selectProfile = func(context.Context, profile.SelectConfig) (string, error) { + return "", cmdio.ErrCtrlC + } + + _, picked, err := chooseProfile(t.Context(), "", true) + require.ErrorIs(t, err, huh.ErrUserAborted) + assert.True(t, picked) + assert.Equal(t, "user aborted", err.Error()) + assert.Equal(t, protos.AitoolsErrorCategoryUserAborted, errorCategory(err)) +} + +func TestChooseProfileSingleProfileIsNotAsked(t *testing.T) { + writeDatabricksCfg(t, "[logfood]\nhost = https://logfood.test\n") + + origSelect := selectProfile + t.Cleanup(func() { selectProfile = origSelect }) + selectProfile = func(context.Context, profile.SelectConfig) (string, error) { + t.Fatal("the picker must not run with one workspace profile") + return "", nil + } + + name, picked, err := chooseProfile(t.Context(), "", true) + require.NoError(t, err) + assert.Equal(t, "logfood", name) + assert.False(t, picked) +} + +func TestChooseProfileNoProfiles(t *testing.T) { + writeDatabricksCfg(t, "") + _, _, err := chooseProfile(t.Context(), "", true) + require.Error(t, err) + assert.Contains(t, err.Error(), "no workspace profiles in") +} + +func TestChooseProfileWithoutTerminalDoesNotPrompt(t *testing.T) { + writeDatabricksCfg(t, "[logfood]\nhost = https://logfood.test\n\n[dogfood]\nhost = https://dogfood.test\n") + + origSelect := selectProfile + t.Cleanup(func() { selectProfile = origSelect }) + selectProfile = func(context.Context, profile.SelectConfig) (string, error) { + t.Fatal("the picker must not run without a terminal") + return "", nil + } + + _, _, err := chooseProfile(t.Context(), "", false) + require.Error(t, err) + assert.Contains(t, err.Error(), "several workspace profiles exist; pass --profile") + + name, picked, err := chooseProfile(t.Context(), "dogfood", true) + require.NoError(t, err) + assert.Equal(t, "dogfood", name) + assert.False(t, picked) +} + +// TestChooseProfileEnvironmentIsAnExplicitChoice checks that DATABRICKS_CONFIG_PROFILE chooses the profile in a +// terminal too, as --profile does: the picker does not run, and the profile resolves as it does without a terminal. +func TestChooseProfileEnvironmentIsAnExplicitChoice(t *testing.T) { + writeDatabricksCfg(t, "[logfood]\nhost = https://logfood.test\n\n[dogfood]\nhost = https://dogfood.test\n") + t.Setenv("DATABRICKS_CONFIG_PROFILE", "dogfood") + + origSelect := selectProfile + t.Cleanup(func() { selectProfile = origSelect }) + selectProfile = func(context.Context, profile.SelectConfig) (string, error) { + t.Fatal("the picker must not run when DATABRICKS_CONFIG_PROFILE is set") + return "", nil + } + + name, picked, err := chooseProfile(t.Context(), "", true) + require.NoError(t, err) + assert.Equal(t, "dogfood", name) + assert.False(t, picked) + + // A variable that names a profile missing from the file is what a non-interactive run gets too. + t.Setenv("DATABRICKS_CONFIG_PROFILE", "missing") + interactive, _, err := chooseProfile(t.Context(), "", true) + require.NoError(t, err) + plain, _, err := chooseProfile(t.Context(), "", false) + require.NoError(t, err) + assert.Equal(t, "missing", interactive) + assert.Equal(t, plain, interactive) +} + +func TestMcpAgentChoicesListInstalledAgents(t *testing.T) { + withHome(t) + writeFakeCLI(t, agents.ByName(agents.NameCodex).Binary) + choices, notFound := mcpAgentChoices(t.Context()) + + var listed []string + for _, c := range choices { + assert.True(t, c.agent.SupportsMCP(), c.agent.Name) + assert.True(t, c.preselect, c.agent.Name) + listed = append(listed, c.agent.Name) + } + assert.Contains(t, listed, agents.NameCodex) + assert.NotContains(t, listed, agents.NameCursor) + assert.Contains(t, notFound, agents.ByName(agents.NameCursor).DisplayName) + assert.NotContains(t, notFound, agents.ByName(agents.NameCodex).DisplayName) +} + +// TestMcpAgentChoicesFollowInstalledForMCP checks that an agent is listed when InstalledForMCP says so: Codex on PATH +// counts as installed for MCP, even without a ~/.codex directory, and with CODEX_HOME set only that directory counts. +// TestMcpAgentChoicesNamesAgentWhoseCheckFailed checks that an agent whose installed check fails is not offered, and is +// named as not found with that error, as a named agent reports it. +func TestMcpAgentChoicesNamesAgentWhoseCheckFailed(t *testing.T) { + t.Setenv(env.HomeEnvVar(), "") + t.Setenv("PATH", t.TempDir()) + for _, name := range agentHomeEnv { + t.Setenv(name, "") + } + ctx := cmdio.MockDiscard(t.Context()) + claude := agents.ByName(agents.NameClaudeCode) + _, checkErr := claude.InstalledForMCP(ctx) + require.Error(t, checkErr, "the check needs a home directory") + + choices, notFound := mcpAgentChoices(ctx) + for _, c := range choices { + assert.NotEqual(t, claude, c.agent) + } + assert.Contains(t, notFound, fmt.Sprintf("%s (%s)", claude.DisplayName, checkErr)) +} + +func TestMcpAgentChoicesFollowInstalledForMCP(t *testing.T) { + home := withHome(t) + writeFakeCLI(t, agents.ByName(agents.NameCodex).Binary) + assert.True(t, listsAgent(t, agents.NameCodex)) + + // The binary on PATH does not count when CODEX_HOME names a directory that is not there, and nothing is created. + missing := filepath.Join(home, "missing-codex-home") + t.Setenv(codexHomeEnv, missing) + assert.False(t, listsAgent(t, agents.NameCodex)) + assert.NoDirExists(t, missing) +} + +// listsAgent reports whether the agent picker lists the agent named name. +func listsAgent(t *testing.T, name string) bool { + t.Helper() + choices, _ := mcpAgentChoices(t.Context()) + return slices.ContainsFunc(choices, func(c agentChoice) bool { return c.agent.Name == name }) +} + +func TestChooseRemovals(t *testing.T) { + origRemove := promptRemoveSelection + t.Cleanup(func() { promptRemoveSelection = origRemove }) + promptRemoveSelection = func([]mcp.Entry, []*agents.Agent) ([]mcp.Entry, error) { + t.Fatal("the picker must not run with nothing installed") + return nil, nil + } + got, offered, err := chooseRemovals(&mcp.State{}, nil) + require.NoError(t, err) + assert.False(t, offered) + assert.Empty(t, got) + + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "github-logfood", Service: "system.ai.github", Profile: "logfood"}, + {Name: "github-dogfood", Service: "system.ai.github", Profile: "dogfood"}, + }} + promptRemoveSelection = func(entries []mcp.Entry, _ []*agents.Agent) ([]mcp.Entry, error) { + assert.Len(t, entries, 2) + return entries[1:], nil + } + got, offered, err = chooseRemovals(state, nil) + require.NoError(t, err) + assert.True(t, offered) + require.Len(t, got, 1) + assert.Equal(t, "github-dogfood", got[0].Name) + + // --agents narrows the picker to the servers installed for those agents. + state.Servers[0].Agents = map[string]mcp.AgentRecord{agents.NameCodex: {}} + state.Servers[1].Agents = map[string]mcp.AgentRecord{agents.NameClaudeCode: {}} + promptRemoveSelection = func(entries []mcp.Entry, _ []*agents.Agent) ([]mcp.Entry, error) { + require.Len(t, entries, 1) + assert.Equal(t, "github-logfood", entries[0].Name) + return entries, nil + } + got, offered, err = chooseRemovals(state, []*agents.Agent{agents.ByName(agents.NameCodex)}) + require.NoError(t, err) + assert.True(t, offered) + require.Len(t, got, 1) + assert.Equal(t, "github-logfood", got[0].Name) + + // No server for those agents: nothing is offered. + _, offered, err = chooseRemovals(state, []*agents.Agent{agents.ByName(agents.NameCursor)}) + require.NoError(t, err) + assert.False(t, offered) +} + +func TestRemoveChoiceLabel(t *testing.T) { + entry := mcp.Entry{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", + Agents: map[string]mcp.AgentRecord{agents.NameCodex: {}, agents.NameClaudeCode: {}}, + } + assert.Equal(t, "github-logfood system.ai.github logfood Claude Code, Codex CLI", removeChoiceLabel(entry, nil)) + + // With --agents, the label names only the agents the pick is removed from. + codex := []*agents.Agent{agents.ByName(agents.NameCodex)} + assert.Equal(t, "github-logfood system.ai.github logfood Codex CLI", removeChoiceLabel(entry, codex)) +} diff --git a/cmd/aitools/mcp_remove.go b/cmd/aitools/mcp_remove.go index 6c62b7d8eae..b4f2f254c93 100644 --- a/cmd/aitools/mcp_remove.go +++ b/cmd/aitools/mcp_remove.go @@ -18,7 +18,7 @@ import ( ) // removeFlagsHint is the error for a run that names no servers. Tests check it too. -const removeFlagsHint = "pass --names, --profile or --all; interactive selection is not available yet" +const removeFlagsHint = "pass --names, --profile or --all so the command runs without interactive prompts" type mcpRemoveFlags struct { names string @@ -83,7 +83,8 @@ func NewMcpRemoveCmd() *cobra.Command { Choose servers with --names (server names as listed by 'aitools mcp list', or service names; the last part of a service name, such as github, is enough when only one schema has it), --profile, or --all. --profile narrows --all or --names to one profile, and --agents narrows them to some agents. -Interactive selection is not available yet, so one of --names, --profile or --all is required. +In a terminal, with none of --names, --profile or --all, the command shows the installed servers to +choose from. Without a terminal, one of them is required. Only entries this CLI wrote are removed. An entry that was changed outside this CLI is left in place and reported. When an agent's config moved, the entry is removed from the file it was written to. @@ -128,7 +129,8 @@ func runMcpRemove(ctx context.Context, cmd *cobra.Command, f mcpRemoveFlags, jso names := splitAndTrim(f.names) agentNames := splitAndTrim(f.agents) profileName := profileFlag(cmd) - if err := validateMCPRemoveFlags(f.all, names, profileName); err != nil { + interactive := !jsonMode && cmdio.IsPromptSupported(ctx) + if err := validateMCPRemoveFlags(f.all, names, profileName, interactive); err != nil { return err } @@ -146,12 +148,40 @@ func runMcpRemove(ctx context.Context, cmd *cobra.Command, f mcpRemoveFlags, jso if err != nil { return err } - selected, err := selectInstalls(state, names, profileName) - if err != nil { - return err - } - if err := checkTargetsInstalled(selected, targets); err != nil { - return err + var selected []mcp.Entry + if interactive && !f.all && len(names) == 0 && profileName == "" { + // An agent with no installed server is refused as it is without a terminal, before the picker offers nothing for it. + if err := checkTargetsInstalled(state.Servers, targets); err != nil { + return err + } + var offered bool + selected, offered, err = chooseRemovals(state, targets) + if err != nil { + return err + } + if offered && len(selected) == 0 { + cmdio.LogString(ctx, "No servers selected; nothing removed.") + return nil + } + if offered { + // The picker chose the servers, so the removal is confirmed, as add confirms its plan and uninstall its removal. + proceed, err := promptProceed() + if err != nil { + return err + } + if !proceed { + cmdio.LogString(ctx, cancelledMessage) + return nil + } + } + } else { + selected, err = selectInstalls(state, names, profileName) + if err != nil { + return err + } + if err := checkTargetsInstalled(selected, targets); err != nil { + return err + } } if targets != nil { // --agents chooses the servers installed for those agents, so a server with no agent left is not chosen. @@ -220,11 +250,12 @@ func runMcpRemove(ctx context.Context, cmd *cobra.Command, f mcpRemoveFlags, jso } // validateMCPRemoveFlags rejects a choice of servers that cannot run, before anything is read or written. -func validateMCPRemoveFlags(all bool, names []string, profileName string) error { +// Without a choice, a terminal run shows the picker; any other run needs one. +func validateMCPRemoveFlags(all bool, names []string, profileName string, interactive bool) error { if all && len(names) > 0 { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, "--all cannot be combined with --names") } - if !all && len(names) == 0 && profileName == "" { + if !all && len(names) == 0 && profileName == "" && !interactive { return failure(protos.AitoolsErrorCategoryInvalidFlags, nil, removeFlagsHint) } return nil @@ -334,7 +365,7 @@ func checkTargetsInstalled(selected []mcp.Entry, targets []*agents.Agent) error _, ok := e.Agents[a.Name] return ok }) { - return failure(categoryServiceNotFound, nil, "no selected MCP server is installed for %s", agentDisplayName(a.Name)) + return failure(categoryServiceNotFound, nil, "no MCP server is installed for %s", agentDisplayName(a.Name)) } } return nil diff --git a/cmd/aitools/mcp_remove_test.go b/cmd/aitools/mcp_remove_test.go index 4e537b0ecf3..d97ed4a6034 100644 --- a/cmd/aitools/mcp_remove_test.go +++ b/cmd/aitools/mcp_remove_test.go @@ -36,7 +36,7 @@ func TestValidateMCPRemoveFlags(t *testing.T) { } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { - err := validateMCPRemoveFlags(tc.all, tc.names, tc.profile) + err := validateMCPRemoveFlags(tc.all, tc.names, tc.profile, false) if tc.want == "" { assert.NoError(t, err) return @@ -46,7 +46,10 @@ func TestValidateMCPRemoveFlags(t *testing.T) { }) } - err := validateMCPRemoveFlags(false, nil, "") + // In a terminal, no choice opens the picker instead of failing. + assert.NoError(t, validateMCPRemoveFlags(false, nil, "", true)) + + err := validateMCPRemoveFlags(false, nil, "", false) assert.Equal(t, protos.AitoolsErrorCategoryInvalidFlags, errorCategory(err)) } @@ -402,7 +405,7 @@ func TestMcpRemoveRefusesProfileAndAgentWithNothingInstalled(t *testing.T) { stdout, _, err = runMcpCmd(t, "remove", "--names", "genie_one_mcp-logfood", "--agents", "cursor", "--output", "json") require.ErrorIs(t, err, root.ErrAlreadyPrinted) out = readRemoveOutput(t, stdout.String()) - assert.Equal(t, "no selected MCP server is installed for Cursor", out.Error) + assert.Equal(t, "no MCP server is installed for Cursor", out.Error) assert.Equal(t, categoryServiceNotFound, out.ErrorCategory) // Nothing was removed by either refusal. @@ -622,9 +625,9 @@ func TestMcpRemoveWithAgentsSkipsRecordWithoutAgents(t *testing.T) { require.Len(t, state.Servers, 1) assert.Equal(t, "dangling", state.Servers[0].Name) - // Named explicitly, the same entry is a refusal: no selected server is installed for the agent. + // Named explicitly, the same entry is a refusal: no server is installed for the agent. _, _, err = runMcpCmd(t, "remove", "--names", "dangling", "--agents", "claude-code") - require.ErrorContains(t, err, "no selected MCP server is installed for Claude Code") + require.ErrorContains(t, err, "no MCP server is installed for Claude Code") } // TestMcpRemoveKeptReasonForDatabricksEntryOmitsForce checks that an entry this CLI wrote, but that serves another @@ -647,3 +650,125 @@ func TestMcpRemoveKeptReasonForDatabricksEntryOmitsForce(t *testing.T) { out.Skipped[0].Reason) assert.NotContains(t, out.Skipped[0].Reason, "--force") } + +// TestMcpRemovePickerRemovesTheChosenServers checks that the remove picker, shown in a terminal with no choice given, +// removes the servers that are checked and keeps the others. +func TestMcpRemovePickerRemovesTheChosenServers(t *testing.T) { + home := withHome(t) + claudePath := filepath.Join(home, ".claude.json") + require.NoError(t, os.WriteFile(claudePath, []byte(`{"mcpServers": {`+ + `"github-logfood": `+claudeEntry("system.ai.github", "logfood", "")+`, `+ + `"dbsql-logfood": `+claudeEntry("system.ai.dbsql", "logfood", "")+`}}`), 0o600)) + saveState(t, claudeServerEntry(claudePath, "github-logfood", "system.ai.github"), claudeServerEntry(claudePath, "dbsql-logfood", "system.ai.dbsql")) + + origRemove, origProceed := promptRemoveSelection, promptProceed + t.Cleanup(func() { promptRemoveSelection, promptProceed = origRemove, origProceed }) + promptRemoveSelection = func(entries []mcp.Entry, _ []*agents.Agent) ([]mcp.Entry, error) { + require.Len(t, entries, 2) + return entries[1:], nil + } + promptProceed = func() (bool, error) { return true, nil } + + require.NoError(t, runInteractive(t, "mcp", "remove")) + servers := readJSONFile(t, claudePath)["mcpServers"].(map[string]any) + assert.Contains(t, servers, "github-logfood") + assert.NotContains(t, servers, "dbsql-logfood") + + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + require.Len(t, state.Servers, 1) + assert.Equal(t, "github-logfood", state.Servers[0].Name) +} + +// TestMcpRemovePickerRefusesAgentWithNoServer checks that --agents naming an agent with no installed server is refused +// before the picker runs, as a run without a terminal refuses it. +func TestMcpRemovePickerRefusesAgentWithNoServer(t *testing.T) { + withHome(t) + saveState(t, claudeServerEntry("", "github-logfood", "system.ai.github")) + + origRemove := promptRemoveSelection + t.Cleanup(func() { promptRemoveSelection = origRemove }) + promptRemoveSelection = func([]mcp.Entry, []*agents.Agent) ([]mcp.Entry, error) { + t.Error("the picker ran for an agent with no installed server") + return nil, nil + } + + err := runInteractive(t, "mcp", "remove", "--agents", "codex") + require.Error(t, err) + assert.Equal(t, "no MCP server is installed for "+agents.ByName(agents.NameCodex).DisplayName, err.Error()) + assert.Equal(t, categoryServiceNotFound, errorCategory(err)) + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Len(t, state.Servers, 1) +} + +// TestMcpRemovePickerWithTwoAgentsNeedsOnePickPerServer checks that --agents with two agents accepts a pick installed for +// only one of them: the pick is removed from that agent, and the server installed for the other agent, which is not +// picked, stays. +func TestMcpRemovePickerWithTwoAgentsNeedsOnePickPerServer(t *testing.T) { + home := withHome(t) + claudePath := filepath.Join(home, ".claude.json") + require.NoError(t, os.WriteFile(claudePath, []byte(`{"mcpServers": {`+ + `"github-logfood": `+claudeEntry("system.ai.github", "logfood", "")+`, `+ + `"dbsql-logfood": `+claudeEntry("system.ai.dbsql", "logfood", "")+`}}`), 0o600)) + dbsql := claudeServerEntry(claudePath, "dbsql-logfood", "system.ai.dbsql") + dbsql.Agents[agents.NameCodex] = mcp.AgentRecord{Path: filepath.Join(home, ".codex", "config.toml"), Scope: "user"} + saveState(t, claudeServerEntry(claudePath, "github-logfood", "system.ai.github"), dbsql) + + origRemove, origProceed := promptRemoveSelection, promptProceed + t.Cleanup(func() { promptRemoveSelection, promptProceed = origRemove, origProceed }) + promptRemoveSelection = func(entries []mcp.Entry, targets []*agents.Agent) ([]mcp.Entry, error) { + require.Len(t, targets, 2) + require.Len(t, entries, 2) + return entries[:1], nil + } + promptProceed = func() (bool, error) { return true, nil } + + require.NoError(t, runInteractive(t, "mcp", "remove", "--agents", "claude-code,codex")) + servers := readJSONFile(t, claudePath)["mcpServers"].(map[string]any) + assert.NotContains(t, servers, "github-logfood") + assert.Contains(t, servers, "dbsql-logfood") +} + +// TestMcpRemovePickerDeclinedRemovesNothing checks that a picked removal the user declines at Proceed? removes nothing. +func TestMcpRemovePickerDeclinedRemovesNothing(t *testing.T) { + withHome(t) + saveState(t, claudeServerEntry("", "github-logfood", "system.ai.github")) + + origRemove, origProceed := promptRemoveSelection, promptProceed + t.Cleanup(func() { promptRemoveSelection, promptProceed = origRemove, origProceed }) + promptRemoveSelection = func(entries []mcp.Entry, _ []*agents.Agent) ([]mcp.Entry, error) { + return entries, nil + } + promptProceed = func() (bool, error) { return false, nil } + + require.NoError(t, runInteractive(t, "mcp", "remove")) + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Len(t, state.Servers, 1) +} + +// TestMcpRemovePickerEmptyPickRemovesNothing checks that a picker closed with nothing checked removes nothing. +func TestMcpRemovePickerEmptyPickRemovesNothing(t *testing.T) { + withHome(t) + saveState(t, claudeServerEntry("", "github-logfood", "system.ai.github")) + + origRemove := promptRemoveSelection + t.Cleanup(func() { promptRemoveSelection = origRemove }) + promptRemoveSelection = func([]mcp.Entry, []*agents.Agent) ([]mcp.Entry, error) { + return nil, nil + } + + require.NoError(t, runInteractive(t, "mcp", "remove")) + state, err := mcp.LoadState(t.Context()) + require.NoError(t, err) + assert.Len(t, state.Servers, 1) +} + +// claudeServerEntry is the state entry of a server installed for Claude Code, in the config file at claudePath. +func claudeServerEntry(claudePath, name, service string) mcp.Entry { + return mcp.Entry{ + Name: name, Service: service, Profile: "logfood", Host: "https://adb-1.test", Command: testDatabricksCommand, + Agents: map[string]mcp.AgentRecord{agents.NameClaudeCode: {Path: claudePath, Scope: "user"}}, + } +} diff --git a/cmd/aitools/uninstall.go b/cmd/aitools/uninstall.go index 787156ff519..d043a4a39c5 100644 --- a/cmd/aitools/uninstall.go +++ b/cmd/aitools/uninstall.go @@ -89,7 +89,7 @@ The command exits with code 1 when every removal it attempts fails.`, return err } if !proceed { - cmdio.LogString(ctx, "Cancelled.") + cmdio.LogString(ctx, cancelledMessage) funnel.finishWith(ctx, protos.AitoolsCommandOutcomeCancelled, protos.AitoolsErrorCategoryUnspecified) return nil } diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index 6d51207a89c..0088799425c 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -429,6 +429,28 @@ func envTable(v any) (map[string]any, error) { return table, nil } +// Unchanged reports whether Upsert would leave the entry current as it is: current is a Databricks entry whose launch +// keys already match the entry Upsert writes for cmd, args and env. Upsert uses the same comparison. +func Unchanged(a *agents.Agent, cmd string, args []string, env map[string]string, current map[string]any) (bool, error) { + spec, err := specOf(a) + if err != nil { + return false, err + } + ours, err := normalize(spec.Entry(cmd, args, env)) + if err != nil { + return false, err + } + if !mcpconfig.IsDatabricksEntry(current) { + return false, nil + } + keys := ownedKeys(spec) + merged, err := keepUserKeys(current, ours, keys) + if err != nil { + return false, err + } + return sameEntry(launchOf(current, keys), launchOf(merged, keys)) +} + // sameEntry compares two entries as JSON values, so the in-memory Go types of a // freshly built entry and one decoded from a file compare equal. func sameEntry(a, b map[string]any) (bool, error) { diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index b4c127894f7..1a530a73efa 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -154,6 +154,14 @@ func PinnedFile(entry *Entry, configFile string) string { return "" } +// CheckProfile returns the PROFILE_NOT_FOUND error for a profile that configFile does not name with a host, or nil when +// it does. It is CheckBinding for a caller that has no service yet, so its message names the profile and no server. It +// reads the config files only and makes no request. +func CheckProfile(profileName, configFile string) error { + _, err := profileFor("", profileName, configFile, nil) + return err +} + // CheckBinding returns the state entry for service and profileName bound to configFile, the config file the caller // resolved with ResolveConfigFile, or nil when there is none. It returns the error Bind returns when the profile does not // match: the profile is gone or has no host, or with an entry, its host or workspace changed. The profile is checked @@ -179,7 +187,8 @@ func CheckBinding(ctx context.Context, state *State, service, profileName, confi } // profileFor returns the profile profileName in configFile, or the PROFILE_NOT_FOUND error that says it -// is gone. name and entry describe the state entry the profile serves; entry is nil without one. +// is gone. name and entry describe the state entry the profile serves; entry is nil without one, and name is +// "" when no server is chosen yet. func profileFor(name, profileName, configFile string, entry *Entry) (profile.Profile, error) { p, err := configFiles{}.profile(configFile, profileName) switch { @@ -227,12 +236,16 @@ func ConfigFileCommand(configFile, command string) string { // profileGoneError builds the PROFILE_NOT_FOUND error for a profile that the config file does not // name with a host, either because it is absent or because its section has no host. problem completes // the sentence "which ... in ". Without a state entry nothing was set up for the profile, so the -// message only says how to set the profile up, without a host. An entry pinned to a config file puts that -// file in the recreate hint. +// message only says how to set the profile up, without a host, and it names the server only when name is set. An entry +// pinned to a config file puts that file in the recreate hint. func profileGoneError(name, profileName, path string, entry *Entry, problem string) error { if entry == nil { login := authLoginCommand + " --profile " + shellquote.BashArg(profileName) - message := fmt.Sprintf("MCP server %s needs Databricks profile \"%s\", which %s in %s. Set it up with: %s", name, profileName, problem, path, login) + subject := "Databricks profile \"" + profileName + "\"" + if name != "" { + subject = "MCP server " + name + " needs " + subject + } + message := fmt.Sprintf("%s, which %s in %s. Set it up with: %s", subject, problem, path, login) return &BindError{Category: BindProfileNotFound, message: message} } pinnedPath := "" diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 86b7b15b495..372c141cae3 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -880,6 +880,46 @@ func TestCheckBindingChecksProfileWithoutEntry(t *testing.T) { } } +// TestCheckProfileWithoutServer checks the profile check of a run that has no service yet. A missing profile and a +// profile with no host name the profile and no server, and an unreadable config file is uncategorized. +func TestCheckProfileWithoutServer(t *testing.T) { + login := "databricks auth login --profile " + bindingTestProfile + tests := []struct { + name string + contents string + want string + category mcp.BindCategory + }{ + { + name: "missing", + contents: "[other]\nhost = https://other.test\n", + want: "Databricks profile \"logfood\", which does not exist in %s. Set it up with: " + login, + category: mcp.BindProfileNotFound, + }, + { + name: "no host", + contents: "[" + bindingTestProfile + "]\naccount_id = 123\n", + want: "Databricks profile \"logfood\", which has no host in %s. Set it up with: " + login, + category: mcp.BindProfileNotFound, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + path := bindingTestResolved(t, bindingTestWriteConfig(t, tt.contents)) + + err := mcp.CheckProfile(bindingTestProfile, path) + requireBindError(t, err, tt.category) + assert.Equal(t, fmt.Sprintf(tt.want, filepath.ToSlash(path)), err.Error()) + }) + } + + t.Run("unreadable", func(t *testing.T) { + // A directory cannot be read as a config file. + err := mcp.CheckProfile(bindingTestProfile, bindingTestResolved(t, t.TempDir())) + requireBindError(t, err, mcp.BindUncategorized) + }) +} + func TestAuthErrorNamesPinnedConfigFile(t *testing.T) { cause := errors.New("Invalid access token.") assert.EqualError(t, mcp.AuthError("logfood", "/work/cfg/logfood.cfg", cause), diff --git a/libs/cmdio/io.go b/libs/cmdio/io.go index 7f0dd853e8c..ea1cde703c8 100644 --- a/libs/cmdio/io.go +++ b/libs/cmdio/io.go @@ -11,9 +11,9 @@ import ( "github.com/databricks/cli/libs/flags" ) -// errCtrlC is returned when the user cancels a TUI prompt with Ctrl+C. The +// ErrCtrlC is returned when the user cancels a TUI prompt with Ctrl+C. The // "^C" string matches the historical wire format; goldens depend on it. -var errCtrlC = errors.New("^C") +var ErrCtrlC = errors.New("^C") // runTUI runs a tea.Program through cmdIO's tea program slot so spinners and // pagers can't fight a prompt for the terminal. Blocks until the model quits. @@ -21,7 +21,7 @@ func (c *cmdIO) runTUI(m tea.Model) (tea.Model, error) { p := tea.NewProgram(m, tea.WithInput(c.in), tea.WithOutput(c.err), - // Ctrl+C is delivered as a key event so the model can return errCtrlC. + // Ctrl+C is delivered as a key event so the model can return ErrCtrlC. tea.WithoutSignalHandler(), ) c.acquireTeaProgram(p) diff --git a/libs/cmdio/prompt.go b/libs/cmdio/prompt.go index 4469465e972..3f84d18a2ff 100644 --- a/libs/cmdio/prompt.go +++ b/libs/cmdio/prompt.go @@ -259,7 +259,7 @@ func (c *cmdIO) runPromptModel(m *promptModel) (string, error) { pm := final.(*promptModel) switch { case pm.cancelled: - return "", errCtrlC + return "", ErrCtrlC case pm.deleted: return "", io.EOF } diff --git a/libs/cmdio/select.go b/libs/cmdio/select.go index 2d481139bae..c4a9a710c3f 100644 --- a/libs/cmdio/select.go +++ b/libs/cmdio/select.go @@ -521,7 +521,7 @@ func (c *cmdIO) runSelectModel(m *selectModel) (int, error) { } sm := final.(*selectModel) if sm.cancelled { - return 0, errCtrlC + return 0, ErrCtrlC } return sm.originalIndex(), nil }