From 943ce2ed0ec6d5d305381eeb5b4a0973c56b9cb6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 11:36:54 +0000 Subject: [PATCH 1/7] Add MCP state file, drift check, and profile binding checks Adds the state file that records MCP servers the CLI wrote, a drift check against the Databricks profiles file, and the binding checks a stdio proxy runs before it reads stdin. These are the building blocks for `aitools mcp`. Co-authored-by: Isaac --- libs/aitools/mcp/binding.go | 129 ++++++++++++++++++ libs/aitools/mcp/binding_test.go | 218 +++++++++++++++++++++++++++++++ libs/aitools/mcp/state.go | 154 ++++++++++++++++++++++ libs/aitools/mcp/state_test.go | 189 +++++++++++++++++++++++++++ 4 files changed, 690 insertions(+) create mode 100644 libs/aitools/mcp/binding.go create mode 100644 libs/aitools/mcp/binding_test.go create mode 100644 libs/aitools/mcp/state.go create mode 100644 libs/aitools/mcp/state_test.go diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go new file mode 100644 index 00000000000..0f278fe92d6 --- /dev/null +++ b/libs/aitools/mcp/binding.go @@ -0,0 +1,129 @@ +package mcp + +import ( + "context" + "errors" + "fmt" + "io" + "net/http" + "slices" + + "github.com/databricks/cli/libs/databrickscfg" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/databricks-sdk-go/config" +) + +// bindExitCode is the exit status for binding failures. Agents and `list --check` +// use it to tell binding failures apart from transport errors (SPEC 11.2). +const bindExitCode = 2 + +// BindCategory is the error category for a binding failure. Values match the +// AitoolsErrorCategory names so telemetry can map them directly. +type BindCategory string + +const ( + BindProfileNotFound BindCategory = "PROFILE_NOT_FOUND" + BindProfileHostChanged BindCategory = "PROFILE_HOST_CHANGED" + BindAuthFailed BindCategory = "AUTH_FAILED" + BindUncategorized BindCategory = "UNCATEGORIZED" +) + +// BindOptions selects what a proxy binds to. Service is the full name, for example system.ai.github. +type BindOptions struct { + Profile string + Service string + State *State + // Stderr receives the notice printed when no install record exists. + Stderr io.Writer +} + +// BindError is a failed binding check. Its message is the stderr text, byte-exact with SPEC 11.2. +type BindError struct { + Category BindCategory + message string + cause error +} + +func (e *BindError) Error() string { + return e.message +} + +func (e *BindError) Unwrap() error { + return e.cause +} + +// ExitCode returns the process exit status for this failure. +func (e *BindError) ExitCode() int { + return bindExitCode +} + +// Bind runs the binding checks of SPEC 11.2 for one profile and service. It never reads +// stdin, prompts, or loads a bundle. It returns the resolved config and the matching state +// entry, which is nil when none exists. +func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) { + entry := findEntry(opts.State, opts.Service, opts.Profile) + name := opts.Service + if entry != nil { + name = entry.Name + } + + profiler := profile.GetProfiler(ctx) + path, err := profiler.GetPath(ctx) + if err != nil { + return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + } + profiles, err := profiler.LoadProfiles(ctx, profile.WithName(opts.Profile)) + if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { + return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + } + if len(profiles) == 0 { + return nil, nil, profileGoneError(name, opts.Profile, path, entry) + } + + cfg := &config.Config{Profile: opts.Profile, Loaders: databrickscfg.ProfileAuthLoaders} + if err := cfg.EnsureResolved(); err != nil { + return nil, nil, authError(opts.Profile, err) + } + + if entry != nil && NormalizeHost(entry.Host) != NormalizeHost(cfg.Host) { + message := fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: databricks aitools mcp remove --profile %s and add it again.", + name, entry.Host, opts.Profile, cfg.Host, opts.Profile) + return nil, nil, &BindError{Category: BindProfileHostChanged, message: message} + } + + if entry == nil { + fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s.\n", opts.Service, opts.Profile, cfg.Host) + } + + if err := cfg.Authenticate(&http.Request{Header: make(http.Header)}); err != nil { + return nil, nil, authError(opts.Profile, err) + } + return cfg, entry, nil +} + +// profileGoneError builds the SPEC 11.2 step 2 error. Without a state entry the +// recorded host is unknown, so the recreate hint omits --host. +func profileGoneError(name, profileName, path string, entry *Entry) error { + recreate := "databricks auth login --profile " + profileName + if entry != nil { + recreate = "databricks auth login --host " + entry.Host + " --profile " + profileName + } + message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which no longer exists in %s. Recreate it with: %s or remove the server with: databricks aitools mcp remove --profile %s", + name, profileName, path, recreate, profileName) + return &BindError{Category: BindProfileNotFound, message: message} +} + +func authError(profileName string, cause error) error { + message := fmt.Sprintf("%s\nRun: databricks auth login --profile %s", cause, profileName) + return &BindError{Category: BindAuthFailed, message: message, cause: cause} +} + +func findEntry(state *State, service, profileName string) *Entry { + i := slices.IndexFunc(state.Servers, func(e Entry) bool { + return e.Service == service && e.Profile == profileName + }) + if i < 0 { + return nil + } + return &state.Servers[i] +} diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go new file mode 100644 index 00000000000..3c0a1c111a3 --- /dev/null +++ b/libs/aitools/mcp/binding_test.go @@ -0,0 +1,218 @@ +package mcp_test + +import ( + "bytes" + "errors" + "fmt" + "io" + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + bindingTestService = "system.ai.github" + bindingTestProfile = "logfood" + bindingTestHost = "https://adb-123.test" + bindingTestToken = "dapi-profile-token" +) + +// bindingTestConfig writes a .databrickscfg with contents, points DATABRICKS_CONFIG_FILE at it, +// and sets steering variables that name another workspace. Bind must ignore those. +func bindingTestConfig(t *testing.T, contents string) string { + t.Helper() + path := filepath.Join(t.TempDir(), ".databrickscfg") + require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) + t.Setenv("DATABRICKS_CONFIG_FILE", path) + t.Setenv("DATABRICKS_HOST", "https://adb-999.test") + t.Setenv("DATABRICKS_TOKEN", "dapi-env-token") + t.Setenv("DATABRICKS_CONFIG_PROFILE", "other") + t.Setenv("DATABRICKS_AUTH_TYPE", "basic") + return path +} + +func bindingTestProfileContents(token string) string { + return fmt.Sprintf(`[%s] +host = %s +auth_type = pat +token = %s +`, bindingTestProfile, bindingTestHost, token) +} + +func bindingTestState(host string) *mcp.State { + return &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", + Service: bindingTestService, + Profile: bindingTestProfile, + Host: host, + }}} +} + +func TestBindSucceedsWithStateEntry(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + stderr := &bytes.Buffer{} + + cfg, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(bindingTestHost), + Stderr: stderr, + }) + require.NoError(t, err) + assert.Equal(t, bindingTestProfile, cfg.Profile) + assert.Equal(t, bindingTestHost, cfg.Host) + assert.Equal(t, bindingTestToken, cfg.Token) + require.NotNil(t, entry) + assert.Equal(t, "github-logfood", entry.Name) + assert.Empty(t, stderr.String()) +} + +func TestBindPrintsNoticeWithoutStateEntry(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + stderr := &bytes.Buffer{} + + cfg, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: &mcp.State{}, + Stderr: stderr, + }) + require.NoError(t, err) + assert.Nil(t, entry) + assert.Equal(t, bindingTestHost, cfg.Host) + assert.Equal(t, "No install record for system.ai.github on profile \"logfood\"; using its current host https://adb-123.test.\n", stderr.String()) +} + +func TestBindProfileAbsent(t *testing.T) { + path := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + + tests := []struct { + name string + state *mcp.State + want string + }{ + { + name: "with state entry", + state: &mcp.State{Servers: []mcp.Entry{{ + Name: "github-gone", + Service: bindingTestService, + Profile: "gone", + Host: bindingTestHost, + }}}, + want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, path), + }, + { + name: "without state entry", + state: &mcp.State{}, + want: fmt.Sprintf(`MCP server system.ai.github is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, path), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: "gone", + Service: bindingTestService, + State: tt.state, + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileNotFound) + assert.Equal(t, tt.want, err.Error()) + }) + } +} + +func TestBindConfigFileAbsent(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + path := filepath.Join(t.TempDir(), "absent") + t.Setenv("DATABRICKS_CONFIG_FILE", path) + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileNotFound) + assert.Contains(t, err.Error(), "which no longer exists in "+path+".") +} + +func TestBindHostChanged(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState("https://adb-999.test"), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Equal(t, "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again.", err.Error()) +} + +func TestBindHostUnchangedWhenOnlyTrailingSlashDiffers(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState("https://adb-123.test/"), + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.NotNil(t, entry) +} + +func TestBindTokenCannotBeMinted(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents("")) + t.Setenv("DATABRICKS_TOKEN", "") + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindAuthFailed) + assert.Contains(t, err.Error(), "\nRun: databricks auth login --profile logfood") + assert.Error(t, errors.Unwrap(err)) +} + +// TestBindDoesNotReadStdin feeds a message on stdin and checks Bind leaves it unread. +func TestBindDoesNotReadStdin(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + r, w, err := os.Pipe() + require.NoError(t, err) + t.Cleanup(func() { _ = r.Close() }) + const message = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\"}\n" + _, err = w.WriteString(message) + require.NoError(t, err) + require.NoError(t, w.Close()) + stdin := os.Stdin + os.Stdin = r + t.Cleanup(func() { os.Stdin = stdin }) + + _, _, err = mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + + rest, err := io.ReadAll(r) + require.NoError(t, err) + assert.Equal(t, message, string(rest)) +} + +func requireBindError(t *testing.T, err error, want mcp.BindCategory) { + t.Helper() + require.Error(t, err) + var bindErr *mcp.BindError + require.ErrorAs(t, err, &bindErr) + assert.Equal(t, want, bindErr.Category) + assert.Equal(t, 2, bindErr.ExitCode()) +} diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go new file mode 100644 index 00000000000..5533d94f976 --- /dev/null +++ b/libs/aitools/mcp/state.go @@ -0,0 +1,154 @@ +package mcp + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "slices" + "time" + + "github.com/databricks/cli/libs/atomicfile" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" + "github.com/databricks/databricks-sdk-go/config" +) + +const ( + stateSchemaVersion = 1 + stateFileName = "state.json" +) + +// Drift kinds reported by Reconcile. +const ( + DriftProfileMissing DriftKind = "profile_missing" + DriftHostChanged DriftKind = "host_changed" +) + +// State is the record of MCP servers this CLI wrote. Agent config files stay the +// source of truth for existence; state records ownership and the binding. +type State struct { + SchemaVersion int `json:"schema_version"` + Servers []Entry `json:"servers"` +} + +// Entry is one installed server: a service bound to one profile for one set of agents. +type Entry struct { + Name string `json:"name"` + Service string `json:"service"` + Profile string `json:"profile"` + Host string `json:"host"` + WorkspaceID string `json:"workspace_id"` + SignInRequired bool `json:"sign_in_required"` + Command string `json:"command"` + ConfigFile string `json:"config_file"` + Agents map[string]AgentRecord `json:"agents"` + InstalledAt time.Time `json:"installed_at"` + CLIVersion string `json:"cli_version"` +} + +// AgentRecord is where an entry was written for one agent. +type AgentRecord struct { + Path string `json:"path"` + Scope string `json:"scope"` +} + +// DriftKind names how a state entry no longer matches the profiles file. +type DriftKind string + +// Drift is one state entry that no longer matches the Databricks config file. +type Drift struct { + Kind DriftKind + Entry Entry + // CurrentHost is the profile's host when Kind is DriftHostChanged. + CurrentHost string +} + +// LoadState reads the state file. A missing file yields an empty state. +func LoadState(ctx context.Context) (*State, error) { + path, err := statePath(ctx) + if err != nil { + return nil, err + } + data, err := os.ReadFile(path) + if errors.Is(err, fs.ErrNotExist) { + return &State{SchemaVersion: stateSchemaVersion}, nil + } + if err != nil { + return nil, fmt.Errorf("failed to read MCP state file: %w", err) + } + + var state State + if err := json.Unmarshal(data, &state); err != nil { + return nil, fmt.Errorf("failed to parse MCP state file %s: %w", path, err) + } + if err := migrateState(&state); err != nil { + return nil, err + } + return &state, nil +} + +// SaveState writes the state file atomically with mode 0600. It stamps the current schema version. +func SaveState(ctx context.Context, state *State) error { + path, err := statePath(ctx) + if err != nil { + return err + } + state.SchemaVersion = stateSchemaVersion + data, err := json.MarshalIndent(state, "", " ") + if err != nil { + return fmt.Errorf("failed to marshal MCP state: %w", err) + } + data = append(data, '\n') + return atomicfile.Write(path, data, 0o600, atomicfile.MkDir(0o755)) +} + +// migrateState brings a loaded state forward to stateSchemaVersion. It is forward-only: +// a file from a newer CLI is rejected instead of being rewritten. +func migrateState(state *State) error { + if state.SchemaVersion > stateSchemaVersion { + return fmt.Errorf("MCP state file has schema_version %d, but this CLI supports up to %d; upgrade the CLI", state.SchemaVersion, stateSchemaVersion) + } + state.SchemaVersion = stateSchemaVersion + return nil +} + +func statePath(ctx context.Context) (string, error) { + home, err := env.UserHomeDir(ctx) + if err != nil { + return "", err + } + return filepath.Join(home, ".databricks", "aitools", "mcp", stateFileName), nil +} + +// Reconcile reports entries whose profile is gone or whose host differs from the +// recorded one. A missing Databricks config file means every entry's profile is missing. +func Reconcile(ctx context.Context, profiler profile.Profiler, state *State) ([]Drift, error) { + profiles, err := profiler.LoadProfiles(ctx, profile.MatchAllProfiles) + if errors.Is(err, profile.ErrNoConfiguration) { + profiles = nil + } else if err != nil { + return nil, fmt.Errorf("failed to read Databricks profiles: %w", err) + } + + var drifts []Drift + for _, entry := range state.Servers { + i := slices.IndexFunc(profiles, profile.WithName(entry.Profile)) + switch { + case i < 0: + drifts = append(drifts, Drift{Kind: DriftProfileMissing, Entry: entry}) + case NormalizeHost(profiles[i].Host) != NormalizeHost(entry.Host): + drifts = append(drifts, Drift{Kind: DriftHostChanged, Entry: entry, CurrentHost: profiles[i].Host}) + } + } + return drifts, nil +} + +// NormalizeHost returns the host form that profile.WithHost compares, so host +// equality in state checks agrees with profile lookup. +func NormalizeHost(host string) string { + return (&config.Config{Host: host}).CanonicalHostName() +} diff --git a/libs/aitools/mcp/state_test.go b/libs/aitools/mcp/state_test.go new file mode 100644 index 00000000000..14f44324d03 --- /dev/null +++ b/libs/aitools/mcp/state_test.go @@ -0,0 +1,189 @@ +package mcp_test + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" + + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const stateTestFileRel = ".databricks/aitools/mcp/state.json" + +// stateTestCtx returns a context whose home directory is a fresh temp dir, plus the +// path of the state file inside it. +func stateTestCtx(t *testing.T) (context.Context, string) { + t.Helper() + home := t.TempDir() + return env.WithUserHomeDir(t.Context(), home), filepath.Join(home, stateTestFileRel) +} + +func stateTestEntry() mcp.Entry { + return mcp.Entry{ + Name: "github-logfood", + Service: "system.ai.github", + Profile: "logfood", + Host: "https://adb-123.test", + WorkspaceID: "123", + SignInRequired: true, + Command: "/usr/local/bin/databricks", + Agents: map[string]mcp.AgentRecord{ + "claude-code": {Path: "/home/me/.claude.json", Scope: "user"}, + }, + InstalledAt: time.Date(2026, 11, 12, 10, 12, 0, 0, time.UTC), + CLIVersion: "0.300.0", + } +} + +func TestLoadStateMissingFileIsEmpty(t *testing.T) { + ctx, _ := stateTestCtx(t) + + state, err := mcp.LoadState(ctx) + require.NoError(t, err) + assert.Equal(t, 1, state.SchemaVersion) + assert.Empty(t, state.Servers) +} + +func TestSaveStateThenLoadStateRoundTrips(t *testing.T) { + ctx, _ := stateTestCtx(t) + + saved := &mcp.State{Servers: []mcp.Entry{stateTestEntry()}} + require.NoError(t, mcp.SaveState(ctx, saved)) + + loaded, err := mcp.LoadState(ctx) + require.NoError(t, err) + assert.Equal(t, saved, loaded) +} + +func TestSaveStateWritesSection10Shape(t *testing.T) { + ctx, path := stateTestCtx(t) + + require.NoError(t, mcp.SaveState(ctx, &mcp.State{Servers: []mcp.Entry{stateTestEntry()}})) + + data, err := os.ReadFile(path) + require.NoError(t, err) + want := `{ + "schema_version": 1, + "servers": [ + { + "name": "github-logfood", + "service": "system.ai.github", + "profile": "logfood", + "host": "https://adb-123.test", + "workspace_id": "123", + "sign_in_required": true, + "command": "/usr/local/bin/databricks", + "config_file": "", + "agents": { + "claude-code": { + "path": "/home/me/.claude.json", + "scope": "user" + } + }, + "installed_at": "2026-11-12T10:12:00Z", + "cli_version": "0.300.0" + } + ] +} +` + assert.Equal(t, want, string(data)) +} + +func TestLoadStateMigratesMissingSchemaVersion(t *testing.T) { + ctx, path := stateTestCtx(t) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(`{"servers": []}`), 0o600)) + + state, err := mcp.LoadState(ctx) + require.NoError(t, err) + assert.Equal(t, 1, state.SchemaVersion) +} + +func TestLoadStateRejectsNewerSchemaVersion(t *testing.T) { + ctx, path := stateTestCtx(t) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(`{"schema_version": 2, "servers": []}`), 0o600)) + + _, err := mcp.LoadState(ctx) + require.ErrorContains(t, err, "schema_version 2") +} + +func TestLoadStateRejectsInvalidJSON(t *testing.T) { + ctx, path := stateTestCtx(t) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(`{not json`), 0o600)) + + _, err := mcp.LoadState(ctx) + require.ErrorContains(t, err, "failed to parse MCP state file") +} + +// stateTestProfiles is the profiles file used by the drift tests. +func stateTestProfiles() profile.InMemoryProfiler { + return profile.InMemoryProfiler{Profiles: profile.Profiles{ + {Name: "logfood", Host: "https://adb-123.test"}, + {Name: "dogfood", Host: "https://adb-456.test"}, + }} +} + +func TestReconcileReportsNoDriftForMatchingHost(t *testing.T) { + entry := stateTestEntry() + entry.Host = "https://adb-123.test/" + state := &mcp.State{Servers: []mcp.Entry{entry}} + + drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) + require.NoError(t, err) + assert.Empty(t, drifts) +} + +func TestReconcileReportsProfileMissing(t *testing.T) { + entry := stateTestEntry() + entry.Profile = "gone" + state := &mcp.State{Servers: []mcp.Entry{entry}} + + drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) +} + +func TestReconcileReportsHostChanged(t *testing.T) { + entry := stateTestEntry() + entry.Profile = "dogfood" + entry.Host = "https://adb-123.test" + state := &mcp.State{Servers: []mcp.Entry{entry}} + + drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftHostChanged, Entry: entry, CurrentHost: "https://adb-456.test"}}, drifts) +} + +func TestReconcileWithoutConfigFileReportsEveryProfileMissing(t *testing.T) { + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) + entry := stateTestEntry() + state := &mcp.State{Servers: []mcp.Entry{entry}} + + drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) +} + +// TestNormalizeHostMatchesWithHost checks that NormalizeHost agrees with profile.WithHost. +func TestNormalizeHostMatchesWithHost(t *testing.T) { + cases := [][2]string{ + {"https://adb-123.test", "adb-123.test"}, + {"https://adb-123.test/", "https://adb-123.test"}, + {"adb-123.test", "https://adb-123.test"}, + {"https://adb-123.test", "https://adb-456.test"}, + {"https://adb-123.test:443", "https://adb-123.test"}, + } + for _, c := range cases { + want := profile.WithHost(c[0])(profile.Profile{Host: c[1]}) + got := mcp.NormalizeHost(c[0]) == mcp.NormalizeHost(c[1]) + assert.Equal(t, want, got, "%q vs %q", c[0], c[1]) + } +} From 9ef3f0cdd80aab8dd7d88734d166cd2d1c0b43c0 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 15:09:39 +0000 Subject: [PATCH 2/7] Address review findings for MCP state and binding checks Keep MCP records in the shared aitools state file as an optional mcp section (schema v3). Uninstall keeps the file while MCP records remain. Binding checks now compare the host and the workspace ID with the profile before any credential is resolved, read the config file named in the context, and never fill credentials from the environment. Drift reconciliation checks each entry against its pinned config file, and entries match on service, profile and config file. Host comparison is case-insensitive. Error and notice messages no longer end with a period, and paths are slash-separated. Co-authored-by: Isaac --- libs/aitools/installer/cleanup_test.go | 4 +- libs/aitools/installer/detect_test.go | 2 +- libs/aitools/installer/installer.go | 2 +- libs/aitools/installer/installer_test.go | 2 +- libs/aitools/installer/plugin.go | 2 +- libs/aitools/installer/plugin_test.go | 18 +- libs/aitools/installer/state.go | 49 ++++- libs/aitools/installer/state_test.go | 75 ++++++- libs/aitools/installer/uninstall.go | 7 +- libs/aitools/installer/uninstall_test.go | 23 ++- libs/aitools/mcp/binding.go | 73 ++++--- libs/aitools/mcp/binding_test.go | 176 ++++++++++++++--- libs/aitools/mcp/state.go | 170 ++++++++-------- libs/aitools/mcp/state_test.go | 236 +++++++++++++---------- 14 files changed, 569 insertions(+), 270 deletions(-) diff --git a/libs/aitools/installer/cleanup_test.go b/libs/aitools/installer/cleanup_test.go index b19d6272653..4acc11dac88 100644 --- a/libs/aitools/installer/cleanup_test.go +++ b/libs/aitools/installer/cleanup_test.go @@ -51,7 +51,7 @@ func TestRemoveLegacyRawSkills(t *testing.T) { require.NoError(t, os.WriteFile(filepath.Join(agentDir, "delta", "extra.md"), []byte("mine"), 0o644)) require.NoError(t, SaveState(baseDir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Files: map[string]FileRecord{ "alpha/SKILL.md": {SHA256: sha("alpha")}, "beta/SKILL.md": {SHA256: sha("beta-content")}, @@ -93,7 +93,7 @@ func TestHasManagedRawSkillsForAgent(t *testing.T) { require.NoError(t, os.WriteFile(filepath.Join(agentDir, "thirdparty", "SKILL.md"), []byte("mine"), 0o644)) require.NoError(t, SaveState(baseDir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Files: map[string]FileRecord{ "alpha/SKILL.md": {SHA256: sha("alpha")}, }, diff --git a/libs/aitools/installer/detect_test.go b/libs/aitools/installer/detect_test.go index b5ab58f3191..5c52d8b0b37 100644 --- a/libs/aitools/installer/detect_test.go +++ b/libs/aitools/installer/detect_test.go @@ -171,7 +171,7 @@ func savePluginState(t *testing.T, dirFn func(context.Context) (string, error), dir, err := dirFn(t.Context()) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Plugins: plugins, })) } diff --git a/libs/aitools/installer/installer.go b/libs/aitools/installer/installer.go index fa30d4537ec..8e5115a1998 100644 --- a/libs/aitools/installer/installer.go +++ b/libs/aitools/installer/installer.go @@ -444,7 +444,7 @@ func InstallSkillsForAgents(ctx context.Context, src ManifestSource, targetAgent // previous installs (e.g., experimental skills from a prior run) are preserved. if state == nil { state = &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Skills: make(map[string]string, len(targetSkills)), RepoDirs: make(map[string]string, len(targetSkills)), } diff --git a/libs/aitools/installer/installer_test.go b/libs/aitools/installer/installer_test.go index 959029d23c6..ad61cf02044 100644 --- a/libs/aitools/installer/installer_test.go +++ b/libs/aitools/installer/installer_test.go @@ -442,7 +442,7 @@ func TestInstallSkillsForAgentsWritesState(t *testing.T) { state, err := LoadState(globalDir) require.NoError(t, err) require.NotNil(t, state) - assert.Equal(t, schemaVersionV2, state.SchemaVersion) + assert.Equal(t, CurrentSchemaVersion, state.SchemaVersion) assert.Equal(t, testSkillsRef, state.Release) assert.Len(t, state.Skills, 2) assert.Equal(t, "0.1.0", state.Skills["databricks-sql"]) diff --git a/libs/aitools/installer/plugin.go b/libs/aitools/installer/plugin.go index d623a75c496..51cb0b2fce2 100644 --- a/libs/aitools/installer/plugin.go +++ b/libs/aitools/installer/plugin.go @@ -510,7 +510,7 @@ func RecordPluginInstalls(ctx context.Context, cliScope string, records map[stri // Initialize all maps so a later skills install/update can assign into a // plugin-only state without hitting a nil map. state = &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{}, RepoDirs: map[string]string{}, Files: map[string]FileRecord{}, diff --git a/libs/aitools/installer/plugin_test.go b/libs/aitools/installer/plugin_test.go index 645dec6a853..2b61b6e4e8a 100644 --- a/libs/aitools/installer/plugin_test.go +++ b/libs/aitools/installer/plugin_test.go @@ -380,7 +380,7 @@ func TestUninstallSkillsWithResultTearsDownPlugin(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.6", Plugins: map[string]PluginRecord{ agents.NameCopilot: {Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user", InstalledMarketplace: true}, @@ -411,7 +411,7 @@ func TestUninstallSkillsWithResultTargetsPluginAgents(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.6", Plugins: map[string]PluginRecord{ agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", Scope: "user"}, @@ -447,7 +447,7 @@ func TestUninstallNeverDeregistersSharedMarketplace(t *testing.T) { // Claude's claude-plugins-official is a Shared marketplace; even a stale // InstalledMarketplace=true must never trigger a de-register. require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Plugins: map[string]PluginRecord{ agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", InstalledMarketplace: true}, }, @@ -476,7 +476,7 @@ func TestUninstallKeepMarketplace(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Plugins: map[string]PluginRecord{ agents.NameCopilot: {Marketplace: "databricks-agent-skills", Plugin: "databricks", InstalledMarketplace: true}, }, @@ -497,7 +497,7 @@ func TestUninstallKeepsUnknownAgentRecord(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Plugins: map[string]PluginRecord{ "mystery-agent": {Marketplace: "databricks-agent-skills", Plugin: "databricks"}, }, @@ -526,7 +526,7 @@ func TestUninstallClearsRecordWhenMarketplaceRemoveFails(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Plugins: map[string]PluginRecord{ agents.NameCopilot: {Marketplace: "databricks-agent-skills", Plugin: "databricks", InstalledMarketplace: true}, }, @@ -551,7 +551,7 @@ func TestUpdateInstalledPlugins(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.6", Plugins: map[string]PluginRecord{ agents.NameCopilot: {Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user", Version: "0.2.6"}, @@ -635,7 +635,7 @@ func TestUpdateInstalledPluginsUsesRecordedClaudeScope(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.6", Plugins: map[string]PluginRecord{ agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", Scope: scope, Version: "0.2.6"}, @@ -769,7 +769,7 @@ func TestUninstallSkillsWithResultUsesRecordedClaudeScope(t *testing.T) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Plugins: map[string]PluginRecord{ agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", Scope: scope}, }, diff --git a/libs/aitools/installer/state.go b/libs/aitools/installer/state.go index eba214eb79e..1af228a91f4 100644 --- a/libs/aitools/installer/state.go +++ b/libs/aitools/installer/state.go @@ -28,9 +28,10 @@ func clearFileRecords(files map[string]FileRecord, skillName string) { const stateFileName = ".state.json" -// schemaVersionV2 is the current on-disk state schema version. v2 adds the -// additive Plugins and Files maps; v1 state loads forward without data changes. -const schemaVersionV2 = 2 +// CurrentSchemaVersion is the on-disk state schema version this CLI writes. v2 +// added the Plugins and Files maps; v3 adds the optional MCP section. Older state +// loads forward without data changes. +const CurrentSchemaVersion = 3 // Scope constants for skill installation. const ( @@ -57,6 +58,37 @@ type InstallState struct { // e.g. "databricks/SKILL.md"). Added in schema v2; used to prune only the // skills we wrote that the user hasn't modified. Files map[string]FileRecord `json:"files,omitempty"` + // MCP records the Databricks MCP servers the `aitools mcp` commands wrote. + // Added in schema v3; omitted when none are recorded. Other writers keep it + // by loading the file first, since they save the whole struct. + MCP *MCPState `json:"mcp,omitempty"` +} + +// MCPState is the MCP section of the install state: the servers this CLI added +// to agent configs, and how each one is bound to a Databricks profile. +type MCPState struct { + Servers []MCPEntry `json:"servers"` +} + +// MCPEntry is one installed MCP server: a service bound to one profile for one set of agents. +type MCPEntry struct { + Name string `json:"name"` + Service string `json:"service"` + Profile string `json:"profile"` + Host string `json:"host"` + WorkspaceID string `json:"workspace_id"` + SignInRequired bool `json:"sign_in_required"` + Command string `json:"command"` + ConfigFile string `json:"config_file"` + Agents map[string]MCPAgentRecord `json:"agents"` + InstalledAt time.Time `json:"installed_at"` + CLIVersion string `json:"cli_version"` +} + +// MCPAgentRecord is where an MCP entry was written for one agent. +type MCPAgentRecord struct { + Path string `json:"path"` + Scope string `json:"scope"` } // PluginRecord records a databricks plugin installed for an agent through the @@ -108,12 +140,13 @@ func LoadState(dir string) (*InstallState, error) { } // migrateState brings a loaded state forward to the current schema version. It -// is forward-only and idempotent. v1 -> v2 needs no data transformation (the -// Plugins/Files maps are additive and optional), so it only stamps the version; -// writers lazily initialize the maps, matching how RepoDirs is handled. +// is forward-only and idempotent. The v1 -> v2 and v2 -> v3 steps need no data +// transformation (the Plugins, Files and MCP fields are additive and optional), +// so migration only stamps the version; writers lazily initialize the maps, +// matching how RepoDirs is handled. func migrateState(state *InstallState) { - if state.SchemaVersion < schemaVersionV2 { - state.SchemaVersion = schemaVersionV2 + if state.SchemaVersion < CurrentSchemaVersion { + state.SchemaVersion = CurrentSchemaVersion } } diff --git a/libs/aitools/installer/state_test.go b/libs/aitools/installer/state_test.go index 4abee62d8cb..10f092a3b25 100644 --- a/libs/aitools/installer/state_test.go +++ b/libs/aitools/installer/state_test.go @@ -23,7 +23,7 @@ func TestLoadStateNonexistentFile(t *testing.T) { func TestSaveAndLoadStateRoundtrip(t *testing.T) { dir := t.TempDir() original := &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.0", LastUpdated: time.Date(2026, 3, 22, 10, 0, 0, 0, time.UTC), Skills: map[string]string{ @@ -136,7 +136,7 @@ func TestLoadStateMigratesV1ToV2(t *testing.T) { loaded, err := LoadState(dir) require.NoError(t, err) - assert.Equal(t, schemaVersionV2, loaded.SchemaVersion) + assert.Equal(t, CurrentSchemaVersion, loaded.SchemaVersion) // Migration is additive: existing data is untouched and the new maps stay nil. assert.Equal(t, map[string]string{"databricks": "1.0.0"}, loaded.Skills) assert.Nil(t, loaded.Plugins) @@ -148,7 +148,7 @@ func TestMigrateStateIsIdempotent(t *testing.T) { // second migrateState is a no-op. state := &InstallState{SchemaVersion: 1, Skills: map[string]string{"databricks": "1.0.0"}} migrateState(state) - assert.Equal(t, schemaVersionV2, state.SchemaVersion) + assert.Equal(t, CurrentSchemaVersion, state.SchemaVersion) migrated := *state migrateState(state) @@ -158,7 +158,7 @@ func TestMigrateStateIsIdempotent(t *testing.T) { func TestSaveAndLoadStateWithPluginAndFileRecords(t *testing.T) { dir := t.TempDir() original := &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.6", LastUpdated: time.Date(2026, 6, 24, 0, 0, 0, 0, time.UTC), Skills: map[string]string{"databricks": "1.0.0"}, @@ -187,7 +187,7 @@ func TestSaveAndLoadStateWithPluginAndFileRecords(t *testing.T) { func TestSaveAndLoadStateWithOptionalFields(t *testing.T) { dir := t.TempDir() original := &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, IncludeExperimental: true, Release: "v0.3.0", LastUpdated: time.Date(2026, 3, 22, 12, 30, 0, 0, time.UTC), @@ -209,3 +209,68 @@ func TestSaveAndLoadStateWithOptionalFields(t *testing.T) { require.NoError(t, err) assert.Equal(t, original, loaded) } + +func TestSaveAndLoadStateKeepsMCPSection(t *testing.T) { + dir := t.TempDir() + original := &InstallState{ + SchemaVersion: CurrentSchemaVersion, + Skills: map[string]string{}, + MCP: &MCPState{Servers: []MCPEntry{{ + Name: "github-logfood", + Service: "system.ai.github", + Profile: "logfood", + Host: "https://adb-123.test", + WorkspaceID: "123", + SignInRequired: true, + Command: "/usr/local/bin/databricks", + Agents: map[string]MCPAgentRecord{ + "claude-code": {Path: "/home/me/.claude.json", Scope: "user"}, + }, + InstalledAt: time.Date(2026, 11, 12, 10, 12, 0, 0, time.UTC), + CLIVersion: "0.300.0", + }}}, + } + require.NoError(t, SaveState(dir, original)) + + loaded, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, original, loaded) +} + +// TestSaveStateKeepsMCPSectionWhenInstallerSaves checks that a skills write, which loads the +// file before saving it, leaves the MCP section in place. +func TestSaveStateKeepsMCPSectionWhenInstallerSaves(t *testing.T) { + dir := t.TempDir() + require.NoError(t, SaveState(dir, &InstallState{ + SchemaVersion: CurrentSchemaVersion, + MCP: &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}}, + })) + + state, err := LoadState(dir) + require.NoError(t, err) + state.Skills = map[string]string{"databricks": "1.0.0"} + require.NoError(t, SaveState(dir, state)) + + loaded, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, map[string]string{"databricks": "1.0.0"}, loaded.Skills) + require.NotNil(t, loaded.MCP) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) +} + +func TestLoadStateMigratesV2ToV3AndSavesV3(t *testing.T) { + dir := t.TempDir() + v2 := `{"schema_version":2,"release":"v0.2.0","last_updated":"2026-03-22T10:00:00Z","skills":{"databricks":"1.0.0"}}` + require.NoError(t, os.WriteFile(filepath.Join(dir, stateFileName), []byte(v2), 0o644)) + + loaded, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, 3, loaded.SchemaVersion) + assert.Nil(t, loaded.MCP) + + require.NoError(t, SaveState(dir, loaded)) + data, err := os.ReadFile(filepath.Join(dir, stateFileName)) + require.NoError(t, err) + assert.Contains(t, string(data), `"schema_version": 3`) + assert.NotContains(t, string(data), `"mcp"`) +} diff --git a/libs/aitools/installer/uninstall.go b/libs/aitools/installer/uninstall.go index 8f7cf1d2c10..b07b920af6e 100644 --- a/libs/aitools/installer/uninstall.go +++ b/libs/aitools/installer/uninstall.go @@ -197,10 +197,13 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni } } - // If nothing remains in this scope, clean up orphaned symlinks and delete the - // state file; otherwise persist the trimmed state. + // If nothing remains in this scope, clean up orphaned symlinks. Delete the + // state file unless it still holds MCP records, which uninstall does not own; + // otherwise persist the trimmed state. if len(state.Skills) == 0 && len(state.Plugins) == 0 { cleanOrphanedSymlinks(ctx, baseDir, scope, cwd) + } + if len(state.Skills) == 0 && len(state.Plugins) == 0 && (state.MCP == nil || len(state.MCP.Servers) == 0) { stateFile := filepath.Join(baseDir, stateFileName) if err := os.Remove(stateFile); err != nil && !errors.Is(err, fs.ErrNotExist) { return nil, fmt.Errorf("failed to remove state file: %w", err) diff --git a/libs/aitools/installer/uninstall_test.go b/libs/aitools/installer/uninstall_test.go index b2f70d6501a..eddfb317c82 100644 --- a/libs/aitools/installer/uninstall_test.go +++ b/libs/aitools/installer/uninstall_test.go @@ -602,7 +602,7 @@ func TestUninstallTargetedUntrackedSkillGivesRecoveryGuidance(t *testing.T) { globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") require.NoError(t, SaveState(globalDir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks-jobs": "0.1.0"}, })) @@ -612,3 +612,24 @@ func TestUninstallTargetedUntrackedSkillGivesRecoveryGuidance(t *testing.T) { assert.Contains(t, err.Error(), "Cursor") assert.Contains(t, err.Error(), "install --skills-only --agents cursor") } + +func TestUninstallKeepsStateFileWithMCPRecords(t *testing.T) { + tmp := setupTestHome(t) + globalDir := installTestSkills(t, tmp) + + state, err := LoadState(globalDir) + require.NoError(t, err) + state.MCP = &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}} + require.NoError(t, SaveState(globalDir, state)) + + ctx := cmdio.MockDiscard(t.Context()) + require.NoError(t, UninstallSkills(ctx)) + + // The skills are gone, but the MCP records stay in the file. + loaded, err := LoadState(globalDir) + require.NoError(t, err) + require.NotNil(t, loaded) + assert.Empty(t, loaded.Skills) + require.NotNil(t, loaded.MCP) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) +} diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 0f278fe92d6..78e1e565bfd 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -6,19 +6,21 @@ import ( "fmt" "io" "net/http" + "path/filepath" "slices" "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" + "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" ) -// bindExitCode is the exit status for binding failures. Agents and `list --check` -// use it to tell binding failures apart from transport errors (SPEC 11.2). +// bindExitCode is the exit status for binding failures, so callers can tell them apart +// from other errors. const bindExitCode = 2 -// BindCategory is the error category for a binding failure. Values match the -// AitoolsErrorCategory names so telemetry can map them directly. +// BindCategory names the kind of a binding failure. The values are stable identifiers +// for the failure, not telemetry values. type BindCategory string const ( @@ -37,7 +39,7 @@ type BindOptions struct { Stderr io.Writer } -// BindError is a failed binding check. Its message is the stderr text, byte-exact with SPEC 11.2. +// BindError is a failed binding check. Its message is the text printed to stderr. type BindError struct { Category BindCategory message string @@ -57,11 +59,16 @@ func (e *BindError) ExitCode() int { return bindExitCode } -// Bind runs the binding checks of SPEC 11.2 for one profile and service. It never reads -// stdin, prompts, or loads a bundle. It returns the resolved config and the matching state -// entry, which is nil when none exists. +// Bind checks that the profile still names the host and workspace recorded for the service, +// then authenticates with the profile's own credentials. It never reads stdin, prompts, or +// loads a bundle. It returns the resolved config and the matching state entry, which is nil +// when none exists. +// +// The profile is checked before any credential is resolved, so a re-pointed host is never +// contacted. The config file is the one in the context, the same file the profile lookup reads. func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) { - entry := findEntry(opts.State, opts.Service, opts.Profile) + configFile := env.Get(ctx, configFileEnv) + entry := findEntry(opts.State, opts.Service, opts.Profile, configFile) name := opts.Service if entry != nil { name = entry.Name @@ -77,32 +84,51 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} } if len(profiles) == 0 { - return nil, nil, profileGoneError(name, opts.Profile, path, entry) + return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(path), entry) } + p := profiles[0] - cfg := &config.Config{Profile: opts.Profile, Loaders: databrickscfg.ProfileAuthLoaders} - if err := cfg.EnsureResolved(); err != nil { - return nil, nil, authError(opts.Profile, err) + if entry != nil && hostChanged(*entry, p) { + message := fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: databricks aitools mcp remove --profile %s and add it again", + name, entry.Host, opts.Profile, p.Host, opts.Profile) + return nil, nil, &BindError{Category: BindProfileHostChanged, message: message} } - - if entry != nil && NormalizeHost(entry.Host) != NormalizeHost(cfg.Host) { - message := fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: databricks aitools mcp remove --profile %s and add it again.", - name, entry.Host, opts.Profile, cfg.Host, opts.Profile) + if entry != nil && workspaceChanged(*entry, p) { + message := fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: databricks aitools mcp remove --profile %s and add it again", + name, entry.WorkspaceID, opts.Profile, p.WorkspaceID, opts.Profile) return nil, nil, &BindError{Category: BindProfileHostChanged, message: message} } + // The loaders take the profile's own settings and non-auth environment only. The + // profile loader chain in databrickscfg also fills missing credentials from the + // environment, but the proxy must not send a token meant for another workspace. + cfg := &config.Config{ + Profile: opts.Profile, + ConfigFile: configFile, + Loaders: []config.Loader{databrickscfg.ResolveNonAuthFromEnv, config.ConfigFile}, + } + if err := cfg.EnsureResolved(); err != nil { + return nil, nil, authError(opts.Profile, err) + } + if entry == nil { - fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s.\n", opts.Service, opts.Profile, cfg.Host) + fmt.Fprintf(opts.Stderr, "No install record for %s on profile \"%s\"; using its current host %s\n", opts.Service, opts.Profile, cfg.Host) } - if err := cfg.Authenticate(&http.Request{Header: make(http.Header)}); err != nil { + // Authenticate only writes headers onto req, which is never sent. The request takes + // the command's context, as in cmd/root/auth.go. + req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil) + if err != nil { + return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + } + if err := cfg.Authenticate(req); err != nil { return nil, nil, authError(opts.Profile, err) } return cfg, entry, nil } -// profileGoneError builds the SPEC 11.2 step 2 error. Without a state entry the -// recorded host is unknown, so the recreate hint omits --host. +// profileGoneError builds the error for a profile that is missing from the config file. +// Without a state entry the recorded host is unknown, so the recreate hint omits --host. func profileGoneError(name, profileName, path string, entry *Entry) error { recreate := "databricks auth login --profile " + profileName if entry != nil { @@ -118,9 +144,10 @@ func authError(profileName string, cause error) error { return &BindError{Category: BindAuthFailed, message: message, cause: cause} } -func findEntry(state *State, service, profileName string) *Entry { +// findEntry returns the entry for the service and profile that is pinned to configFile, or nil. +func findEntry(state *State, service, profileName, configFile string) *Entry { i := slices.IndexFunc(state.Servers, func(e Entry) bool { - return e.Service == service && e.Profile == profileName + return e.Service == service && e.Profile == profileName && e.ConfigFile == configFile }) if i < 0 { return nil diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 3c0a1c111a3..a7b5d741e9f 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -5,11 +5,14 @@ import ( "errors" "fmt" "io" + "net/http" + "net/http/httptest" "os" "path/filepath" "testing" "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -21,12 +24,21 @@ const ( bindingTestToken = "dapi-profile-token" ) -// bindingTestConfig writes a .databrickscfg with contents, points DATABRICKS_CONFIG_FILE at it, -// and sets steering variables that name another workspace. Bind must ignore those. -func bindingTestConfig(t *testing.T, contents string) string { +// bindingTestWriteConfig writes a .databrickscfg with contents and returns its path. +func bindingTestWriteConfig(t *testing.T, contents string) string { t.Helper() path := filepath.Join(t.TempDir(), ".databrickscfg") require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) + return path +} + +// bindingTestConfig writes a .databrickscfg, points DATABRICKS_CONFIG_FILE at it, and sets +// steering variables that name another workspace. Bind must ignore those. The home directory +// is a separate temp dir so the config path is never abbreviated to "~" in messages. +func bindingTestConfig(t *testing.T, contents string) string { + t.Helper() + path := bindingTestWriteConfig(t, contents) + t.Setenv(env.HomeEnvVar(), t.TempDir()) t.Setenv("DATABRICKS_CONFIG_FILE", path) t.Setenv("DATABRICKS_HOST", "https://adb-999.test") t.Setenv("DATABRICKS_TOKEN", "dapi-env-token") @@ -43,23 +55,25 @@ token = %s `, bindingTestProfile, bindingTestHost, token) } -func bindingTestState(host string) *mcp.State { +// bindingTestState returns a state with one entry pinned to configFile, the file the test wrote. +func bindingTestState(configFile, host string) *mcp.State { return &mcp.State{Servers: []mcp.Entry{{ - Name: "github-logfood", - Service: bindingTestService, - Profile: bindingTestProfile, - Host: host, + Name: "github-logfood", + Service: bindingTestService, + Profile: bindingTestProfile, + Host: host, + ConfigFile: configFile, }}} } func TestBindSucceedsWithStateEntry(t *testing.T) { - bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) stderr := &bytes.Buffer{} cfg, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, - State: bindingTestState(bindingTestHost), + State: bindingTestState(cfgPath, bindingTestHost), Stderr: stderr, }) require.NoError(t, err) @@ -84,7 +98,7 @@ func TestBindPrintsNoticeWithoutStateEntry(t *testing.T) { require.NoError(t, err) assert.Nil(t, entry) assert.Equal(t, bindingTestHost, cfg.Host) - assert.Equal(t, "No install record for system.ai.github on profile \"logfood\"; using its current host https://adb-123.test.\n", stderr.String()) + assert.Equal(t, "No install record for system.ai.github on profile \"logfood\"; using its current host https://adb-123.test\n", stderr.String()) } func TestBindProfileAbsent(t *testing.T) { @@ -98,10 +112,11 @@ func TestBindProfileAbsent(t *testing.T) { { name: "with state entry", state: &mcp.State{Servers: []mcp.Entry{{ - Name: "github-gone", - Service: bindingTestService, - Profile: "gone", - Host: bindingTestHost, + Name: "github-gone", + Service: bindingTestService, + Profile: "gone", + Host: bindingTestHost, + ConfigFile: path, }}}, want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, path), }, @@ -126,54 +141,161 @@ func TestBindProfileAbsent(t *testing.T) { } func TestBindConfigFileAbsent(t *testing.T) { - bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) path := filepath.Join(t.TempDir(), "absent") t.Setenv("DATABRICKS_CONFIG_FILE", path) _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, - State: bindingTestState(bindingTestHost), + State: bindingTestState(cfgPath, bindingTestHost), Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindProfileNotFound) - assert.Contains(t, err.Error(), "which no longer exists in "+path+".") + assert.Contains(t, err.Error(), "which no longer exists in "+filepath.ToSlash(path)+". Recreate") } func TestBindHostChanged(t *testing.T) { - bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, - State: bindingTestState("https://adb-999.test"), + State: bindingTestState(cfgPath, "https://adb-999.test"), Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindProfileHostChanged) - assert.Equal(t, "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again.", err.Error()) + assert.Equal(t, "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) +} + +// TestBindHostChangedDoesNotContactNewHost checks that a re-pointed profile is rejected before +// any request reaches the new host. +func TestBindHostChangedDoesNotContactNewHost(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + t.Errorf("unexpected request to the re-pointed host: %s %s", r.Method, r.URL) + w.WriteHeader(http.StatusInternalServerError) + })) + t.Cleanup(server.Close) + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, server.URL, bindingTestToken)) + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(cfgPath, bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) +} + +func TestBindWorkspaceChanged(t *testing.T) { + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nworkspace_id = 222\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, bindingTestToken)) + state := bindingTestState(cfgPath, bindingTestHost) + state.Servers[0].WorkspaceID = "111" + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Equal(t, "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) +} + +func TestBindWorkspaceNotComparedWhenProfileNamesNone(t *testing.T) { + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nworkspace_id = none\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, bindingTestToken)) + state := bindingTestState(cfgPath, bindingTestHost) + state.Servers[0].WorkspaceID = "111" + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.NotNil(t, entry) } func TestBindHostUnchangedWhenOnlyTrailingSlashDiffers(t *testing.T) { - bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, - State: bindingTestState("https://adb-123.test/"), + State: bindingTestState(cfgPath, "https://adb-123.test/"), Stderr: &bytes.Buffer{}, }) require.NoError(t, err) assert.NotNil(t, entry) } +// TestBindNeverUsesEnvTokenForProfileWithoutToken checks that a profile without a token does not +// pick up DATABRICKS_TOKEN from the environment, which names another workspace here. +func TestBindNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents("")) + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(cfgPath, bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindAuthFailed) + assert.Contains(t, err.Error(), "\nRun: databricks auth login --profile logfood") + assert.NotContains(t, err.Error(), "dapi-env-token") + assert.Error(t, errors.Unwrap(err)) +} + +// TestBindReadsConfigFileFromContext checks that the config file named in the context, not the +// process environment, is used for both the profile lookup and authentication. +func TestBindReadsConfigFileFromContext(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + path := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) + t.Setenv("DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", path) + + cfg, _, err := mcp.Bind(ctx, mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(path, bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.Equal(t, bindingTestToken, cfg.Token) +} + +// TestBindMatchesEntryByConfigFile checks that an entry pinned to another config file does not +// bind a profile of the same name in the current file. +func TestBindMatchesEntryByConfigFile(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + current := bindingTestWriteConfig(t, "[logfood]\nhost = https://adb-456.test\nauth_type = pat\ntoken = dapi-current\n") + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", current) + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "other", Service: bindingTestService, Profile: bindingTestProfile, Host: bindingTestHost, ConfigFile: "/elsewhere/.databrickscfg"}, + {Name: "current", Service: bindingTestService, Profile: bindingTestProfile, Host: "https://adb-456.test", ConfigFile: current}, + }} + + cfg, entry, err := mcp.Bind(ctx, mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + require.NotNil(t, entry) + assert.Equal(t, "current", entry.Name) + assert.Equal(t, "dapi-current", cfg.Token) +} + func TestBindTokenCannotBeMinted(t *testing.T) { - bindingTestConfig(t, bindingTestProfileContents("")) + cfgPath := bindingTestConfig(t, bindingTestProfileContents("")) t.Setenv("DATABRICKS_TOKEN", "") _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, - State: bindingTestState(bindingTestHost), + State: bindingTestState(cfgPath, bindingTestHost), Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindAuthFailed) @@ -183,7 +305,7 @@ func TestBindTokenCannotBeMinted(t *testing.T) { // TestBindDoesNotReadStdin feeds a message on stdin and checks Bind leaves it unread. func TestBindDoesNotReadStdin(t *testing.T) { - bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) r, w, err := os.Pipe() require.NoError(t, err) t.Cleanup(func() { _ = r.Close() }) @@ -198,7 +320,7 @@ func TestBindDoesNotReadStdin(t *testing.T) { _, _, err = mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, - State: bindingTestState(bindingTestHost), + State: bindingTestState(cfgPath, bindingTestHost), Stderr: &bytes.Buffer{}, }) require.NoError(t, err) diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go index 5533d94f976..02985b25513 100644 --- a/libs/aitools/mcp/state.go +++ b/libs/aitools/mcp/state.go @@ -2,25 +2,18 @@ package mcp import ( "context" - "encoding/json" "errors" "fmt" - "io/fs" - "os" - "path/filepath" - "slices" - "time" + "strings" - "github.com/databricks/cli/libs/atomicfile" + "github.com/databricks/cli/libs/aitools/installer" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" ) -const ( - stateSchemaVersion = 1 - stateFileName = "state.json" -) +// configFileEnv names the variable that selects the Databricks config file. +const configFileEnv = "DATABRICKS_CONFIG_FILE" // Drift kinds reported by Reconcile. const ( @@ -28,33 +21,15 @@ const ( DriftHostChanged DriftKind = "host_changed" ) -// State is the record of MCP servers this CLI wrote. Agent config files stay the -// source of truth for existence; state records ownership and the binding. -type State struct { - SchemaVersion int `json:"schema_version"` - Servers []Entry `json:"servers"` -} +// State is the MCP section of the aitools install state. It lives in the same file as +// the skills state, so writers that save the whole file keep it. +type State = installer.MCPState // Entry is one installed server: a service bound to one profile for one set of agents. -type Entry struct { - Name string `json:"name"` - Service string `json:"service"` - Profile string `json:"profile"` - Host string `json:"host"` - WorkspaceID string `json:"workspace_id"` - SignInRequired bool `json:"sign_in_required"` - Command string `json:"command"` - ConfigFile string `json:"config_file"` - Agents map[string]AgentRecord `json:"agents"` - InstalledAt time.Time `json:"installed_at"` - CLIVersion string `json:"cli_version"` -} +type Entry = installer.MCPEntry // AgentRecord is where an entry was written for one agent. -type AgentRecord struct { - Path string `json:"path"` - Scope string `json:"scope"` -} +type AgentRecord = installer.MCPAgentRecord // DriftKind names how a state entry no longer matches the profiles file. type DriftKind string @@ -65,90 +40,105 @@ type Drift struct { Entry Entry // CurrentHost is the profile's host when Kind is DriftHostChanged. CurrentHost string + // CurrentWorkspaceID is the profile's workspace_id when Kind is DriftHostChanged, + // or "" when the profile names no workspace. + CurrentWorkspaceID string } -// LoadState reads the state file. A missing file yields an empty state. +// LoadState reads the MCP section of the aitools state file. A missing file or section +// yields an empty state. func LoadState(ctx context.Context) (*State, error) { - path, err := statePath(ctx) + dir, err := installer.GlobalSkillsDir(ctx) if err != nil { return nil, err } - data, err := os.ReadFile(path) - if errors.Is(err, fs.ErrNotExist) { - return &State{SchemaVersion: stateSchemaVersion}, nil - } + st, err := installer.LoadState(dir) if err != nil { - return nil, fmt.Errorf("failed to read MCP state file: %w", err) - } - - var state State - if err := json.Unmarshal(data, &state); err != nil { - return nil, fmt.Errorf("failed to parse MCP state file %s: %w", path, err) - } - if err := migrateState(&state); err != nil { return nil, err } - return &state, nil + if st == nil || st.MCP == nil { + return &State{}, nil + } + return st.MCP, nil } -// SaveState writes the state file atomically with mode 0600. It stamps the current schema version. +// SaveState replaces the MCP section of the aitools state file. The file is re-read first, +// so the skills and plugins records written since the caller's LoadState are kept. func SaveState(ctx context.Context, state *State) error { - path, err := statePath(ctx) + dir, err := installer.GlobalSkillsDir(ctx) if err != nil { return err } - state.SchemaVersion = stateSchemaVersion - data, err := json.MarshalIndent(state, "", " ") + st, err := installer.LoadState(dir) if err != nil { - return fmt.Errorf("failed to marshal MCP state: %w", err) + return err } - data = append(data, '\n') - return atomicfile.Write(path, data, 0o600, atomicfile.MkDir(0o755)) -} - -// migrateState brings a loaded state forward to stateSchemaVersion. It is forward-only: -// a file from a newer CLI is rejected instead of being rewritten. -func migrateState(state *State) error { - if state.SchemaVersion > stateSchemaVersion { - return fmt.Errorf("MCP state file has schema_version %d, but this CLI supports up to %d; upgrade the CLI", state.SchemaVersion, stateSchemaVersion) + if st == nil { + st = &installer.InstallState{SchemaVersion: installer.CurrentSchemaVersion} } - state.SchemaVersion = stateSchemaVersion - return nil + st.MCP = state + return installer.SaveState(dir, st) } -func statePath(ctx context.Context) (string, error) { - home, err := env.UserHomeDir(ctx) - if err != nil { - return "", err - } - return filepath.Join(home, ".databricks", "aitools", "mcp", stateFileName), nil -} - -// Reconcile reports entries whose profile is gone or whose host differs from the -// recorded one. A missing Databricks config file means every entry's profile is missing. +// Reconcile reports entries whose profile is gone, or whose profile now names another host, +// or another workspace when both the entry and the profile name one. Each entry is checked +// against the config file it is pinned to; an entry with no pinned file is checked against +// the current one. func Reconcile(ctx context.Context, profiler profile.Profiler, state *State) ([]Drift, error) { - profiles, err := profiler.LoadProfiles(ctx, profile.MatchAllProfiles) - if errors.Is(err, profile.ErrNoConfiguration) { - profiles = nil - } else if err != nil { - return nil, fmt.Errorf("failed to read Databricks profiles: %w", err) - } - var drifts []Drift for _, entry := range state.Servers { - i := slices.IndexFunc(profiles, profile.WithName(entry.Profile)) - switch { - case i < 0: + entryCtx := ctx + if entry.ConfigFile != "" { + entryCtx = env.Set(ctx, configFileEnv, entry.ConfigFile) + } + profiles, err := profiler.LoadProfiles(entryCtx, profile.WithName(entry.Profile)) + if errors.Is(err, profile.ErrNoConfiguration) { + profiles = nil + } else if err != nil { + return nil, fmt.Errorf("failed to read Databricks profiles: %w", err) + } + if len(profiles) == 0 { drifts = append(drifts, Drift{Kind: DriftProfileMissing, Entry: entry}) - case NormalizeHost(profiles[i].Host) != NormalizeHost(entry.Host): - drifts = append(drifts, Drift{Kind: DriftHostChanged, Entry: entry, CurrentHost: profiles[i].Host}) + continue + } + p := profiles[0] + if hostChanged(entry, p) || workspaceChanged(entry, p) { + drifts = append(drifts, Drift{ + Kind: DriftHostChanged, + Entry: entry, + CurrentHost: p.Host, + CurrentWorkspaceID: namedWorkspaceID(p.WorkspaceID), + }) } } return drifts, nil } -// NormalizeHost returns the host form that profile.WithHost compares, so host -// equality in state checks agrees with profile lookup. +// hostChanged reports whether the profile names another host than the one recorded. +func hostChanged(entry Entry, p profile.Profile) bool { + return NormalizeHost(entry.Host) != NormalizeHost(p.Host) +} + +// workspaceChanged reports whether the entry and the profile each name a workspace and the +// two differ. A profile without a workspace has nothing to compare. +func workspaceChanged(entry Entry, p profile.Profile) bool { + recorded := namedWorkspaceID(entry.WorkspaceID) + current := namedWorkspaceID(p.WorkspaceID) + return recorded != "" && current != "" && recorded != current +} + +// namedWorkspaceID returns the workspace ID a value names, or "" when it names none. The +// "none" sentinel that the CLI writes to skip workspace selection names none. +func namedWorkspaceID(id string) string { + if id == profile.WorkspaceIDNone { + return "" + } + return id +} + +// NormalizeHost returns the form in which two hosts compare equal for drift and binding +// checks. It applies the SDK's host canonicalization (scheme and trailing slash), then +// lowercases the result, because hostnames are case-insensitive. func NormalizeHost(host string) string { - return (&config.Config{Host: host}).CanonicalHostName() + return strings.ToLower((&config.Config{Host: host}).CanonicalHostName()) } diff --git a/libs/aitools/mcp/state_test.go b/libs/aitools/mcp/state_test.go index 14f44324d03..dffabe6f359 100644 --- a/libs/aitools/mcp/state_test.go +++ b/libs/aitools/mcp/state_test.go @@ -2,11 +2,13 @@ package mcp_test import ( "context" + "encoding/json" "os" "path/filepath" "testing" "time" + "github.com/databricks/cli/libs/aitools/installer" "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" @@ -14,10 +16,15 @@ import ( "github.com/stretchr/testify/require" ) -const stateTestFileRel = ".databricks/aitools/mcp/state.json" +const ( + stateTestFileRel = ".databricks/aitools/skills/.state.json" + stateTestWorkspaceID = "123" + stateTestOtherProfile = "dogfood" + stateTestOtherHost = "https://adb-456.test" +) -// stateTestCtx returns a context whose home directory is a fresh temp dir, plus the -// path of the state file inside it. +// stateTestCtx returns a context whose home directory is a fresh temp dir, plus the path of +// the aitools state file inside it. func stateTestCtx(t *testing.T) (context.Context, string) { t.Helper() home := t.TempDir() @@ -27,10 +34,10 @@ func stateTestCtx(t *testing.T) (context.Context, string) { func stateTestEntry() mcp.Entry { return mcp.Entry{ Name: "github-logfood", - Service: "system.ai.github", - Profile: "logfood", - Host: "https://adb-123.test", - WorkspaceID: "123", + Service: bindingTestService, + Profile: bindingTestProfile, + Host: bindingTestHost, + WorkspaceID: stateTestWorkspaceID, SignInRequired: true, Command: "/usr/local/bin/databricks", Agents: map[string]mcp.AgentRecord{ @@ -41,12 +48,27 @@ func stateTestEntry() mcp.Entry { } } +// stateTestEntryWith returns the test entry with its profile, host and workspace replaced. +func stateTestEntryWith(profileName, host, workspaceID string) mcp.Entry { + entry := stateTestEntry() + entry.Profile = profileName + entry.Host = host + entry.WorkspaceID = workspaceID + return entry +} + +// writeStateFile writes raw JSON to the aitools state file at path. +func writeStateFile(t *testing.T, path, raw string) { + t.Helper() + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(raw), 0o600)) +} + func TestLoadStateMissingFileIsEmpty(t *testing.T) { ctx, _ := stateTestCtx(t) state, err := mcp.LoadState(ctx) require.NoError(t, err) - assert.Equal(t, 1, state.SchemaVersion) assert.Empty(t, state.Servers) } @@ -61,129 +83,145 @@ func TestSaveStateThenLoadStateRoundTrips(t *testing.T) { assert.Equal(t, saved, loaded) } -func TestSaveStateWritesSection10Shape(t *testing.T) { - ctx, path := stateTestCtx(t) - - require.NoError(t, mcp.SaveState(ctx, &mcp.State{Servers: []mcp.Entry{stateTestEntry()}})) - - data, err := os.ReadFile(path) +// TestSaveStateKeepsInstallerRecords checks that an MCP save changes only the MCP section of the +// shared file. +func TestSaveStateKeepsInstallerRecords(t *testing.T) { + ctx, _ := stateTestCtx(t) + dir, err := installer.GlobalSkillsDir(ctx) require.NoError(t, err) - want := `{ - "schema_version": 1, - "servers": [ - { - "name": "github-logfood", - "service": "system.ai.github", - "profile": "logfood", - "host": "https://adb-123.test", - "workspace_id": "123", - "sign_in_required": true, - "command": "/usr/local/bin/databricks", - "config_file": "", - "agents": { - "claude-code": { - "path": "/home/me/.claude.json", - "scope": "user" - } - }, - "installed_at": "2026-11-12T10:12:00Z", - "cli_version": "0.300.0" - } - ] -} -` - assert.Equal(t, want, string(data)) -} + original := &installer.InstallState{ + SchemaVersion: installer.CurrentSchemaVersion, + Release: "v0.2.0", + LastUpdated: time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC), + Skills: map[string]string{"databricks": "1.0.0"}, + RepoDirs: map[string]string{"databricks": "skills"}, + Scope: installer.ScopeGlobal, + Plugins: map[string]installer.PluginRecord{ + "claude-code": {Marketplace: "databricks", Plugin: "databricks", Scope: "user", Version: "1.0.0"}, + }, + Files: map[string]installer.FileRecord{ + "databricks/SKILL.md": {SHA256: "abc123", Origin: "skills"}, + }, + } + require.NoError(t, installer.SaveState(dir, original)) -func TestLoadStateMigratesMissingSchemaVersion(t *testing.T) { - ctx, path := stateTestCtx(t) - require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) - require.NoError(t, os.WriteFile(path, []byte(`{"servers": []}`), 0o600)) + require.NoError(t, mcp.SaveState(ctx, &mcp.State{Servers: []mcp.Entry{stateTestEntry()}})) - state, err := mcp.LoadState(ctx) + loaded, err := installer.LoadState(dir) require.NoError(t, err) - assert.Equal(t, 1, state.SchemaVersion) + original.MCP = &installer.MCPState{Servers: []installer.MCPEntry{stateTestEntry()}} + assert.Equal(t, original, loaded) } -func TestLoadStateRejectsNewerSchemaVersion(t *testing.T) { +// TestSaveStateUpgradesV2FileToV3 checks that the first MCP save on a v2 file writes schema v3. +func TestSaveStateUpgradesV2FileToV3(t *testing.T) { ctx, path := stateTestCtx(t) - require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) - require.NoError(t, os.WriteFile(path, []byte(`{"schema_version": 2, "servers": []}`), 0o600)) - - _, err := mcp.LoadState(ctx) - require.ErrorContains(t, err, "schema_version 2") -} + writeStateFile(t, path, `{"schema_version": 2, "release": "v0.2.0", "skills": {"databricks": "1.0.0"}}`) -func TestLoadStateRejectsInvalidJSON(t *testing.T) { - ctx, path := stateTestCtx(t) - require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) - require.NoError(t, os.WriteFile(path, []byte(`{not json`), 0o600)) + require.NoError(t, mcp.SaveState(ctx, &mcp.State{Servers: []mcp.Entry{stateTestEntry()}})) - _, err := mcp.LoadState(ctx) - require.ErrorContains(t, err, "failed to parse MCP state file") + data, err := os.ReadFile(path) + require.NoError(t, err) + var raw map[string]any + require.NoError(t, json.Unmarshal(data, &raw)) + assert.EqualValues(t, 3, raw["schema_version"]) + assert.Equal(t, map[string]any{"databricks": "1.0.0"}, raw["skills"]) + assert.Contains(t, raw, "mcp") } // stateTestProfiles is the profiles file used by the drift tests. func stateTestProfiles() profile.InMemoryProfiler { return profile.InMemoryProfiler{Profiles: profile.Profiles{ - {Name: "logfood", Host: "https://adb-123.test"}, - {Name: "dogfood", Host: "https://adb-456.test"}, + {Name: bindingTestProfile, Host: bindingTestHost, WorkspaceID: stateTestWorkspaceID}, + {Name: stateTestOtherProfile, Host: stateTestOtherHost, WorkspaceID: "456"}, }} } -func TestReconcileReportsNoDriftForMatchingHost(t *testing.T) { - entry := stateTestEntry() - entry.Host = "https://adb-123.test/" - state := &mcp.State{Servers: []mcp.Entry{entry}} - - drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) - require.NoError(t, err) - assert.Empty(t, drifts) +func TestReconcile(t *testing.T) { + tests := []struct { + name string + entry mcp.Entry + wantKind mcp.DriftKind + wantHost string + wantWorkspaceID string + }{ + { + name: "matching host and workspace", + entry: stateTestEntryWith(bindingTestProfile, "https://adb-123.test/", stateTestWorkspaceID), + }, + { + name: "host differs only in case", + entry: stateTestEntryWith(bindingTestProfile, "https://ADB-123.test", stateTestWorkspaceID), + }, + { + name: "workspace not compared when the entry names none", + entry: stateTestEntryWith(bindingTestProfile, bindingTestHost, "none"), + }, + { + name: "profile missing", + entry: stateTestEntryWith("gone", bindingTestHost, stateTestWorkspaceID), + wantKind: mcp.DriftProfileMissing, + }, + { + name: "host changed", + entry: stateTestEntryWith(stateTestOtherProfile, bindingTestHost, stateTestWorkspaceID), + wantKind: mcp.DriftHostChanged, + wantHost: stateTestOtherHost, + wantWorkspaceID: "456", + }, + { + name: "workspace changed on the same host", + entry: stateTestEntryWith(bindingTestProfile, bindingTestHost, "999"), + wantKind: mcp.DriftHostChanged, + wantHost: bindingTestHost, + wantWorkspaceID: stateTestWorkspaceID, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + state := &mcp.State{Servers: []mcp.Entry{tt.entry}} + + drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) + require.NoError(t, err) + if tt.wantKind == "" { + assert.Empty(t, drifts) + return + } + assert.Equal(t, []mcp.Drift{{ + Kind: tt.wantKind, + Entry: tt.entry, + CurrentHost: tt.wantHost, + CurrentWorkspaceID: tt.wantWorkspaceID, + }}, drifts) + }) + } } -func TestReconcileReportsProfileMissing(t *testing.T) { +func TestReconcileWithoutConfigFileReportsEveryProfileMissing(t *testing.T) { + ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) entry := stateTestEntry() - entry.Profile = "gone" state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) + drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) require.NoError(t, err) assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) } -func TestReconcileReportsHostChanged(t *testing.T) { - entry := stateTestEntry() - entry.Profile = "dogfood" - entry.Host = "https://adb-123.test" - state := &mcp.State{Servers: []mcp.Entry{entry}} - - drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) - require.NoError(t, err) - assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftHostChanged, Entry: entry, CurrentHost: "https://adb-456.test"}}, drifts) -} - -func TestReconcileWithoutConfigFileReportsEveryProfileMissing(t *testing.T) { +// TestReconcileChecksEntryAgainstPinnedConfigFile checks that an entry pinned to a config file +// is checked against that file, not the one the environment names. +func TestReconcileChecksEntryAgainstPinnedConfigFile(t *testing.T) { + pinned := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) entry := stateTestEntry() + entry.ConfigFile = pinned state := &mcp.State{Servers: []mcp.Entry{entry}} drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) require.NoError(t, err) - assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) + assert.Empty(t, drifts) } -// TestNormalizeHostMatchesWithHost checks that NormalizeHost agrees with profile.WithHost. -func TestNormalizeHostMatchesWithHost(t *testing.T) { - cases := [][2]string{ - {"https://adb-123.test", "adb-123.test"}, - {"https://adb-123.test/", "https://adb-123.test"}, - {"adb-123.test", "https://adb-123.test"}, - {"https://adb-123.test", "https://adb-456.test"}, - {"https://adb-123.test:443", "https://adb-123.test"}, - } - for _, c := range cases { - want := profile.WithHost(c[0])(profile.Profile{Host: c[1]}) - got := mcp.NormalizeHost(c[0]) == mcp.NormalizeHost(c[1]) - assert.Equal(t, want, got, "%q vs %q", c[0], c[1]) - } +func TestNormalizeHostIgnoresSchemeCaseAndTrailingSlash(t *testing.T) { + assert.Equal(t, mcp.NormalizeHost("https://ADB-123.test/"), mcp.NormalizeHost("adb-123.test")) + assert.NotEqual(t, mcp.NormalizeHost("https://adb-123.test"), mcp.NormalizeHost("https://adb-456.test")) } From e9758407deeae8a4be469e43c2ad408887ac8faa Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 15:34:26 +0000 Subject: [PATCH 3/7] Address pass 2 review of MCP state and binding checks - Compare the workspace a profile names in its host query (?o=, ?w=, ?workspace_id=) as well as in workspace_id. "none" names no workspace. - Compare config files by resolved absolute path. An entry with no pinned file is the default ~/.databrickscfg, and Reconcile checks it there. - Treat a state file that holds only MCP records as having no skills or plugins in version, update, uninstall, scope detection and the legacy install guard. - Installer saves keep the MCP section on disk. SaveMCPState replaces it. Uninstall re-reads the MCP section before it deletes the state file. - Move the DATABRICKS_CONFIG_FILE name into mcpconfig.ConfigFileEnv, shared with the agent entry writer. - Read the home directory only when a config file path needs it. Co-authored-by: Isaac --- cmd/aitools/scope.go | 7 +- cmd/aitools/scope_test.go | 15 +++ cmd/aitools/uninstall.go | 2 +- cmd/aitools/uninstall_test.go | 11 ++ cmd/aitools/update.go | 2 +- cmd/aitools/update_test.go | 18 +++ cmd/aitools/version.go | 6 +- cmd/aitools/version_test.go | 20 +++ libs/aitools/installer/installer.go | 4 +- libs/aitools/installer/installer_test.go | 20 +++ libs/aitools/installer/state.go | 68 +++++++++- libs/aitools/installer/state_test.go | 135 +++++++++++++++----- libs/aitools/installer/uninstall.go | 14 +- libs/aitools/installer/uninstall_test.go | 22 +++- libs/aitools/installer/update.go | 2 +- libs/aitools/installer/update_test.go | 12 ++ libs/aitools/mcp/agentconfig/writer.go | 13 +- libs/aitools/mcp/agentconfig/writer_test.go | 6 +- libs/aitools/mcp/binding.go | 68 ++++++---- libs/aitools/mcp/binding_test.go | 123 +++++++++++++++++- libs/aitools/mcp/state.go | 60 ++++++--- libs/aitools/mcp/state_test.go | 47 ++++++- libs/aitools/mcpconfig/spec.go | 4 + 23 files changed, 565 insertions(+), 114 deletions(-) diff --git a/cmd/aitools/scope.go b/cmd/aitools/scope.go index 20f5545f542..9e13642a308 100644 --- a/cmd/aitools/scope.go +++ b/cmd/aitools/scope.go @@ -150,7 +150,8 @@ func parseScopeFlag(scopeFlag string, projectFlag, globalFlag, allowBoth bool) ( } } -// detectInstalledScopes checks which scopes have a .state.json file present. +// detectInstalledScopes checks which scopes have skills or plugins recorded in their state file. +// MCP records in the file do not count. func detectInstalledScopes(globalDir, projectDir string) (global, project bool, err error) { globalState, err := installer.LoadState(globalDir) if err != nil { @@ -162,7 +163,7 @@ func detectInstalledScopes(globalDir, projectDir string) (global, project bool, return false, false, err } - return globalState != nil, projectState != nil, nil + return globalState.HasSkillsOrPlugins(), projectState.HasSkillsOrPlugins(), nil } // explicitScope returns the single scope the flags parsed by parseScopeFlag @@ -293,7 +294,7 @@ func withExplicitScopeCheck(dir, scope, verb, projectDir string, hasGlobal, hasP if err != nil { return nil, err } - if state == nil { + if !state.HasSkillsOrPlugins() { return nil, scopeNotInstalledError(scope, verb, projectDir, hasGlobal, hasProject) } diff --git a/cmd/aitools/scope_test.go b/cmd/aitools/scope_test.go index da941dbb316..1b5a47b92f2 100644 --- a/cmd/aitools/scope_test.go +++ b/cmd/aitools/scope_test.go @@ -620,3 +620,18 @@ func TestResolveScopeForUpdateProjectFlagHintsUpdate(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "with --scope=global to update those") } + +// TestDetectInstalledScopesIgnoresMCPOnlyState checks that MCP records alone do not make a scope +// installed. +func TestDetectInstalledScopesIgnoresMCPOnlyState(t *testing.T) { + globalDir, projectDir := setupScopeTest(t) + require.NoError(t, installer.SaveMCPState(globalDir, &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}})) + + hasGlobal, hasProject, err := detectInstalledScopes(globalDir, projectDir) + require.NoError(t, err) + assert.False(t, hasGlobal) + assert.False(t, hasProject) + + _, err = withExplicitScopeCheck(globalDir, installer.ScopeGlobal, "update", projectDir, false, false) + assert.Error(t, err) +} diff --git a/cmd/aitools/uninstall.go b/cmd/aitools/uninstall.go index 787156ff519..d6462c30e6b 100644 --- a/cmd/aitools/uninstall.go +++ b/cmd/aitools/uninstall.go @@ -158,7 +158,7 @@ func confirmUninstall(ctx context.Context, state *installer.InstallState, opts i // uninstallConfirmMessage builds the human summary of what an uninstall will // remove. ask is false when there is nothing recorded to confirm. func uninstallConfirmMessage(state *installer.InstallState, opts installer.UninstallOptions) (msg string, ask bool) { - if state == nil { + if !state.HasSkillsOrPlugins() { return "", false } target := "all agents" diff --git a/cmd/aitools/uninstall_test.go b/cmd/aitools/uninstall_test.go index 6314254d727..70760d24402 100644 --- a/cmd/aitools/uninstall_test.go +++ b/cmd/aitools/uninstall_test.go @@ -133,3 +133,14 @@ func TestUninstallConfirmMessage(t *testing.T) { require.True(t, ask3) assert.Contains(t, msg3, "from cursor") } + +// TestUninstallConfirmMessageMCPOnlyState checks that an explicit skill list does not prompt when +// the state holds only MCP records. +func TestUninstallConfirmMessageMCPOnlyState(t *testing.T) { + st := &installer.InstallState{MCP: &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}}} + + _, ask := uninstallConfirmMessage(st, installer.UninstallOptions{Scope: installer.ScopeGlobal}) + assert.False(t, ask) + _, ask = uninstallConfirmMessage(st, installer.UninstallOptions{Scope: installer.ScopeGlobal, Skills: []string{"databricks-sql"}}) + assert.False(t, ask) +} diff --git a/cmd/aitools/update.go b/cmd/aitools/update.go index bdddafbc56c..6bf8ad67d0c 100644 --- a/cmd/aitools/update.go +++ b/cmd/aitools/update.go @@ -177,7 +177,7 @@ The command exits with code 1 when every update or check it attempts fails.`, // Reconcile file skills for non-plugin agents. Skip entirely for a // pure-plugin install (no file skills to maintain); keep calling for // no-state installs so the "no skills or plugins installed" guidance still fires. - if state == nil || len(state.Skills) > 0 { + if !state.HasSkillsOrPlugins() || len(state.Skills) > 0 { opts := installer.UpdateOptions{ Check: check, Force: force, diff --git a/cmd/aitools/update_test.go b/cmd/aitools/update_test.go index 74e869213ac..ad6ad104776 100644 --- a/cmd/aitools/update_test.go +++ b/cmd/aitools/update_test.go @@ -616,3 +616,21 @@ func TestUpdateWithoutStateKeepsTheInstallGuidance(t *testing.T) { err := cmd.Execute() require.ErrorIs(t, err, installer.ErrNothingInstalled) } + +// TestUpdateMCPOnlyStateReachesSkillsUpdate checks that a state file holding only MCP records still +// runs the skills update, which reports that nothing is installed. +func TestUpdateMCPOnlyStateReachesSkillsUpdate(t *testing.T) { + home := setupTestAgents(t) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.6") + calls := setupUpdateMock(t) + + globalDir := filepath.Join(home, ".databricks", "aitools", "skills") + require.NoError(t, installer.SaveMCPState(globalDir, &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}})) + + ctx, _ := cmdio.NewTestContextWithStderr(t.Context()) + cmd := NewUpdateCmd() + cmd.SetContext(ctx) + cmd.SetArgs([]string{"--scope", "global", "--check"}) + require.NoError(t, cmd.Execute()) + assert.Len(t, *calls, 1) +} diff --git a/cmd/aitools/version.go b/cmd/aitools/version.go index e1d268487bf..b55308f2c9f 100644 --- a/cmd/aitools/version.go +++ b/cmd/aitools/version.go @@ -42,7 +42,7 @@ func NewVersionCmd() *cobra.Command { } } - if globalState == nil && projectState == nil { + if !globalState.HasSkillsOrPlugins() && !projectState.HasSkillsOrPlugins() { cmdio.LogString(ctx, "No Databricks skills or plugins installed.") cmdio.LogString(ctx, "") cmdio.LogString(ctx, "Run 'databricks aitools install' to get started.") @@ -57,14 +57,14 @@ func NewVersionCmd() *cobra.Command { // Always label the scope (global / project) for both skills and // plugins so it is unambiguous where each thing is installed. - if globalState != nil { + if globalState.HasSkillsOrPlugins() { if len(globalState.Skills) > 0 { printVersionLine(ctx, "Skills (global)", globalState, latestRef) } printPluginLines(ctx, globalState, "global") } - if projectState != nil { + if projectState.HasSkillsOrPlugins() { if len(projectState.Skills) > 0 { printVersionLine(ctx, "Skills (project)", projectState, latestRef) } diff --git a/cmd/aitools/version_test.go b/cmd/aitools/version_test.go index cf4a6516e0a..cf4b25755d8 100644 --- a/cmd/aitools/version_test.go +++ b/cmd/aitools/version_test.go @@ -157,3 +157,23 @@ func TestVersionAlwaysLabelsScope(t *testing.T) { assert.Contains(t, output, "Skills (global): v0.1.0") assert.NotContains(t, output, "Skills (project)") } + +// TestVersionMCPOnlyStateReportsNothingInstalled checks that a state file holding only MCP records +// is reported as having no skills or plugins. +func TestVersionMCPOnlyStateReportsNothingInstalled(t *testing.T) { + tmp := t.TempDir() + t.Setenv("HOME", tmp) + t.Setenv("USERPROFILE", tmp) + t.Chdir(tmp) + + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + require.NoError(t, installer.SaveMCPState(globalDir, &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}})) + + ctx, stderr := cmdio.NewTestContextWithStderr(t.Context()) + cmd := NewVersionCmd() + cmd.SetContext(ctx) + require.NoError(t, cmd.RunE(cmd, nil)) + + assert.Contains(t, stderr.String(), "No Databricks skills or plugins installed.") + assert.NotContains(t, stderr.String(), "Databricks skills and plugins:") +} diff --git a/libs/aitools/installer/installer.go b/libs/aitools/installer/installer.go index 8e5115a1998..178dbebb49b 100644 --- a/libs/aitools/installer/installer.go +++ b/libs/aitools/installer/installer.go @@ -358,13 +358,13 @@ func InstallSkillsForAgents(ctx context.Context, src ManifestSource, targetAgent } // A "legacy install" is one from before the CLI tracked install state in a - // JSON file: skills exist on disk but there's no state file (state == nil). + // JSON file: skills exist on disk but there's no record of them in state. // A targeted install (--skills foo) here would write a state file listing only // foo, leaving the other on-disk skills untracked. Block it so the user runs a // full install first, which rebuilds complete state. Global scope only, since // legacy installs only ever wrote to the global dir. // hasLegacyInstall lives in update.go (shared with the update/uninstall paths). - legacyInstall := state == nil && scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) + legacyInstall := !state.HasSkillsOrPlugins() && scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) if legacyInstall && len(opts.SpecificSkills) > 0 { return &RefusedError{Err: errors.New("legacy install detected without state tracking; run 'databricks aitools install --scope=global --skills-only' (without --skills) first to rebuild state")} } diff --git a/libs/aitools/installer/installer_test.go b/libs/aitools/installer/installer_test.go index ad61cf02044..8a55fad9d00 100644 --- a/libs/aitools/installer/installer_test.go +++ b/libs/aitools/installer/installer_test.go @@ -1654,3 +1654,23 @@ func TestInstallAllSkillsWithNoSkillToInstall(t *testing.T) { require.NoError(t, InstallAllSkills(ctx)) assert.Contains(t, stderr.String(), NoSkillsResolvedMessage) } + +// TestLegacyTargetedInstallBlockedWithMCPOnlyState checks that MCP records in the state file do not +// count as tracked skills, so skills on disk without a skills record still block a targeted install. +func TestLegacyTargetedInstallBlockedWithMCPOnlyState(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + require.NoError(t, os.MkdirAll(filepath.Join(globalDir, "databricks-sql"), 0o755)) + require.NoError(t, SaveMCPState(globalDir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + + src := &mockManifestSource{manifest: testManifest()} + err := InstallSkillsForAgents(ctx, src, []*agents.Agent{testAgent(tmp)}, InstallOptions{ + SpecificSkills: []string{"databricks-sql"}, + }) + require.Error(t, err) + assert.Contains(t, err.Error(), "legacy install detected") +} diff --git a/libs/aitools/installer/state.go b/libs/aitools/installer/state.go index 1af228a91f4..fa49f476a3d 100644 --- a/libs/aitools/installer/state.go +++ b/libs/aitools/installer/state.go @@ -119,6 +119,13 @@ type FileRecord struct { Origin string `json:"origin,omitempty"` } +// HasSkillsOrPlugins reports whether the state records an installed skill or plugin. MCP records +// share the file but are not installs, so a state holding only MCP records reports false, and so +// does a nil state. +func (s *InstallState) HasSkillsOrPlugins() bool { + return s != nil && (len(s.Skills) > 0 || len(s.Plugins) > 0) +} + // LoadState reads install state from the given directory. // Returns (nil, nil) when the state file does not exist. func LoadState(dir string) (*InstallState, error) { @@ -150,9 +157,66 @@ func migrateState(state *InstallState) { } } -// SaveState writes install state to the given directory atomically. -// Creates the directory if it does not exist. +// SaveState writes the skills and plugins records of state to the given directory atomically, +// creating the directory if it does not exist. The MCP section is written by SaveMCPState, so +// SaveState keeps the one on disk, not the one in state. This keeps MCP records saved after state +// was loaded. func SaveState(dir string, state *InstallState) error { + onDisk, err := LoadState(dir) + if err != nil { + return err + } + out := *state + out.MCP = nil + if onDisk != nil { + out.MCP = onDisk.MCP + } + return writeState(dir, &out) +} + +// SaveMCPState replaces the MCP section of the state file in the given directory. The file is read +// again first, so the skills and plugins records are kept. A nil or empty state removes the MCP +// section, and the file is removed once no skills, plugins or MCP servers remain. +func SaveMCPState(dir string, mcp *MCPState) error { + st, err := LoadState(dir) + if err != nil { + return err + } + if st == nil { + if mcp == nil || len(mcp.Servers) == 0 { + return nil + } + st = &InstallState{ + SchemaVersion: CurrentSchemaVersion, + Skills: map[string]string{}, + RepoDirs: map[string]string{}, + Files: map[string]FileRecord{}, + } + } + st.MCP = nil + if mcp != nil && len(mcp.Servers) > 0 { + st.MCP = mcp + } + if !st.HasSkillsOrPlugins() && st.MCP == nil { + err := os.Remove(filepath.Join(dir, stateFileName)) + if err != nil && !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("failed to remove state file: %w", err) + } + return nil + } + return writeState(dir, st) +} + +// mcpRecordedOnDisk reports whether the state file in the given directory holds MCP servers. +func mcpRecordedOnDisk(dir string) (bool, error) { + st, err := LoadState(dir) + if err != nil { + return false, err + } + return st != nil && st.MCP != nil && len(st.MCP.Servers) > 0, nil +} + +func writeState(dir string, state *InstallState) error { data, err := json.MarshalIndent(state, "", " ") if err != nil { return fmt.Errorf("failed to marshal state: %w", err) diff --git a/libs/aitools/installer/state_test.go b/libs/aitools/installer/state_test.go index 10f092a3b25..65f3eb2162d 100644 --- a/libs/aitools/installer/state_test.go +++ b/libs/aitools/installer/state_test.go @@ -212,52 +212,125 @@ func TestSaveAndLoadStateWithOptionalFields(t *testing.T) { func TestSaveAndLoadStateKeepsMCPSection(t *testing.T) { dir := t.TempDir() - original := &InstallState{ - SchemaVersion: CurrentSchemaVersion, - Skills: map[string]string{}, - MCP: &MCPState{Servers: []MCPEntry{{ - Name: "github-logfood", - Service: "system.ai.github", - Profile: "logfood", - Host: "https://adb-123.test", - WorkspaceID: "123", - SignInRequired: true, - Command: "/usr/local/bin/databricks", - Agents: map[string]MCPAgentRecord{ - "claude-code": {Path: "/home/me/.claude.json", Scope: "user"}, - }, - InstalledAt: time.Date(2026, 11, 12, 10, 12, 0, 0, time.UTC), - CLIVersion: "0.300.0", - }}}, - } - require.NoError(t, SaveState(dir, original)) + original := &MCPState{Servers: []MCPEntry{{ + Name: "github-logfood", + Service: "system.ai.github", + Profile: "logfood", + Host: "https://adb-123.test", + WorkspaceID: "123", + SignInRequired: true, + Command: "/usr/local/bin/databricks", + Agents: map[string]MCPAgentRecord{ + "claude-code": {Path: "/home/me/.claude.json", Scope: "user"}, + }, + InstalledAt: time.Date(2026, 11, 12, 10, 12, 0, 0, time.UTC), + CLIVersion: "0.300.0", + }}} + require.NoError(t, SaveMCPState(dir, original)) loaded, err := LoadState(dir) require.NoError(t, err) - assert.Equal(t, original, loaded) + assert.Equal(t, original, loaded.MCP) } -// TestSaveStateKeepsMCPSectionWhenInstallerSaves checks that a skills write, which loads the -// file before saving it, leaves the MCP section in place. -func TestSaveStateKeepsMCPSectionWhenInstallerSaves(t *testing.T) { +// TestSaveStateKeepsMCPRecordsSavedAfterLoad checks that an installer save writes the MCP section +// on disk, not the copy loaded before an MCP save was made. +func TestSaveStateKeepsMCPRecordsSavedAfterLoad(t *testing.T) { dir := t.TempDir() - require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: CurrentSchemaVersion, - MCP: &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}}, - })) + require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) - state, err := LoadState(dir) + stale, err := LoadState(dir) require.NoError(t, err) - state.Skills = map[string]string{"databricks": "1.0.0"} - require.NoError(t, SaveState(dir, state)) + require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + + stale.Skills["databricks"] = "1.1.0" + require.NoError(t, SaveState(dir, stale)) loaded, err := LoadState(dir) require.NoError(t, err) - assert.Equal(t, map[string]string{"databricks": "1.0.0"}, loaded.Skills) + assert.Equal(t, map[string]string{"databricks": "1.1.0"}, loaded.Skills) require.NotNil(t, loaded.MCP) assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) } +// TestSaveMCPStateKeepsSkillRecords checks that an MCP save changes only the MCP section. +func TestSaveMCPStateKeepsSkillRecords(t *testing.T) { + dir := t.TempDir() + require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) + + require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + require.NoError(t, SaveMCPState(dir, &MCPState{})) + + loaded, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, map[string]string{"databricks": "1.0.0"}, loaded.Skills) + assert.Nil(t, loaded.MCP) +} + +// TestSaveMCPStateRemovesFileWhenNothingLeft checks that the file goes once neither skills, plugins +// nor MCP servers remain, and that it is not created for an empty MCP section. +func TestSaveMCPStateRemovesFileWhenNothingLeft(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, stateFileName) + + require.NoError(t, SaveMCPState(dir, &MCPState{})) + _, err := os.Stat(path) + assert.ErrorIs(t, err, os.ErrNotExist) + + require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + require.NoError(t, SaveMCPState(dir, &MCPState{})) + _, err = os.Stat(path) + assert.ErrorIs(t, err, os.ErrNotExist) +} + +// TestSaveMCPStateCreatesMapsForInstaller checks that a file created by an MCP save loads with the +// maps the installer writes into, so a later skills write does not panic. +func TestSaveMCPStateCreatesMapsForInstaller(t *testing.T) { + dir := t.TempDir() + require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + + loaded, err := LoadState(dir) + require.NoError(t, err) + assert.NotNil(t, loaded.Skills) + assert.False(t, loaded.HasSkillsOrPlugins()) +} + +func TestHasSkillsOrPlugins(t *testing.T) { + tests := []struct { + name string + state *InstallState + want bool + }{ + {name: "nil", state: nil}, + {name: "mcp only", state: &InstallState{MCP: &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}}}}, + {name: "skills", state: &InstallState{Skills: map[string]string{"databricks": "1.0.0"}}, want: true}, + {name: "plugins", state: &InstallState{Plugins: map[string]PluginRecord{"claude-code": {Plugin: "databricks"}}}, want: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.state.HasSkillsOrPlugins()) + }) + } +} + +// TestMCPRecordedOnDisk checks the re-read that uninstall makes before it deletes the state file. +func TestMCPRecordedOnDisk(t *testing.T) { + dir := t.TempDir() + recorded, err := mcpRecordedOnDisk(dir) + require.NoError(t, err) + assert.False(t, recorded, "no file") + + require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) + recorded, err = mcpRecordedOnDisk(dir) + require.NoError(t, err) + assert.False(t, recorded, "skills only") + + require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + recorded, err = mcpRecordedOnDisk(dir) + require.NoError(t, err) + assert.True(t, recorded) +} + func TestLoadStateMigratesV2ToV3AndSavesV3(t *testing.T) { dir := t.TempDir() v2 := `{"schema_version":2,"release":"v0.2.0","last_updated":"2026-03-22T10:00:00Z","skills":{"databricks":"1.0.0"}}` diff --git a/libs/aitools/installer/uninstall.go b/libs/aitools/installer/uninstall.go index b07b920af6e..a7feb8aae05 100644 --- a/libs/aitools/installer/uninstall.go +++ b/libs/aitools/installer/uninstall.go @@ -75,7 +75,7 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni return nil, fmt.Errorf("failed to load install state: %w", err) } - if state == nil { + if !state.HasSkillsOrPlugins() { if scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) { return nil, fmt.Errorf("%w; run 'databricks aitools install' first, then uninstall", ErrLegacyInstall) } @@ -199,11 +199,17 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni // If nothing remains in this scope, clean up orphaned symlinks. Delete the // state file unless it still holds MCP records, which uninstall does not own; - // otherwise persist the trimmed state. - if len(state.Skills) == 0 && len(state.Plugins) == 0 { + // otherwise persist the trimmed state. The MCP records are read from disk now, + // since an MCP save may have happened after state was loaded above. + noSkillsOrPlugins := len(state.Skills) == 0 && len(state.Plugins) == 0 + if noSkillsOrPlugins { cleanOrphanedSymlinks(ctx, baseDir, scope, cwd) } - if len(state.Skills) == 0 && len(state.Plugins) == 0 && (state.MCP == nil || len(state.MCP.Servers) == 0) { + mcpRecorded, err := mcpRecordedOnDisk(baseDir) + if err != nil { + return err + } + if noSkillsOrPlugins && !mcpRecorded { stateFile := filepath.Join(baseDir, stateFileName) if err := os.Remove(stateFile); err != nil && !errors.Is(err, fs.ErrNotExist) { return nil, fmt.Errorf("failed to remove state file: %w", err) diff --git a/libs/aitools/installer/uninstall_test.go b/libs/aitools/installer/uninstall_test.go index eddfb317c82..3d7cea0bc5f 100644 --- a/libs/aitools/installer/uninstall_test.go +++ b/libs/aitools/installer/uninstall_test.go @@ -616,11 +616,7 @@ func TestUninstallTargetedUntrackedSkillGivesRecoveryGuidance(t *testing.T) { func TestUninstallKeepsStateFileWithMCPRecords(t *testing.T) { tmp := setupTestHome(t) globalDir := installTestSkills(t, tmp) - - state, err := LoadState(globalDir) - require.NoError(t, err) - state.MCP = &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}} - require.NoError(t, SaveState(globalDir, state)) + require.NoError(t, SaveMCPState(globalDir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) ctx := cmdio.MockDiscard(t.Context()) require.NoError(t, UninstallSkills(ctx)) @@ -633,3 +629,19 @@ func TestUninstallKeepsStateFileWithMCPRecords(t *testing.T) { require.NotNil(t, loaded.MCP) assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) } + +// TestUninstallMCPOnlyStateReturnsError checks that a state file holding only MCP records counts +// as having no skills or plugins, and that uninstall leaves the MCP records in place. +func TestUninstallMCPOnlyStateReturnsError(t *testing.T) { + tmp := setupTestHome(t) + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + require.NoError(t, SaveMCPState(globalDir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + + err := UninstallSkills(cmdio.MockDiscard(t.Context())) + require.Error(t, err) + assert.Contains(t, err.Error(), "no skills or plugins installed") + + recorded, err := mcpRecordedOnDisk(globalDir) + require.NoError(t, err) + assert.True(t, recorded) +} diff --git a/libs/aitools/installer/update.go b/libs/aitools/installer/update.go index 30f1d98ab48..b4bdf3cad3b 100644 --- a/libs/aitools/installer/update.go +++ b/libs/aitools/installer/update.go @@ -82,7 +82,7 @@ func UpdateSkills(ctx context.Context, src ManifestSource, targetAgents []*agent return nil, fmt.Errorf("failed to load install state: %w", err) } - if state == nil { + if !state.HasSkillsOrPlugins() { if scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) { return nil, fmt.Errorf("%w; run 'databricks aitools install' to refresh before updating", ErrLegacyInstall) } diff --git a/libs/aitools/installer/update_test.go b/libs/aitools/installer/update_test.go index dad9840ce0a..8ad8146cc01 100644 --- a/libs/aitools/installer/update_test.go +++ b/libs/aitools/installer/update_test.go @@ -695,3 +695,15 @@ func TestFormatUpdateResultCheckMode(t *testing.T) { assert.Contains(t, checkOutput, "would add databricks-notebooks") assert.Contains(t, checkOutput, "Would update 2 skills.") } + +func TestUpdateMCPOnlyStateReturnsInstallHint(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + require.NoError(t, SaveMCPState(globalDir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) + + src := &mockManifestSource{manifest: testManifest()} + _, err := UpdateSkills(ctx, src, nil, UpdateOptions{}) + require.Error(t, err) + assert.Contains(t, err.Error(), "no skills or plugins installed") +} diff --git a/libs/aitools/mcp/agentconfig/writer.go b/libs/aitools/mcp/agentconfig/writer.go index b1b7ad26bce..6c0b27823c7 100644 --- a/libs/aitools/mcp/agentconfig/writer.go +++ b/libs/aitools/mcp/agentconfig/writer.go @@ -66,16 +66,11 @@ var launchKeys = []string{"command", "args", "env", "environment", "type"} // environment in OpenCode. var envKeys = []string{"env", "environment"} -// pinnedEnv is the environment variable that this CLI pins into an entry when it -// is set in the caller's environment. A write that does not pin it removes it from -// the entry. Other variables are the user's, and are kept. -const pinnedEnv = "DATABRICKS_CONFIG_FILE" - // ownedKeys returns the launch keys that the agent's builder writes. Upsert // updates these keys and keeps every other key of the entry. The probe passes a // variable, since a builder writes its env key only when it has variables. func ownedKeys(spec *mcpconfig.Spec) []string { - probe := spec.Entry("", nil, map[string]string{pinnedEnv: ""}) + probe := spec.Entry("", nil, map[string]string{mcpconfig.ConfigFileEnv: ""}) var keys []string for _, key := range launchKeys { if _, ok := probe[key]; ok { @@ -343,8 +338,8 @@ func keepUserKeys(current, want map[string]any, keys []string) (map[string]any, } // mergeEnv returns the env table after a write. It holds the variables of want, -// and the variables of have other than pinnedEnv. It reports false when the table -// is absent: want lacks it and no variable is left. +// and the variables of have other than the pinned config file variable. It +// reports false when the table is absent: want lacks it and no variable is left. func mergeEnv(have, want any, inWant bool) (any, bool, error) { haveEnv, err := envTable(have) if err != nil { @@ -353,7 +348,7 @@ func mergeEnv(have, want any, inWant bool) (any, bool, error) { wantEnv, _ := want.(map[string]any) out := map[string]any{} for k, v := range haveEnv { - if k != pinnedEnv { + if k != mcpconfig.ConfigFileEnv { out[k] = v } } diff --git a/libs/aitools/mcp/agentconfig/writer_test.go b/libs/aitools/mcp/agentconfig/writer_test.go index f0ab74fb6e2..4819ff495f7 100644 --- a/libs/aitools/mcp/agentconfig/writer_test.go +++ b/libs/aitools/mcp/agentconfig/writer_test.go @@ -562,12 +562,12 @@ func TestUpsertUpdateAddsAndRemovesLaunchKeys(t *testing.T) { writeFile(t, path, []byte(`{"mcpServers":{"`+testName+`":{"command":"`+testCmd+`","args":["aitools","mcp","serve","--agent","test"]}}}`), 0o600) a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") - _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{pinnedEnv: "/cfg"}, false) + _, status, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{mcpconfig.ConfigFileEnv: "/cfg"}, false) require.NoError(t, err) assert.Equal(t, StatusUpdated, status) entries, err := Read(t.Context(), a) require.NoError(t, err) - assert.Equal(t, map[string]any{pinnedEnv: "/cfg"}, entries[testName]["env"]) + assert.Equal(t, map[string]any{mcpconfig.ConfigFileEnv: "/cfg"}, entries[testName]["env"]) requireStrictJSON(t, string(readFile(t, path))) _, status, err = Upsert(t.Context(), a, testName, testCmd, testArgs("test"), nil, false) @@ -780,7 +780,7 @@ func TestUpsertRefusesEnvThatIsNotATable(t *testing.T) { writeFile(t, path, []byte(data), 0o600) a := testAgent(path, mcpconfig.FormatJSON, "mcpServers") - _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{pinnedEnv: "/cfg"}, false) + _, _, err := Upsert(t.Context(), a, testName, testCmd, testArgs("test"), map[string]string{mcpconfig.ConfigFileEnv: "/cfg"}, false) require.ErrorIs(t, err, ErrConfigInvalid) assert.Equal(t, data, string(readFile(t, path))) } diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 78e1e565bfd..089abf7fa4f 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -7,8 +7,8 @@ import ( "io" "net/http" "path/filepath" - "slices" + "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/databrickscfg" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" @@ -19,6 +19,12 @@ import ( // from other errors. const bindExitCode = 2 +// Commands that binding messages tell the user to run. +const ( + authLoginCommand = "databricks auth login" + mcpRemoveCommand = "databricks aitools mcp remove" +) + // BindCategory names the kind of a binding failure. The values are stable identifiers // for the failure, not telemetry values. type BindCategory string @@ -67,35 +73,41 @@ func (e *BindError) ExitCode() int { // The profile is checked before any credential is resolved, so a re-pointed host is never // contacted. The config file is the one in the context, the same file the profile lookup reads. func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) { - configFile := env.Get(ctx, configFileEnv) - entry := findEntry(opts.State, opts.Service, opts.Profile, configFile) + configFile, err := resolveConfigFile(ctx, env.Get(ctx, mcpconfig.ConfigFileEnv)) + if err != nil { + return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + } + entry, err := findEntry(ctx, opts.State, opts.Service, opts.Profile, configFile) + if err != nil { + return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + } name := opts.Service if entry != nil { name = entry.Name } profiler := profile.GetProfiler(ctx) - path, err := profiler.GetPath(ctx) - if err != nil { - return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} - } profiles, err := profiler.LoadProfiles(ctx, profile.WithName(opts.Profile)) if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} } if len(profiles) == 0 { + path, err := profiler.GetPath(ctx) + if err != nil { + return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + } return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(path), entry) } p := profiles[0] if entry != nil && hostChanged(*entry, p) { - message := fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: databricks aitools mcp remove --profile %s and add it again", - name, entry.Host, opts.Profile, p.Host, opts.Profile) + message := fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: %s --profile %s and add it again", + name, entry.Host, opts.Profile, p.Host, mcpRemoveCommand, opts.Profile) return nil, nil, &BindError{Category: BindProfileHostChanged, message: message} } if entry != nil && workspaceChanged(*entry, p) { - message := fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: databricks aitools mcp remove --profile %s and add it again", - name, entry.WorkspaceID, opts.Profile, p.WorkspaceID, opts.Profile) + message := fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: %s --profile %s and add it again", + name, entry.WorkspaceID, opts.Profile, profileWorkspaceID(p), mcpRemoveCommand, opts.Profile) return nil, nil, &BindError{Category: BindProfileHostChanged, message: message} } @@ -130,27 +142,35 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) // profileGoneError builds the error for a profile that is missing from the config file. // Without a state entry the recorded host is unknown, so the recreate hint omits --host. func profileGoneError(name, profileName, path string, entry *Entry) error { - recreate := "databricks auth login --profile " + profileName + recreate := authLoginCommand + " --profile " + profileName if entry != nil { - recreate = "databricks auth login --host " + entry.Host + " --profile " + profileName + recreate = authLoginCommand + " --host " + entry.Host + " --profile " + profileName } - message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which no longer exists in %s. Recreate it with: %s or remove the server with: databricks aitools mcp remove --profile %s", - name, profileName, path, recreate, profileName) + message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which no longer exists in %s. Recreate it with: %s or remove the server with: %s --profile %s", + name, profileName, path, recreate, mcpRemoveCommand, profileName) return &BindError{Category: BindProfileNotFound, message: message} } func authError(profileName string, cause error) error { - message := fmt.Sprintf("%s\nRun: databricks auth login --profile %s", cause, profileName) + message := fmt.Sprintf("%s\nRun: %s --profile %s", cause, authLoginCommand, profileName) return &BindError{Category: BindAuthFailed, message: message, cause: cause} } -// findEntry returns the entry for the service and profile that is pinned to configFile, or nil. -func findEntry(state *State, service, profileName, configFile string) *Entry { - i := slices.IndexFunc(state.Servers, func(e Entry) bool { - return e.Service == service && e.Profile == profileName && e.ConfigFile == configFile - }) - if i < 0 { - return nil +// findEntry returns the entry for the service and profile whose config file is configFile, or nil. +// Both are compared as resolved paths, so an entry with no pinned file matches the default file. +func findEntry(ctx context.Context, state *State, service, profileName, configFile string) (*Entry, error) { + for i := range state.Servers { + entry := &state.Servers[i] + if entry.Service != service || entry.Profile != profileName { + continue + } + path, err := resolveConfigFile(ctx, entry.ConfigFile) + if err != nil { + return nil, err + } + if path == configFile { + return entry, nil + } } - return &state.Servers[i] + return nil, nil } diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index a7b5d741e9f..12e6951efc6 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -118,12 +118,12 @@ func TestBindProfileAbsent(t *testing.T) { Host: bindingTestHost, ConfigFile: path, }}}, - want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, path), + want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), }, { name: "without state entry", state: &mcp.State{}, - want: fmt.Sprintf(`MCP server system.ai.github is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, path), + want: fmt.Sprintf(`MCP server system.ai.github is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), }, } for _, tt := range tests { @@ -338,3 +338,122 @@ func requireBindError(t *testing.T, err error, want mcp.BindCategory) { assert.Equal(t, want, bindErr.Category) assert.Equal(t, 2, bindErr.ExitCode()) } + +// bindingTestDefaultConfig writes contents to ~/.databrickscfg in a fresh home directory, with +// DATABRICKS_CONFIG_FILE empty, and returns the path of the default file. +func bindingTestDefaultConfig(t *testing.T, contents string) string { + t.Helper() + home := t.TempDir() + t.Setenv(env.HomeEnvVar(), home) + t.Setenv("DATABRICKS_CONFIG_FILE", "") + t.Setenv("DATABRICKS_HOST", "https://adb-999.test") + t.Setenv("DATABRICKS_TOKEN", "dapi-env-token") + path := filepath.Join(home, ".databrickscfg") + require.NoError(t, os.WriteFile(path, []byte(contents), 0o600)) + return path +} + +// TestBindWorkspaceFromHostQueryChanged checks that a workspace named in the profile's host query +// is compared with the recorded one, as the SDK resolves it. +func TestBindWorkspaceFromHostQueryChanged(t *testing.T) { + for _, query := range []string{"?o=222", "?w=222", "?workspace_id=222"} { + t.Run(query, func(t *testing.T) { + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s/%s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, query, bindingTestToken)) + state := bindingTestState(cfgPath, bindingTestHost) + state.Servers[0].WorkspaceID = "111" + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Equal(t, "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) + }) + } +} + +// TestBindWorkspaceFromHostQueryNoneIsUnset checks that a host query naming no workspace is not +// compared. +func TestBindWorkspaceFromHostQueryNoneIsUnset(t *testing.T) { + for _, query := range []string{"?o=none", "?w=none"} { + t.Run(query, func(t *testing.T) { + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s/%s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, query, bindingTestToken)) + state := bindingTestState(cfgPath, bindingTestHost) + state.Servers[0].WorkspaceID = "111" + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.NotNil(t, entry) + }) + } +} + +// TestBindUnpinnedEntryMatchesDefaultConfigFile checks that an entry with no pinned config file +// names the default file, so it matches whether DATABRICKS_CONFIG_FILE is unset or names that file. +func TestBindUnpinnedEntryMatchesDefaultConfigFile(t *testing.T) { + defaultPath := bindingTestDefaultConfig(t, bindingTestProfileContents(bindingTestToken)) + state := &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", + Service: bindingTestService, + Profile: bindingTestProfile, + Host: "https://adb-999.test", + }}} + + for _, configFile := range []string{"", defaultPath} { + t.Run("env="+configFile, func(t *testing.T) { + t.Setenv("DATABRICKS_CONFIG_FILE", configFile) + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) + }) + } +} + +// TestBindUnpinnedEntryDoesNotMatchOtherConfigFile checks that an entry with no pinned config file +// does not bind a profile of the same name in another config file. +func TestBindUnpinnedEntryDoesNotMatchOtherConfigFile(t *testing.T) { + bindingTestDefaultConfig(t, bindingTestProfileContents(bindingTestToken)) + other := bindingTestWriteConfig(t, "[logfood]\nhost = https://adb-456.test\nauth_type = pat\ntoken = dapi-other\n") + t.Setenv("DATABRICKS_CONFIG_FILE", other) + stderr := &bytes.Buffer{} + + cfg, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState("", bindingTestHost), + Stderr: stderr, + }) + require.NoError(t, err) + assert.Nil(t, entry) + assert.Equal(t, "https://adb-456.test", cfg.Host) + assert.Contains(t, stderr.String(), "No install record for system.ai.github on profile \"logfood\"") +} + +// TestBindWithoutHomeAndAbsoluteConfigFile checks that a bind needs no home directory when the +// config file is named by an absolute path. +func TestBindWithoutHomeAndAbsoluteConfigFile(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + t.Setenv(env.HomeEnvVar(), "") + + cfg, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(cfgPath, bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.NotNil(t, entry) + assert.Equal(t, bindingTestToken, cfg.Token) +} diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go index 02985b25513..db7ef54a815 100644 --- a/libs/aitools/mcp/state.go +++ b/libs/aitools/mcp/state.go @@ -4,17 +4,17 @@ import ( "context" "errors" "fmt" + "path/filepath" "strings" "github.com/databricks/cli/libs/aitools/installer" + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" ) -// configFileEnv names the variable that selects the Databricks config file. -const configFileEnv = "DATABRICKS_CONFIG_FILE" - // Drift kinds reported by Reconcile. const ( DriftProfileMissing DriftKind = "profile_missing" @@ -62,35 +62,28 @@ func LoadState(ctx context.Context) (*State, error) { return st.MCP, nil } -// SaveState replaces the MCP section of the aitools state file. The file is re-read first, -// so the skills and plugins records written since the caller's LoadState are kept. +// SaveState replaces the MCP section of the aitools state file. The skills and plugins records +// are kept as they are on disk, including those written since the caller's LoadState. func SaveState(ctx context.Context, state *State) error { dir, err := installer.GlobalSkillsDir(ctx) if err != nil { return err } - st, err := installer.LoadState(dir) - if err != nil { - return err - } - if st == nil { - st = &installer.InstallState{SchemaVersion: installer.CurrentSchemaVersion} - } - st.MCP = state - return installer.SaveState(dir, st) + return installer.SaveMCPState(dir, state) } // Reconcile reports entries whose profile is gone, or whose profile now names another host, // or another workspace when both the entry and the profile name one. Each entry is checked // against the config file it is pinned to; an entry with no pinned file is checked against -// the current one. +// the default file, ~/.databrickscfg, whatever the environment names. func Reconcile(ctx context.Context, profiler profile.Profiler, state *State) ([]Drift, error) { var drifts []Drift for _, entry := range state.Servers { - entryCtx := ctx - if entry.ConfigFile != "" { - entryCtx = env.Set(ctx, configFileEnv, entry.ConfigFile) + path, err := resolveConfigFile(ctx, entry.ConfigFile) + if err != nil { + return nil, err } + entryCtx := env.Set(ctx, mcpconfig.ConfigFileEnv, path) profiles, err := profiler.LoadProfiles(entryCtx, profile.WithName(entry.Profile)) if errors.Is(err, profile.ErrNoConfiguration) { profiles = nil @@ -107,7 +100,7 @@ func Reconcile(ctx context.Context, profiler profile.Profiler, state *State) ([] Kind: DriftHostChanged, Entry: entry, CurrentHost: p.Host, - CurrentWorkspaceID: namedWorkspaceID(p.WorkspaceID), + CurrentWorkspaceID: profileWorkspaceID(p), }) } } @@ -123,10 +116,20 @@ func hostChanged(entry Entry, p profile.Profile) bool { // two differ. A profile without a workspace has nothing to compare. func workspaceChanged(entry Entry, p profile.Profile) bool { recorded := namedWorkspaceID(entry.WorkspaceID) - current := namedWorkspaceID(p.WorkspaceID) + current := profileWorkspaceID(p) return recorded != "" && current != "" && recorded != current } +// profileWorkspaceID returns the workspace a profile names: its workspace_id when it has one, +// otherwise the workspace in its host's query, which the SDK resolves the same way. It returns +// "" when the profile names none. +func profileWorkspaceID(p profile.Profile) string { + if p.WorkspaceID != "" { + return namedWorkspaceID(p.WorkspaceID) + } + return namedWorkspaceID(auth.ExtractHostQueryParams(p.Host).WorkspaceID) +} + // namedWorkspaceID returns the workspace ID a value names, or "" when it names none. The // "none" sentinel that the CLI writes to skip workspace selection names none. func namedWorkspaceID(id string) string { @@ -142,3 +145,20 @@ func namedWorkspaceID(id string) string { func NormalizeHost(host string) string { return strings.ToLower((&config.Config{Host: host}).CanonicalHostName()) } + +// resolveConfigFile returns the absolute path of the config file that name refers to. An empty +// name is the default file, ~/.databrickscfg. A leading "~" is the home directory, as the +// profile loader reads it. Only an absolute name needs no home directory. +func resolveConfigFile(ctx context.Context, name string) (string, error) { + if name == "" { + name = "~/.databrickscfg" + } + if strings.HasPrefix(name, "~") { + home, err := env.UserHomeDir(ctx) + if err != nil { + return "", err + } + name = filepath.Join(home, name[1:]) + } + return filepath.Abs(name) +} diff --git a/libs/aitools/mcp/state_test.go b/libs/aitools/mcp/state_test.go index dffabe6f359..cb912eb5fb4 100644 --- a/libs/aitools/mcp/state_test.go +++ b/libs/aitools/mcp/state_test.go @@ -129,15 +129,19 @@ func TestSaveStateUpgradesV2FileToV3(t *testing.T) { assert.Contains(t, raw, "mcp") } -// stateTestProfiles is the profiles file used by the drift tests. +// stateTestProfiles is the profiles file used by the drift tests. The profiles named +// spog-query and spog-none name their workspace in the host query instead of workspace_id. func stateTestProfiles() profile.InMemoryProfiler { return profile.InMemoryProfiler{Profiles: profile.Profiles{ {Name: bindingTestProfile, Host: bindingTestHost, WorkspaceID: stateTestWorkspaceID}, {Name: stateTestOtherProfile, Host: stateTestOtherHost, WorkspaceID: "456"}, + {Name: "spog-query", Host: "https://spog.test/?o=222"}, + {Name: "spog-none", Host: "https://spog.test/?w=none"}, }} } func TestReconcile(t *testing.T) { + ctx, _ := stateTestCtx(t) tests := []struct { name string entry mcp.Entry @@ -157,6 +161,21 @@ func TestReconcile(t *testing.T) { name: "workspace not compared when the entry names none", entry: stateTestEntryWith(bindingTestProfile, bindingTestHost, "none"), }, + { + name: "workspace from host query matches the entry", + entry: stateTestEntryWith("spog-query", "https://spog.test", "222"), + }, + { + name: "workspace from host query differs from the entry", + entry: stateTestEntryWith("spog-query", "https://spog.test", stateTestWorkspaceID), + wantKind: mcp.DriftHostChanged, + wantHost: "https://spog.test/?o=222", + wantWorkspaceID: "222", + }, + { + name: "host query naming none is not compared", + entry: stateTestEntryWith("spog-none", "https://spog.test", stateTestWorkspaceID), + }, { name: "profile missing", entry: stateTestEntryWith("gone", bindingTestHost, stateTestWorkspaceID), @@ -181,7 +200,7 @@ func TestReconcile(t *testing.T) { t.Run(tt.name, func(t *testing.T) { state := &mcp.State{Servers: []mcp.Entry{tt.entry}} - drifts, err := mcp.Reconcile(t.Context(), stateTestProfiles(), state) + drifts, err := mcp.Reconcile(ctx, stateTestProfiles(), state) require.NoError(t, err) if tt.wantKind == "" { assert.Empty(t, drifts) @@ -198,7 +217,8 @@ func TestReconcile(t *testing.T) { } func TestReconcileWithoutConfigFileReportsEveryProfileMissing(t *testing.T) { - ctx := env.Set(t.Context(), "DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) + ctx, _ := stateTestCtx(t) + ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", filepath.Join(t.TempDir(), "absent")) entry := stateTestEntry() state := &mcp.State{Servers: []mcp.Entry{entry}} @@ -221,6 +241,27 @@ func TestReconcileChecksEntryAgainstPinnedConfigFile(t *testing.T) { assert.Empty(t, drifts) } +// TestReconcileUnpinnedEntryChecksDefaultConfigFile checks that an entry with no pinned config file +// is checked against ~/.databrickscfg, even when DATABRICKS_CONFIG_FILE names another file. +func TestReconcileUnpinnedEntryChecksDefaultConfigFile(t *testing.T) { + ctx, _ := stateTestCtx(t) + home, err := env.UserHomeDir(ctx) + require.NoError(t, err) + writeStateFile(t, filepath.Join(home, ".databrickscfg"), "[logfood]\nhost = https://adb-456.test\n") + ambient := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) + ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", ambient) + state := &mcp.State{Servers: []mcp.Entry{stateTestEntry()}} + + drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{ + Kind: mcp.DriftHostChanged, + Entry: stateTestEntry(), + CurrentHost: "https://adb-456.test", + CurrentWorkspaceID: "", + }}, drifts) +} + func TestNormalizeHostIgnoresSchemeCaseAndTrailingSlash(t *testing.T) { assert.Equal(t, mcp.NormalizeHost("https://ADB-123.test/"), mcp.NormalizeHost("adb-123.test")) assert.NotEqual(t, mcp.NormalizeHost("https://adb-123.test"), mcp.NormalizeHost("https://adb-456.test")) diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 9eb80bab97d..3e66624b74b 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -9,6 +9,10 @@ import ( "strings" ) +// ConfigFileEnv is the environment variable that selects the Databricks config file. An agent +// entry pins its value when this CLI sees it set, and the MCP binding reads the same variable. +const ConfigFileEnv = "DATABRICKS_CONFIG_FILE" + // Format is the on-disk format of an agent's MCP config file. type Format int From 0c699edce87067041fbad9d976350bfe9270dfc5 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 16:28:04 +0000 Subject: [PATCH 4/7] Address pass 3 review of MCP state and binding checks - Derive the profile's host and workspace with the SDK's own resolution, without network: workspace_id first, then the ?w= query, then a numeric ?o= or ?workspace_id=. "none" names no workspace. - Resolve the config file once. Symlinks are resolved before the path is made absolute, so ".." after a symlink matches the file the OS reads. Paths compare case-insensitively on Windows and macOS. The profile lookup, the entry match and the SDK all use that path. - An entry whose "~" path cannot be resolved is skipped while another entry matches. It fails the bind only when nothing else matches. Reconcile reports the error for that entry and still checks the others. - Share the per-entry drift check between Bind and Reconcile, and the profile lookup too. A section with no host is reported as having no host, not as missing. - SDK configuration errors are UNCATEGORIZED with no login hint. Only failures from Authenticate are AUTH_FAILED. - Export databrickscfg.ResolveConfigFilePath and reuse it. - SaveState removes the state file when no skills, plugins, files, repo directories or MCP servers remain. Uninstall uses this instead of its own re-read of the MCP section. - aitools list treats a state file with only MCP records as no install. - Fix the spacing in the profile-gone message. Document ConfigFileEnv and the helpers that lacked comments. Co-authored-by: Isaac --- cmd/aitools/list.go | 6 +- cmd/aitools/list_test.go | 26 +++ libs/aitools/installer/state.go | 24 +-- libs/aitools/installer/state_test.go | 34 ++-- libs/aitools/installer/uninstall.go | 20 +-- libs/aitools/installer/uninstall_test.go | 6 +- libs/aitools/mcp/binding.go | 90 ++++++---- libs/aitools/mcp/binding_test.go | 204 ++++++++++++++++++++++- libs/aitools/mcp/configfile.go | 44 +++++ libs/aitools/mcp/configfile_test.go | 91 ++++++++++ libs/aitools/mcp/state.go | 154 +++++++++-------- libs/aitools/mcp/state_test.go | 58 ++++++- libs/aitools/mcpconfig/spec.go | 1 + libs/databrickscfg/ops.go | 9 +- 14 files changed, 616 insertions(+), 151 deletions(-) create mode 100644 libs/aitools/mcp/configfile.go create mode 100644 libs/aitools/mcp/configfile_test.go diff --git a/cmd/aitools/list.go b/cmd/aitools/list.go index 86c273e6bfc..7128d5e9821 100644 --- a/cmd/aitools/list.go +++ b/cmd/aitools/list.go @@ -288,7 +288,8 @@ func agentHasSkillsInScope(ctx context.Context, a *agents.Agent, scope string) b // loadStateForScope returns the install state for the named scope when the // scope filter allows it. excludeScope is the scope value that means "skip // loading this one" (so passing ScopeProject to the global loader skips -// global when --scope=project). +// global when --scope=project). A state file that holds only MCP records has no +// skills or plugins, so it is not an install and yields nil. func loadStateForScope(ctx context.Context, scopeFilter, excludeScope string, dirFn func(context.Context) (string, error), label string) *installer.InstallState { if scopeFilter == excludeScope { return nil @@ -302,6 +303,9 @@ func loadStateForScope(ctx context.Context, scopeFilter, excludeScope string, di log.Debugf(ctx, "Could not load %s install state: %v", label, err) return nil } + if !state.HasSkillsOrPlugins() { + return nil + } return state } diff --git a/cmd/aitools/list_test.go b/cmd/aitools/list_test.go index 257300846c2..31234a62d49 100644 --- a/cmd/aitools/list_test.go +++ b/cmd/aitools/list_test.go @@ -624,3 +624,29 @@ func TestListScopeFlag(t *testing.T) { }) } } + +// TestLoadStateForScopeIgnoresMCPOnlyState checks that a state file holding only MCP records is not an +// install of skills or plugins, so the scope is not reported as loaded. +func TestLoadStateForScopeIgnoresMCPOnlyState(t *testing.T) { + ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) + dir, err := installer.GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, installer.SaveMCPState(dir, &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}})) + + state := loadStateForScope(ctx, "", installer.ScopeProject, installer.GlobalSkillsDir, "global") + assert.Nil(t, state) +} + +// TestLoadStateForScopeKeepsSkillsState checks that a state file with skills is loaded, with or without +// MCP records next to them. +func TestLoadStateForScopeKeepsSkillsState(t *testing.T) { + ctx := env.WithUserHomeDir(t.Context(), t.TempDir()) + dir, err := installer.GlobalSkillsDir(ctx) + require.NoError(t, err) + require.NoError(t, installer.SaveState(dir, &installer.InstallState{SchemaVersion: installer.CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) + require.NoError(t, installer.SaveMCPState(dir, &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}})) + + state := loadStateForScope(ctx, "", installer.ScopeProject, installer.GlobalSkillsDir, "global") + require.NotNil(t, state) + assert.Equal(t, "1.0.0", state.Skills["databricks"]) +} diff --git a/libs/aitools/installer/state.go b/libs/aitools/installer/state.go index fa49f476a3d..5f316fa8611 100644 --- a/libs/aitools/installer/state.go +++ b/libs/aitools/installer/state.go @@ -160,7 +160,7 @@ func migrateState(state *InstallState) { // SaveState writes the skills and plugins records of state to the given directory atomically, // creating the directory if it does not exist. The MCP section is written by SaveMCPState, so // SaveState keeps the one on disk, not the one in state. This keeps MCP records saved after state -// was loaded. +// was loaded. The file is removed when no skills, plugins or MCP servers remain. func SaveState(dir string, state *InstallState) error { onDisk, err := LoadState(dir) if err != nil { @@ -171,7 +171,7 @@ func SaveState(dir string, state *InstallState) error { if onDisk != nil { out.MCP = onDisk.MCP } - return writeState(dir, &out) + return saveOrRemove(dir, &out) } // SaveMCPState replaces the MCP section of the state file in the given directory. The file is read @@ -197,7 +197,14 @@ func SaveMCPState(dir string, mcp *MCPState) error { if mcp != nil && len(mcp.Servers) > 0 { st.MCP = mcp } - if !st.HasSkillsOrPlugins() && st.MCP == nil { + return saveOrRemove(dir, st) +} + +// saveOrRemove writes st to the state file in dir, or removes the file when st records nothing: no +// skills, plugins, files or repo directories, and no MCP servers. Files and repo directories count +// even without a skill record, because the legacy raw-skill cleanup reads them on its own. +func saveOrRemove(dir string, st *InstallState) error { + if len(st.Skills) == 0 && len(st.Plugins) == 0 && len(st.Files) == 0 && len(st.RepoDirs) == 0 && st.MCP == nil { err := os.Remove(filepath.Join(dir, stateFileName)) if err != nil && !errors.Is(err, os.ErrNotExist) { return fmt.Errorf("failed to remove state file: %w", err) @@ -207,15 +214,8 @@ func SaveMCPState(dir string, mcp *MCPState) error { return writeState(dir, st) } -// mcpRecordedOnDisk reports whether the state file in the given directory holds MCP servers. -func mcpRecordedOnDisk(dir string) (bool, error) { - st, err := LoadState(dir) - if err != nil { - return false, err - } - return st != nil && st.MCP != nil && len(st.MCP.Servers) > 0, nil -} - +// writeState replaces the state file in dir with state, atomically. It writes whatever it is given; +// saveOrRemove decides when there is nothing to keep. func writeState(dir string, state *InstallState) error { data, err := json.MarshalIndent(state, "", " ") if err != nil { diff --git a/libs/aitools/installer/state_test.go b/libs/aitools/installer/state_test.go index 65f3eb2162d..c66be4c7004 100644 --- a/libs/aitools/installer/state_test.go +++ b/libs/aitools/installer/state_test.go @@ -48,7 +48,7 @@ func TestSaveStateCreatesDirectory(t *testing.T) { SchemaVersion: 1, Release: "v0.1.0", LastUpdated: time.Date(2026, 3, 22, 9, 0, 0, 0, time.UTC), - Skills: map[string]string{}, + Skills: map[string]string{"databricks": "1.0.0"}, } err := SaveState(dir, state) @@ -65,7 +65,7 @@ func TestSaveStateTrailingNewline(t *testing.T) { SchemaVersion: 1, Release: "v0.1.0", LastUpdated: time.Date(2026, 3, 22, 9, 0, 0, 0, time.UTC), - Skills: map[string]string{}, + Skills: map[string]string{"databricks": "1.0.0"}, } err := SaveState(dir, state) @@ -313,22 +313,32 @@ func TestHasSkillsOrPlugins(t *testing.T) { } } -// TestMCPRecordedOnDisk checks the re-read that uninstall makes before it deletes the state file. -func TestMCPRecordedOnDisk(t *testing.T) { +// TestSaveStateRemovesFileWhenNothingRemains checks that SaveState removes the state file once +// the skills and plugins are gone and no MCP servers are recorded. +func TestSaveStateRemovesFileWhenNothingRemains(t *testing.T) { dir := t.TempDir() - recorded, err := mcpRecordedOnDisk(dir) - require.NoError(t, err) - assert.False(t, recorded, "no file") - require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) - recorded, err = mcpRecordedOnDisk(dir) + + require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion})) + loaded, err := LoadState(dir) require.NoError(t, err) - assert.False(t, recorded, "skills only") + assert.Nil(t, loaded) +} +// TestSaveStateKeepsMCPRecordsWhenNothingElseRemains checks that SaveState, once the skills are +// gone, keeps the file for the MCP servers recorded on disk and writes the skills-free state. +func TestSaveStateKeepsMCPRecordsWhenNothingElseRemains(t *testing.T) { + dir := t.TempDir() + require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) - recorded, err = mcpRecordedOnDisk(dir) + + require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion})) + loaded, err := LoadState(dir) require.NoError(t, err) - assert.True(t, recorded) + require.NotNil(t, loaded) + assert.Empty(t, loaded.Skills) + require.NotNil(t, loaded.MCP) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) } func TestLoadStateMigratesV2ToV3AndSavesV3(t *testing.T) { diff --git a/libs/aitools/installer/uninstall.go b/libs/aitools/installer/uninstall.go index a7feb8aae05..fa756aeb35d 100644 --- a/libs/aitools/installer/uninstall.go +++ b/libs/aitools/installer/uninstall.go @@ -197,28 +197,16 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni } } - // If nothing remains in this scope, clean up orphaned symlinks. Delete the - // state file unless it still holds MCP records, which uninstall does not own; - // otherwise persist the trimmed state. The MCP records are read from disk now, - // since an MCP save may have happened after state was loaded above. + // If nothing remains in this scope, clean up orphaned symlinks. SaveState then + // removes the state file, unless it still holds MCP records, which uninstall does + // not own; otherwise it persists the trimmed state. noSkillsOrPlugins := len(state.Skills) == 0 && len(state.Plugins) == 0 if noSkillsOrPlugins { cleanOrphanedSymlinks(ctx, baseDir, scope, cwd) } - mcpRecorded, err := mcpRecordedOnDisk(baseDir) - if err != nil { + if err := SaveState(baseDir, state); err != nil { return err } - if noSkillsOrPlugins && !mcpRecorded { - stateFile := filepath.Join(baseDir, stateFileName) - if err := os.Remove(stateFile); err != nil && !errors.Is(err, fs.ErrNotExist) { - return nil, fmt.Errorf("failed to remove state file: %w", err) - } - } else { - if err := SaveState(baseDir, state); err != nil { - return nil, err - } - } if pluginCount > 0 { noun := "agent" diff --git a/libs/aitools/installer/uninstall_test.go b/libs/aitools/installer/uninstall_test.go index 3d7cea0bc5f..eb94cf6ad79 100644 --- a/libs/aitools/installer/uninstall_test.go +++ b/libs/aitools/installer/uninstall_test.go @@ -641,7 +641,9 @@ func TestUninstallMCPOnlyStateReturnsError(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "no skills or plugins installed") - recorded, err := mcpRecordedOnDisk(globalDir) + loaded, err := LoadState(globalDir) require.NoError(t, err) - assert.True(t, recorded) + require.NotNil(t, loaded) + require.NotNil(t, loaded.MCP) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) } diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 089abf7fa4f..ee34a7018ae 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -70,45 +70,37 @@ func (e *BindError) ExitCode() int { // loads a bundle. It returns the resolved config and the matching state entry, which is nil // when none exists. // -// The profile is checked before any credential is resolved, so a re-pointed host is never -// contacted. The config file is the one in the context, the same file the profile lookup reads. +// The profile is checked before the SDK resolves the config, so a re-pointed host is never +// contacted. The config file is the one in the context, resolved once: the profile lookup, +// the entry match and the SDK all read that path. func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) { configFile, err := resolveConfigFile(ctx, env.Get(ctx, mcpconfig.ConfigFileEnv)) if err != nil { - return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + return nil, nil, uncategorizedError(err) } + ctx = env.Set(ctx, mcpconfig.ConfigFileEnv, configFile) entry, err := findEntry(ctx, opts.State, opts.Service, opts.Profile, configFile) if err != nil { - return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + return nil, nil, uncategorizedError(err) } name := opts.Service if entry != nil { name = entry.Name } - profiler := profile.GetProfiler(ctx) - profiles, err := profiler.LoadProfiles(ctx, profile.WithName(opts.Profile)) - if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { - return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + p, err := loadProfile(ctx, profile.GetProfiler(ctx), opts.Profile) + switch { + case errors.Is(err, errProfileMissing): + return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(configFile), entry, "no longer exists") + case errors.Is(err, errProfileNoHost): + return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(configFile), entry, "has no host") + case err != nil: + return nil, nil, uncategorizedError(err) } - if len(profiles) == 0 { - path, err := profiler.GetPath(ctx) - if err != nil { - return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + if entry != nil { + if drift := driftOf(*entry, p); drift != nil { + return nil, nil, driftError(name, opts.Profile, *entry, *drift) } - return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(path), entry) - } - p := profiles[0] - - if entry != nil && hostChanged(*entry, p) { - message := fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: %s --profile %s and add it again", - name, entry.Host, opts.Profile, p.Host, mcpRemoveCommand, opts.Profile) - return nil, nil, &BindError{Category: BindProfileHostChanged, message: message} - } - if entry != nil && workspaceChanged(*entry, p) { - message := fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: %s --profile %s and add it again", - name, entry.WorkspaceID, opts.Profile, profileWorkspaceID(p), mcpRemoveCommand, opts.Profile) - return nil, nil, &BindError{Category: BindProfileHostChanged, message: message} } // The loaders take the profile's own settings and non-auth environment only. The @@ -120,7 +112,8 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) Loaders: []config.Loader{databrickscfg.ResolveNonAuthFromEnv, config.ConfigFile}, } if err := cfg.EnsureResolved(); err != nil { - return nil, nil, authError(opts.Profile, err) + // Configuration that the SDK rejects is not a credential failure, so a login would not fix it. + return nil, nil, uncategorizedError(err) } if entry == nil { @@ -131,7 +124,7 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) // the command's context, as in cmd/root/auth.go. req, err := http.NewRequestWithContext(ctx, http.MethodGet, "", nil) if err != nil { - return nil, nil, &BindError{Category: BindUncategorized, message: err.Error(), cause: err} + return nil, nil, uncategorizedError(err) } if err := cfg.Authenticate(req); err != nil { return nil, nil, authError(opts.Profile, err) @@ -139,18 +132,41 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) return cfg, entry, nil } -// profileGoneError builds the error for a profile that is missing from the config file. -// Without a state entry the recorded host is unknown, so the recreate hint omits --host. -func profileGoneError(name, profileName, path string, entry *Entry) error { +// uncategorizedError wraps a failure that has no category of its own. Its message is the cause's. +func uncategorizedError(err error) error { + return &BindError{Category: BindUncategorized, message: err.Error(), cause: err} +} + +// 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 the recorded host is unknown, so the +// recreate hint omits --host. +func profileGoneError(name, profileName, path string, entry *Entry, problem string) error { recreate := authLoginCommand + " --profile " + profileName if entry != nil { recreate = authLoginCommand + " --host " + entry.Host + " --profile " + profileName } - message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which no longer exists in %s. Recreate it with: %s or remove the server with: %s --profile %s", - name, profileName, path, recreate, mcpRemoveCommand, profileName) + message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which %s in %s. Recreate it with: %s or remove the server with: %s --profile %s", + name, profileName, problem, path, recreate, mcpRemoveCommand, profileName) return &BindError{Category: BindProfileNotFound, message: message} } +// driftError is the PROFILE_HOST_CHANGED error for an entry whose profile now names another host, +// or another workspace on the same host. +func driftError(name, profileName string, entry Entry, drift Drift) error { + var message string + if NormalizeHost(entry.Host) != NormalizeHost(drift.CurrentHost) { + message = fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: %s --profile %s and add it again", + name, entry.Host, profileName, drift.CurrentHost, mcpRemoveCommand, profileName) + } else { + message = fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: %s --profile %s and add it again", + name, entry.WorkspaceID, profileName, drift.CurrentWorkspaceID, mcpRemoveCommand, profileName) + } + return &BindError{Category: BindProfileHostChanged, message: message} +} + +// authError is the AUTH_FAILED error for a credential that the profile's own settings could not +// provide. The message points at the login command for the profile. func authError(profileName string, cause error) error { message := fmt.Sprintf("%s\nRun: %s --profile %s", cause, authLoginCommand, profileName) return &BindError{Category: BindAuthFailed, message: message, cause: cause} @@ -158,7 +174,10 @@ func authError(profileName string, cause error) error { // findEntry returns the entry for the service and profile whose config file is configFile, or nil. // Both are compared as resolved paths, so an entry with no pinned file matches the default file. +// An entry whose file cannot be resolved, such as a "~" path with no home directory, cannot be +// compared. It is skipped, and its error is returned only when no other entry matches. func findEntry(ctx context.Context, state *State, service, profileName, configFile string) (*Entry, error) { + var unresolved error for i := range state.Servers { entry := &state.Servers[i] if entry.Service != service || entry.Profile != profileName { @@ -166,11 +185,12 @@ func findEntry(ctx context.Context, state *State, service, profileName, configFi } path, err := resolveConfigFile(ctx, entry.ConfigFile) if err != nil { - return nil, err + unresolved = errors.Join(unresolved, fmt.Errorf("MCP server %s: %w", entry.Name, err)) + continue } - if path == configFile { + if samePath(path, configFile) { return entry, nil } } - return nil, nil + return nil, unresolved } diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 12e6951efc6..7b5dc95d0d0 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -101,8 +101,17 @@ func TestBindPrintsNoticeWithoutStateEntry(t *testing.T) { assert.Equal(t, "No install record for system.ai.github on profile \"logfood\"; using its current host https://adb-123.test\n", stderr.String()) } +// bindingTestResolved returns path with its symlinks resolved, which is how Bind names an existing +// config file. Temp directories are symlinks on some systems, for example macOS. +func bindingTestResolved(t *testing.T, path string) string { + t.Helper() + resolved, err := filepath.EvalSymlinks(path) + require.NoError(t, err) + return resolved +} + func TestBindProfileAbsent(t *testing.T) { - path := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + path := bindingTestResolved(t, bindingTestConfig(t, bindingTestProfileContents(bindingTestToken))) tests := []struct { name string @@ -118,12 +127,12 @@ func TestBindProfileAbsent(t *testing.T) { Host: bindingTestHost, ConfigFile: path, }}}, - want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), + want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), }, { name: "without state entry", state: &mcp.State{}, - want: fmt.Sprintf(`MCP server system.ai.github is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), + want: fmt.Sprintf(`MCP server system.ai.github is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), }, } for _, tt := range tests { @@ -457,3 +466,192 @@ func TestBindWithoutHomeAndAbsoluteConfigFile(t *testing.T) { assert.NotNil(t, entry) assert.Equal(t, bindingTestToken, cfg.Token) } + +// TestBindWorkspaceFollowsSDKResolution checks that the workspace a profile names is the one the SDK +// resolves: workspace_id first, then the ?w= query, then a numeric ?o= or ?workspace_id=. A workspace +// named "none" is not compared. +func TestBindWorkspaceFollowsSDKResolution(t *testing.T) { + tests := []struct { + name string + workspaceID string // the profile's workspace_id, "" when it has none + query string // the host query, with its leading "?" + recorded string + // wantWorkspace is the workspace the drift names. It is "" when no drift is expected. + wantWorkspace string + }{ + {name: "w wins over o", query: "?o=111&w=222", recorded: "111", wantWorkspace: "222"}, + {name: "w wins over o when recorded", query: "?o=111&w=222", recorded: "222"}, + {name: "non-numeric o is ignored when w names", query: "?o=abc&w=222", recorded: "222"}, + {name: "non-numeric o names none", query: "?o=abc", recorded: "111"}, + {name: "workspace_id wins over the query", workspaceID: "333", query: "?o=222", recorded: "222", wantWorkspace: "333"}, + {name: "workspace_id wins when recorded", workspaceID: "333", query: "?o=222", recorded: "333"}, + {name: "none in workspace_id names none", workspaceID: "none", query: "?o=222", recorded: "111"}, + {name: "none in o names none", query: "?o=none", recorded: "111"}, + {name: "none in w names none", query: "?w=none", recorded: "111"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + contents := fmt.Sprintf("[%s]\nhost = %s/%s\n", bindingTestProfile, bindingTestHost, tt.query) + if tt.workspaceID != "" { + contents += "workspace_id = " + tt.workspaceID + "\n" + } + contents += "auth_type = pat\ntoken = " + bindingTestToken + "\n" + cfgPath := bindingTestConfig(t, contents) + state := bindingTestState(cfgPath, bindingTestHost) + state.Servers[0].WorkspaceID = tt.recorded + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + if tt.wantWorkspace == "" { + require.NoError(t, err) + assert.NotNil(t, entry) + return + } + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Contains(t, err.Error(), "now points at workspace "+tt.wantWorkspace+".") + }) + } +} + +// TestBindMatchesEntryWhenHomeIsSymlink checks that an entry pinned as "~/..." matches the config file +// named by its real path when HOME is a symlink to the directory that holds the file. +func TestBindMatchesEntryWhenHomeIsSymlink(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + link := filepath.Join(t.TempDir(), "home") + if err := os.Symlink(filepath.Dir(cfgPath), link); err != nil { + t.Skipf("cannot create symlink: %v", err) + } + t.Setenv(env.HomeEnvVar(), link) + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState("~/.databrickscfg", bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.NotNil(t, entry) +} + +// TestBindMatchesEntryWhenDotDotFollowsSymlink checks that a config path with ".." after a symlink names +// the file the OS reads through the link's target, which is also the entry's resolved path. +func TestBindMatchesEntryWhenDotDotFollowsSymlink(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + base := t.TempDir() + cfgPath := filepath.Join(base, "a", ".databrickscfg") + require.NoError(t, os.MkdirAll(filepath.Join(base, "a", "b"), 0o755)) + require.NoError(t, os.WriteFile(cfgPath, []byte(bindingTestProfileContents(bindingTestToken)), 0o600)) + link := filepath.Join(base, "link") + if err := os.Symlink(filepath.Join(base, "a", "b"), link); err != nil { + t.Skipf("cannot create symlink: %v", err) + } + // filepath.Join would clean the ".." away, so the path is joined by hand. + sep := string(filepath.Separator) + t.Setenv("DATABRICKS_CONFIG_FILE", link+sep+".."+sep+".databrickscfg") + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(bindingTestResolved(t, cfgPath), bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.NotNil(t, entry) +} + +// TestBindSkipsEntryWithoutHomeWhenAbsoluteEntryMatches checks that an entry whose "~" path cannot be +// resolved, because HOME is unset, does not stop the match against an entry with an absolute path. +func TestBindSkipsEntryWithoutHomeWhenAbsoluteEntryMatches(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + t.Setenv(env.HomeEnvVar(), "") + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "github-home", Service: bindingTestService, Profile: bindingTestProfile, Host: "https://adb-999.test", ConfigFile: "~/.databrickscfg"}, + {Name: "github-absolute", Service: bindingTestService, Profile: bindingTestProfile, Host: bindingTestHost, ConfigFile: cfgPath}, + }} + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + require.NotNil(t, entry) + assert.Equal(t, "github-absolute", entry.Name) +} + +// TestBindFailsWhenUnresolvableEntryMightMatch checks that an entry whose "~" path cannot be resolved +// fails the bind when no other entry matches, since the bind cannot rule that entry out. +func TestBindFailsWhenUnresolvableEntryMightMatch(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + t.Setenv(env.HomeEnvVar(), "") + state := &mcp.State{Servers: []mcp.Entry{ + {Name: "github-home", Service: bindingTestService, Profile: bindingTestProfile, Host: bindingTestHost, ConfigFile: "~/.databrickscfg"}, + }} + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindUncategorized) + assert.Contains(t, err.Error(), "MCP server github-home: cannot find homedir: $HOME is not set") +} + +// TestBindProfileWithoutHost checks that a profile section with no host is reported as having no host, +// not as missing from the file. +func TestBindProfileWithoutHost(t *testing.T) { + path := bindingTestConfig(t, fmt.Sprintf("[%s]\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestToken)) + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(path, bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileNotFound) + assert.Equal(t, fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "logfood", which has no host in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile logfood or remove the server with: databricks aitools mcp remove --profile logfood`, filepath.ToSlash(bindingTestResolved(t, path))), err.Error()) +} + +// TestBindHostMetadataFailureIsNotAuthFailure checks that a host whose metadata cannot be fetched does +// not fail the bind. The SDK logs that failure and goes on, and a profile with a token still binds. +func TestBindHostMetadataFailureIsNotAuthFailure(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.NotFound(w, r) + })) + t.Cleanup(server.Close) + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, server.URL, bindingTestToken)) + + cfg, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(cfgPath, server.URL), + Stderr: &bytes.Buffer{}, + }) + require.NoError(t, err) + assert.NotNil(t, entry) + assert.Equal(t, bindingTestToken, cfg.Token) +} + +// TestBindConfigErrorIsUncategorized checks that a configuration error from the SDK, here an +// environment value that does not parse, is reported as uncategorized, not as an authentication +// failure, so the message does not suggest logging in. +func TestBindConfigErrorIsUncategorized(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + t.Setenv("DATABRICKS_RATE_LIMIT", "soon") + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(cfgPath, bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindUncategorized) + assert.NotContains(t, err.Error(), "Run:") + assert.NotContains(t, err.Error(), "auth login") +} diff --git a/libs/aitools/mcp/configfile.go b/libs/aitools/mcp/configfile.go new file mode 100644 index 00000000000..586e81f1cd7 --- /dev/null +++ b/libs/aitools/mcp/configfile.go @@ -0,0 +1,44 @@ +package mcp + +import ( + "context" + "errors" + "io/fs" + "path/filepath" + "runtime" + "strings" + + "github.com/databricks/cli/libs/databrickscfg" +) + +// foldCase reports whether config file paths compare case-insensitively. Windows and macOS +// file systems are case-insensitive by default. Tests set it to cover both comparisons. +var foldCase = runtime.GOOS == "windows" || runtime.GOOS == "darwin" + +// resolveConfigFile returns the absolute path of the config file that name refers to. An empty +// name is the default file, and a leading "~" is the home directory. Symlinks are resolved while +// the path is still as the user wrote it: the OS reads "link/../x" through the link's target, so +// the same file has one path however it is named. A path that does not exist is made absolute as +// written, since there is no file to resolve. +func resolveConfigFile(ctx context.Context, name string) (string, error) { + path, err := databrickscfg.ResolveConfigFilePath(ctx, name) + if err != nil { + return "", err + } + resolved, err := filepath.EvalSymlinks(path) + switch { + case err == nil: + path = resolved + case !errors.Is(err, fs.ErrNotExist): + return "", err + } + return filepath.Abs(path) +} + +// samePath reports whether two resolved config file paths name the same file. +func samePath(a, b string) bool { + if foldCase { + return strings.EqualFold(a, b) + } + return a == b +} diff --git a/libs/aitools/mcp/configfile_test.go b/libs/aitools/mcp/configfile_test.go new file mode 100644 index 00000000000..981c944c41a --- /dev/null +++ b/libs/aitools/mcp/configfile_test.go @@ -0,0 +1,91 @@ +package mcp + +import ( + "os" + "path/filepath" + "testing" + + "github.com/databricks/cli/libs/env" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// setFoldCase sets the case-folding seam for the test and restores it afterwards. The seam lets both +// comparisons run on any OS, where the platform default would pick only one. +func setFoldCase(t *testing.T, fold bool) { + t.Helper() + prev := foldCase + t.Cleanup(func() { foldCase = prev }) + foldCase = fold +} + +// TestSamePathFollowsFoldCase checks that paths compare case-insensitively only when the seam says so. +func TestSamePathFollowsFoldCase(t *testing.T) { + const lower = "/home/u/.databrickscfg" + const upper = "/HOME/U/.DATABRICKSCFG" + + setFoldCase(t, true) + assert.True(t, samePath(lower, upper)) + assert.True(t, samePath(lower, lower)) + + setFoldCase(t, false) + assert.False(t, samePath(lower, upper)) + assert.True(t, samePath(lower, lower)) +} + +// TestFindEntryMatchesCaseFoldedPath checks that an entry whose file name differs only in case matches +// the config file when paths fold case, as they do on Windows and macOS. +func TestFindEntryMatchesCaseFoldedPath(t *testing.T) { + setFoldCase(t, true) + // Resolve the directory first, so the file the entry names and the file in the context match on + // systems where the temp directory is a symlink. + dir, err := filepath.EvalSymlinks(t.TempDir()) + require.NoError(t, err) + configFile := filepath.Join(dir, ".databrickscfg") + state := &State{Servers: []Entry{{ + Name: "github-logfood", + Service: "system.ai.github", + Profile: "logfood", + ConfigFile: filepath.Join(dir, ".DATABRICKSCFG"), + }}} + + entry, err := findEntry(t.Context(), state, "system.ai.github", "logfood", configFile) + require.NoError(t, err) + require.NotNil(t, entry) + assert.Equal(t, "github-logfood", entry.Name) +} + +// TestResolveConfigFileResolvesSymlinkBeforeDotDot checks that ".." after a symlink is taken from the +// link's target, as the OS reads the path, not from the link's own directory. +func TestResolveConfigFileResolvesSymlinkBeforeDotDot(t *testing.T) { + base, err := filepath.EvalSymlinks(t.TempDir()) + require.NoError(t, err) + target := filepath.Join(base, "a", "b") + require.NoError(t, os.MkdirAll(target, 0o755)) + want := filepath.Join(base, "a", ".databrickscfg") + require.NoError(t, os.WriteFile(want, []byte("[logfood]\n"), 0o600)) + link := filepath.Join(base, "link") + if err := os.Symlink(target, link); err != nil { + t.Skipf("cannot create symlink: %v", err) + } + + // filepath.Join would clean the ".." away, so the path is joined by hand. + sep := string(filepath.Separator) + got, err := resolveConfigFile(t.Context(), link+sep+".."+sep+".databrickscfg") + require.NoError(t, err) + assert.Equal(t, want, got) +} + +// TestResolveConfigFileWithoutHomeAndAbsoluteName checks that an absolute name resolves with no home +// directory, and that a "~" name reports the missing home directory. +func TestResolveConfigFileWithoutHomeAndAbsoluteName(t *testing.T) { + ctx := env.WithUserHomeDir(t.Context(), "") + name := filepath.Join(t.TempDir(), ".databrickscfg") + + got, err := resolveConfigFile(ctx, name) + require.NoError(t, err) + assert.True(t, filepath.IsAbs(got)) + + _, err = resolveConfigFile(ctx, "~/.databrickscfg") + require.ErrorIs(t, err, env.ErrNoHomeEnv) +} diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go index db7ef54a815..1f8271877b2 100644 --- a/libs/aitools/mcp/state.go +++ b/libs/aitools/mcp/state.go @@ -4,12 +4,10 @@ import ( "context" "errors" "fmt" - "path/filepath" "strings" "github.com/databricks/cli/libs/aitools/installer" "github.com/databricks/cli/libs/aitools/mcpconfig" - "github.com/databricks/cli/libs/auth" "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" @@ -21,6 +19,15 @@ const ( DriftHostChanged DriftKind = "host_changed" ) +// Profile lookups that do not yield a usable profile. +var ( + // errProfileMissing means the config file has no profile of the name. + errProfileMissing = errors.New("profile missing") + // errProfileNoHost means the config file has a section of the name without a host. + // LoadProfiles skips such sections, so the lookup reports them separately. + errProfileNoHost = errors.New("profile has no host") +) + // State is the MCP section of the aitools install state. It lives in the same file as // the skills state, so writers that save the whole file keep it. type State = installer.MCPState @@ -38,10 +45,10 @@ type DriftKind string type Drift struct { Kind DriftKind Entry Entry - // CurrentHost is the profile's host when Kind is DriftHostChanged. + // CurrentHost is the profile's host as the config file writes it, when Kind is DriftHostChanged. CurrentHost string - // CurrentWorkspaceID is the profile's workspace_id when Kind is DriftHostChanged, - // or "" when the profile names no workspace. + // CurrentWorkspaceID is the workspace the profile names when Kind is DriftHostChanged: its + // workspace_id, else the workspace in its host query. It is "" when the profile names none. CurrentWorkspaceID string } @@ -72,66 +79,100 @@ func SaveState(ctx context.Context, state *State) error { return installer.SaveMCPState(dir, state) } -// Reconcile reports entries whose profile is gone, or whose profile now names another host, -// or another workspace when both the entry and the profile name one. Each entry is checked -// against the config file it is pinned to; an entry with no pinned file is checked against -// the default file, ~/.databrickscfg, whatever the environment names. +// Reconcile reports entries whose profile is gone or has no host, and entries whose profile now +// names another host, or another workspace when both the entry and the profile name one. Each +// entry is checked against the config file it is pinned to; an entry with no pinned file is checked +// against the default file, ~/.databrickscfg, whatever the environment names. +// +// An entry that cannot be checked, such as one whose "~" path has no home directory to resolve, does +// not stop the others. Its error is joined into the returned error, and the drifts of the other +// entries are still returned. func Reconcile(ctx context.Context, profiler profile.Profiler, state *State) ([]Drift, error) { var drifts []Drift + var errs []error for _, entry := range state.Servers { - path, err := resolveConfigFile(ctx, entry.ConfigFile) + drift, err := checkEntry(ctx, profiler, entry) if err != nil { - return nil, err - } - entryCtx := env.Set(ctx, mcpconfig.ConfigFileEnv, path) - profiles, err := profiler.LoadProfiles(entryCtx, profile.WithName(entry.Profile)) - if errors.Is(err, profile.ErrNoConfiguration) { - profiles = nil - } else if err != nil { - return nil, fmt.Errorf("failed to read Databricks profiles: %w", err) - } - if len(profiles) == 0 { - drifts = append(drifts, Drift{Kind: DriftProfileMissing, Entry: entry}) + errs = append(errs, fmt.Errorf("MCP server %s: %w", entry.Name, err)) continue } - p := profiles[0] - if hostChanged(entry, p) || workspaceChanged(entry, p) { - drifts = append(drifts, Drift{ - Kind: DriftHostChanged, - Entry: entry, - CurrentHost: p.Host, - CurrentWorkspaceID: profileWorkspaceID(p), - }) + if drift != nil { + drifts = append(drifts, *drift) } } - return drifts, nil + return drifts, errors.Join(errs...) } -// hostChanged reports whether the profile names another host than the one recorded. -func hostChanged(entry Entry, p profile.Profile) bool { - return NormalizeHost(entry.Host) != NormalizeHost(p.Host) +// checkEntry returns the drift of one entry, or nil when its profile still names the recorded host +// and workspace. The entry's profile is read from the config file the entry is pinned to. +func checkEntry(ctx context.Context, profiler profile.Profiler, entry Entry) (*Drift, error) { + path, err := resolveConfigFile(ctx, entry.ConfigFile) + if err != nil { + return nil, err + } + p, err := loadProfile(env.Set(ctx, mcpconfig.ConfigFileEnv, path), profiler, entry.Profile) + if errors.Is(err, errProfileMissing) || errors.Is(err, errProfileNoHost) { + return &Drift{Kind: DriftProfileMissing, Entry: entry}, nil + } + if err != nil { + return nil, err + } + return driftOf(entry, p), nil } -// workspaceChanged reports whether the entry and the profile each name a workspace and the -// two differ. A profile without a workspace has nothing to compare. -func workspaceChanged(entry Entry, p profile.Profile) bool { - recorded := namedWorkspaceID(entry.WorkspaceID) - current := profileWorkspaceID(p) - return recorded != "" && current != "" && recorded != current +// loadProfile returns the profile named name from the config file in ctx. It returns errProfileMissing +// when the file has no such profile, and errProfileNoHost when it has a section of that name without a +// host. +func loadProfile(ctx context.Context, profiler profile.Profiler, name string) (profile.Profile, error) { + profiles, err := profiler.LoadProfiles(ctx, profile.WithName(name)) + if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { + return profile.Profile{}, fmt.Errorf("failed to read Databricks profiles: %w", err) + } + if len(profiles) > 0 { + return profiles[0], nil + } + file, err := profile.FileProfilerImpl{}.Get(ctx) + if errors.Is(err, profile.ErrNoConfiguration) { + return profile.Profile{}, errProfileMissing + } + if err != nil { + return profile.Profile{}, fmt.Errorf("failed to read Databricks profiles: %w", err) + } + if file.HasSection(name) { + return profile.Profile{}, errProfileNoHost + } + return profile.Profile{}, errProfileMissing } -// profileWorkspaceID returns the workspace a profile names: its workspace_id when it has one, -// otherwise the workspace in its host's query, which the SDK resolves the same way. It returns -// "" when the profile names none. -func profileWorkspaceID(p profile.Profile) string { - if p.WorkspaceID != "" { - return namedWorkspaceID(p.WorkspaceID) +// driftOf returns the drift of an entry against the profile it names, or nil when the profile still +// names the recorded host and workspace. +func driftOf(entry Entry, p profile.Profile) *Drift { + host, workspaceID := profileTarget(p) + if NormalizeHost(entry.Host) == NormalizeHost(host) && !workspaceChanged(entry.WorkspaceID, workspaceID) { + return nil } - return namedWorkspaceID(auth.ExtractHostQueryParams(p.Host).WorkspaceID) + return &Drift{Kind: DriftHostChanged, Entry: entry, CurrentHost: p.Host, CurrentWorkspaceID: workspaceID} +} + +// profileTarget returns the host and workspace ID that the SDK resolves for a profile. It runs the +// SDK's host handling only, so it makes no network request: the SDK keeps a workspace_id the profile +// sets, and otherwise takes the workspace from the host query. The host has its query removed. The +// workspace is "" when the profile names none, or names "none". +func profileTarget(p profile.Profile) (host, workspaceID string) { + c := &config.Config{Host: p.Host, WorkspaceID: p.WorkspaceID} + host = c.CanonicalHostName() + return host, namedWorkspaceID(c.WorkspaceID) +} + +// workspaceChanged reports whether the recorded and current workspace IDs both name a workspace and +// the two differ. A workspace that is not named is not compared. +func workspaceChanged(recorded, current string) bool { + recorded = namedWorkspaceID(recorded) + return recorded != "" && current != "" && recorded != current } -// namedWorkspaceID returns the workspace ID a value names, or "" when it names none. The -// "none" sentinel that the CLI writes to skip workspace selection names none. +// namedWorkspaceID returns the workspace ID a value names, or "" when it names none. The "none" +// sentinel that the CLI writes to skip workspace selection names none. func namedWorkspaceID(id string) string { if id == profile.WorkspaceIDNone { return "" @@ -145,20 +186,3 @@ func namedWorkspaceID(id string) string { func NormalizeHost(host string) string { return strings.ToLower((&config.Config{Host: host}).CanonicalHostName()) } - -// resolveConfigFile returns the absolute path of the config file that name refers to. An empty -// name is the default file, ~/.databrickscfg. A leading "~" is the home directory, as the -// profile loader reads it. Only an absolute name needs no home directory. -func resolveConfigFile(ctx context.Context, name string) (string, error) { - if name == "" { - name = "~/.databrickscfg" - } - if strings.HasPrefix(name, "~") { - home, err := env.UserHomeDir(ctx) - if err != nil { - return "", err - } - name = filepath.Join(home, name[1:]) - } - return filepath.Abs(name) -} diff --git a/libs/aitools/mcp/state_test.go b/libs/aitools/mcp/state_test.go index cb912eb5fb4..9cc77d4ddd1 100644 --- a/libs/aitools/mcp/state_test.go +++ b/libs/aitools/mcp/state_test.go @@ -130,13 +130,16 @@ func TestSaveStateUpgradesV2FileToV3(t *testing.T) { } // stateTestProfiles is the profiles file used by the drift tests. The profiles named -// spog-query and spog-none name their workspace in the host query instead of workspace_id. +// spog-query, spog-none, spog-both and spog-abc name their workspace in the host query instead of +// workspace_id. func stateTestProfiles() profile.InMemoryProfiler { return profile.InMemoryProfiler{Profiles: profile.Profiles{ {Name: bindingTestProfile, Host: bindingTestHost, WorkspaceID: stateTestWorkspaceID}, {Name: stateTestOtherProfile, Host: stateTestOtherHost, WorkspaceID: "456"}, {Name: "spog-query", Host: "https://spog.test/?o=222"}, {Name: "spog-none", Host: "https://spog.test/?w=none"}, + {Name: "spog-both", Host: "https://spog.test/?o=111&w=222"}, + {Name: "spog-abc", Host: "https://spog.test/?o=abc&w=222"}, }} } @@ -176,6 +179,21 @@ func TestReconcile(t *testing.T) { name: "host query naming none is not compared", entry: stateTestEntryWith("spog-none", "https://spog.test", stateTestWorkspaceID), }, + { + name: "w in the host query wins over o", + entry: stateTestEntryWith("spog-both", "https://spog.test", "222"), + }, + { + name: "w in the host query differs from the o the entry recorded", + entry: stateTestEntryWith("spog-both", "https://spog.test", "111"), + wantKind: mcp.DriftHostChanged, + wantHost: "https://spog.test/?o=111&w=222", + wantWorkspaceID: "222", + }, + { + name: "non-numeric o is ignored when w names the workspace", + entry: stateTestEntryWith("spog-abc", "https://spog.test", "222"), + }, { name: "profile missing", entry: stateTestEntryWith("gone", bindingTestHost, stateTestWorkspaceID), @@ -266,3 +284,41 @@ func TestNormalizeHostIgnoresSchemeCaseAndTrailingSlash(t *testing.T) { assert.Equal(t, mcp.NormalizeHost("https://ADB-123.test/"), mcp.NormalizeHost("adb-123.test")) assert.NotEqual(t, mcp.NormalizeHost("https://adb-123.test"), mcp.NormalizeHost("https://adb-456.test")) } + +// TestReconcileProfileWithoutHostIsMissing checks that a profile section with no host is reported as +// missing, since it names no host to compare. It reads a real file, because the in-memory profiler +// does not model sections without a host. +func TestReconcileProfileWithoutHostIsMissing(t *testing.T) { + ctx, _ := stateTestCtx(t) + path := bindingTestWriteConfig(t, "[logfood]\nauth_type = pat\ntoken = dapi-profile-token\n") + ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", path) + entry := stateTestEntry() + state := &mcp.State{Servers: []mcp.Entry{entry}} + + drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) +} + +// TestReconcileReportsEntryErrorAndKeepsOthers checks that an entry that cannot be checked, here a "~" +// path with no home directory, is reported as an error without stopping the check of the other entries. +func TestReconcileReportsEntryErrorAndKeepsOthers(t *testing.T) { + ctx, _ := stateTestCtx(t) + ctx = env.WithUserHomeDir(ctx, "") + unresolvable := stateTestEntry() + unresolvable.Name = "github-home" + unresolvable.ConfigFile = "~/.databrickscfg" + changed := stateTestEntryWith(bindingTestProfile, "https://adb-999.test", stateTestWorkspaceID) + changed.ConfigFile = filepath.Join(t.TempDir(), ".databrickscfg") + state := &mcp.State{Servers: []mcp.Entry{unresolvable, changed}} + + drifts, err := mcp.Reconcile(ctx, stateTestProfiles(), state) + require.Error(t, err) + assert.Contains(t, err.Error(), "MCP server github-home: cannot find homedir: $HOME is not set") + assert.Equal(t, []mcp.Drift{{ + Kind: mcp.DriftHostChanged, + Entry: changed, + CurrentHost: bindingTestHost, + CurrentWorkspaceID: stateTestWorkspaceID, + }}, drifts) +} diff --git a/libs/aitools/mcpconfig/spec.go b/libs/aitools/mcpconfig/spec.go index 3e66624b74b..07c9a910f33 100644 --- a/libs/aitools/mcpconfig/spec.go +++ b/libs/aitools/mcpconfig/spec.go @@ -11,6 +11,7 @@ import ( // ConfigFileEnv is the environment variable that selects the Databricks config file. An agent // entry pins its value when this CLI sees it set, and the MCP binding reads the same variable. +// A write that does not pin it removes it; other variables are the user's. const ConfigFileEnv = "DATABRICKS_CONFIG_FILE" // Format is the on-disk format of an agent's MCP config file. diff --git a/libs/databrickscfg/ops.go b/libs/databrickscfg/ops.go index 2d6faf1ba9c..95a224babd9 100644 --- a/libs/databrickscfg/ops.go +++ b/libs/databrickscfg/ops.go @@ -147,7 +147,7 @@ func GetSectionNames(ctx context.Context, configFilePath string) ([]string, erro // loadConfigFile loads a config file without creating it if it doesn't exist. // Returns (nil, nil) when the file is not found. func loadConfigFile(ctx context.Context, filename string) (*config.File, error) { - filename, err := resolveConfigFilePath(ctx, filename) + filename, err := ResolveConfigFilePath(ctx, filename) if err != nil { return nil, err } @@ -161,8 +161,9 @@ func loadConfigFile(ctx context.Context, filename string) (*config.File, error) return configFile, nil } -// resolveConfigFilePath defaults to ~/.databrickscfg and expands ~ to the home directory. -func resolveConfigFilePath(ctx context.Context, filename string) (string, error) { +// ResolveConfigFilePath defaults to ~/.databrickscfg and expands ~ to the home directory. +// It does not make the path absolute or resolve symlinks. +func ResolveConfigFilePath(ctx context.Context, filename string) (string, error) { if filename == "" { filename = "~/.databrickscfg" } @@ -346,7 +347,7 @@ func ClearDefaultProfile(ctx context.Context, profileName, configFilePath string } func loadOrCreateConfigFile(ctx context.Context, filename string) (*config.File, error) { - filename, err := resolveConfigFilePath(ctx, filename) + filename, err := ResolveConfigFilePath(ctx, filename) if err != nil { return nil, err } From 9f4af9666103ebf5abac0aa3868b0babd7786ad6 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 16:57:33 +0000 Subject: [PATCH 5/7] Address pass 4 review of MCP state and binding checks - Bind fails closed when an entry for the service and profile is pinned to another config file, instead of binding through the file in use. - A profile with an empty host is reported as having no host. - The recorded workspace is read from the recorded host's query, as the SDK resolves the profile's. - Each config file is read once per check; Reconcile no longer takes a Profiler. - A path through a missing directory is kept as written, and existing files are compared by identity. - The recreate, remove and login hints quote their values, and pinned entries name their config file. - A state file that holds only MCP records loads as no install, so installer and cmd behavior for users without MCP records is main's again. SaveState no longer removes the file; SaveMCPState and uninstall do. Co-authored-by: Isaac --- cmd/aitools/list.go | 6 +- cmd/aitools/scope.go | 7 +- cmd/aitools/uninstall.go | 2 +- cmd/aitools/uninstall_test.go | 7 +- cmd/aitools/update.go | 2 +- cmd/aitools/version.go | 6 +- libs/aitools/installer/installer.go | 4 +- libs/aitools/installer/installer_test.go | 17 +++ libs/aitools/installer/state.go | 105 ++++++++----- libs/aitools/installer/state_test.go | 67 ++++---- libs/aitools/installer/uninstall.go | 20 +-- libs/aitools/installer/uninstall_test.go | 32 +++- libs/aitools/installer/update.go | 2 +- libs/aitools/installer/update_test.go | 26 ++++ libs/aitools/mcp/binding.go | 80 +++++++--- libs/aitools/mcp/binding_test.go | 187 +++++++++++++++++++---- libs/aitools/mcp/configfile.go | 41 +++-- libs/aitools/mcp/configfile_test.go | 74 ++++----- libs/aitools/mcp/state.go | 75 +++++---- libs/aitools/mcp/state_test.go | 68 ++++++--- 20 files changed, 571 insertions(+), 257 deletions(-) diff --git a/cmd/aitools/list.go b/cmd/aitools/list.go index 7128d5e9821..86c273e6bfc 100644 --- a/cmd/aitools/list.go +++ b/cmd/aitools/list.go @@ -288,8 +288,7 @@ func agentHasSkillsInScope(ctx context.Context, a *agents.Agent, scope string) b // loadStateForScope returns the install state for the named scope when the // scope filter allows it. excludeScope is the scope value that means "skip // loading this one" (so passing ScopeProject to the global loader skips -// global when --scope=project). A state file that holds only MCP records has no -// skills or plugins, so it is not an install and yields nil. +// global when --scope=project). func loadStateForScope(ctx context.Context, scopeFilter, excludeScope string, dirFn func(context.Context) (string, error), label string) *installer.InstallState { if scopeFilter == excludeScope { return nil @@ -303,9 +302,6 @@ func loadStateForScope(ctx context.Context, scopeFilter, excludeScope string, di log.Debugf(ctx, "Could not load %s install state: %v", label, err) return nil } - if !state.HasSkillsOrPlugins() { - return nil - } return state } diff --git a/cmd/aitools/scope.go b/cmd/aitools/scope.go index 9e13642a308..20f5545f542 100644 --- a/cmd/aitools/scope.go +++ b/cmd/aitools/scope.go @@ -150,8 +150,7 @@ func parseScopeFlag(scopeFlag string, projectFlag, globalFlag, allowBoth bool) ( } } -// detectInstalledScopes checks which scopes have skills or plugins recorded in their state file. -// MCP records in the file do not count. +// detectInstalledScopes checks which scopes have a .state.json file present. func detectInstalledScopes(globalDir, projectDir string) (global, project bool, err error) { globalState, err := installer.LoadState(globalDir) if err != nil { @@ -163,7 +162,7 @@ func detectInstalledScopes(globalDir, projectDir string) (global, project bool, return false, false, err } - return globalState.HasSkillsOrPlugins(), projectState.HasSkillsOrPlugins(), nil + return globalState != nil, projectState != nil, nil } // explicitScope returns the single scope the flags parsed by parseScopeFlag @@ -294,7 +293,7 @@ func withExplicitScopeCheck(dir, scope, verb, projectDir string, hasGlobal, hasP if err != nil { return nil, err } - if !state.HasSkillsOrPlugins() { + if state == nil { return nil, scopeNotInstalledError(scope, verb, projectDir, hasGlobal, hasProject) } diff --git a/cmd/aitools/uninstall.go b/cmd/aitools/uninstall.go index d6462c30e6b..787156ff519 100644 --- a/cmd/aitools/uninstall.go +++ b/cmd/aitools/uninstall.go @@ -158,7 +158,7 @@ func confirmUninstall(ctx context.Context, state *installer.InstallState, opts i // uninstallConfirmMessage builds the human summary of what an uninstall will // remove. ask is false when there is nothing recorded to confirm. func uninstallConfirmMessage(state *installer.InstallState, opts installer.UninstallOptions) (msg string, ask bool) { - if !state.HasSkillsOrPlugins() { + if state == nil { return "", false } target := "all agents" diff --git a/cmd/aitools/uninstall_test.go b/cmd/aitools/uninstall_test.go index 70760d24402..ba9b30ae487 100644 --- a/cmd/aitools/uninstall_test.go +++ b/cmd/aitools/uninstall_test.go @@ -135,9 +135,12 @@ func TestUninstallConfirmMessage(t *testing.T) { } // TestUninstallConfirmMessageMCPOnlyState checks that an explicit skill list does not prompt when -// the state holds only MCP records. +// the state file holds only MCP records, as loaded for uninstall. func TestUninstallConfirmMessageMCPOnlyState(t *testing.T) { - st := &installer.InstallState{MCP: &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}}} + dir := t.TempDir() + require.NoError(t, installer.SaveMCPState(dir, &installer.MCPState{Servers: []installer.MCPEntry{{Name: "github-logfood"}}})) + st, err := installer.LoadState(dir) + require.NoError(t, err) _, ask := uninstallConfirmMessage(st, installer.UninstallOptions{Scope: installer.ScopeGlobal}) assert.False(t, ask) diff --git a/cmd/aitools/update.go b/cmd/aitools/update.go index 6bf8ad67d0c..bdddafbc56c 100644 --- a/cmd/aitools/update.go +++ b/cmd/aitools/update.go @@ -177,7 +177,7 @@ The command exits with code 1 when every update or check it attempts fails.`, // Reconcile file skills for non-plugin agents. Skip entirely for a // pure-plugin install (no file skills to maintain); keep calling for // no-state installs so the "no skills or plugins installed" guidance still fires. - if !state.HasSkillsOrPlugins() || len(state.Skills) > 0 { + if state == nil || len(state.Skills) > 0 { opts := installer.UpdateOptions{ Check: check, Force: force, diff --git a/cmd/aitools/version.go b/cmd/aitools/version.go index b55308f2c9f..e1d268487bf 100644 --- a/cmd/aitools/version.go +++ b/cmd/aitools/version.go @@ -42,7 +42,7 @@ func NewVersionCmd() *cobra.Command { } } - if !globalState.HasSkillsOrPlugins() && !projectState.HasSkillsOrPlugins() { + if globalState == nil && projectState == nil { cmdio.LogString(ctx, "No Databricks skills or plugins installed.") cmdio.LogString(ctx, "") cmdio.LogString(ctx, "Run 'databricks aitools install' to get started.") @@ -57,14 +57,14 @@ func NewVersionCmd() *cobra.Command { // Always label the scope (global / project) for both skills and // plugins so it is unambiguous where each thing is installed. - if globalState.HasSkillsOrPlugins() { + if globalState != nil { if len(globalState.Skills) > 0 { printVersionLine(ctx, "Skills (global)", globalState, latestRef) } printPluginLines(ctx, globalState, "global") } - if projectState.HasSkillsOrPlugins() { + if projectState != nil { if len(projectState.Skills) > 0 { printVersionLine(ctx, "Skills (project)", projectState, latestRef) } diff --git a/libs/aitools/installer/installer.go b/libs/aitools/installer/installer.go index 178dbebb49b..8e5115a1998 100644 --- a/libs/aitools/installer/installer.go +++ b/libs/aitools/installer/installer.go @@ -358,13 +358,13 @@ func InstallSkillsForAgents(ctx context.Context, src ManifestSource, targetAgent } // A "legacy install" is one from before the CLI tracked install state in a - // JSON file: skills exist on disk but there's no record of them in state. + // JSON file: skills exist on disk but there's no state file (state == nil). // A targeted install (--skills foo) here would write a state file listing only // foo, leaving the other on-disk skills untracked. Block it so the user runs a // full install first, which rebuilds complete state. Global scope only, since // legacy installs only ever wrote to the global dir. // hasLegacyInstall lives in update.go (shared with the update/uninstall paths). - legacyInstall := !state.HasSkillsOrPlugins() && scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) + legacyInstall := state == nil && scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) if legacyInstall && len(opts.SpecificSkills) > 0 { return &RefusedError{Err: errors.New("legacy install detected without state tracking; run 'databricks aitools install --scope=global --skills-only' (without --skills) first to rebuild state")} } diff --git a/libs/aitools/installer/installer_test.go b/libs/aitools/installer/installer_test.go index 8a55fad9d00..0c8c86b42d1 100644 --- a/libs/aitools/installer/installer_test.go +++ b/libs/aitools/installer/installer_test.go @@ -1674,3 +1674,20 @@ func TestLegacyTargetedInstallBlockedWithMCPOnlyState(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "legacy install detected") } + +// TestInstallSpecificSkillWithEmptyStateFile checks that an empty state file, as an update that pruned +// every skill leaves, does not count as a legacy install, so a targeted install still runs. +func TestInstallSpecificSkillWithEmptyStateFile(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + require.NoError(t, os.MkdirAll(filepath.Join(globalDir, "databricks-old"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(globalDir, ".state.json"), []byte(`{"schema_version": 2, "skills": {}}`+"\n"), 0o600)) + + err := InstallSkillsForAgents(ctx, &mockManifestSource{manifest: testManifest()}, []*agents.Agent{testAgent(tmp)}, InstallOptions{ + SpecificSkills: []string{"databricks-sql"}, + }) + require.NoError(t, err) +} diff --git a/libs/aitools/installer/state.go b/libs/aitools/installer/state.go index 5f316fa8611..60563afb54f 100644 --- a/libs/aitools/installer/state.go +++ b/libs/aitools/installer/state.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "io/fs" "os" "path/filepath" "strings" @@ -59,8 +60,8 @@ type InstallState struct { // skills we wrote that the user hasn't modified. Files map[string]FileRecord `json:"files,omitempty"` // MCP records the Databricks MCP servers the `aitools mcp` commands wrote. - // Added in schema v3; omitted when none are recorded. Other writers keep it - // by loading the file first, since they save the whole struct. + // Added in schema v3; omitted when none are recorded. SaveState ignores this + // field and keeps the section on disk; SaveMCPState is the only writer of it. MCP *MCPState `json:"mcp,omitempty"` } @@ -119,18 +120,45 @@ type FileRecord struct { Origin string `json:"origin,omitempty"` } -// HasSkillsOrPlugins reports whether the state records an installed skill or plugin. MCP records -// share the file but are not installs, so a state holding only MCP records reports false, and so -// does a nil state. -func (s *InstallState) HasSkillsOrPlugins() bool { - return s != nil && (len(s.Skills) > 0 || len(s.Plugins) > 0) +// hasInstallerRecords reports whether the state holds anything the installer writes: a release, +// skills, plugins, files or repo dirs. +func hasInstallerRecords(s *InstallState) bool { + return s.Release != "" || len(s.Skills) > 0 || len(s.Plugins) > 0 || len(s.Files) > 0 || len(s.RepoDirs) > 0 +} + +// isMCPOnly reports whether the state holds MCP records and nothing the installer wrote. The installer +// records a release in every state it saves, so such a file was written by the MCP commands alone. A file +// without an MCP section is never MCP-only, so a state the installer left behind, even an empty one, keeps +// the behavior it always had. +func (s *InstallState) isMCPOnly() bool { + return s != nil && s.MCP != nil && !hasInstallerRecords(s) } // LoadState reads install state from the given directory. -// Returns (nil, nil) when the state file does not exist. +// Returns (nil, nil) when the state file does not exist or holds only MCP records, since those are not +// an install of skills or plugins. LoadMCPState reads the MCP records. func LoadState(dir string) (*InstallState, error) { - path := filepath.Join(dir, stateFileName) - data, err := os.ReadFile(path) + state, err := readState(dir) + if err != nil || state.isMCPOnly() { + return nil, err + } + return state, nil +} + +// LoadMCPState reads the MCP section of the state file in the given directory. It returns nil when the +// file does not exist or has no MCP section. +func LoadMCPState(dir string) (*MCPState, error) { + state, err := readState(dir) + if err != nil || state == nil { + return nil, err + } + return state.MCP, nil +} + +// readState reads the state file in the given directory, whatever it holds. It returns (nil, nil) when +// the file does not exist. +func readState(dir string) (*InstallState, error) { + data, err := os.ReadFile(filepath.Join(dir, stateFileName)) if errors.Is(err, os.ErrNotExist) { return nil, nil } @@ -157,12 +185,11 @@ func migrateState(state *InstallState) { } } -// SaveState writes the skills and plugins records of state to the given directory atomically, -// creating the directory if it does not exist. The MCP section is written by SaveMCPState, so -// SaveState keeps the one on disk, not the one in state. This keeps MCP records saved after state -// was loaded. The file is removed when no skills, plugins or MCP servers remain. +// SaveState writes state to the given directory atomically, creating the directory if it does not +// exist. The MCP section is the one on disk: SaveState does not write the MCP field of state, so +// MCP records saved after state was loaded are kept. func SaveState(dir string, state *InstallState) error { - onDisk, err := LoadState(dir) + onDisk, err := readState(dir) if err != nil { return err } @@ -171,14 +198,14 @@ func SaveState(dir string, state *InstallState) error { if onDisk != nil { out.MCP = onDisk.MCP } - return saveOrRemove(dir, &out) + return writeState(dir, &out) } -// SaveMCPState replaces the MCP section of the state file in the given directory. The file is read -// again first, so the skills and plugins records are kept. A nil or empty state removes the MCP -// section, and the file is removed once no skills, plugins or MCP servers remain. +// SaveMCPState replaces the MCP section of the state file in the given directory with mcp. The skills +// and plugins records are kept as they are on disk. A nil or empty mcp removes the section, and the file +// is removed once it holds nothing else. func SaveMCPState(dir string, mcp *MCPState) error { - st, err := LoadState(dir) + st, err := readState(dir) if err != nil { return err } @@ -197,25 +224,35 @@ func SaveMCPState(dir string, mcp *MCPState) error { if mcp != nil && len(mcp.Servers) > 0 { st.MCP = mcp } - return saveOrRemove(dir, st) + if st.MCP == nil && !hasInstallerRecords(st) { + return removeStateFile(dir) + } + return writeState(dir, st) } -// saveOrRemove writes st to the state file in dir, or removes the file when st records nothing: no -// skills, plugins, files or repo directories, and no MCP servers. Files and repo directories count -// even without a skill record, because the legacy raw-skill cleanup reads them on its own. -func saveOrRemove(dir string, st *InstallState) error { - if len(st.Skills) == 0 && len(st.Plugins) == 0 && len(st.Files) == 0 && len(st.RepoDirs) == 0 && st.MCP == nil { - err := os.Remove(filepath.Join(dir, stateFileName)) - if err != nil && !errors.Is(err, os.ErrNotExist) { - return fmt.Errorf("failed to remove state file: %w", err) - } - return nil +// removeState deletes the state file once uninstall has removed every skill and plugin. Uninstall does +// not own MCP records, so when servers remain the file is rewritten to hold only them. +func removeState(dir string) error { + st, err := readState(dir) + if err != nil { + return err } - return writeState(dir, st) + if st != nil && st.MCP != nil && len(st.MCP.Servers) > 0 { + return writeState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, MCP: st.MCP}) + } + return removeStateFile(dir) +} + +// removeStateFile deletes the state file in the given directory. A missing file is not an error. +func removeStateFile(dir string) error { + err := os.Remove(filepath.Join(dir, stateFileName)) + if err != nil && !errors.Is(err, fs.ErrNotExist) { + return fmt.Errorf("failed to remove state file: %w", err) + } + return nil } -// writeState replaces the state file in dir with state, atomically. It writes whatever it is given; -// saveOrRemove decides when there is nothing to keep. +// writeState writes state to the state file in the given directory atomically, as written. func writeState(dir string, state *InstallState) error { data, err := json.MarshalIndent(state, "", " ") if err != nil { diff --git a/libs/aitools/installer/state_test.go b/libs/aitools/installer/state_test.go index c66be4c7004..feeb59d81bd 100644 --- a/libs/aitools/installer/state_test.go +++ b/libs/aitools/installer/state_test.go @@ -48,7 +48,7 @@ func TestSaveStateCreatesDirectory(t *testing.T) { SchemaVersion: 1, Release: "v0.1.0", LastUpdated: time.Date(2026, 3, 22, 9, 0, 0, 0, time.UTC), - Skills: map[string]string{"databricks": "1.0.0"}, + Skills: map[string]string{}, } err := SaveState(dir, state) @@ -65,7 +65,7 @@ func TestSaveStateTrailingNewline(t *testing.T) { SchemaVersion: 1, Release: "v0.1.0", LastUpdated: time.Date(2026, 3, 22, 9, 0, 0, 0, time.UTC), - Skills: map[string]string{"databricks": "1.0.0"}, + Skills: map[string]string{}, } err := SaveState(dir, state) @@ -228,9 +228,9 @@ func TestSaveAndLoadStateKeepsMCPSection(t *testing.T) { }}} require.NoError(t, SaveMCPState(dir, original)) - loaded, err := LoadState(dir) + loaded, err := LoadMCPState(dir) require.NoError(t, err) - assert.Equal(t, original, loaded.MCP) + assert.Equal(t, original, loaded) } // TestSaveStateKeepsMCPRecordsSavedAfterLoad checks that an installer save writes the MCP section @@ -283,62 +283,77 @@ func TestSaveMCPStateRemovesFileWhenNothingLeft(t *testing.T) { assert.ErrorIs(t, err, os.ErrNotExist) } -// TestSaveMCPStateCreatesMapsForInstaller checks that a file created by an MCP save loads with the -// maps the installer writes into, so a later skills write does not panic. -func TestSaveMCPStateCreatesMapsForInstaller(t *testing.T) { +// TestSaveMCPStateKeepsInstallerLoadAsNoInstall checks that a file holding only MCP records loads as no +// install, so the installer treats it as it treats a missing file, and that a later skills write keeps +// the MCP records. +func TestSaveMCPStateKeepsInstallerLoadAsNoInstall(t *testing.T) { dir := t.TempDir() require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) loaded, err := LoadState(dir) require.NoError(t, err) - assert.NotNil(t, loaded.Skills) - assert.False(t, loaded.HasSkillsOrPlugins()) + assert.Nil(t, loaded) + + require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) + mcp, err := LoadMCPState(dir) + require.NoError(t, err) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, mcp.Servers) } -func TestHasSkillsOrPlugins(t *testing.T) { +// TestLoadStateHidesOnlyMCPOnlyFiles checks which files load as no install. A file with no MCP section +// loads as it always did, even when empty, since the installer wrote such files on main. Only a file +// holding MCP records and nothing the installer wrote loads as nil. +func TestLoadStateHidesOnlyMCPOnlyFiles(t *testing.T) { + mcp := &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}} tests := []struct { name string state *InstallState want bool }{ - {name: "nil", state: nil}, - {name: "mcp only", state: &InstallState{MCP: &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}}}}, - {name: "skills", state: &InstallState{Skills: map[string]string{"databricks": "1.0.0"}}, want: true}, - {name: "plugins", state: &InstallState{Plugins: map[string]PluginRecord{"claude-code": {Plugin: "databricks"}}}, want: true}, + {name: "mcp only", state: &InstallState{MCP: mcp}}, + {name: "empty without mcp", state: &InstallState{SchemaVersion: 2}, want: true}, + {name: "release without skills, with mcp", state: &InstallState{Release: "v0.1.5", MCP: mcp}, want: true}, + {name: "files without skills", state: &InstallState{Files: map[string]FileRecord{"databricks/SKILL.md": {SHA256: "abc"}}}, want: true}, + {name: "repo dirs without skills", state: &InstallState{RepoDirs: map[string]string{"databricks": "skills"}}, want: true}, + {name: "skills", state: &InstallState{Skills: map[string]string{"databricks": "1.0.0"}, MCP: mcp}, want: true}, + {name: "plugins", state: &InstallState{Plugins: map[string]PluginRecord{"claude-code": {Plugin: "databricks"}}, MCP: mcp}, want: true}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equal(t, tt.want, tt.state.HasSkillsOrPlugins()) + dir := t.TempDir() + require.NoError(t, writeState(dir, tt.state)) + + loaded, err := LoadState(dir) + require.NoError(t, err) + assert.Equal(t, tt.want, loaded != nil) }) } } -// TestSaveStateRemovesFileWhenNothingRemains checks that SaveState removes the state file once -// the skills and plugins are gone and no MCP servers are recorded. -func TestSaveStateRemovesFileWhenNothingRemains(t *testing.T) { +// TestSaveStateWritesEmptyState checks that SaveState writes the state it is given even when nothing +// is left in it, as the installer did before MCP records shared the file. +func TestSaveStateWritesEmptyState(t *testing.T) { dir := t.TempDir() require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion})) loaded, err := LoadState(dir) require.NoError(t, err) - assert.Nil(t, loaded) + require.NotNil(t, loaded) + assert.Empty(t, loaded.Skills) } -// TestSaveStateKeepsMCPRecordsWhenNothingElseRemains checks that SaveState, once the skills are -// gone, keeps the file for the MCP servers recorded on disk and writes the skills-free state. +// TestSaveStateKeepsMCPRecordsWhenNothingElseRemains checks that SaveState, once the skills are gone, +// keeps the MCP servers recorded on disk. func TestSaveStateKeepsMCPRecordsWhenNothingElseRemains(t *testing.T) { dir := t.TempDir() require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks": "1.0.0"}})) require.NoError(t, SaveMCPState(dir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) require.NoError(t, SaveState(dir, &InstallState{SchemaVersion: CurrentSchemaVersion})) - loaded, err := LoadState(dir) + mcp, err := LoadMCPState(dir) require.NoError(t, err) - require.NotNil(t, loaded) - assert.Empty(t, loaded.Skills) - require.NotNil(t, loaded.MCP) - assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, mcp.Servers) } func TestLoadStateMigratesV2ToV3AndSavesV3(t *testing.T) { diff --git a/libs/aitools/installer/uninstall.go b/libs/aitools/installer/uninstall.go index fa756aeb35d..819f3b565d7 100644 --- a/libs/aitools/installer/uninstall.go +++ b/libs/aitools/installer/uninstall.go @@ -75,7 +75,7 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni return nil, fmt.Errorf("failed to load install state: %w", err) } - if !state.HasSkillsOrPlugins() { + if state == nil { if scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) { return nil, fmt.Errorf("%w; run 'databricks aitools install' first, then uninstall", ErrLegacyInstall) } @@ -197,15 +197,17 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni } } - // If nothing remains in this scope, clean up orphaned symlinks. SaveState then - // removes the state file, unless it still holds MCP records, which uninstall does - // not own; otherwise it persists the trimmed state. - noSkillsOrPlugins := len(state.Skills) == 0 && len(state.Plugins) == 0 - if noSkillsOrPlugins { + // If nothing remains in this scope, clean up orphaned symlinks and delete the + // state file; otherwise persist the trimmed state. + if len(state.Skills) == 0 && len(state.Plugins) == 0 { cleanOrphanedSymlinks(ctx, baseDir, scope, cwd) - } - if err := SaveState(baseDir, state); err != nil { - return err + if err := removeState(baseDir); err != nil { + return err + } + } else { + if err := SaveState(baseDir, state); err != nil { + return err + } } if pluginCount > 0 { diff --git a/libs/aitools/installer/uninstall_test.go b/libs/aitools/installer/uninstall_test.go index eb94cf6ad79..cba9f61155e 100644 --- a/libs/aitools/installer/uninstall_test.go +++ b/libs/aitools/installer/uninstall_test.go @@ -624,10 +624,10 @@ func TestUninstallKeepsStateFileWithMCPRecords(t *testing.T) { // The skills are gone, but the MCP records stay in the file. loaded, err := LoadState(globalDir) require.NoError(t, err) - require.NotNil(t, loaded) - assert.Empty(t, loaded.Skills) - require.NotNil(t, loaded.MCP) - assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) + assert.Nil(t, loaded) + mcp, err := LoadMCPState(globalDir) + require.NoError(t, err) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, mcp.Servers) } // TestUninstallMCPOnlyStateReturnsError checks that a state file holding only MCP records counts @@ -641,9 +641,25 @@ func TestUninstallMCPOnlyStateReturnsError(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "no skills or plugins installed") - loaded, err := LoadState(globalDir) + mcp, err := LoadMCPState(globalDir) require.NoError(t, err) - require.NotNil(t, loaded) - require.NotNil(t, loaded.MCP) - assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, loaded.MCP.Servers) + assert.Equal(t, []MCPEntry{{Name: "github-logfood"}}, mcp.Servers) +} + +// TestUninstallRemovesLeftoverFilesWithoutSkills checks that a state file with file and repo-dir +// records but no skills or plugins is still uninstalled, and the file is removed, as on main. +func TestUninstallRemovesLeftoverFilesWithoutSkills(t *testing.T) { + tmp := setupTestHome(t) + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + require.NoError(t, SaveState(globalDir, &InstallState{ + SchemaVersion: CurrentSchemaVersion, + Skills: map[string]string{}, + RepoDirs: map[string]string{"databricks-sql": "repo"}, + Files: map[string]FileRecord{"databricks-sql/SKILL.md": {SHA256: "abc"}}, + })) + + require.NoError(t, UninstallSkills(cmdio.MockDiscard(t.Context()))) + + _, err := os.Stat(filepath.Join(globalDir, ".state.json")) + assert.ErrorIs(t, err, fs.ErrNotExist) } diff --git a/libs/aitools/installer/update.go b/libs/aitools/installer/update.go index b4bdf3cad3b..30f1d98ab48 100644 --- a/libs/aitools/installer/update.go +++ b/libs/aitools/installer/update.go @@ -82,7 +82,7 @@ func UpdateSkills(ctx context.Context, src ManifestSource, targetAgents []*agent return nil, fmt.Errorf("failed to load install state: %w", err) } - if !state.HasSkillsOrPlugins() { + if state == nil { if scope == ScopeGlobal && hasLegacyInstall(ctx, baseDir) { return nil, fmt.Errorf("%w; run 'databricks aitools install' to refresh before updating", ErrLegacyInstall) } diff --git a/libs/aitools/installer/update_test.go b/libs/aitools/installer/update_test.go index 8ad8146cc01..18d2cc521b9 100644 --- a/libs/aitools/installer/update_test.go +++ b/libs/aitools/installer/update_test.go @@ -707,3 +707,29 @@ func TestUpdateMCPOnlyStateReturnsInstallHint(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "no skills or plugins installed") } + +// TestUpdateAfterPruningEverySkillAddsNewSkills checks that an update which prunes every skill leaves +// a state file behind, so the next update adds the skills the manifest now lists. +func TestUpdateAfterPruningEverySkillAddsNewSkills(t *testing.T) { + tmp := setupTestHome(t) + ctx := cmdio.MockDiscard(t.Context()) + setupFetchMock(t) + t.Setenv("DATABRICKS_SKILLS_REF", testSkillsRef) + globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") + agent := testAgent(tmp) + + require.NoError(t, InstallSkillsForAgents(ctx, &mockManifestSource{manifest: testManifest()}, []*agents.Agent{agent}, InstallOptions{})) + t.Setenv("DATABRICKS_SKILLS_REF", "v0.2.0") + empty := &Manifest{Version: "1", UpdatedAt: "2024-02-01", Skills: map[string]SkillMeta{}} + _, err := UpdateSkills(ctx, &mockManifestSource{manifest: empty}, []*agents.Agent{agent}, UpdateOptions{}) + require.NoError(t, err) + + t.Setenv("DATABRICKS_SKILLS_REF", "v0.3.0") + _, err = UpdateSkills(ctx, &mockManifestSource{manifest: testManifest()}, []*agents.Agent{agent}, UpdateOptions{}) + require.NoError(t, err) + + state, err := LoadState(globalDir) + require.NoError(t, err) + require.NotNil(t, state) + assert.Contains(t, state.Skills, "databricks-sql") +} diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index ee34a7018ae..17320c755e3 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -10,8 +10,8 @@ import ( "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/databrickscfg" - "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" + "github.com/databricks/cli/libs/shellquote" "github.com/databricks/databricks-sdk-go/config" ) @@ -41,7 +41,7 @@ type BindOptions struct { Profile string Service string State *State - // Stderr receives the notice printed when no install record exists. + // Stderr receives the notice printed when no install record exists. It must not be nil. Stderr io.Writer } @@ -70,9 +70,11 @@ func (e *BindError) ExitCode() int { // loads a bundle. It returns the resolved config and the matching state entry, which is nil // when none exists. // -// The profile is checked before the SDK resolves the config, so a re-pointed host is never -// contacted. The config file is the one in the context, resolved once: the profile lookup, -// the entry match and the SDK all read that path. +// The config file is the one that DATABRICKS_CONFIG_FILE names in the context, resolved once: +// the profile lookup, the entry match and cfg.ConfigFile all use that path. The profile is +// checked before the SDK resolves the config, so a re-pointed host is never contacted. The CLI's +// token store reads DATABRICKS_CONFIG_FILE from the process environment instead, so a caller +// that sets the variable in the context must also set it in the process environment. func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) { configFile, err := resolveConfigFile(ctx, env.Get(ctx, mcpconfig.ConfigFileEnv)) if err != nil { @@ -81,14 +83,14 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) ctx = env.Set(ctx, mcpconfig.ConfigFileEnv, configFile) entry, err := findEntry(ctx, opts.State, opts.Service, opts.Profile, configFile) if err != nil { - return nil, nil, uncategorizedError(err) + return nil, nil, err } name := opts.Service if entry != nil { name = entry.Name } - p, err := loadProfile(ctx, profile.GetProfiler(ctx), opts.Profile) + p, err := configFiles{}.profile(configFile, opts.Profile) switch { case errors.Is(err, errProfileMissing): return nil, nil, profileGoneError(name, opts.Profile, filepath.ToSlash(configFile), entry, "no longer exists") @@ -137,47 +139,70 @@ func uncategorizedError(err error) error { return &BindError{Category: BindUncategorized, message: err.Error(), cause: err} } +// removeCommand returns the command that removes the server bound to profileName. +func removeCommand(profileName string) string { + return mcpRemoveCommand + " --profile " + shellquote.BashArg(profileName) +} + // 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 the recorded host is unknown, so the -// recreate hint omits --host. +// recreate hint omits --host. 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 { - recreate := authLoginCommand + " --profile " + profileName + recreate := authLoginCommand + " --profile " + shellquote.BashArg(profileName) if entry != nil { - recreate = authLoginCommand + " --host " + entry.Host + " --profile " + profileName + recreate = authLoginCommand + " --host " + shellquote.BashArg(entry.Host) + " --profile " + shellquote.BashArg(profileName) + if entry.ConfigFile != "" { + recreate = "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(path) + " " + recreate + } } - message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which %s in %s. Recreate it with: %s or remove the server with: %s --profile %s", - name, profileName, problem, path, recreate, mcpRemoveCommand, profileName) + message := fmt.Sprintf("MCP server %s is bound to Databricks profile \"%s\", which %s in %s. Recreate it with: %s or remove the server with: %s", + name, profileName, problem, path, recreate, removeCommand(profileName)) return &BindError{Category: BindProfileNotFound, message: message} } // driftError is the PROFILE_HOST_CHANGED error for an entry whose profile now names another host, // or another workspace on the same host. func driftError(name, profileName string, entry Entry, drift Drift) error { + recordedHost, recordedWorkspaceID := recordedTarget(entry) var message string - if NormalizeHost(entry.Host) != NormalizeHost(drift.CurrentHost) { - message = fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: %s --profile %s and add it again", - name, entry.Host, profileName, drift.CurrentHost, mcpRemoveCommand, profileName) + if NormalizeHost(recordedHost) != NormalizeHost(drift.CurrentHost) { + message = fmt.Sprintf("MCP server %s was set up for %s, but profile \"%s\" now points at %s. Run: %s and add it again", + name, recordedHost, profileName, drift.CurrentHost, removeCommand(profileName)) } else { - message = fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: %s --profile %s and add it again", - name, entry.WorkspaceID, profileName, drift.CurrentWorkspaceID, mcpRemoveCommand, profileName) + message = fmt.Sprintf("MCP server %s was set up for workspace %s, but profile \"%s\" now points at workspace %s. Run: %s and add it again", + name, recordedWorkspaceID, profileName, drift.CurrentWorkspaceID, removeCommand(profileName)) } return &BindError{Category: BindProfileHostChanged, message: message} } +// configFileError is the PROFILE_HOST_CHANGED error for an entry whose config file is not the one in +// use. The profile may name the same host there, but the server was set up against the recorded file. +func configFileError(name, profileName, recordedPath, currentPath string) error { + message := fmt.Sprintf("MCP server %s was set up with config file %s, but profile \"%s\" is read from %s now. Run: %s and add it again, or set DATABRICKS_CONFIG_FILE to %s", + name, recordedPath, profileName, currentPath, removeCommand(profileName), shellquote.BashArg(recordedPath)) + return &BindError{Category: BindProfileHostChanged, message: message} +} + // authError is the AUTH_FAILED error for a credential that the profile's own settings could not // provide. The message points at the login command for the profile. func authError(profileName string, cause error) error { - message := fmt.Sprintf("%s\nRun: %s --profile %s", cause, authLoginCommand, profileName) + message := fmt.Sprintf("%s\nRun: %s --profile %s", cause, authLoginCommand, shellquote.BashArg(profileName)) return &BindError{Category: BindAuthFailed, message: message, cause: cause} } -// findEntry returns the entry for the service and profile whose config file is configFile, or nil. -// Both are compared as resolved paths, so an entry with no pinned file matches the default file. -// An entry whose file cannot be resolved, such as a "~" path with no home directory, cannot be -// compared. It is skipped, and its error is returned only when no other entry matches. +// findEntry returns the entry for the service and profile whose config file is configFile, or nil when +// the service and profile have no entry at all. Both sides are compared as resolved paths, so an entry +// with no pinned file matches the default file. +// +// An entry for the service and profile in another file is an error, not a missing entry: the server was +// set up against that file, and binding through this one would switch it without a record. An entry +// whose file cannot be resolved, such as a "~" path with no home directory, cannot be compared. The +// bind fails on it, since it might be the match. func findEntry(ctx context.Context, state *State, service, profileName, configFile string) (*Entry, error) { var unresolved error + var other *Entry + var otherPath string for i := range state.Servers { entry := &state.Servers[i] if entry.Service != service || entry.Profile != profileName { @@ -191,6 +216,15 @@ func findEntry(ctx context.Context, state *State, service, profileName, configFi if samePath(path, configFile) { return entry, nil } + if other == nil { + other, otherPath = entry, path + } + } + if unresolved != nil { + return nil, uncategorizedError(unresolved) + } + if other != nil { + return nil, configFileError(other.Name, profileName, filepath.ToSlash(otherPath), filepath.ToSlash(configFile)) } - return nil, unresolved + return nil, nil } diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 7b5dc95d0d0..418738aa67e 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -127,7 +127,7 @@ func TestBindProfileAbsent(t *testing.T) { Host: bindingTestHost, ConfigFile: path, }}}, - want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path)), + want: fmt.Sprintf(`MCP server github-gone is bound to Databricks profile "gone", which no longer exists in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile gone or remove the server with: databricks aitools mcp remove --profile gone`, filepath.ToSlash(path), filepath.ToSlash(path)), }, { name: "without state entry", @@ -149,15 +149,17 @@ func TestBindProfileAbsent(t *testing.T) { } } +// TestBindConfigFileAbsent checks that an entry pinned to a config file that no longer exists reports +// the profile as gone from that file. func TestBindConfigFileAbsent(t *testing.T) { - cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) path := filepath.Join(t.TempDir(), "absent") t.Setenv("DATABRICKS_CONFIG_FILE", path) _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, - State: bindingTestState(cfgPath, bindingTestHost), + State: bindingTestState(path, bindingTestHost), Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindProfileNotFound) @@ -165,16 +167,36 @@ func TestBindConfigFileAbsent(t *testing.T) { } func TestBindHostChanged(t *testing.T) { - cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + tests := []struct { + name string + entry string + want string + }{ + { + name: "host", + entry: "https://adb-999.test", + want: "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again", + }, + { + name: "host with a query naming another workspace on the same host", + entry: "https://adb-999.test/?o=111", + want: "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) - _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ - Profile: bindingTestProfile, - Service: bindingTestService, - State: bindingTestState(cfgPath, "https://adb-999.test"), - Stderr: &bytes.Buffer{}, - }) - requireBindError(t, err, mcp.BindProfileHostChanged) - assert.Equal(t, "MCP server github-logfood was set up for https://adb-999.test, but profile \"logfood\" now points at https://adb-123.test. Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(cfgPath, tt.entry), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Equal(t, tt.want, err.Error()) + }) + } } // TestBindHostChangedDoesNotContactNewHost checks that a re-pointed profile is rejected before @@ -197,18 +219,41 @@ func TestBindHostChangedDoesNotContactNewHost(t *testing.T) { } func TestBindWorkspaceChanged(t *testing.T) { - cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\nworkspace_id = 222\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, bindingTestToken)) - state := bindingTestState(cfgPath, bindingTestHost) - state.Servers[0].WorkspaceID = "111" + const want = "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --profile logfood and add it again" + tests := []struct { + name string + entryHost string + entryWS string + profileLine string + }{ + { + name: "workspace_id in the profile", + entryHost: bindingTestHost, + entryWS: "111", + profileLine: "workspace_id = 222\n", + }, + { + name: "workspace in the recorded host query", + entryHost: bindingTestHost + "/?o=111", + profileLine: "workspace_id = 222\n", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost = %s\n%sauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, tt.profileLine, bindingTestToken)) + state := bindingTestState(cfgPath, tt.entryHost) + state.Servers[0].WorkspaceID = tt.entryWS - _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ - Profile: bindingTestProfile, - Service: bindingTestService, - State: state, - Stderr: &bytes.Buffer{}, - }) - requireBindError(t, err, mcp.BindProfileHostChanged) - assert.Equal(t, "MCP server github-logfood was set up for workspace 111, but profile \"logfood\" now points at workspace 222. Run: databricks aitools mcp remove --profile logfood and add it again", err.Error()) + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Equal(t, want, err.Error()) + }) + } } func TestBindWorkspaceNotComparedWhenProfileNamesNone(t *testing.T) { @@ -432,22 +477,24 @@ func TestBindUnpinnedEntryMatchesDefaultConfigFile(t *testing.T) { // TestBindUnpinnedEntryDoesNotMatchOtherConfigFile checks that an entry with no pinned config file // does not bind a profile of the same name in another config file. -func TestBindUnpinnedEntryDoesNotMatchOtherConfigFile(t *testing.T) { - bindingTestDefaultConfig(t, bindingTestProfileContents(bindingTestToken)) +// TestBindUnpinnedEntryFailsForOtherConfigFile checks that an unpinned entry, which names the default +// file, stops a bind through another file. The profile there points at another host, and the bind must +// not switch the server to it silently. +func TestBindUnpinnedEntryFailsForOtherConfigFile(t *testing.T) { + defaultPath := bindingTestDefaultConfig(t, bindingTestProfileContents(bindingTestToken)) other := bindingTestWriteConfig(t, "[logfood]\nhost = https://adb-456.test\nauth_type = pat\ntoken = dapi-other\n") t.Setenv("DATABRICKS_CONFIG_FILE", other) stderr := &bytes.Buffer{} - cfg, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ Profile: bindingTestProfile, Service: bindingTestService, State: bindingTestState("", bindingTestHost), Stderr: stderr, }) - require.NoError(t, err) - assert.Nil(t, entry) - assert.Equal(t, "https://adb-456.test", cfg.Host) - assert.Contains(t, stderr.String(), "No install record for system.ai.github on profile \"logfood\"") + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Contains(t, err.Error(), "was set up with config file "+bindingTestResolved(t, defaultPath)) + assert.Empty(t, stderr.String()) } // TestBindWithoutHomeAndAbsoluteConfigFile checks that a bind needs no home directory when the @@ -615,7 +662,8 @@ func TestBindProfileWithoutHost(t *testing.T) { Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindProfileNotFound) - assert.Equal(t, fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "logfood", which has no host in %s. Recreate it with: databricks auth login --host https://adb-123.test --profile logfood or remove the server with: databricks aitools mcp remove --profile logfood`, filepath.ToSlash(bindingTestResolved(t, path))), err.Error()) + resolved := filepath.ToSlash(bindingTestResolved(t, path)) + assert.Equal(t, fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "logfood", which has no host in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile logfood or remove the server with: databricks aitools mcp remove --profile logfood`, resolved, resolved), err.Error()) } // TestBindHostMetadataFailureIsNotAuthFailure checks that a host whose metadata cannot be fetched does @@ -655,3 +703,80 @@ func TestBindConfigErrorIsUncategorized(t *testing.T) { assert.NotContains(t, err.Error(), "Run:") assert.NotContains(t, err.Error(), "auth login") } + +// TestBindEntryInOtherConfigFileFailsClosed checks that an entry for the service and profile, pinned to +// another config file, stops the bind when the file in use names no entry. Binding there would be a new +// host with no record of the switch. +func TestBindEntryInOtherConfigFileFailsClosed(t *testing.T) { + recordedPath := bindingTestWriteConfig(t, bindingTestProfileContents(bindingTestToken)) + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + stderr := &bytes.Buffer{} + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(recordedPath, bindingTestHost), + Stderr: stderr, + }) + requireBindError(t, err, mcp.BindProfileHostChanged) + assert.Contains(t, err.Error(), "was set up with config file "+bindingTestResolved(t, recordedPath)) + assert.Empty(t, stderr.String()) +} + +// TestBindEntryForOtherProfileKeepsNotice checks that an entry for a different profile does not stop +// a bind to a profile that has no entry, which still gets the notice. +func TestBindEntryForOtherProfileKeepsNotice(t *testing.T) { + bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + state := bindingTestState(bindingTestWriteConfig(t, ""), bindingTestHost) + state.Servers[0].Profile = "other" + stderr := &bytes.Buffer{} + + _, entry, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: state, + Stderr: stderr, + }) + require.NoError(t, err) + assert.Nil(t, entry) + assert.Contains(t, stderr.String(), "No install record for system.ai.github") +} + +// TestBindProfileWithEmptyHost checks that a profile whose host is set to nothing is reported as having +// no host, not as a profile whose host changed to "". +func TestBindProfileWithEmptyHost(t *testing.T) { + cfgPath := bindingTestConfig(t, fmt.Sprintf("[%s]\nhost =\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestToken)) + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: bindingTestProfile, + Service: bindingTestService, + State: bindingTestState(cfgPath, bindingTestHost), + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileNotFound) + assert.Contains(t, err.Error(), "which has no host in") +} + +// TestBindRecreateHintQuotesValues checks that the recreate hint quotes a profile name that needs it, +// and names the pinned config file it was set up with. +func TestBindRecreateHintQuotesValues(t *testing.T) { + cfgPath := bindingTestConfig(t, bindingTestProfileContents(bindingTestToken)) + state := &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", + Service: bindingTestService, + Profile: "my profile", + Host: bindingTestHost, + ConfigFile: cfgPath, + }}} + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: "my profile", + Service: bindingTestService, + State: state, + Stderr: &bytes.Buffer{}, + }) + requireBindError(t, err, mcp.BindProfileNotFound) + want := fmt.Sprintf(`MCP server github-logfood is bound to Databricks profile "my profile", which no longer exists in %s. Recreate it with: DATABRICKS_CONFIG_FILE=%s databricks auth login --host https://adb-123.test --profile 'my profile' or remove the server with: databricks aitools mcp remove --profile 'my profile'`, + filepath.ToSlash(bindingTestResolved(t, cfgPath)), filepath.ToSlash(bindingTestResolved(t, cfgPath))) + assert.Equal(t, want, err.Error()) +} diff --git a/libs/aitools/mcp/configfile.go b/libs/aitools/mcp/configfile.go index 586e81f1cd7..7244fd9bab2 100644 --- a/libs/aitools/mcp/configfile.go +++ b/libs/aitools/mcp/configfile.go @@ -3,23 +3,19 @@ package mcp import ( "context" "errors" + "fmt" "io/fs" + "os" "path/filepath" - "runtime" - "strings" "github.com/databricks/cli/libs/databrickscfg" ) -// foldCase reports whether config file paths compare case-insensitively. Windows and macOS -// file systems are case-insensitive by default. Tests set it to cover both comparisons. -var foldCase = runtime.GOOS == "windows" || runtime.GOOS == "darwin" - // resolveConfigFile returns the absolute path of the config file that name refers to. An empty // name is the default file, and a leading "~" is the home directory. Symlinks are resolved while // the path is still as the user wrote it: the OS reads "link/../x" through the link's target, so -// the same file has one path however it is named. A path that does not exist is made absolute as -// written, since there is no file to resolve. +// the same file has one path however it is named. A path that does not exist is kept as written, +// since there is no file to resolve and cleaning it could name another file. func resolveConfigFile(ctx context.Context, name string) (string, error) { path, err := databrickscfg.ResolveConfigFilePath(ctx, name) if err != nil { @@ -28,17 +24,34 @@ func resolveConfigFile(ctx context.Context, name string) (string, error) { resolved, err := filepath.EvalSymlinks(path) switch { case err == nil: - path = resolved - case !errors.Is(err, fs.ErrNotExist): + return filepath.Abs(resolved) + case errors.Is(err, fs.ErrNotExist): + return absoluteAsWritten(path) + default: return "", err } - return filepath.Abs(path) } -// samePath reports whether two resolved config file paths name the same file. +// absoluteAsWritten makes path absolute by joining it to the working directory, without cleaning it. +func absoluteAsWritten(path string) (string, error) { + if filepath.IsAbs(path) { + return path, nil + } + wd, err := os.Getwd() + if err != nil { + return "", fmt.Errorf("failed to determine working directory: %w", err) + } + return wd + string(filepath.Separator) + path, nil +} + +// samePath reports whether two resolved config file paths name the same file. Existing files are +// compared by identity, which also matches hard links and case variants on case-insensitive file +// systems. Paths that do not exist are compared as text. func samePath(a, b string) bool { - if foldCase { - return strings.EqualFold(a, b) + fa, errA := os.Stat(a) + fb, errB := os.Stat(b) + if errA == nil && errB == nil { + return os.SameFile(fa, fb) } return a == b } diff --git a/libs/aitools/mcp/configfile_test.go b/libs/aitools/mcp/configfile_test.go index 981c944c41a..b1f16412ba3 100644 --- a/libs/aitools/mcp/configfile_test.go +++ b/libs/aitools/mcp/configfile_test.go @@ -10,51 +10,6 @@ import ( "github.com/stretchr/testify/require" ) -// setFoldCase sets the case-folding seam for the test and restores it afterwards. The seam lets both -// comparisons run on any OS, where the platform default would pick only one. -func setFoldCase(t *testing.T, fold bool) { - t.Helper() - prev := foldCase - t.Cleanup(func() { foldCase = prev }) - foldCase = fold -} - -// TestSamePathFollowsFoldCase checks that paths compare case-insensitively only when the seam says so. -func TestSamePathFollowsFoldCase(t *testing.T) { - const lower = "/home/u/.databrickscfg" - const upper = "/HOME/U/.DATABRICKSCFG" - - setFoldCase(t, true) - assert.True(t, samePath(lower, upper)) - assert.True(t, samePath(lower, lower)) - - setFoldCase(t, false) - assert.False(t, samePath(lower, upper)) - assert.True(t, samePath(lower, lower)) -} - -// TestFindEntryMatchesCaseFoldedPath checks that an entry whose file name differs only in case matches -// the config file when paths fold case, as they do on Windows and macOS. -func TestFindEntryMatchesCaseFoldedPath(t *testing.T) { - setFoldCase(t, true) - // Resolve the directory first, so the file the entry names and the file in the context match on - // systems where the temp directory is a symlink. - dir, err := filepath.EvalSymlinks(t.TempDir()) - require.NoError(t, err) - configFile := filepath.Join(dir, ".databrickscfg") - state := &State{Servers: []Entry{{ - Name: "github-logfood", - Service: "system.ai.github", - Profile: "logfood", - ConfigFile: filepath.Join(dir, ".DATABRICKSCFG"), - }}} - - entry, err := findEntry(t.Context(), state, "system.ai.github", "logfood", configFile) - require.NoError(t, err) - require.NotNil(t, entry) - assert.Equal(t, "github-logfood", entry.Name) -} - // TestResolveConfigFileResolvesSymlinkBeforeDotDot checks that ".." after a symlink is taken from the // link's target, as the OS reads the path, not from the link's own directory. func TestResolveConfigFileResolvesSymlinkBeforeDotDot(t *testing.T) { @@ -89,3 +44,32 @@ func TestResolveConfigFileWithoutHomeAndAbsoluteName(t *testing.T) { _, err = resolveConfigFile(ctx, "~/.databrickscfg") require.ErrorIs(t, err, env.ErrNoHomeEnv) } + +// TestResolveConfigFileKeepsPathThroughMissingDir checks that a path through a missing directory is kept +// as written. Cleaning "missing/.." would name the parent's file, which the OS never opens through a +// missing directory. +func TestResolveConfigFileKeepsPathThroughMissingDir(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "cfg"), []byte("[logfood]\n"), 0o600)) + sep := string(filepath.Separator) + name := dir + sep + "missing" + sep + ".." + sep + "cfg" + + got, err := resolveConfigFile(t.Context(), name) + require.NoError(t, err) + assert.Equal(t, name, got) +} + +// TestSamePathComparesExistingFilesByIdentity checks that two names of one existing file compare equal, +// as a hard link does, while names of files that do not exist are compared as text. +func TestSamePathComparesExistingFilesByIdentity(t *testing.T) { + dir := t.TempDir() + a := filepath.Join(dir, "a") + b := filepath.Join(dir, "b") + require.NoError(t, os.WriteFile(a, []byte("[logfood]\n"), 0o600)) + if err := os.Link(a, b); err != nil { + t.Skipf("cannot create hard link: %v", err) + } + + assert.True(t, samePath(a, b)) + assert.False(t, samePath(filepath.Join(dir, "x"), filepath.Join(dir, "X"))) +} diff --git a/libs/aitools/mcp/state.go b/libs/aitools/mcp/state.go index 1f8271877b2..ceeaf7ab69c 100644 --- a/libs/aitools/mcp/state.go +++ b/libs/aitools/mcp/state.go @@ -4,12 +4,11 @@ import ( "context" "errors" "fmt" + "io/fs" "strings" "github.com/databricks/cli/libs/aitools/installer" - "github.com/databricks/cli/libs/aitools/mcpconfig" "github.com/databricks/cli/libs/databrickscfg/profile" - "github.com/databricks/cli/libs/env" "github.com/databricks/databricks-sdk-go/config" ) @@ -21,10 +20,9 @@ const ( // Profile lookups that do not yield a usable profile. var ( - // errProfileMissing means the config file has no profile of the name. + // errProfileMissing means the config file has no profile of the name, or does not exist. errProfileMissing = errors.New("profile missing") - // errProfileNoHost means the config file has a section of the name without a host. - // LoadProfiles skips such sections, so the lookup reports them separately. + // errProfileNoHost means the config file has a section of the name whose host is missing or empty. errProfileNoHost = errors.New("profile has no host") ) @@ -59,14 +57,14 @@ func LoadState(ctx context.Context) (*State, error) { if err != nil { return nil, err } - st, err := installer.LoadState(dir) + mcp, err := installer.LoadMCPState(dir) if err != nil { return nil, err } - if st == nil || st.MCP == nil { + if mcp == nil { return &State{}, nil } - return st.MCP, nil + return mcp, nil } // SaveState replaces the MCP section of the aitools state file. The skills and plugins records @@ -82,16 +80,18 @@ func SaveState(ctx context.Context, state *State) error { // Reconcile reports entries whose profile is gone or has no host, and entries whose profile now // names another host, or another workspace when both the entry and the profile name one. Each // entry is checked against the config file it is pinned to; an entry with no pinned file is checked -// against the default file, ~/.databrickscfg, whatever the environment names. +// against the default file, ~/.databrickscfg, whatever the environment names. Each config file is +// read once. // // An entry that cannot be checked, such as one whose "~" path has no home directory to resolve, does // not stop the others. Its error is joined into the returned error, and the drifts of the other // entries are still returned. -func Reconcile(ctx context.Context, profiler profile.Profiler, state *State) ([]Drift, error) { +func Reconcile(ctx context.Context, state *State) ([]Drift, error) { + files := configFiles{} var drifts []Drift var errs []error for _, entry := range state.Servers { - drift, err := checkEntry(ctx, profiler, entry) + drift, err := checkEntry(ctx, files, entry) if err != nil { errs = append(errs, fmt.Errorf("MCP server %s: %w", entry.Name, err)) continue @@ -105,12 +105,12 @@ func Reconcile(ctx context.Context, profiler profile.Profiler, state *State) ([] // checkEntry returns the drift of one entry, or nil when its profile still names the recorded host // and workspace. The entry's profile is read from the config file the entry is pinned to. -func checkEntry(ctx context.Context, profiler profile.Profiler, entry Entry) (*Drift, error) { +func checkEntry(ctx context.Context, files configFiles, entry Entry) (*Drift, error) { path, err := resolveConfigFile(ctx, entry.ConfigFile) if err != nil { return nil, err } - p, err := loadProfile(env.Set(ctx, mcpconfig.ConfigFileEnv, path), profiler, entry.Profile) + p, err := files.profile(path, entry.Profile) if errors.Is(err, errProfileMissing) || errors.Is(err, errProfileNoHost) { return &Drift{Kind: DriftProfileMissing, Entry: entry}, nil } @@ -120,40 +120,57 @@ func checkEntry(ctx context.Context, profiler profile.Profiler, entry Entry) (*D return driftOf(entry, p), nil } -// loadProfile returns the profile named name from the config file in ctx. It returns errProfileMissing -// when the file has no such profile, and errProfileNoHost when it has a section of that name without a -// host. -func loadProfile(ctx context.Context, profiler profile.Profiler, name string) (profile.Profile, error) { - profiles, err := profiler.LoadProfiles(ctx, profile.WithName(name)) - if err != nil && !errors.Is(err, profile.ErrNoConfiguration) { - return profile.Profile{}, fmt.Errorf("failed to read Databricks profiles: %w", err) - } - if len(profiles) > 0 { - return profiles[0], nil +// configFiles holds the config files that one lookup has read, by resolved path, so that each file is +// parsed once. A nil file is one that does not exist. +type configFiles map[string]*config.File + +// profile returns the profile named name from the config file at path. It returns errProfileMissing +// when the file does not exist or has no section of that name, and errProfileNoHost when the section +// has no host or an empty one. +func (c configFiles) profile(path, name string) (profile.Profile, error) { + file, ok := c[path] + if !ok { + loaded, err := config.LoadFile(path) + switch { + case errors.Is(err, fs.ErrNotExist): + loaded = nil + case err != nil: + return profile.Profile{}, fmt.Errorf("failed to read Databricks profiles: %w", err) + } + file = loaded + c[path] = file } - file, err := profile.FileProfilerImpl{}.Get(ctx) - if errors.Is(err, profile.ErrNoConfiguration) { + if file == nil { return profile.Profile{}, errProfileMissing } + section, err := file.GetSection(name) if err != nil { - return profile.Profile{}, fmt.Errorf("failed to read Databricks profiles: %w", err) + return profile.Profile{}, errProfileMissing } - if file.HasSection(name) { + keys := section.KeysHash() + if keys["host"] == "" { return profile.Profile{}, errProfileNoHost } - return profile.Profile{}, errProfileMissing + return profile.Profile{Name: name, Host: keys["host"], WorkspaceID: keys["workspace_id"]}, nil } // driftOf returns the drift of an entry against the profile it names, or nil when the profile still // names the recorded host and workspace. func driftOf(entry Entry, p profile.Profile) *Drift { + recordedHost, recordedWorkspaceID := recordedTarget(entry) host, workspaceID := profileTarget(p) - if NormalizeHost(entry.Host) == NormalizeHost(host) && !workspaceChanged(entry.WorkspaceID, workspaceID) { + if NormalizeHost(recordedHost) == NormalizeHost(host) && !workspaceChanged(recordedWorkspaceID, workspaceID) { return nil } return &Drift{Kind: DriftHostChanged, Entry: entry, CurrentHost: p.Host, CurrentWorkspaceID: workspaceID} } +// recordedTarget returns the host and workspace ID that an entry recorded, resolved as the SDK resolves +// a profile's, so that a workspace named in the recorded host's query is compared too. +func recordedTarget(entry Entry) (host, workspaceID string) { + return profileTarget(profile.Profile{Host: entry.Host, WorkspaceID: entry.WorkspaceID}) +} + // profileTarget returns the host and workspace ID that the SDK resolves for a profile. It runs the // SDK's host handling only, so it makes no network request: the SDK keeps a workspace_id the profile // sets, and otherwise takes the workspace from the host query. The host has its query removed. The diff --git a/libs/aitools/mcp/state_test.go b/libs/aitools/mcp/state_test.go index 9cc77d4ddd1..06cca4350d7 100644 --- a/libs/aitools/mcp/state_test.go +++ b/libs/aitools/mcp/state_test.go @@ -3,6 +3,7 @@ package mcp_test import ( "context" "encoding/json" + "fmt" "os" "path/filepath" "testing" @@ -10,7 +11,6 @@ import ( "github.com/databricks/cli/libs/aitools/installer" "github.com/databricks/cli/libs/aitools/mcp" - "github.com/databricks/cli/libs/databrickscfg/profile" "github.com/databricks/cli/libs/env" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -132,19 +132,37 @@ func TestSaveStateUpgradesV2FileToV3(t *testing.T) { // stateTestProfiles is the profiles file used by the drift tests. The profiles named // spog-query, spog-none, spog-both and spog-abc name their workspace in the host query instead of // workspace_id. -func stateTestProfiles() profile.InMemoryProfiler { - return profile.InMemoryProfiler{Profiles: profile.Profiles{ - {Name: bindingTestProfile, Host: bindingTestHost, WorkspaceID: stateTestWorkspaceID}, - {Name: stateTestOtherProfile, Host: stateTestOtherHost, WorkspaceID: "456"}, - {Name: "spog-query", Host: "https://spog.test/?o=222"}, - {Name: "spog-none", Host: "https://spog.test/?w=none"}, - {Name: "spog-both", Host: "https://spog.test/?o=111&w=222"}, - {Name: "spog-abc", Host: "https://spog.test/?o=abc&w=222"}, - }} +// stateTestWriteProfiles writes the profiles that the Reconcile tests check to the default config file +// of the test home directory. +func stateTestWriteProfiles(t *testing.T, ctx context.Context) { + t.Helper() + home, err := env.UserHomeDir(ctx) + require.NoError(t, err) + writeStateFile(t, filepath.Join(home, ".databrickscfg"), fmt.Sprintf(`[%s] +host = %s +workspace_id = %s + +[%s] +host = %s +workspace_id = 456 + +[spog-query] +host = https://spog.test/?o=222 + +[spog-none] +host = https://spog.test/?w=none + +[spog-both] +host = https://spog.test/?o=111&w=222 + +[spog-abc] +host = https://spog.test/?o=abc&w=222 +`, bindingTestProfile, bindingTestHost, stateTestWorkspaceID, stateTestOtherProfile, stateTestOtherHost)) } func TestReconcile(t *testing.T) { ctx, _ := stateTestCtx(t) + stateTestWriteProfiles(t, ctx) tests := []struct { name string entry mcp.Entry @@ -218,7 +236,7 @@ func TestReconcile(t *testing.T) { t.Run(tt.name, func(t *testing.T) { state := &mcp.State{Servers: []mcp.Entry{tt.entry}} - drifts, err := mcp.Reconcile(ctx, stateTestProfiles(), state) + drifts, err := mcp.Reconcile(ctx, state) require.NoError(t, err) if tt.wantKind == "" { assert.Empty(t, drifts) @@ -240,7 +258,7 @@ func TestReconcileWithoutConfigFileReportsEveryProfileMissing(t *testing.T) { entry := stateTestEntry() state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) + drifts, err := mcp.Reconcile(ctx, state) require.NoError(t, err) assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) } @@ -254,7 +272,7 @@ func TestReconcileChecksEntryAgainstPinnedConfigFile(t *testing.T) { entry.ConfigFile = pinned state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) + drifts, err := mcp.Reconcile(ctx, state) require.NoError(t, err) assert.Empty(t, drifts) } @@ -270,7 +288,7 @@ func TestReconcileUnpinnedEntryChecksDefaultConfigFile(t *testing.T) { ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", ambient) state := &mcp.State{Servers: []mcp.Entry{stateTestEntry()}} - drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) + drifts, err := mcp.Reconcile(ctx, state) require.NoError(t, err) assert.Equal(t, []mcp.Drift{{ Kind: mcp.DriftHostChanged, @@ -288,6 +306,18 @@ func TestNormalizeHostIgnoresSchemeCaseAndTrailingSlash(t *testing.T) { // TestReconcileProfileWithoutHostIsMissing checks that a profile section with no host is reported as // missing, since it names no host to compare. It reads a real file, because the in-memory profiler // does not model sections without a host. +func TestReconcileProfileWithEmptyHostIsMissing(t *testing.T) { + ctx, _ := stateTestCtx(t) + path := bindingTestWriteConfig(t, "[logfood]\nhost =\nauth_type = pat\ntoken = dapi-profile-token\n") + ctx = env.Set(ctx, "DATABRICKS_CONFIG_FILE", path) + entry := stateTestEntry() + state := &mcp.State{Servers: []mcp.Entry{entry}} + + drifts, err := mcp.Reconcile(ctx, state) + require.NoError(t, err) + assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) +} + func TestReconcileProfileWithoutHostIsMissing(t *testing.T) { ctx, _ := stateTestCtx(t) path := bindingTestWriteConfig(t, "[logfood]\nauth_type = pat\ntoken = dapi-profile-token\n") @@ -295,7 +325,7 @@ func TestReconcileProfileWithoutHostIsMissing(t *testing.T) { entry := stateTestEntry() state := &mcp.State{Servers: []mcp.Entry{entry}} - drifts, err := mcp.Reconcile(ctx, profile.FileProfilerImpl{}, state) + drifts, err := mcp.Reconcile(ctx, state) require.NoError(t, err) assert.Equal(t, []mcp.Drift{{Kind: mcp.DriftProfileMissing, Entry: entry}}, drifts) } @@ -309,12 +339,12 @@ func TestReconcileReportsEntryErrorAndKeepsOthers(t *testing.T) { unresolvable.Name = "github-home" unresolvable.ConfigFile = "~/.databrickscfg" changed := stateTestEntryWith(bindingTestProfile, "https://adb-999.test", stateTestWorkspaceID) - changed.ConfigFile = filepath.Join(t.TempDir(), ".databrickscfg") + changed.ConfigFile = bindingTestWriteConfig(t, fmt.Sprintf("[%s]\nhost = %s\nworkspace_id = %s\nauth_type = pat\ntoken = %s\n", bindingTestProfile, bindingTestHost, stateTestWorkspaceID, bindingTestToken)) state := &mcp.State{Servers: []mcp.Entry{unresolvable, changed}} - drifts, err := mcp.Reconcile(ctx, stateTestProfiles(), state) - require.Error(t, err) - assert.Contains(t, err.Error(), "MCP server github-home: cannot find homedir: $HOME is not set") + drifts, err := mcp.Reconcile(ctx, state) + require.ErrorIs(t, err, env.ErrNoHomeEnv) + assert.Contains(t, err.Error(), "MCP server github-home: ") assert.Equal(t, []mcp.Drift{{ Kind: mcp.DriftHostChanged, Entry: changed, From ce1e19a7e0e18330348f4058bf0f47a393b6bb0d Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 12 Nov 2026 17:25:06 +0000 Subject: [PATCH 6/7] Compare config-file paths with forward slashes in binding tests The two config-file mismatch tests built the expected message from the native resolved path, while the message prints it with forward slashes, so they failed on Windows. Co-authored-by: Isaac --- libs/aitools/mcp/binding_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 418738aa67e..7e0220c0165 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -493,7 +493,7 @@ func TestBindUnpinnedEntryFailsForOtherConfigFile(t *testing.T) { Stderr: stderr, }) requireBindError(t, err, mcp.BindProfileHostChanged) - assert.Contains(t, err.Error(), "was set up with config file "+bindingTestResolved(t, defaultPath)) + assert.Contains(t, err.Error(), "was set up with config file "+filepath.ToSlash(bindingTestResolved(t, defaultPath))) assert.Empty(t, stderr.String()) } @@ -719,7 +719,7 @@ func TestBindEntryInOtherConfigFileFailsClosed(t *testing.T) { Stderr: stderr, }) requireBindError(t, err, mcp.BindProfileHostChanged) - assert.Contains(t, err.Error(), "was set up with config file "+bindingTestResolved(t, recordedPath)) + assert.Contains(t, err.Error(), "was set up with config file "+filepath.ToSlash(bindingTestResolved(t, recordedPath))) assert.Empty(t, stderr.String()) } From 03c01a081bbcbcca2c817a594da66a126654b9a3 Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Fri, 13 Nov 2026 14:46:09 +0000 Subject: [PATCH 7/7] Adapt MCP state changes to main's installer API Main renamed UninstallSkillsOpts to UninstallSkillsWithResult, which returns the result as well, and replaced the schema v2 constant with the current schema version. Return the new signature, use the current schema version in the test fixtures, and restore the state path that readState needs. Co-authored-by: Isaac --- libs/aitools/installer/plugin_test.go | 6 +++--- libs/aitools/installer/state.go | 3 ++- libs/aitools/installer/uninstall.go | 4 ++-- libs/aitools/installer/uninstall_test.go | 14 ++++++++------ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/libs/aitools/installer/plugin_test.go b/libs/aitools/installer/plugin_test.go index 2b61b6e4e8a..09120d406fe 100644 --- a/libs/aitools/installer/plugin_test.go +++ b/libs/aitools/installer/plugin_test.go @@ -596,7 +596,7 @@ func TestUpdateInstalledPluginsRecordsUnknownVersionWhenRefreshFails(t *testing. dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v" + tc.recorded, Plugins: map[string]PluginRecord{ agents.NameClaudeCode: {Marketplace: "claude-plugins-official", Plugin: "databricks", Scope: "user", Version: tc.recorded}, @@ -729,7 +729,7 @@ func saveUnknownAgentPlugin(t *testing.T) (context.Context, string) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.6", Plugins: map[string]PluginRecord{ "future-agent": {Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user", Version: "0.2.6"}, @@ -748,7 +748,7 @@ func saveCopilotPlugin(t *testing.T) (context.Context, string) { dir, err := GlobalSkillsDir(ctx) require.NoError(t, err) require.NoError(t, SaveState(dir, &InstallState{ - SchemaVersion: schemaVersionV2, + SchemaVersion: CurrentSchemaVersion, Release: "v0.2.6", Plugins: map[string]PluginRecord{ agents.NameCopilot: {Marketplace: "databricks-agent-skills", Plugin: "databricks", Scope: "user", Version: "0.2.6"}, diff --git a/libs/aitools/installer/state.go b/libs/aitools/installer/state.go index 60563afb54f..f44c33921d4 100644 --- a/libs/aitools/installer/state.go +++ b/libs/aitools/installer/state.go @@ -158,7 +158,8 @@ func LoadMCPState(dir string) (*MCPState, error) { // readState reads the state file in the given directory, whatever it holds. It returns (nil, nil) when // the file does not exist. func readState(dir string) (*InstallState, error) { - data, err := os.ReadFile(filepath.Join(dir, stateFileName)) + path := filepath.Join(dir, stateFileName) + data, err := os.ReadFile(path) if errors.Is(err, os.ErrNotExist) { return nil, nil } diff --git a/libs/aitools/installer/uninstall.go b/libs/aitools/installer/uninstall.go index 819f3b565d7..ffa36c9b769 100644 --- a/libs/aitools/installer/uninstall.go +++ b/libs/aitools/installer/uninstall.go @@ -202,11 +202,11 @@ func UninstallSkillsWithResult(ctx context.Context, opts UninstallOptions) (*Uni if len(state.Skills) == 0 && len(state.Plugins) == 0 { cleanOrphanedSymlinks(ctx, baseDir, scope, cwd) if err := removeState(baseDir); err != nil { - return err + return nil, err } } else { if err := SaveState(baseDir, state); err != nil { - return err + return nil, err } } diff --git a/libs/aitools/installer/uninstall_test.go b/libs/aitools/installer/uninstall_test.go index cba9f61155e..1413cde08c2 100644 --- a/libs/aitools/installer/uninstall_test.go +++ b/libs/aitools/installer/uninstall_test.go @@ -488,7 +488,7 @@ func TestUninstallCountsRemovedSkills(t *testing.T) { // so the skill counts as removed. globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") require.NoError(t, os.RemoveAll(filepath.Join(globalDir, "databricks-sql"))) - require.NoError(t, SaveState(globalDir, &InstallState{SchemaVersion: schemaVersionV2, Skills: map[string]string{"databricks-sql": "0.1.0"}})) + require.NoError(t, SaveState(globalDir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: map[string]string{"databricks-sql": "0.1.0"}})) result, err = UninstallSkillsWithResult(ctx, UninstallOptions{}) require.NoError(t, err) assert.Equal(t, 1, result.SkillsChanged) @@ -574,7 +574,7 @@ func TestUninstallSkillFailuresAreSorted(t *testing.T) { require.NoError(t, os.MkdirAll(filepath.Join(globalDir, name), 0o755)) skills[name] = "0.1.0" } - require.NoError(t, SaveState(globalDir, &InstallState{SchemaVersion: schemaVersionV2, Skills: skills})) + require.NoError(t, SaveState(globalDir, &InstallState{SchemaVersion: CurrentSchemaVersion, Skills: skills})) orig := removeAllFn t.Cleanup(func() { removeAllFn = orig }) @@ -619,7 +619,8 @@ func TestUninstallKeepsStateFileWithMCPRecords(t *testing.T) { require.NoError(t, SaveMCPState(globalDir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) ctx := cmdio.MockDiscard(t.Context()) - require.NoError(t, UninstallSkills(ctx)) + _, err := UninstallSkillsWithResult(ctx, UninstallOptions{}) + require.NoError(t, err) // The skills are gone, but the MCP records stay in the file. loaded, err := LoadState(globalDir) @@ -637,7 +638,7 @@ func TestUninstallMCPOnlyStateReturnsError(t *testing.T) { globalDir := filepath.Join(tmp, ".databricks", "aitools", "skills") require.NoError(t, SaveMCPState(globalDir, &MCPState{Servers: []MCPEntry{{Name: "github-logfood"}}})) - err := UninstallSkills(cmdio.MockDiscard(t.Context())) + _, err := UninstallSkillsWithResult(cmdio.MockDiscard(t.Context()), UninstallOptions{}) require.Error(t, err) assert.Contains(t, err.Error(), "no skills or plugins installed") @@ -658,8 +659,9 @@ func TestUninstallRemovesLeftoverFilesWithoutSkills(t *testing.T) { Files: map[string]FileRecord{"databricks-sql/SKILL.md": {SHA256: "abc"}}, })) - require.NoError(t, UninstallSkills(cmdio.MockDiscard(t.Context()))) + _, err := UninstallSkillsWithResult(cmdio.MockDiscard(t.Context()), UninstallOptions{}) + require.NoError(t, err) - _, err := os.Stat(filepath.Join(globalDir, ".state.json")) + _, err = os.Stat(filepath.Join(globalDir, ".state.json")) assert.ErrorIs(t, err, fs.ErrNotExist) }