From ad9ad88ab83d4352234b9bfeeb047d54424a858e Mon Sep 17 00:00:00 2001 From: Simon Faltum Date: Thu, 19 Nov 2026 11:08:00 +0000 Subject: [PATCH] Add aitools mcp serve, the stdio proxy for managed MCP servers One commit for the restack onto main. The individual commits, up to 029c1beb0fe7b6536f7c419677dbc6dae250c9ab, are in the pull request's history. Co-authored-by: Isaac --- .../mcp/serve-binding-errors/.databrickscfg | 3 + .../mcp/serve-binding-errors/out.test.toml | 1 + .../mcp/serve-binding-errors/output.txt | 41 + .../mcp/serve-binding-errors/requests.ndjson | 4 + .../aitools/mcp/serve-binding-errors/script | 55 + .../mcp/serve-passthrough/.databrickscfg | 3 + .../mcp/serve-passthrough/out.test.toml | 1 + .../aitools/mcp/serve-passthrough/output.txt | 32 + .../mcp/serve-passthrough/requests.ndjson | 4 + .../cmd/aitools/mcp/serve-passthrough/script | 31 + cmd/aitools/mcp.go | 1 + cmd/aitools/mcp_add.go | 2 +- cmd/aitools/mcp_add_test.go | 22 + cmd/aitools/mcp_serve.go | 507 +++++++ cmd/aitools/mcp_serve_test.go | 1192 +++++++++++++++++ cmd/aitools/mcp_test.go | 2 +- cmd/root/logger.go | 28 +- cmd/root/root.go | 31 +- cmd/root/root_test.go | 61 + cmd/root/silent_err.go | 23 + cmd/root/silent_err_test.go | 40 + cmd/root/stale_version.go | 11 +- cmd/root/stale_version_test.go | 15 +- cmd/root/stdout_protocol.go | 51 + cmd/root/stdout_protocol_other.go | 10 + cmd/root/stdout_protocol_test.go | 220 +++ cmd/root/stdout_protocol_windows.go | 10 + libs/aitools/mcp/binding.go | 62 +- libs/aitools/mcp/binding_test.go | 56 +- libs/aitools/mcp/proxy/bridge.go | 10 +- libs/aitools/mcp/proxy/bridge_test.go | 26 + libs/aitools/mcp/proxy/run.go | 68 + libs/aitools/mcp/proxy/run_test.go | 91 ++ libs/testserver/handlers.go | 4 + libs/testserver/mcp_endpoint.go | 133 ++ libs/testserver/mcp_endpoint_test.go | 96 ++ main.go | 3 +- 37 files changed, 2912 insertions(+), 38 deletions(-) create mode 100644 acceptance/cmd/aitools/mcp/serve-binding-errors/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/serve-binding-errors/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/serve-binding-errors/output.txt create mode 100644 acceptance/cmd/aitools/mcp/serve-binding-errors/requests.ndjson create mode 100644 acceptance/cmd/aitools/mcp/serve-binding-errors/script create mode 100644 acceptance/cmd/aitools/mcp/serve-passthrough/.databrickscfg create mode 100644 acceptance/cmd/aitools/mcp/serve-passthrough/out.test.toml create mode 100644 acceptance/cmd/aitools/mcp/serve-passthrough/output.txt create mode 100644 acceptance/cmd/aitools/mcp/serve-passthrough/requests.ndjson create mode 100644 acceptance/cmd/aitools/mcp/serve-passthrough/script create mode 100644 cmd/aitools/mcp_serve.go create mode 100644 cmd/aitools/mcp_serve_test.go create mode 100644 cmd/root/silent_err_test.go create mode 100644 cmd/root/stdout_protocol.go create mode 100644 cmd/root/stdout_protocol_other.go create mode 100644 cmd/root/stdout_protocol_test.go create mode 100644 cmd/root/stdout_protocol_windows.go create mode 100644 libs/aitools/mcp/proxy/run.go create mode 100644 libs/aitools/mcp/proxy/run_test.go create mode 100644 libs/testserver/mcp_endpoint.go create mode 100644 libs/testserver/mcp_endpoint_test.go diff --git a/acceptance/cmd/aitools/mcp/serve-binding-errors/.databrickscfg b/acceptance/cmd/aitools/mcp/serve-binding-errors/.databrickscfg new file mode 100644 index 00000000000..8d80c7ca7da --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-binding-errors/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME diff --git a/acceptance/cmd/aitools/mcp/serve-binding-errors/out.test.toml b/acceptance/cmd/aitools/mcp/serve-binding-errors/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-binding-errors/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/serve-binding-errors/output.txt b/acceptance/cmd/aitools/mcp/serve-binding-errors/output.txt new file mode 100644 index 00000000000..9abc38dc719 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-binding-errors/output.txt @@ -0,0 +1,41 @@ + +=== The bound profile is not in the config file + +>>> errcode [CLI] aitools mcp serve --profile gone --service system.ai.genie_one_mcp +Error: MCP server system.ai.genie_one_mcp needs Databricks profile "gone", which does not exist in [TEST_TMP_DIR]/.databrickscfg. Set it up with: databricks auth login --profile gone + +Exit code: 2 +stdout bytes: 0 + +=== The profile now points at another host than the one recorded + +>>> errcode [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp +Error: MCP server genie_one_mcp-logfood was set up for https://other.test, but profile "logfood" now points at [DATABRICKS_URL]. Run: databricks aitools mcp remove --names genie_one_mcp-logfood and add it again + +Exit code: 2 +stdout bytes: 0 + +=== Flags are required and validated before the binding checks + +>>> errcode [CLI] aitools mcp serve --service system.ai.genie_one_mcp +Error: --profile is required + +Exit code: 1 + +>>> errcode [CLI] aitools mcp serve --profile logfood --service github +Error: --service needs a full service name such as system.ai.github + +Exit code: 1 + +>>> errcode [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --output json +Error: --output does not apply to serve: its stdout carries the MCP protocol + +Exit code: 1 + +=== A log file on stdout is refused before anything is logged + +>>> errcode [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --log-file stdout --log-level debug +Error: databricks aitools mcp serve writes protocol messages to stdout, so its logs cannot go to stdout; use --log-file stderr or a file + +Exit code: 1 +stdout bytes: 0 diff --git a/acceptance/cmd/aitools/mcp/serve-binding-errors/requests.ndjson b/acceptance/cmd/aitools/mcp/serve-binding-errors/requests.ndjson new file mode 100644 index 00000000000..e1176541638 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-binding-errors/requests.ndjson @@ -0,0 +1,4 @@ +{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test-client","version":"0.0.1"}}} +{"jsonrpc":"2.0","method":"notifications/initialized"} +{"jsonrpc":"2.0","id":2,"method":"tools/list"} +{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{"text":"hello"}}} diff --git a/acceptance/cmd/aitools/mcp/serve-binding-errors/script b/acceptance/cmd/aitools/mcp/serve-binding-errors/script new file mode 100644 index 00000000000..d4ff2c2bbfd --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-binding-errors/script @@ -0,0 +1,55 @@ +# Isolate HOME so the state file written below lands in this test directory. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +# The state records the config file as the binary spells it. Git Bash's $PWD is /c/..., which the Windows binary does +# not match, so there use pwd -W, as sethome does. +case "$OSTYPE" in + msys*|cygwin*) cfgdir="$(pwd -W)" ;; + *) cfgdir="$PWD" ;; +esac + +# Binding failures exit 2 with one message on stderr. Nothing reaches stdout, and stdin is not read. +title "The bound profile is not in the config file" +trace errcode $CLI aitools mcp serve --profile gone --service system.ai.genie_one_mcp < requests.ndjson > LOG.stdout +echo "stdout bytes: $(wc -c < LOG.stdout | tr -d ' ')" + +# A state record for logfood that points at another host: the proxy never follows the profile. +mkdir -p home/.databricks/aitools/skills +cat > home/.databricks/aitools/skills/.state.json < LOG.stdout +echo "stdout bytes: $(wc -c < LOG.stdout | tr -d ' ')" + +title "Flags are required and validated before the binding checks" +trace errcode $CLI aitools mcp serve --service system.ai.genie_one_mcp < requests.ndjson > LOG.stdout +trace errcode $CLI aitools mcp serve --profile logfood --service github < requests.ndjson > LOG.stdout +trace errcode $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --output json < requests.ndjson > LOG.stdout + +# Logs and the stale-version warning never share stdout with the protocol, even at debug level. +title "A log file on stdout is refused before anything is logged" +trace errcode $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --log-file stdout --log-level debug < requests.ndjson > LOG.stdout +echo "stdout bytes: $(wc -c < LOG.stdout | tr -d ' ')" diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/.databrickscfg b/acceptance/cmd/aitools/mcp/serve-passthrough/.databrickscfg new file mode 100644 index 00000000000..8d80c7ca7da --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood-$UNIQUE_NAME diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/out.test.toml b/acceptance/cmd/aitools/mcp/serve-passthrough/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/output.txt b/acceptance/cmd/aitools/mcp/serve-passthrough/output.txt new file mode 100644 index 00000000000..963e3adc19f --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/output.txt @@ -0,0 +1,32 @@ + +=== Stdout holds one JSON-RPC message per line; the notification gets no reply +{"id":1,"instructions":"Fake Databricks MCP service for tests.\n\nThis server is bound to Databricks workspace [DATABRICKS_URL] (CLI profile \"logfood\"). Its tools act on that workspace only.","tools":[],"text":null,"error":null} +{"id":2,"instructions":null,"tools":["echo"],"text":null,"error":null} +{"id":3,"instructions":null,"tools":[],"text":"hello","error":null} + +=== Diagnostics go to stderr only + +>>> cat LOG.stderr + +>>> [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code +No install record for system.ai.genie_one_mcp on profile "logfood"; using its current host [DATABRICKS_URL] + +=== An agent this binary does not know is accepted +{"id":1,"instructions":"Fake Databricks MCP service for tests.\n\nThis server is bound to Databricks workspace [DATABRICKS_URL] (CLI profile \"logfood\"). Its tools act on that workspace only.","tools":[],"text":null,"error":null} +{"id":2,"instructions":null,"tools":["echo"],"text":null,"error":null} +{"id":3,"instructions":null,"tools":[],"text":"hello","error":null} + +>>> cat LOG.future + +>>> [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent future-agent +No install record for system.ai.genie_one_mcp on profile "logfood"; using its current host [DATABRICKS_URL] +unknown agent "future-agent"; it is left out of the User-Agent + +=== Debug level logs one line per upstream request + +>>> [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code --log-file LOG.debug --log-level debug +No install record for system.ai.genie_one_mcp on profile "logfood"; using its current host [DATABRICKS_URL] +upstream request http_method=POST rpc_method=initialize rpc_id=1 status=200 duration=DUR session_sent=false session_received=false session_replaced=false +upstream request http_method=POST rpc_method=notifications/initialized rpc_id= status=202 duration=DUR session_sent=false session_received=false session_replaced=false +upstream request http_method=POST rpc_method=tools/call rpc_id=3 status=200 duration=DUR session_sent=false session_received=false session_replaced=false +upstream request http_method=POST rpc_method=tools/list rpc_id=2 status=200 duration=DUR session_sent=false session_received=false session_replaced=false diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/requests.ndjson b/acceptance/cmd/aitools/mcp/serve-passthrough/requests.ndjson new file mode 100644 index 00000000000..e1176541638 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/requests.ndjson @@ -0,0 +1,4 @@ +{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"test-client","version":"0.0.1"}}} +{"jsonrpc":"2.0","method":"notifications/initialized"} +{"jsonrpc":"2.0","id":2,"method":"tools/list"} +{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"echo","arguments":{"text":"hello"}}} diff --git a/acceptance/cmd/aitools/mcp/serve-passthrough/script b/acceptance/cmd/aitools/mcp/serve-passthrough/script new file mode 100644 index 00000000000..381b1e2a1bf --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/script @@ -0,0 +1,31 @@ +# Isolate HOME so the proxy finds no state file and no agent config, as on a fresh machine. +sethome home +envsubst < .databrickscfg > out && mv out .databrickscfg +export DATABRICKS_CONFIG_FILE=.databrickscfg + +# DATABRICKS_HOST points elsewhere; the proxy must still talk to the profile's host. +export DATABRICKS_HOST=https://elsewhere.test + +# The service the proxy serves, created in the fake workspace. The fake answers 404 for a service it does not have. +MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood + +# Each stdout line must be one complete JSON value, so a pretty-printed or doubled-up message fails the filter. Responses +# may arrive in any order, so they are sorted by id. +frames='split("\n") | map(select(length > 0) | fromjson) | sort_by(.id)[] | {id, instructions: .result.instructions, tools: [.result.tools[]?.name], text: .result.content[0].text, error}' + +title "Stdout holds one JSON-RPC message per line; the notification gets no reply" +trace $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code < requests.ndjson 2> LOG.stderr | jq -Rsc "$frames" + +title "Diagnostics go to stderr only" +trace cat LOG.stderr + +# An agent this binary does not know still serves; it is left out of the User-Agent, with one line on stderr. +title "An agent this binary does not know is accepted" +trace $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent future-agent < requests.ndjson 2> LOG.future | jq -Rsc "$frames" +trace cat LOG.future + +# At debug level each upstream request is logged once: method, id, status, session flags. The timing is masked, and no +# body or header value is in the line, so the tool argument and the session id stay out of the golden. +title "Debug level logs one line per upstream request" +trace $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code --log-file LOG.debug --log-level debug < requests.ndjson > /dev/null +grep 'Debug: upstream request' LOG.debug | sed -e 's/^.*Debug: //' -e 's/ pid=[0-9]*//' -e 's/duration=[^ ]*/duration=DUR/' | sort diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index 6142f715c46..e2db6d77a2d 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -42,6 +42,7 @@ Use 'add' to install them into coding agents, bound to a Databricks profile.`, cmd.AddCommand(NewMcpAddCmd()) cmd.AddCommand(NewMcpListCmd()) cmd.AddCommand(NewMcpRemoveCmd()) + cmd.AddCommand(NewMcpServeCmd()) return cmd } diff --git a/cmd/aitools/mcp_add.go b/cmd/aitools/mcp_add.go index 56fb5a3402a..266b0f7b12c 100644 --- a/cmd/aitools/mcp_add.go +++ b/cmd/aitools/mcp_add.go @@ -428,7 +428,7 @@ func profileClient(ctx context.Context, name, configFile string) (*databricks.Wo if err != nil { return nil, err } - if err := mcp.AuthenticateBound(ctx, cfg, name); err != nil { + if err := mcp.AuthenticateBound(ctx, cfg, name, ""); err != nil { return nil, err } return w, nil diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 8467ec3d2cb..932a22b1aec 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -1010,6 +1010,28 @@ func TestMcpAddRejectedTokenIsAuthFailed(t *testing.T) { assert.NoFileExists(t, filepath.Join(home, ".claude.json")) } +// TestMcpAddBindingFailureExitsOne checks that a binding failure is an ordinary failure of add: it exits 1 in text and +// in JSON mode. Exit 2 is serve's status for binding failures, which an agent restarts on. +func TestMcpAddBindingFailureExitsOne(t *testing.T) { + home := withHome(t) + withDatabricksBinary(t) + server := noRequestServer(t) + writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n") + require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{ + Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test", + }}})) + args := []string{"--profile", "logfood", "--names", "github", "--agents", "claude-code"} + + _, err := newAddTestCmd(t, args...) + require.Error(t, err) + assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err)) + assert.Equal(t, 1, root.ExitStatus(err)) + + _, err = newAddTestCmd(t, append(args, "--output", "json")...) + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 1, root.ExitStatus(err)) +} + // TestMcpAddRefusesBinaryNotNamedDatabricks checks that the binary must be databricks: an argv[0] with another name, not on PATH, is refused, not replaced. func TestMcpAddRefusesBinaryNotNamedDatabricks(t *testing.T) { home := withHome(t) diff --git a/cmd/aitools/mcp_serve.go b/cmd/aitools/mcp_serve.go new file mode 100644 index 00000000000..2636a0f3531 --- /dev/null +++ b/cmd/aitools/mcp_serve.go @@ -0,0 +1,507 @@ +package aitools + +import ( + "context" + "crypto/tls" + "encoding/json" + "errors" + "fmt" + "io" + "log/slog" + "net" + "net/http" + "os" + "os/signal" + "path/filepath" + "strings" + "sync" + "syscall" + "time" + + "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/aitools/agents" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/aitools/mcp/proxy" + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/auth/storage" + "github.com/databricks/cli/libs/auth/u2m" + "github.com/databricks/cli/libs/log" + "github.com/databricks/databricks-sdk-go/config" + "github.com/databricks/databricks-sdk-go/httpclient" + "github.com/spf13/cobra" + "golang.org/x/oauth2" +) + +// serveConnectTimeout bounds one upstream connect. http.DefaultTransport waits 30 s. +const serveConnectTimeout = 10 * time.Second + +// serveTransport is the transport of the bridge's upstream requests. It starts from +// http.DefaultTransport, so proxy use and the other timeouts stay as they are, and replaces the +// dialer to set the connect timeout. The profile's InsecureSkipVerify is honored; the clone keeps +// HTTP/2 enabled. +func serveTransport(cfg *config.Config) *http.Transport { + tr := http.DefaultTransport.(*http.Transport).Clone() + tr.DialContext = (&net.Dialer{Timeout: serveConnectTimeout}).DialContext + if cfg.InsecureSkipVerify { + if tr.TLSClientConfig == nil { + tr.TLSClientConfig = &tls.Config{} + } + tr.TLSClientConfig.InsecureSkipVerify = true + } + return tr +} + +// sessionHeader is the MCP session header. The proxy package keeps its copy unexported. +const sessionHeader = "Mcp-Session-Id" + +// upstreamLog is the transport of the bridge's upstream requests when serve runs at debug level. It logs one debug line +// per request, with the JSON-RPC method and id from the body, the HTTP status, the time to the response headers, and +// whether a session id was sent, received, or replaced. It logs no body and no header value, so no token or session id +// reaches the log. +type upstreamLog struct { + next http.RoundTripper + + mu sync.Mutex + session string // the last session id the server sent, "" before the first one +} + +func newUpstreamLog(next http.RoundTripper) *upstreamLog { + return &upstreamLog{next: next} +} + +// RoundTrip sends req with next and logs the exchange. Below debug level it only forwards the request. +func (u *upstreamLog) RoundTrip(req *http.Request) (*http.Response, error) { + ctx := req.Context() + logger := log.GetLogger(ctx) + if !logger.Enabled(ctx, log.LevelDebug) { + return u.next.RoundTrip(req) + } + rpcMethod, rpcID := jsonRPCEnvelope(req) + start := time.Now() + resp, err := u.next.RoundTrip(req) + sent := req.Header.Get(sessionHeader) != "" + received := "" + status := 0 + if resp != nil { + received = resp.Header.Get(sessionHeader) + status = resp.StatusCode + } + replaced := u.observeSession(received) + attrs := []slog.Attr{ + slog.String("http_method", req.Method), + slog.String("rpc_method", rpcMethod), + slog.String("rpc_id", rpcID), + slog.Int("status", status), + slog.Duration("duration", time.Since(start)), + slog.Bool("session_sent", sent), + slog.Bool("session_received", received != ""), + slog.Bool("session_replaced", replaced), + } + if err != nil { + attrs = append(attrs, slog.String("error", err.Error())) + } + logger.LogAttrs(ctx, slog.LevelDebug, "upstream request", attrs...) + return resp, err +} + +// observeSession records the session id that a response carried and reports whether it replaced the one seen before. +// An empty id is no session and changes nothing. +func (u *upstreamLog) observeSession(received string) bool { + if received == "" { + return false + } + u.mu.Lock() + defer u.mu.Unlock() + replaced := u.session != "" && received != u.session + u.session = received + return replaced +} + +// jsonRPCEnvelope returns the method and the raw id of the JSON-RPC message in req's body. It returns empty strings +// when there is no body, as for the session DELETE, and when the body cannot be read or decoded, since a debug line +// should not fail the request. The body is read through GetBody, so the request itself is unchanged. +func jsonRPCEnvelope(req *http.Request) (method, id string) { + if req.GetBody == nil { + return "", "" + } + body, err := req.GetBody() + if err != nil { + return "", "" + } + defer body.Close() + var msg struct { + Method string `json:"method"` + ID json.RawMessage `json:"id"` + } + if err := json.NewDecoder(body).Decode(&msg); err != nil { + return "", "" + } + return msg.Method, string(msg.ID) +} + +// signInRedirect is the outermost transport of the bridge's upstream requests. The MCP endpoint answers a token that it does +// not accept with a 3xx redirect to sign-in, and the bridge does not follow it. Such a response fails its request with the +// login command for the profile, as a rejected credential does. Serve keeps running, and later requests are still sent. The +// Location is not read, since its query can carry state. +type signInRedirect struct { + next http.RoundTripper + profile string + configFile string // the config file the profile is pinned to, or "" when the login's default file is used +} + +// RoundTrip sends req with next. A 3xx response becomes the login error; any other response, or a transport error, is +// returned as it is. +func (s *signInRedirect) RoundTrip(req *http.Request) (*http.Response, error) { + resp, err := s.next.RoundTrip(req) + if err != nil || resp.StatusCode/100 != 3 { + return resp, err + } + resp.Body.Close() + return nil, s.loginError(resp.StatusCode) +} + +// loginError is the error for a redirect to sign-in. The login command names DATABRICKS_CONFIG_FILE when the profile is +// pinned to a config file, since the login must write that file. +func (s *signInRedirect) loginError(status int) error { + return fmt.Errorf("the MCP endpoint redirected to sign-in (HTTP %d), so it did not accept the token for Databricks profile \"%s\"\nRun: %s\nThen restart the MCP server in your agent", status, s.profile, mcp.LoginCommand(s.profile, s.configFile)) +} + +type mcpServeFlags struct { + service string + agent string +} + +// NewMcpServeCmd returns the hidden `aitools mcp serve`: the stdio MCP proxy an agent starts for one +// installed server. Its stdout carries only JSON-RPC messages; every diagnostic goes to stderr. +func NewMcpServeCmd() *cobra.Command { + var f mcpServeFlags + cmd := &cobra.Command{ + Use: "serve", + Short: "Serve one Databricks managed MCP server over stdio", + Hidden: true, + Args: cobra.NoArgs, + // Stdout carries the MCP protocol, so root keeps logs and the stale-version warning off it. + Annotations: map[string]string{root.StdoutCarriesProtocol: "true"}, + RunE: func(cmd *cobra.Command, args []string) error { + return runMcpServe(cmd, f) + }, + } + // The names are the ones `aitools mcp add` writes into each agent's config, so an entry binds what add wrote. + cmd.Flags().StringVar(&f.service, strings.TrimPrefix(mcpconfig.ServiceFlag, "--"), "", + "Full name of the MCP service, for example system.ai.github") + cmd.Flags().StringVar(&f.agent, strings.TrimPrefix(mcpconfig.AgentFlag, "--"), "", + "Registry name of the calling agent, used only for the User-Agent") + return cmd +} + +// runMcpServe runs the binding checks, then bridges stdio to the service until stdin closes or a +// signal arrives. +func runMcpServe(cmd *cobra.Command, f mcpServeFlags) error { + if cmd.Flag("output").Changed { + return errors.New("--output does not apply to serve: its stdout carries the MCP protocol") + } + profileName := profileFlag(cmd) + if profileName == "" { + return fmt.Errorf("%s is required", mcpconfig.ProfileFlag) + } + if f.service == "" { + return fmt.Errorf("%s is required", mcpconfig.ServiceFlag) + } + if !isFullName(strings.Split(f.service, ".")) { + return fmt.Errorf("%s needs a full service name such as system.ai.github", mcpconfig.ServiceFlag) + } + + state, err := mcp.LoadState(cmd.Context()) + if err != nil { + return serveFailure(cmd, err, serveBindStatus) + } + cfg, entry, err := mcp.Bind(cmd.Context(), mcp.BindOptions{ + Profile: profileName, + Service: f.service, + State: state, + Stderr: cmd.ErrOrStderr(), + }) + if err != nil { + return serveBindFailure(cmd, profileName, err) + } + + configFile := pinnedConfigFile(entry, cfg) + authenticate := rebindingAuthenticate(bindingCheck(cmd.Context(), state, entry, f.service, profileName, cfg), cfg.Authenticate) + return serveProxy(cmd, proxy.Options{ + In: cmd.InOrStdin(), + Out: cmd.OutOrStdout(), + Err: cmd.ErrOrStderr(), + Endpoint: mcp.ServiceURL(cfg.Host, f.service), + Config: cfg, + Authenticate: authenticate, + Profile: profileName, + Agent: serveAgent(f.agent, cmd.ErrOrStderr()), + Transport: &signInRedirect{ + next: newUpstreamLog(serveTransport(cfg)), + profile: profileName, + configFile: configFile, + }, + }, configFile) +} + +// pinnedConfigFile returns the config file that entry pins its profile to, or "" when the entry has none. +func pinnedConfigFile(entry *mcp.Entry, cfg *config.Config) string { + if entry == nil || entry.ConfigFile == "" { + return "" + } + return filepath.ToSlash(cfg.ConfigFile) +} + +// bindingCheck returns the check that runs before each token is fetched. With an install record, entry, it checks the +// record. Without one, nothing records the host the profile was bound to, so it holds the profile to the host and +// workspace that cfg, which Bind resolved at start, names. +func bindingCheck(ctx context.Context, state *mcp.State, entry *mcp.Entry, service, profileName string, cfg *config.Config) func() error { + if entry == nil { + return func() error { + return mcp.CheckUnrecordedBinding(service, profileName, cfg.ConfigFile, cfg) + } + } + return func() error { + _, err := mcp.CheckBinding(ctx, state, service, profileName, cfg.ConfigFile) + return err + } +} + +// rebindingAuthenticate runs check before each token is fetched. A profile re-pointed after start fails the request +// here, before its token is fetched. The CLI token cache is keyed by profile name alone, so that token would otherwise +// reach the host serve is bound to. +func rebindingAuthenticate(check func() error, authenticate func(*http.Request) error) func(*http.Request) error { + return func(r *http.Request) error { + if err := checkBindingWithRetry(check); err != nil { + return err + } + return authenticate(r) + } +} + +// bindingRetryDelay is the pause before a changed binding is checked again. A write that replaces .databrickscfg +// truncates it first, so one read can find no profile. +const bindingRetryDelay = 50 * time.Millisecond + +// checkBindingWithRetry runs check. If the binding looks changed, it waits bindingRetryDelay and runs check once more, +// and returns that result. Other failures are returned at once. +func checkBindingWithRetry(check func() error) error { + err := check() + if !bindingChanged(err) { + return err + } + time.Sleep(bindingRetryDelay) + return check() +} + +// serveProxy bridges opts to the service until stdin closes or a signal arrives. Authenticate goes through a +// refreshGuard, so a rejected token or a changed binding stops the proxy; see proxyResult. +func serveProxy(cmd *cobra.Command, opts proxy.Options, configFile string) error { + ctx, stop := serveSignalContext(cmd.Context()) + defer stop() + refresh := &refreshGuard{authenticate: opts.Authenticate, profile: opts.Profile, configFile: configFile, cancel: stop} + opts.Authenticate = refresh.authenticateRequest + return proxyResult(cmd, refresh, proxy.Run(ctx, opts)) +} + +// proxyResult returns the outcome of serve once the bridge has returned with runErr. A rejected token wins, since a +// stdin error after it is a consequence. The final line always names the rejection, as the bridge may not have logged it. +func proxyResult(cmd *cobra.Command, refresh *refreshGuard, runErr error) error { + if refreshErr := refresh.failure(); refreshErr != nil { + fmt.Fprintf(cmd.ErrOrStderr(), "Error: stopped after %s for profile \"%s\"\n", stopReason(refreshErr), refresh.profile) + return exitAlreadyPrinted(refreshErr, serveBindStatus) + } + if runErr != nil { + return serveFailure(cmd, runErr, 1) + } + return nil +} + +// serveSignalContext returns a context that the first SIGINT or SIGTERM cancels. The handler is released +// before the context reports done, so a second signal during shutdown ends the process rather than being +// swallowed. stop releases the handler and cancels the context. +func serveSignalContext(parent context.Context) (context.Context, context.CancelFunc) { + ctx, cancel := context.WithCancel(parent) + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, os.Interrupt, syscall.SIGTERM) + go func() { + select { + case <-sigs: + signal.Stop(sigs) + cancel() + case <-ctx.Done(): + signal.Stop(sigs) + } + }() + return ctx, cancel +} + +// serveAgent returns the registry name of the calling agent for the User-Agent. A name this binary does +// not know, such as an agent added by a later release, is left out and reported on stderr, so the server +// still serves. The registry filter also keeps useragent.With from panicking on a value it does not accept. +func serveAgent(name string, stderr io.Writer) string { + if name == "" || agents.ByName(name) != nil { + return name + } + fmt.Fprintf(stderr, "unknown agent %q; it is left out of the User-Agent\n", name) + return "" +} + +// serveBindStatus is the exit status of a binding or auth failure. It is not 1, so an agent can tell a failed +// binding or sign-in from the other failures, which exit 1. +const serveBindStatus = 2 + +// serveFailure prints err to stderr as `Error: `, the form Execute uses for every other failure, and returns it +// with the exit status. Execute does not print it again, so the message is written exactly once. +func serveFailure(cmd *cobra.Command, err error, status int) error { + fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s\n", err) + return exitAlreadyPrinted(err, status) +} + +// serveBindFailure returns the error for a failed binding check at startup. A token failure is classified as it is at +// runtime (see credentialRejected). A rejection needs a new login, so it keeps the login hint and exits serveBindStatus. +// Any other token failure is temporary: it has no hint, and exits 1, the status of the other failures, since +// serveBindStatus says the credential was rejected. +// +// config.ErrCannotConfigureDefault is also a login failure. The SDK returns it when no strategy configures a credential, +// as for a profile with no auth_type and no cached token, and it drops each strategy's error. It is checked only here, +// since the provider is configured once at startup and the sentinel cannot come from a later request. +func serveBindFailure(cmd *cobra.Command, profileName string, err error) error { + bindErr, ok := errors.AsType[*mcp.BindError](err) + if !ok || bindErr.Category != mcp.BindAuthFailed || credentialRejected(bindErr.Unwrap()) || errors.Is(err, config.ErrCannotConfigureDefault) { + return serveFailure(cmd, err, serveBindStatus) + } + temporary := fmt.Errorf("could not get a token for Databricks profile \"%s\", a temporary failure: %w; try again", profileName, bindErr.Unwrap()) + return serveFailure(cmd, temporary, 1) +} + +// exitAlreadyPrinted returns err as an error that exits with status and that Execute does not print again. +func exitAlreadyPrinted(err error, status int) error { + return errors.Join(root.ErrAlreadyPrinted, &root.ExitStatusError{Status: status, Err: err}) +} + +// refreshGuard wraps the profile's authenticate for the bridge. A rejected token (see credentialRejected) fails that +// request with an error that names the profile and its login command, then stops the proxy, which exits 2. A changed +// binding (see rebindingAuthenticate) stops it the same way, with the error of the binding check. Later requests fail +// at once, without asking for a token again. A transient failure fails only its request, and the next request asks for +// a token again. A request whose own context ended does not stop it, nor does the session DELETE at shutdown, which is +// skipped after a failure. +type refreshGuard struct { + authenticate func(*http.Request) error + profile string + configFile string // the config file the profile is pinned to, or "" when the login's default file is used + cancel context.CancelFunc + + mu sync.Mutex + err error +} + +// authenticateRequest is the bridge's Authenticate. It is safe for concurrent use. +func (g *refreshGuard) authenticateRequest(r *http.Request) error { + if g.failure() != nil { + if r.Method == http.MethodDelete { + return proxy.ErrSkipRequest + } + return g.stoppedError() + } + err := g.authenticate(r) + if err == nil { + return nil + } + if r.Context().Err() != nil || r.Method == http.MethodDelete { + return err + } + if bindingChanged(err) { + // The check failed before any token was fetched, so the error says what changed and how to fix it. + return g.stop(err) + } + if !credentialRejected(err) { + // A failure that is not a rejection, such as a network blip, says nothing about the credential: a login would + // not fix it, and the next request asks for a token again, so the message names neither. + return fmt.Errorf("could not get a token for Databricks profile \"%s\": %w; the next request tries again", g.profile, err) + } + // The hint says to restart the server: a new spawn re-runs the binding checks, and it is the one that picks up the + // new login. + authErr := fmt.Errorf("%w\nThen restart the MCP server in your agent", mcp.AuthErrorIn(g.profile, g.configFile, err)) + return g.stop(authErr) +} + +// stop records err as the first failure of the guard and cancels the proxy, then returns err. A failure after the first +// returns the stopped error and does not cancel again. +func (g *refreshGuard) stop(err error) error { + g.mu.Lock() + first := g.err == nil + if first { + g.err = err + } + g.mu.Unlock() + if !first { + return g.stoppedError() + } + g.cancel() + return err +} + +// stoppedError is the error for a request that arrives after the failure. It does not repeat the login hint. +func (g *refreshGuard) stoppedError() error { + return fmt.Errorf("the session is stopping after %s for profile \"%s\"", stopReason(g.failure()), g.profile) +} + +// bindingChanged reports whether err is a binding check that failed because the profile no longer names the bound host +// and workspace, or no longer exists in the config file. +func bindingChanged(err error) bool { + bindErr, ok := errors.AsType[*mcp.BindError](err) + return ok && (bindErr.Category == mcp.BindProfileHostChanged || bindErr.Category == mcp.BindProfileNotFound) +} + +// stopReason says what stopped the proxy, for the messages that report it. +func stopReason(err error) string { + if bindingChanged(err) { + return "the binding changed" + } + return "a token refresh failure" +} + +// failure returns the first rejection, or nil while no token has been rejected. +func (g *refreshGuard) failure() error { + g.mu.Lock() + defer g.mu.Unlock() + return g.err +} + +// credentialRejected reports whether a token failure is one that a new login fixes. That is the CLI's U2M reporting an +// invalid refresh token, a missing refresh token or a missing cached token, or a token endpoint that answers 400 or 401 +// with the RFC 6749 section 5.2 error invalid_grant or invalid_client. Everything else is transient and returns false: +// network errors, timeouts, 5xx responses, and any error this check cannot classify. A transient failure is retried by +// the next request. +func credentialRejected(err error) bool { + if _, ok := errors.AsType[*u2m.InvalidRefreshTokenError](err); ok { + return true + } + // No refresh token, or no cached token at all (after an auth logout), is also fixed by a login. + if errors.Is(err, u2m.ErrMissingRefreshToken) || errors.Is(err, storage.ErrNotFound) { + return true + } + // The CLI's U2M client returns an *httpclient.HttpError for an error response, with the response body as its message. + if httpErr, ok := errors.AsType[*httpclient.HttpError](err); ok { + var body struct { + Error string `json:"error"` + } + if json.Unmarshal([]byte(httpErr.Message), &body) != nil { + return false + } + return rejectedOAuthError(httpErr.StatusCode, body.Error) + } + // golang.org/x/oauth2 returns this for an error response from its own HTTP client. It always sets Response. + if retrieveErr, ok := errors.AsType[*oauth2.RetrieveError](err); ok { + return rejectedOAuthError(retrieveErr.Response.StatusCode, retrieveErr.ErrorCode) + } + return false +} + +// rejectedOAuthError reports whether a token endpoint's error response, with its HTTP status and RFC 6749 error code, +// is a rejection. +func rejectedOAuthError(status int, code string) bool { + return (status == http.StatusBadRequest || status == http.StatusUnauthorized) && + (code == "invalid_grant" || code == "invalid_client") +} diff --git a/cmd/aitools/mcp_serve_test.go b/cmd/aitools/mcp_serve_test.go new file mode 100644 index 00000000000..2279a4a2f5e --- /dev/null +++ b/cmd/aitools/mcp_serve_test.go @@ -0,0 +1,1192 @@ +package aitools + +import ( + "bufio" + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log/slog" + "net/http" + "net/http/httptest" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/databricks/cli/cmd/root" + "github.com/databricks/cli/libs/aitools/installer" + "github.com/databricks/cli/libs/aitools/mcp" + "github.com/databricks/cli/libs/aitools/mcp/proxy" + "github.com/databricks/cli/libs/aitools/mcpconfig" + "github.com/databricks/cli/libs/auth/storage" + "github.com/databricks/cli/libs/auth/u2m" + "github.com/databricks/cli/libs/cmdio" + "github.com/databricks/cli/libs/log" + "github.com/databricks/databricks-sdk-go/config" + "github.com/databricks/databricks-sdk-go/httpclient" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "golang.org/x/oauth2" +) + +func TestMcpServeIsHidden(t *testing.T) { + assert.True(t, NewMcpServeCmd().Hidden) +} + +// TestServeParsesArgvThatAddWrites checks that the argv that `aitools mcp add` writes into an agent config parses with +// serve's flags. +func TestServeParsesArgvThatAddWrites(t *testing.T) { + parent := newMCPTestRoot() + serve, _, err := parent.Find([]string{"mcp", "serve"}) + require.NoError(t, err) + argv := []string{mcpconfig.ProfileFlag, "logfood", mcpconfig.ServiceFlag, "system.ai.github", mcpconfig.AgentFlag, "claude-code"} + require.NoError(t, serve.ParseFlags(argv)) + + profile, err := serve.Flags().GetString("profile") + require.NoError(t, err) + service, err := serve.Flags().GetString("service") + require.NoError(t, err) + agent, err := serve.Flags().GetString("agent") + require.NoError(t, err) + assert.Equal(t, []string{"logfood", "system.ai.github", "claude-code"}, []string{profile, service, agent}) +} + +func TestRefreshGuardPassesThroughWhileTokensRefresh(t *testing.T) { + cancelled := false + g := &refreshGuard{ + authenticate: func(*http.Request) error { return nil }, + profile: "logfood", + cancel: func() { cancelled = true }, + } + + require.NoError(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) + assert.NoError(t, g.failure()) + assert.False(t, cancelled) +} + +func TestRefreshGuardStopsProxyOnFirstFailure(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + calls := 0 + cause := refreshRejected + g := &refreshGuard{ + authenticate: func(*http.Request) error { + calls++ + return cause + }, + profile: "logfood", + cancel: cancel, + } + + err := g.authenticateRequest(&http.Request{Header: http.Header{}}) + require.Error(t, err) + assert.ErrorIs(t, err, cause) + assert.Contains(t, err.Error(), refreshLoginHint) + assert.Equal(t, context.Canceled, ctx.Err()) + assert.ErrorIs(t, g.failure(), cause) +} + +// TestRefreshGuardStopsLaterRequestsWithoutAskingAgain checks that after the first failure no request asks for a token +// again. Each later request fails at once with a short error that does not repeat the login hint. +func TestRefreshGuardStopsLaterRequestsWithoutAskingAgain(t *testing.T) { + calls := 0 + cause := refreshRejected + g := &refreshGuard{ + authenticate: func(*http.Request) error { + calls++ + return cause + }, + profile: "logfood", + cancel: func() {}, + } + require.Error(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) + + err := g.authenticateRequest(&http.Request{Header: http.Header{}}) + + assert.Equal(t, 1, calls) + assert.EqualError(t, err, "the session is stopping after a token refresh failure for profile \"logfood\"") + assert.ErrorIs(t, g.failure(), cause) +} + +// TestRefreshGuardStoresAuthError checks that the refresh failure reaches the user as the AUTH_FAILED error, which names +// the profile and its login command. +func TestRefreshGuardStoresAuthError(t *testing.T) { + cause := refreshRejected + g := &refreshGuard{ + authenticate: func(*http.Request) error { return cause }, + profile: "my profile", + cancel: func() {}, + } + + _ = g.authenticateRequest(&http.Request{Header: http.Header{}}) + + be, ok := errors.AsType[*mcp.BindError](g.failure()) + require.True(t, ok) + assert.Equal(t, mcp.BindAuthFailed, be.Category) + assert.ErrorIs(t, be, cause) + assert.Contains(t, be.Error(), "databricks auth login --profile 'my profile'") +} + +// TestRefreshGuardAuthErrorNamesPinnedConfigFile checks that the login command for a profile pinned to a config file +// writes that file, as the sign-in redirect's hint does. +func TestRefreshGuardAuthErrorNamesPinnedConfigFile(t *testing.T) { + g := &refreshGuard{ + authenticate: func(*http.Request) error { return refreshRejected }, + profile: "logfood", + configFile: "/work/.databrickscfg", + cancel: func() {}, + } + + _ = g.authenticateRequest(&http.Request{Header: http.Header{}}) + + assert.Contains(t, g.failure().Error(), + "Run: DATABRICKS_CONFIG_FILE=/work/.databrickscfg databricks auth login --profile logfood\nThen restart the MCP server in your agent") +} + +// TestRefreshGuardTransientFailureFailsOnlyThatRequest checks that a token failure which is not a rejection fails that +// request with the "try again" message and no login command. The proxy keeps running, and the next request asks for a +// token again. +func TestRefreshGuardTransientFailureFailsOnlyThatRequest(t *testing.T) { + cancelled := false + asked := 0 + transient := errors.New("dial tcp: i/o timeout") + g := &refreshGuard{ + authenticate: func(*http.Request) error { + asked++ + if asked == 1 { + return transient + } + return nil + }, + profile: "logfood", + cancel: func() { cancelled = true }, + } + + err := g.authenticateRequest(&http.Request{Header: http.Header{}}) + + require.Error(t, err) + assert.ErrorIs(t, err, transient) + assert.Contains(t, err.Error(), `could not get a token for Databricks profile "logfood"`) + assert.NotContains(t, err.Error(), "auth login", "a login does not fix a transient failure") + assert.NotContains(t, err.Error(), "restart") + assert.NoError(t, g.failure()) + assert.False(t, cancelled) + assert.NoError(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) + assert.Equal(t, 2, asked) +} + +// writeLogfoodProfile writes a config file whose logfood profile names host, and returns the error of the write. +func writeLogfoodProfile(path, host string) error { + return os.WriteFile(path, []byte("[logfood]\nhost = "+host+"\ntoken = unused\n"), 0o600) +} + +// boundServerState returns the state of a server bound to the logfood profile at host in the config file at path. +func boundServerState(host, path string) *mcp.State { + return &mcp.State{Servers: []mcp.Entry{{Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: host, ConfigFile: path}}} +} + +// TestRefreshGuardStopsOnBindingChange checks that a profile re-pointed at another host fails the request with the +// binding error, before the token is fetched, and stops the proxy as a rejected token does. Later requests fail at once. +func TestRefreshGuardStopsOnBindingChange(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + path := filepath.Join(t.TempDir(), "databrickscfg") + require.NoError(t, writeLogfoodProfile(path, "https://b.test")) + calls := 0 + state := boundServerState("https://a.test", path) + g := &refreshGuard{ + authenticate: rebindingAuthenticate(bindingCheck(t.Context(), state, &state.Servers[0], "system.ai.github", "logfood", &config.Config{ConfigFile: path}), + func(*http.Request) error { + calls++ + return nil + }), + profile: "logfood", + cancel: cancel, + } + + err := g.authenticateRequest(&http.Request{Header: http.Header{}}) + + be, ok := errors.AsType[*mcp.BindError](err) + require.True(t, ok, err) + assert.Equal(t, mcp.BindProfileHostChanged, be.Category) + assert.Contains(t, err.Error(), `profile "logfood" now points at https://b.test`) + assert.Equal(t, context.Canceled, ctx.Err()) + assert.EqualError(t, g.authenticateRequest(&http.Request{Header: http.Header{}}), + `the session is stopping after the binding changed for profile "logfood"`) + assert.Equal(t, 0, calls, "no token is fetched once the binding has changed") +} + +// TestBindingCheckWithoutRecordChecksStartHost checks that with no install record, the host serve bound to at start is +// the one the per-token check holds the profile to, and that the error names no record to remove. +func TestBindingCheckWithoutRecordChecksStartHost(t *testing.T) { + path := filepath.Join(t.TempDir(), "databrickscfg") + require.NoError(t, writeLogfoodProfile(path, "https://a.test")) + cfg := &config.Config{Profile: "logfood", ConfigFile: path, Host: "https://a.test"} + check := bindingCheck(t.Context(), &mcp.State{}, nil, "system.ai.github", "logfood", cfg) + + require.NoError(t, check()) + + require.NoError(t, writeLogfoodProfile(path, "https://b.test")) + err := check() + assert.ErrorContains(t, err, `profile "logfood" now points at https://b.test`) + assert.NotContains(t, err.Error(), "aitools mcp remove") +} + +// TestServeProxyRepointedProfileSendsNoToken checks that a profile re-pointed at another host while serve runs gets no +// token sent to the host serve is bound to. The re-point happens while the first tools/list is answered, so the next +// request fails with the binding error before its token is fetched. The proxy stops with exit 2, and no request reaches +// the server after the re-point. +func TestServeProxyRepointedProfileSendsNoToken(t *testing.T) { + const cachedToken = "Bearer token-cached-for-logfood" + path := filepath.Join(t.TempDir(), "databrickscfg") + var mu sync.Mutex + var received int + var repointedAt int + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + received++ + mu.Unlock() + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusOK) + return + } + var msg struct { + ID json.RawMessage `json:"id"` + Method string `json:"method"` + } + if err := json.NewDecoder(r.Body).Decode(&msg); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + if msg.Method == "tools/list" { + mu.Lock() + repointedAt = received + mu.Unlock() + assert.NoError(t, writeLogfoodProfile(path, "https://other.test")) + } + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Mcp-Session-Id", "session-1") + result := `{"tools":[]}` + if msg.Method == "initialize" { + result = `{"protocolVersion":"2025-06-18","capabilities":{},"serverInfo":{"name":"fake","version":"1"}}` + } + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":%s}`, msg.ID, result) + })) + t.Cleanup(server.Close) + require.NoError(t, writeLogfoodProfile(path, server.URL)) + state := boundServerState(server.URL, path) + authenticate := rebindingAuthenticate(bindingCheck(t.Context(), state, &state.Servers[0], "system.ai.github", "logfood", &config.Config{ConfigFile: path}), + func(r *http.Request) error { + r.Header.Set("Authorization", cachedToken) + return nil + }) + + var stdout lockedBuffer + var stderr bytes.Buffer + in := feedAfterAnswers(t, &stdout, []string{ + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, + `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, + `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`, + }, nil) + cmd := NewMcpServeCmd() + cmd.SetContext(t.Context()) + cmd.SetErr(&stderr) + + err := serveProxy(cmd, proxy.Options{ + In: in, + Out: &stdout, + Err: &stderr, + Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), + Config: &config.Config{Host: server.URL}, + Authenticate: authenticate, + Profile: "logfood", + }, "") + + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 2, root.ExitStatus(err)) + assert.Contains(t, stdout.String(), `"id":3,"error"`) + assert.Contains(t, stdout.String(), "now points at https://other.test") + assert.True(t, strings.HasSuffix(stderr.String(), "Error: stopped after the binding changed for profile \"logfood\"\n"), stderr.String()) + mu.Lock() + defer mu.Unlock() + assert.Equal(t, 2, repointedAt, "the server answers initialize and the first tools/list before the re-point") + assert.Equal(t, repointedAt, received, "no request reaches the server after the re-point") +} + +// TestCheckBindingWithRetry checks that a changed binding is checked once more, since a write that replaces +// .databrickscfg can hide the profile for a moment. A failure that is not a changed binding is not checked again. +func TestCheckBindingWithRetry(t *testing.T) { + notFound := &mcp.BindError{Category: mcp.BindProfileNotFound} + other := errors.New("dial tcp: i/o timeout") + tests := []struct { + name string + results []error // the result of each call; the last one repeats + wantErr error + wantCalls int + }{ + {name: "passes at once", results: []error{nil}, wantErr: nil, wantCalls: 1}, + {name: "changed once, then passes", results: []error{notFound, nil}, wantErr: nil, wantCalls: 2}, + {name: "still changed", results: []error{notFound, notFound}, wantErr: notFound, wantCalls: 2}, + {name: "not a changed binding", results: []error{other, nil}, wantErr: other, wantCalls: 1}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + calls := 0 + err := checkBindingWithRetry(func() error { + calls++ + return tc.results[min(calls, len(tc.results))-1] + }) + + assert.Equal(t, tc.wantCalls, calls) + if tc.wantErr == nil { + assert.NoError(t, err) + } else { + assert.ErrorIs(t, err, tc.wantErr) + } + }) + } +} + +// oauthResponse is an error response from the CLI's U2M client, which returns an HttpError for a non-2xx answer. +func oauthResponse(status int, body string) error { + return &httpclient.HttpError{Response: &http.Response{StatusCode: status}, Message: body} +} + +// TestCredentialRejected checks which token failures stop the proxy. A rejection is one that a new login fixes. Every +// other failure is transient, including the ones this check cannot classify. +func TestCredentialRejected(t *testing.T) { + tests := []struct { + name string + err error + want bool + }{ + {name: "refresh rejected, golang.org/x/oauth2 error", err: refreshRejected, want: true}, + {name: "invalid_client with 401", err: &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusUnauthorized}, ErrorCode: "invalid_client"}, want: true}, + {name: "invalid_grant with 500", err: &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusInternalServerError}, ErrorCode: "invalid_grant"}, want: false}, + {name: "server_error with 400", err: &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusBadRequest}, ErrorCode: "server_error"}, want: false}, + {name: "oauth2 response without an error code", err: &oauth2.RetrieveError{Response: &http.Response{StatusCode: http.StatusBadRequest}, Body: []byte("")}, want: false}, + {name: "HTTP invalid_grant with 400", err: oauthResponse(400, `{"error":"invalid_grant","error_description":"Refresh token has expired"}`), want: true}, + {name: "HTTP invalid_client with 401", err: oauthResponse(401, `{"error":"invalid_client"}`), want: true}, + {name: "HTTP invalid_grant, wrapped", err: fmt.Errorf("token refresh: %w", oauthResponse(400, `{"error":"invalid_grant"}`)), want: true}, + {name: "HTTP invalid_grant with 503", err: oauthResponse(503, `{"error":"invalid_grant"}`), want: false}, + {name: "HTTP invalid_request with 400", err: oauthResponse(400, `{"error":"invalid_request"}`), want: false}, + {name: "HTTP body is not JSON", err: oauthResponse(400, "Bad Gateway"), want: false}, + {name: "network error", err: errors.New("dial tcp: i/o timeout"), want: false}, + {name: "deadline exceeded", err: context.DeadlineExceeded, want: false}, + {name: "no refresh token", err: fmt.Errorf("token refresh: %w", u2m.ErrMissingRefreshToken), want: true}, + {name: "no cached token after logout", err: fmt.Errorf("cache: %w", storage.ErrNotFound), want: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, credentialRejected(tc.err)) + }) + } +} + +func TestServeFailurePrintsOnceWithStatus(t *testing.T) { + cmd := NewMcpServeCmd() + var stderr bytes.Buffer + cmd.SetErr(&stderr) + cause := mcp.AuthError("logfood", errors.New(refreshTokenExpired)) + + err := serveFailure(cmd, cause, serveBindStatus) + + assert.Equal(t, "Error: "+cause.Error()+"\n", stderr.String()) + assert.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 2, root.ExitStatus(err)) + assert.ErrorIs(t, err, cause) +} + +// TestServeBindFailureTokenStatus checks that a token failure at startup is classified as it is at runtime. A +// rejection needs a new login, so it keeps the login hint and exits 2. Any other failure is temporary: it has no hint +// and exits 1, the status of the other failures, since exit 2 says the credential was rejected. +func TestServeBindFailureTokenStatus(t *testing.T) { + loginHint := mcp.LoginCommand("logfood", "") + tests := []struct { + name string + cause error + status int + hint bool + }{ + {name: "rejected refresh token", cause: refreshRejected, status: 2, hint: true}, + {name: "no cached token", cause: fmt.Errorf("cache: %w", storage.ErrNotFound), status: 2, hint: true}, + {name: "no strategy configures a credential", cause: config.ErrCannotConfigureDefault, status: 2, hint: true}, + {name: "network error", cause: errors.New("dial tcp: i/o timeout"), status: 1, hint: false}, + {name: "server error from the token endpoint", cause: oauthResponse(503, `{"error":"server_error"}`), status: 1, hint: false}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cmd := NewMcpServeCmd() + var stderr bytes.Buffer + cmd.SetErr(&stderr) + + err := serveBindFailure(cmd, "logfood", mcp.AuthErrorIn("logfood", "", tc.cause)) + + assert.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, tc.status, root.ExitStatus(err)) + assert.Equal(t, tc.hint, strings.Contains(stderr.String(), loginHint)) + if tc.status == 1 { + assert.Contains(t, stderr.String(), "temporary failure") + } + }) + } +} + +// TestServeBindHostOnlyProfileNeedsLogin runs the real credential chain on a profile with a host and no auth_type, as a +// profile written before auth login set one, with no cached token. The chain drops each strategy's error and returns +// config.ErrCannotConfigureDefault, which a login fixes, so serve exits 2 with the login hint. +func TestServeBindHostOnlyProfileNeedsLogin(t *testing.T) { + home := withHome(t) + writeHomeConfig(t, home, "[legacy]\nhost = https://legacy.test\n") + + _, _, err := mcp.Bind(t.Context(), mcp.BindOptions{ + Profile: "legacy", + Service: "system.ai.github", + State: &mcp.State{}, + Stderr: &bytes.Buffer{}, + }) + require.ErrorIs(t, err, config.ErrCannotConfigureDefault) + + cmd := NewMcpServeCmd() + var stderr bytes.Buffer + cmd.SetErr(&stderr) + serveErr := serveBindFailure(cmd, "legacy", err) + + assert.Equal(t, serveBindStatus, root.ExitStatus(serveErr)) + assert.Contains(t, stderr.String(), "\nRun: "+mcp.LoginCommand("legacy", "")) + assert.NotContains(t, stderr.String(), "temporary failure") +} + +// TestServeTransportTLSVerification checks that the profile's insecure_skip_verify reaches the transport, and that +// HTTP/2 is kept with it. +func TestServeTransportTLSVerification(t *testing.T) { + tests := []struct { + name string + cfg *config.Config + insecure bool + }{ + {name: "verifies by default", cfg: &config.Config{}, insecure: false}, + {name: "profile skips verification", cfg: &config.Config{InsecureSkipVerify: true}, insecure: true}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + tr := serveTransport(tc.cfg) + assert.Equal(t, tc.insecure, tr.TLSClientConfig != nil && tr.TLSClientConfig.InsecureSkipVerify) + assert.True(t, tr.ForceAttemptHTTP2) + }) + } +} + +func TestServeTransportKeepsHTTP2WhenSkippingVerification(t *testing.T) { + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + fmt.Fprint(w, "ok") + })) + server.EnableHTTP2 = true + server.StartTLS() + defer server.Close() + + client := &http.Client{Transport: serveTransport(&config.Config{InsecureSkipVerify: true})} + resp, err := client.Get(server.URL) + require.NoError(t, err) + defer resp.Body.Close() + assert.Equal(t, 2, resp.ProtoMajor) +} + +// TestRefreshGuardIgnoresRequestThatEnded checks that a request whose own context ended (the client cancelled it, or +// shutdown began) fails that request only: the token was not shown to be unusable. +func TestRefreshGuardIgnoresRequestThatEnded(t *testing.T) { + cancelled := false + g := &refreshGuard{ + authenticate: func(r *http.Request) error { return r.Context().Err() }, + profile: "logfood", + cancel: func() { cancelled = true }, + } + ctx, cancel := context.WithCancel(t.Context()) + cancel() + + err := g.authenticateRequest((&http.Request{Header: http.Header{}}).WithContext(ctx)) + + assert.ErrorIs(t, err, context.Canceled) + assert.NoError(t, g.failure()) + assert.False(t, cancelled) +} + +// TestRefreshGuardIgnoresSessionDelete checks that the session DELETE at shutdown is best effort. Its auth failure is +// logged by the bridge, and a normal shutdown exits 0. +func TestRefreshGuardIgnoresSessionDelete(t *testing.T) { + cancelled := false + g := &refreshGuard{ + authenticate: func(*http.Request) error { return errors.New(refreshTokenExpired) }, + profile: "logfood", + cancel: func() { cancelled = true }, + } + req, err := http.NewRequestWithContext(t.Context(), http.MethodDelete, "https://workspace.test/mcp", nil) + require.NoError(t, err) + + require.Error(t, g.authenticateRequest(req)) + + assert.NoError(t, g.failure()) + assert.False(t, cancelled) +} + +// TestServeAgentKeepsOnlyKnownAgents checks that an unknown agent is left out of the User-Agent and reported once on +// stderr, so the server still serves. +func TestServeAgentKeepsOnlyKnownAgents(t *testing.T) { + var stderr bytes.Buffer + assert.Equal(t, "claude-code", serveAgent("claude-code", &stderr)) + assert.Empty(t, stderr.String()) + + assert.Empty(t, serveAgent("agent-from-a-later-release", &stderr)) + assert.Equal(t, "unknown agent \"agent-from-a-later-release\"; it is left out of the User-Agent\n", stderr.String()) + + stderr.Reset() + assert.Empty(t, serveAgent("", &stderr)) + assert.Empty(t, stderr.String()) +} + +// TestMcpServeRejectsServiceWithoutThreeParts checks that a service that is not a full three-part name is refused before +// any binding check, with the flag named. +func TestMcpServeRejectsServiceWithoutThreeParts(t *testing.T) { + withHome(t) + for _, service := range []string{"github", "system.ai", "system..github", "a.b.c.d"} { + t.Run(service, func(t *testing.T) { + parent := newMCPTestRoot() + parent.SetArgs([]string{"mcp", "serve", "--profile", "logfood", "--service", service}) + err := parent.ExecuteContext(cmdio.MockDiscard(t.Context())) + require.ErrorContains(t, err, "--service needs a full service name such as system.ai.github") + assert.Equal(t, 1, root.ExitStatus(err)) + }) + } +} + +// TestMcpServeStateReadFailureExitsTwo checks that a state file that cannot be read stops the binding checks, so it +// exits 2 like the other binding failures. +func TestMcpServeStateReadFailureExitsTwo(t *testing.T) { + withHome(t) + dir, err := installer.GlobalSkillsDir(t.Context()) + require.NoError(t, err) + require.NoError(t, os.MkdirAll(filepath.Join(dir, ".state.json"), 0o700)) + + parent := newMCPTestRoot() + var stderr bytes.Buffer + parent.SetErr(&stderr) + parent.SetArgs([]string{"mcp", "serve", "--profile", "logfood", "--service", "system.ai.github"}) + err = parent.ExecuteContext(cmdio.MockDiscard(t.Context())) + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 2, root.ExitStatus(err)) + assert.Contains(t, stderr.String(), ".state.json") +} + +// TestRefreshGuardSkipsSessionDeleteAfterFailure checks that after a refresh failure the session DELETE is skipped: the +// session is over, and a DELETE without a credential would fail and log the refresh error a second time. Authenticate is +// not asked. +func TestRefreshGuardSkipsSessionDeleteAfterFailure(t *testing.T) { + asked := 0 + g := &refreshGuard{ + authenticate: func(*http.Request) error { + asked++ + return refreshRejected + }, + profile: "logfood", + cancel: func() {}, + } + require.Error(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) + asked = 0 + + req, err := http.NewRequestWithContext(t.Context(), http.MethodDelete, "https://workspace.test/mcp", nil) + require.NoError(t, err) + assert.ErrorIs(t, g.authenticateRequest(req), proxy.ErrSkipRequest) + assert.Equal(t, 0, asked) +} + +// TestProxyResultRefreshFailureWinsOverStdinError checks that once the bridge has returned, a refresh failure is the exit +// status and the final line, whatever Run returned. A stdin error that follows the failure is a consequence of it and is +// not reported. +func TestProxyResultRefreshFailureWinsOverStdinError(t *testing.T) { + cause := refreshRejected + g := &refreshGuard{ + authenticate: func(*http.Request) error { return cause }, + profile: "logfood", + cancel: func() {}, + } + require.Error(t, g.authenticateRequest(&http.Request{Header: http.Header{}})) + cmd := NewMcpServeCmd() + var stderr bytes.Buffer + cmd.SetErr(&stderr) + + err := proxyResult(cmd, g, errors.New("read stdin: input/output error")) + + assert.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, serveBindStatus, root.ExitStatus(err)) + assert.ErrorIs(t, err, cause) + assert.Equal(t, refreshStoppedLine, stderr.String()) +} + +// TestProxyResultReportsStdinErrorWithoutRefreshFailure checks that without a refresh failure, a stdin error is printed +// and exits 1. +func TestProxyResultReportsStdinErrorWithoutRefreshFailure(t *testing.T) { + g := &refreshGuard{profile: "logfood"} + cmd := NewMcpServeCmd() + var stderr bytes.Buffer + cmd.SetErr(&stderr) + + err := proxyResult(cmd, g, errors.New("read stdin: input/output error")) + + assert.Equal(t, 1, root.ExitStatus(err)) + assert.Equal(t, "Error: read stdin: input/output error\n", stderr.String()) +} + +// The refresh failure fixtures: the cause that the fake token gives, the login hint that serve prints with it, and the +// final line that serve prints once the proxy has stopped. +const ( + refreshTokenExpired = "refresh token expired" + refreshLoginHint = "databricks auth login --profile logfood\nThen restart the MCP server in your agent" + refreshStoppedLine = "Error: stopped after a token refresh failure for profile \"logfood\"\n" +) + +// refreshRejected is the error of a refresh that the token endpoint rejects, which a new login fixes. It is the shape +// the SDK's client returns for an error response from golang.org/x/oauth2. +var refreshRejected = &oauth2.RetrieveError{ + Response: &http.Response{StatusCode: http.StatusBadRequest}, + ErrorCode: "invalid_grant", + ErrorDescription: refreshTokenExpired, +} + +// isToolsList reports whether r is a tools/list POST. It reads a copy of the body, so it is for Authenticate only. +func isToolsList(r *http.Request) (bool, error) { + if r.Method != http.MethodPost { + return false, nil + } + body, err := r.GetBody() + if err != nil { + return false, err + } + data, err := io.ReadAll(body) + body.Close() + if err != nil { + return false, err + } + return strings.Contains(string(data), `"tools/list"`), nil +} + +// expiresOnToolsList is an Authenticate whose token expires with the tools/list request, as a refresh token can +// mid-session. +func expiresOnToolsList(r *http.Request) error { + listed, err := isToolsList(r) + if err != nil || !listed { + return err + } + return refreshRejected +} + +// failsToolsListOnce is an Authenticate whose first tools/list fails with err. Later calls succeed, as they do once the +// token endpoint is back. +func failsToolsListOnce(err error) func(*http.Request) error { + var failed atomic.Bool + return func(r *http.Request) error { + listed, readErr := isToolsList(r) + if readErr != nil { + return readErr + } + if listed && failed.CompareAndSwap(false, true) { + return err + } + return nil + } +} + +// newEchoMCPServer is a fake endpoint that answers each request with its own id. Its initialize answer sets a session ID, +// so the session DELETE at shutdown is sent. +func newEchoMCPServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusOK) + return + } + var msg struct { + ID json.RawMessage `json:"id"` + Method string `json:"method"` + } + if err := json.NewDecoder(r.Body).Decode(&msg); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Mcp-Session-Id", "session-1") + result := `{"tools":[]}` + if msg.Method == "initialize" { + result = `{"protocolVersion":"2025-06-18","capabilities":{},"serverInfo":{"name":"fake","version":"1"}}` + } + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":%s}`, msg.ID, result) + })) + t.Cleanup(server.Close) + return server +} + +// TestServeProxyTransientRefreshFailureKeepsServing checks that a token failure which is not a rejection fails only the +// request that needed the token. The agent gets the error, without a login hint. Serve keeps running, the next request +// asks for a token again and succeeds, and stdin closing exits 0. +func TestServeProxyTransientRefreshFailureKeepsServing(t *testing.T) { + server := newEchoMCPServer(t) + var stdout lockedBuffer + var stderr bytes.Buffer + in := feedAfterAnswers(t, &stdout, []string{ + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, + `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, + `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`, + }, nil) + cmd := NewMcpServeCmd() + cmd.SetContext(t.Context()) + cmd.SetErr(&stderr) + + err := serveProxy(cmd, proxy.Options{ + In: in, + Out: &stdout, + Err: &stderr, + Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), + Config: &config.Config{Host: server.URL}, + Authenticate: failsToolsListOnce(errors.New("dial tcp: i/o timeout")), + Profile: "logfood", + }, "") + + require.NoError(t, err) + out := stdout.String() + assert.Contains(t, out, `"id":2,"error"`) + assert.Contains(t, out, "the next request tries again") + assert.Contains(t, out, `"id":3,"result"`) + assert.NotContains(t, stderr.String(), "stopped after") +} + +// TestServeProxyRefreshRejectionStopsAfterTransientFailure checks that a rejection after a transient failure still stops +// the proxy: the earlier failure does not change how the rejection is handled. +func TestServeProxyRefreshRejectionStopsAfterTransientFailure(t *testing.T) { + server := newEchoMCPServer(t) + var stdout lockedBuffer + var stderr bytes.Buffer + in := feedAfterAnswers(t, &stdout, []string{ + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, + `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, + `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`, + }, nil) + cmd := NewMcpServeCmd() + cmd.SetContext(t.Context()) + cmd.SetErr(&stderr) + transient := failsToolsListOnce(errors.New("dial tcp: i/o timeout")) + rejected := func(r *http.Request) error { + if err := transient(r); err != nil { + return err + } + return expiresOnToolsList(r) + } + + err := serveProxy(cmd, proxy.Options{ + In: in, + Out: &stdout, + Err: &stderr, + Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), + Config: &config.Config{Host: server.URL}, + Authenticate: rejected, + Profile: "logfood", + }, "") + + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 2, root.ExitStatus(err)) + assert.True(t, strings.HasSuffix(stderr.String(), refreshStoppedLine), stderr.String()) +} + +// newRotatingSessionServer is a fake endpoint for the debug log. Its initialize answer starts session sess-alpha. A +// request that carries sess-alpha is answered with sess-beta, a new session the server then expects. +func newRotatingSessionServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusOK) + return + } + var msg struct { + ID json.RawMessage `json:"id"` + Method string `json:"method"` + } + if err := json.NewDecoder(r.Body).Decode(&msg); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + result := `{"tools":[]}` + switch { + case msg.Method == "initialize": + w.Header().Set("Mcp-Session-Id", "sess-alpha") + result = `{"protocolVersion":"2025-06-18","capabilities":{},"serverInfo":{"name":"fake","version":"1"}}` + case r.Header.Get("Mcp-Session-Id") == "sess-alpha": + w.Header().Set("Mcp-Session-Id", "sess-beta") + } + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":%s}`, msg.ID, result) + })) + t.Cleanup(server.Close) + return server +} + +// TestServeLogsEachUpstreamRequestAtDebug checks that at debug level serve logs one line per upstream request: the +// JSON-RPC method and id, the HTTP status, the duration, and whether a session id was sent, received, or replaced. The +// lines carry no body and no header value, so neither the token nor a session id nor a tool argument appears in them. +func TestServeLogsEachUpstreamRequestAtDebug(t *testing.T) { + server := newRotatingSessionServer(t) + var stdout lockedBuffer + var stderr bytes.Buffer + var logs lockedBuffer + in := feedAfterAnswers(t, &stdout, []string{ + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, + `{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{"cursor":"secret-cursor"}}`, + }, nil) + ctx := log.NewContext(t.Context(), slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelDebug}))) + cmd := NewMcpServeCmd() + cmd.SetContext(ctx) + cmd.SetErr(&stderr) + + err := serveProxy(cmd, proxy.Options{ + In: in, + Out: &stdout, + Err: &stderr, + Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), + Config: &config.Config{Host: server.URL}, + Authenticate: func(r *http.Request) error { + r.Header.Set("Authorization", "Bearer secret-token") + return nil + }, + Profile: "logfood", + Transport: newUpstreamLog(http.DefaultTransport), + }, "") + + require.NoError(t, err) + lines := strings.Split(strings.TrimSpace(logs.String()), "\n") + require.Len(t, lines, 3, logs.String()) + // The initialize carries no session id and the server starts one; tools/list sends it and the server replaces it; + // the shutdown DELETE sends the replacement. + assert.Contains(t, lines[0], `msg="upstream request" http_method=POST rpc_method=initialize rpc_id=1 status=200`) + assert.Contains(t, lines[0], "session_sent=false session_received=true session_replaced=false") + assert.Contains(t, lines[1], `http_method=POST rpc_method=tools/list rpc_id=2 status=200`) + assert.Contains(t, lines[1], "session_sent=true session_received=true session_replaced=true") + assert.Contains(t, lines[2], `http_method=DELETE rpc_method="" rpc_id="" status=200`) + assert.Contains(t, lines[2], "session_sent=true session_received=false session_replaced=false") + assert.Contains(t, lines[0], "duration=") + for _, secret := range []string{"secret-token", "secret-cursor", "sess-alpha", "sess-beta"} { + assert.NotContains(t, logs.String(), secret) + } +} + +// newRefreshFailureServer is a fake endpoint that answers initialize. It sets deleted when it gets a session DELETE. +func newRefreshFailureServer(t *testing.T, deleted *atomic.Bool) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodDelete { + deleted.Store(true) + w.WriteHeader(http.StatusOK) + return + } + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Mcp-Session-Id", "session-1") + fmt.Fprint(w, `{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-06-18","capabilities":{},"serverInfo":{"name":"fake","version":"1"}}}`) + })) + t.Cleanup(server.Close) + return server +} + +// runRefreshFailureSession serves an initialize and a tools/list whose token expires. Stdin ends with end, or closes +// cleanly when end is nil. It returns what serve printed to stderr, whether the endpoint got a session DELETE, and +// serve's error. +func runRefreshFailureSession(t *testing.T, end error) (string, bool, error) { + t.Helper() + var deleted atomic.Bool + server := newRefreshFailureServer(t, &deleted) + var stderr bytes.Buffer + var stdout lockedBuffer + in := feedAfterAnswers(t, &stdout, []string{ + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, + `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, + }, end) + cmd := NewMcpServeCmd() + cmd.SetContext(t.Context()) + cmd.SetErr(&stderr) + err := serveProxy(cmd, proxy.Options{ + In: in, + Out: &stdout, + Err: &stderr, + Endpoint: mcp.ServiceURL(server.URL, "system.ai.github"), + Config: &config.Config{Host: server.URL}, + Authenticate: expiresOnToolsList, + Profile: "logfood", + }, "") + return stderr.String(), deleted.Load(), err +} + +// TestServeProxyRefreshFailurePrintsOnceAndExitsTwo checks that a token that expires mid-session fails the request that +// needs it. The bridge logs that failure once, with the profile and the login command. The proxy then exits 2 without +// printing it again, and sends no session DELETE at shutdown. +func TestServeProxyRefreshFailurePrintsOnceAndExitsTwo(t *testing.T) { + stderr, deleted, err := runRefreshFailureSession(t, nil) + + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 2, root.ExitStatus(err)) + assert.Equal(t, "databricks aitools mcp serve: upstream: "+refreshRejected.Error()+"\n"+ + "Run: "+refreshLoginHint+"\n"+ + refreshStoppedLine, stderr) + assert.False(t, deleted, "no session DELETE is sent after a refresh failure") +} + +// lockedBuffer collects what serve writes to stdout. The bridge writes from several goroutines. +type lockedBuffer struct { + mu sync.Mutex + buf bytes.Buffer +} + +func (b *lockedBuffer) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.Write(p) +} + +func (b *lockedBuffer) String() string { + b.mu.Lock() + defer b.mu.Unlock() + return b.buf.String() +} + +// feedAfterAnswers returns stdin for serve. It sends each line once the answer to the line before it is on out, as an +// MCP client does: the bridge runs requests concurrently, so a later request could otherwise overtake the initialize. +// The input then ends with end, or closes when end is nil. The answers are the ones the test expects from the fake +// endpoint, so the feeder waits for the ids of the lines it has sent. +func feedAfterAnswers(t *testing.T, out *lockedBuffer, lines []string, end error) io.Reader { + t.Helper() + pr, pw := io.Pipe() + t.Cleanup(func() { pw.Close() }) + go func() { + for i, line := range lines { + if i > 0 { + want := fmt.Sprintf(`"id":%d,`, i) + deadline := time.Now().Add(10 * time.Second) + for !strings.Contains(out.String(), want) && time.Now().Before(deadline) { + time.Sleep(time.Millisecond) + } + } + if _, err := io.WriteString(pw, line+"\n"); err != nil { + return + } + } + if end != nil { + pw.CloseWithError(end) + return + } + pw.Close() + }() + return pr +} + +// TestServeProxyRefreshFailureWinsOverStdinError checks that a stdin error after the refresh failure does not change the +// exit status or the final line. +func TestServeProxyRefreshFailureWinsOverStdinError(t *testing.T) { + stderr, _, err := runRefreshFailureSession(t, errors.New("input/output error")) + + require.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 2, root.ExitStatus(err)) + assert.NotContains(t, stderr, "input/output error") + assert.True(t, strings.HasSuffix(stderr, refreshStoppedLine), stderr) +} + +// signalHelperEnv marks the child process that TestServeSecondSignalEndsProcess starts. +const signalHelperEnv = "AITOOLS_SERVE_SIGNAL_HELPER" + +// TestServeSignalHelper is the child of TestServeSecondSignalEndsProcess. It runs the signal context serve uses, +// prints when the first signal cancels it, then stays alive as shutdown would. +func TestServeSignalHelper(t *testing.T) { + if os.Getenv(signalHelperEnv) == "" { + t.Skip("runs only as the child of TestServeSecondSignalEndsProcess") + } + ctx, cancel := serveSignalContext(t.Context()) + defer cancel() + fmt.Println("ready") + <-ctx.Done() + fmt.Println("cancelled") + time.Sleep(time.Minute) +} + +// TestServeSecondSignalEndsProcess checks that the first signal cancels the context and the second one ends the +// process. A second signal that the handler swallowed during shutdown would leave the child running until the timeout. +func TestServeSecondSignalEndsProcess(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("Process.Signal cannot send SIGINT on Windows") + } + pr, pw, err := os.Pipe() + require.NoError(t, err) + defer pr.Close() + + cmd := exec.CommandContext(t.Context(), os.Args[0], "-test.run=^TestServeSignalHelper$") + cmd.Env = append(os.Environ(), signalHelperEnv+"=1") + cmd.Stdout = pw + require.NoError(t, cmd.Start()) + require.NoError(t, pw.Close()) + + lines := make(chan string) + go func() { + defer close(lines) + sc := bufio.NewScanner(pr) + for sc.Scan() { + lines <- sc.Text() + } + }() + waitForChildLine(t, lines, "ready") + require.NoError(t, cmd.Process.Signal(os.Interrupt)) + waitForChildLine(t, lines, "cancelled") + require.NoError(t, cmd.Process.Signal(os.Interrupt)) + + exited := make(chan error, 1) + go func() { exited <- cmd.Wait() }() + select { + case err := <-exited: + // A signal ends the child with ExitCode -1. A panic or a normal exit would not, and must fail the test. + var exitErr *exec.ExitError + require.ErrorAs(t, err, &exitErr, "the child exited without a signal") + assert.Equal(t, -1, exitErr.ExitCode(), "the child should be ended by the second signal") + case <-time.After(20 * time.Second): + _ = cmd.Process.Kill() + t.Fatal("the second signal did not end the process") + } +} + +// waitForChildLine reads the child's output lines until one equals want. It fails the test when the child closes its +// output first or the wait takes too long. +func waitForChildLine(t *testing.T, lines <-chan string, want string) { + t.Helper() + for { + select { + case line, ok := <-lines: + require.True(t, ok, "child exited before it printed %q", want) + if line == want { + return + } + case <-time.After(20 * time.Second): + t.Fatalf("timed out waiting for %q", want) + } + } +} + +// newRedirectingMCPServer is a fake endpoint that does not accept the token for tools/list. That request gets a 303 to a +// sign-in page whose query carries state. Other requests are answered as newEchoMCPServer answers them. +func newRedirectingMCPServer(t *testing.T) *httptest.Server { + t.Helper() + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusOK) + return + } + var msg struct { + ID json.RawMessage `json:"id"` + Method string `json:"method"` + } + if err := json.NewDecoder(r.Body).Decode(&msg); err != nil { + w.WriteHeader(http.StatusBadRequest) + return + } + if msg.Method == "tools/list" { + w.Header().Set("Location", "https://sign-in.test/login?state=secret-state") + w.WriteHeader(http.StatusSeeOther) + return + } + w.Header().Set("Content-Type", "application/json") + result := `{"tools":[]}` + if msg.Method == "initialize" { + result = `{"protocolVersion":"2025-06-18","capabilities":{},"serverInfo":{"name":"fake","version":"1"}}` + } + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%s,"result":%s}`, msg.ID, result) + })) + t.Cleanup(server.Close) + return server +} + +// TestServeRedirectFailsRequestWithLoginHintAndKeepsServing checks that a 303 from the endpoint fails only the request that +// got it, with the login command for the profile. The Location is not in the output, and serve keeps answering: a later +// request gets the same login error, and stdin closing exits 0. +func TestServeRedirectFailsRequestWithLoginHintAndKeepsServing(t *testing.T) { + server := newRedirectingMCPServer(t) + var stdout lockedBuffer + var stderr bytes.Buffer + in := feedAfterAnswers(t, &stdout, []string{ + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}`, + `{"jsonrpc":"2.0","id":2,"method":"tools/list"}`, + `{"jsonrpc":"2.0","id":3,"method":"tools/list"}`, + }, nil) + cmd := NewMcpServeCmd() + cmd.SetContext(t.Context()) + cmd.SetErr(&stderr) + + err := serveProxy(cmd, proxy.Options{ + In: in, + Out: &stdout, + Err: &stderr, + Endpoint: mcp.ServiceURL(server.URL, "system.ai.web_search"), + Config: &config.Config{Host: server.URL}, + Authenticate: func(*http.Request) error { return nil }, + Profile: "logfood", + Transport: &signInRedirect{next: http.DefaultTransport, profile: "logfood"}, + }, "") + + require.NoError(t, err) + out := stdout.String() + assert.Contains(t, out, `"id":2,"error"`) + assert.Contains(t, out, `Run: databricks auth login --profile logfood`) + assert.Contains(t, out, `"id":3,"error"`) + assert.NotContains(t, out+stderr.String(), "secret-state") + assert.NotContains(t, out+stderr.String(), "sign-in.test") +} + +// TestSignInRedirectRoundTrip checks that only a 3xx answer becomes the login error. A 200 and a 500 pass through +// unchanged, so the bridge reports them as it does today. A pinned profile's hint names its config file. +func TestSignInRedirectRoundTrip(t *testing.T) { + const redirectCause = "the MCP endpoint redirected to sign-in (HTTP 303), so it did not accept the token for Databricks profile \"logfood\"" + tests := []struct { + name string + status int + configFile string + wantErr string + }{ + {name: "200 passes through", status: http.StatusOK}, + {name: "500 passes through", status: http.StatusInternalServerError}, + {name: "303 fails with the login hint", status: http.StatusSeeOther, wantErr: redirectCause + "\nRun: databricks auth login --profile logfood\nThen restart the MCP server in your agent"}, + { + name: "303 with a pinned config file names it", + status: http.StatusSeeOther, + configFile: "/cfg/.databrickscfg", + wantErr: redirectCause + "\nRun: DATABRICKS_CONFIG_FILE=/cfg/.databrickscfg databricks auth login --profile logfood\nThen restart the MCP server in your agent", + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Location", "https://sign-in.test/login?state=secret-state") + w.WriteHeader(tc.status) + })) + t.Cleanup(server.Close) + rt := &signInRedirect{next: http.DefaultTransport, profile: "logfood", configFile: tc.configFile} + req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, server.URL, nil) + require.NoError(t, err) + + resp, err := rt.RoundTrip(req) + + if tc.wantErr == "" { + require.NoError(t, err) + assert.Equal(t, tc.status, resp.StatusCode) + resp.Body.Close() + return + } + assert.Nil(t, resp) + require.EqualError(t, err, tc.wantErr) + assert.NotContains(t, err.Error(), "secret-state") + }) + } +} diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go index ef10c703091..f4db41b423f 100644 --- a/cmd/aitools/mcp_test.go +++ b/cmd/aitools/mcp_test.go @@ -33,7 +33,7 @@ func TestMcpCommandIsHidden(t *testing.T) { for _, c := range mcp.Commands() { names = append(names, c.Name()) } - assert.Equal(t, []string{"add", "list", "remove"}, names) + assert.Equal(t, []string{"add", "list", "remove", "serve"}, names) for _, c := range root.NewCommand(t.Context(), Spec).Commands() { if c.Name() == "mcp" { diff --git a/cmd/root/logger.go b/cmd/root/logger.go index 7bf948c878b..89dfd021af2 100644 --- a/cmd/root/logger.go +++ b/cmd/root/logger.go @@ -3,6 +3,7 @@ package root import ( "context" "fmt" + "io" "log/slog" "os" @@ -43,7 +44,24 @@ func (f *logFlags) makeLogHandler(ctx context.Context, opts slog.HandlerOptions) } } -func (f *logFlags) initializeContext(ctx context.Context) (context.Context, error) { +// logFileKey is the context key of the logFileSlot that Execute puts in the context. +type logFileKey struct{} + +// logFileSlot holds the log file that initializeContext opened. Execute closes it when it returns. +type logFileSlot struct { + file io.Closer // nil until initializeContext opens the log file +} + +// close closes the log file, if one was opened. +func (s *logFileSlot) close() { + if s.file != nil { + _ = s.file.Close() + } +} + +// initializeContext opens the log file and installs the default logger. For a command whose stdout carries a protocol, +// it refuses a log file on stdout before the logger is installed, so not even a refused command's telemetry reaches stdout. +func (f *logFlags) initializeContext(ctx context.Context, cmd *cobra.Command) (context.Context, error) { if f.debug { err := f.level.Set("debug") if err != nil { @@ -64,6 +82,14 @@ func (f *logFlags) initializeContext(ctx context.Context) (context.Context, erro if err != nil { return nil, err } + // Commands run without Execute have no slot, and then nothing closes the file. + if slot, ok := ctx.Value(logFileKey{}).(*logFileSlot); ok { + slot.file = &f.file + } + + if carriesProtocol(cmd) && writesToStdout(f.file.Writer(), os.Stdout, os.Stderr) { + return nil, fmt.Errorf("%s writes protocol messages to stdout, so its logs cannot go to stdout; use --log-file stderr or a file", cmd.CommandPath()) + } handler, err := f.makeLogHandler(ctx, opts) if err != nil { diff --git a/cmd/root/root.go b/cmd/root/root.go index 65bc5dc8019..a473167237d 100644 --- a/cmd/root/root.go +++ b/cmd/root/root.go @@ -4,10 +4,12 @@ import ( "context" "errors" "fmt" + stdlog "log" //nolint:depguard // slog.SetDefault redirects the standard log package, and Execute restores it "log/slog" "os" "runtime" "runtime/debug" + "strconv" "strings" "time" @@ -68,7 +70,7 @@ func newRootCommand(ctx context.Context) *cobra.Command { } // Configure default logger. - ctx, err = logFlags.initializeContext(ctx) + ctx, err = logFlags.initializeContext(ctx, cmd) if err != nil { return err } @@ -78,7 +80,7 @@ func newRootCommand(ctx context.Context) *cobra.Command { slog.String("version", build.GetInfo().Version), slog.String("args", strings.Join(os.Args, ", "))) - warnIfStaleVersion(ctx) + warnIfStaleVersion(ctx, cmd) // Configure our user agent with the command that's about to be executed. ctx = withCommandInUserAgent(ctx, cmd) @@ -173,6 +175,20 @@ Stack Trace: startTime := time.Now() + // initializeContext puts the log file it opens in this slot. It is closed when Execute returns, even on a panic, and + // the loggers are restored, so nothing writes to the closed file. + var logFile logFileSlot + ctx = context.WithValue(ctx, logFileKey{}, &logFile) + // slog.SetDefault also redirects the standard log package, so both are restored. + prevLogger := slog.Default() + prevLogWriter, prevLogFlags := stdlog.Writer(), stdlog.Flags() + defer func() { + logFile.close() + slog.SetDefault(prevLogger) + stdlog.SetOutput(prevLogWriter) + stdlog.SetFlags(prevLogFlags) + }() + // Run the command cmd, err = cmd.ExecuteContextC(ctx) if err != nil && !errors.Is(err, ErrAlreadyPrinted) { @@ -190,6 +206,8 @@ Stack Trace: fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s\n", err.Error()) } + exitCode := ExitStatus(err) + // Log exit status and error // We only log if logger initialization succeeded and is stored in command // context @@ -199,21 +217,16 @@ Stack Trace: slog.String("exit_code", "0")) } else if errors.Is(err, ErrAlreadyPrinted) { logger.Debug("failed execution", - slog.String("exit_code", "1"), + slog.String("exit_code", strconv.Itoa(exitCode)), ) } else { logger.Info("failed execution", - slog.String("exit_code", "1"), + slog.String("exit_code", strconv.Itoa(exitCode)), slog.String("error", err.Error()), ) } } - exitCode := 0 - if err != nil { - exitCode = 1 - } - commandStr := commandString(cmd) ctx = cmd.Context() diff --git a/cmd/root/root_test.go b/cmd/root/root_test.go index d170e6e5211..cefe82507fe 100644 --- a/cmd/root/root_test.go +++ b/cmd/root/root_test.go @@ -3,8 +3,13 @@ package root import ( "bytes" "context" + "encoding/json" "errors" + "io" + stdlog "log" //nolint:depguard // checks the standard log package that slog.SetDefault redirects "os" + "path/filepath" + "strings" "testing" "github.com/databricks/cli/libs/cmdctx" @@ -196,3 +201,59 @@ func TestExecuteWithNilArgsRunsWithoutArgs(t *testing.T) { assert.NotNil(t, got, "RunE did not run") assert.Empty(t, got) } + +func TestExecuteLogsExitStatus(t *testing.T) { + keepDefaultLogger(t) + logFile := filepath.Join(t.TempDir(), "run.log") + cmd := newRootCommand(t.Context()) + cmd.AddCommand(&cobra.Command{ + Use: "fail", + RunE: func(*cobra.Command, []string) error { + return &ExitStatusError{Status: 2, Err: errors.New("binding failed")} + }, + }) + cmd.SetErr(io.Discard) + + err := execute(t.Context(), cmd, []string{"fail", "--log-file", logFile, "--log-level", "info", "--log-format", "json"}) + require.Error(t, err) + assert.Equal(t, 2, ExitStatus(err)) + + content, readErr := os.ReadFile(logFile) + require.NoError(t, readErr) + var failed map[string]any + for line := range strings.SplitSeq(strings.TrimSpace(string(content)), "\n") { + var rec map[string]any + require.NoError(t, json.Unmarshal([]byte(line), &rec)) + if rec["msg"] == "failed execution" { + failed = rec + } + } + require.NotNil(t, failed, "no failed execution record in %s", logFile) + assert.Equal(t, "2", failed["exit_code"]) +} + +// TestExecuteRestoresStdLogOutput checks that the standard log package writes where it did before Execute, not to the +// log file that Execute closes. slog.SetDefault redirects the standard log package to a non-default handler. +func TestExecuteRestoresStdLogOutput(t *testing.T) { + keepDefaultLogger(t) + var out bytes.Buffer + prevWriter, prevFlags := stdlog.Writer(), stdlog.Flags() + stdlog.SetOutput(&out) + t.Cleanup(func() { + stdlog.SetOutput(prevWriter) + stdlog.SetFlags(prevFlags) + }) + + logFile := filepath.Join(t.TempDir(), "run.log") + cmd := newRootCommand(t.Context()) + cmd.AddCommand(&cobra.Command{ + Use: "ok", + RunE: func(*cobra.Command, []string) error { return nil }, + }) + cmd.SetErr(io.Discard) + require.NoError(t, execute(t.Context(), cmd, []string{"ok", "--log-file", logFile, "--log-level", "debug"})) + + stdlog.Print("after execute") + assert.Equal(t, prevFlags, stdlog.Flags()) + assert.Contains(t, out.String(), "after execute") +} diff --git a/cmd/root/silent_err.go b/cmd/root/silent_err.go index b361cc6b404..7898678b447 100644 --- a/cmd/root/silent_err.go +++ b/cmd/root/silent_err.go @@ -5,3 +5,26 @@ import "errors" // ErrAlreadyPrinted is not printed to the user. It's used to signal that the command should exit with an error, // but the error message was already printed. var ErrAlreadyPrinted = errors.New("AlreadyPrinted") + +// ExitStatusError is an error that exits the process with Status instead of 1. Only a command whose failures need +// a distinct status returns it; `serve` exits 2 for a binding or auth failure. +type ExitStatusError struct { + Status int + Err error +} + +func (e *ExitStatusError) Error() string { return e.Err.Error() } + +func (e *ExitStatusError) Unwrap() error { return e.Err } + +// ExitStatus returns the process exit status for err: 0 for nil, the Status of the first *ExitStatusError in its +// chain, and 1 otherwise. +func ExitStatus(err error) int { + if err == nil { + return 0 + } + if es, ok := errors.AsType[*ExitStatusError](err); ok { + return es.Status + } + return 1 +} diff --git a/cmd/root/silent_err_test.go b/cmd/root/silent_err_test.go new file mode 100644 index 00000000000..221b4b15b7d --- /dev/null +++ b/cmd/root/silent_err_test.go @@ -0,0 +1,40 @@ +package root + +import ( + "errors" + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +// duckStatusError has an ExitStatus method but is not an ExitStatusError, so it does not set the status. +type duckStatusError struct{} + +func (duckStatusError) Error() string { return "duck" } +func (duckStatusError) ExitStatus() int { return 7 } + +func TestExitStatus(t *testing.T) { + plain := errors.New("plain failure") + status := &ExitStatusError{Status: 2, Err: errors.New("binding failed")} + + tests := []struct { + name string + err error + want int + }{ + {name: "nil", err: nil, want: 0}, + {name: "plain error", err: plain, want: 1}, + {name: "status error", err: status, want: 2}, + {name: "wrapped status error", err: fmt.Errorf("serve: %w", status), want: 2}, + {name: "joined with already printed", err: errors.Join(ErrAlreadyPrinted, status), want: 2}, + {name: "joined plain error", err: errors.Join(ErrAlreadyPrinted, plain), want: 1}, + // Only the named type sets a status: an error with an ExitStatus method of its own does not. + {name: "method without the type", err: duckStatusError{}, want: 1}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + assert.Equal(t, tc.want, ExitStatus(tc.err)) + }) + } +} diff --git a/cmd/root/stale_version.go b/cmd/root/stale_version.go index b33c0b98fbc..9d204cb7e54 100644 --- a/cmd/root/stale_version.go +++ b/cmd/root/stale_version.go @@ -9,6 +9,7 @@ import ( "github.com/databricks/cli/libs/cmdio" "github.com/databricks/cli/libs/dbr" "github.com/databricks/cli/libs/versioncheck" + "github.com/spf13/cobra" ) const ( @@ -31,17 +32,19 @@ func staleVersionWarning(buildTime, now time.Time, upgradeCommand string) string return msg + "See " + InstallDocsURL + " to upgrade.\n" } -func skipStaleVersionWarning(ctx context.Context, info build.Info) bool { - if info.IsDevelopment() { +// staleVersionSkipped reports whether cmd gets no stale-version warning. A command whose stdout carries a protocol never +// gets one, nor does a development build, nor a CLI on DBR, which installs the latest version by default. +func staleVersionSkipped(ctx context.Context, cmd *cobra.Command, info build.Info) bool { + if carriesProtocol(cmd) || info.IsDevelopment() { return true } // DBR installs the latest CLI by default, so skip the warning there. return dbr.HasDetection(ctx) && dbr.RunsOnRuntime(ctx) } -func warnIfStaleVersion(ctx context.Context) { +func warnIfStaleVersion(ctx context.Context, cmd *cobra.Command) { info := build.GetInfo() - if skipStaleVersionWarning(ctx, info) { + if staleVersionSkipped(ctx, cmd, info) { return } _, command := versioncheck.DetectInstallMethod(ctx) diff --git a/cmd/root/stale_version_test.go b/cmd/root/stale_version_test.go index a7bcb8f4da6..0d438b84068 100644 --- a/cmd/root/stale_version_test.go +++ b/cmd/root/stale_version_test.go @@ -6,6 +6,7 @@ import ( "github.com/databricks/cli/internal/build" "github.com/databricks/cli/libs/dbr" + "github.com/spf13/cobra" "github.com/stretchr/testify/assert" ) @@ -43,7 +44,7 @@ func TestStaleVersionWarning(t *testing.T) { } } -func TestSkipStaleVersionWarning(t *testing.T) { +func TestStaleVersionSkipped(t *testing.T) { release := build.Info{Version: "1.18.0"} notDBR := dbr.Environment{} @@ -61,7 +62,17 @@ func TestSkipStaleVersionWarning(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { ctx := dbr.MockRuntime(t.Context(), tc.runtime) - assert.Equal(t, tc.want, skipStaleVersionWarning(ctx, tc.info)) + assert.Equal(t, tc.want, staleVersionSkipped(ctx, &cobra.Command{Use: "list"}, tc.info)) }) } } + +// TestStaleVersionSkippedForProtocolCommand checks that a command whose stdout carries a protocol gets no stale-version +// warning, even on a stale build. The warning goes to stderr, but an agent shows the server's stderr every time it starts +// the server. +func TestStaleVersionSkippedForProtocolCommand(t *testing.T) { + ctx := dbr.MockRuntime(t.Context(), dbr.Environment{}) + release := build.Info{Version: "1.18.0"} + assert.True(t, staleVersionSkipped(ctx, protocolTestCommand(new(bool)), release)) + assert.False(t, staleVersionSkipped(ctx, &cobra.Command{Use: "list"}, release)) +} diff --git a/cmd/root/stdout_protocol.go b/cmd/root/stdout_protocol.go new file mode 100644 index 00000000000..528ce78318a --- /dev/null +++ b/cmd/root/stdout_protocol.go @@ -0,0 +1,51 @@ +package root + +import ( + "io" + "os" + + "github.com/spf13/cobra" +) + +// StdoutCarriesProtocol is the cobra annotation of a command whose stdout carries a protocol, such as MCP JSON-RPC +// messages. Root refuses a log file on stdout for such a command and skips the stale-version warning, which an agent +// would show in every session. +const StdoutCarriesProtocol = "stdout-carries-protocol" + +// carriesProtocol reports whether cmd is annotated with StdoutCarriesProtocol. +func carriesProtocol(cmd *cobra.Command) bool { + _, ok := cmd.Annotations[StdoutCarriesProtocol] + return ok +} + +// writesToStdout reports whether w is the open stdout file. It compares files, not paths: on macOS a stat of +// /dev/stdout does not match a stdout pipe, but the opened /dev/stdout does. The streams are parameters so tests can +// pass their own, since under `go test -json` os.Stderr is os.Stdout. +// +// A file that is also stderr is not stdout, because one terminal file carries both diagnostics and protocol. That holds +// on Unix only. On Windows a console is not a regular file, so sameFileInfo never matches it, and `--log-file stdout` +// is refused even in a terminal. +func writesToStdout(w io.Writer, stdout, stderr *os.File) bool { + f, ok := w.(*os.File) + if !ok || f == stderr { + return false + } + return sameFile(f, stdout) && !sameFile(f, stderr) +} + +// sameFile reports whether a and b are the same open file. A stream that cannot be stat'ed, such as a closed one, is +// not the same file as anything. +func sameFile(a, b *os.File) bool { + if a == b { + return true + } + ai, err := a.Stat() + if err != nil { + return false + } + bi, err := b.Stat() + if err != nil { + return false + } + return sameFileInfo(ai, bi) +} diff --git a/cmd/root/stdout_protocol_other.go b/cmd/root/stdout_protocol_other.go new file mode 100644 index 00000000000..5e0cdf31f6c --- /dev/null +++ b/cmd/root/stdout_protocol_other.go @@ -0,0 +1,10 @@ +//go:build !windows + +package root + +import "os" + +// sameFileInfo reports whether a and b describe the same file. +func sameFileInfo(a, b os.FileInfo) bool { + return os.SameFile(a, b) +} diff --git a/cmd/root/stdout_protocol_test.go b/cmd/root/stdout_protocol_test.go new file mode 100644 index 00000000000..f7c684a91d6 --- /dev/null +++ b/cmd/root/stdout_protocol_test.go @@ -0,0 +1,220 @@ +package root + +import ( + "fmt" + "io" + "log/slog" + "os" + "path/filepath" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// protocolTestCommand is a command annotated as writing a protocol to stdout. It records whether it ran. +func protocolTestCommand(ran *bool) *cobra.Command { + return &cobra.Command{ + Use: "serve", + Annotations: map[string]string{StdoutCarriesProtocol: "true"}, + RunE: func(*cobra.Command, []string) error { + *ran = true + return nil + }, + } +} + +// keepDefaultLogger restores the slog default that Execute replaces when it opens a log file. +func keepDefaultLogger(t *testing.T) { + t.Helper() + prev := slog.Default() + t.Cleanup(func() { slog.SetDefault(prev) }) +} + +// useSeparateStderr points os.Stderr at a file of its own, as an agent gives the server separate stdout and stderr. +// Under `go test -json` os.Stderr is os.Stdout, which would make the stdout cases below look like stderr. +func useSeparateStderr(t *testing.T) { + t.Helper() + f, err := os.Create(filepath.Join(t.TempDir(), "stderr.txt")) + require.NoError(t, err) + prev := os.Stderr + os.Stderr = f + t.Cleanup(func() { + os.Stderr = prev + f.Close() + }) +} + +func TestExecuteRefusesStdoutLogForProtocolCommand(t *testing.T) { + useSeparateStderr(t) + for _, logFile := range []string{"stdout", "STDOUT", "/dev/stdout"} { + t.Run(logFile, func(t *testing.T) { + if logFile == "/dev/stdout" { + if _, err := os.Stat(logFile); err != nil { + t.Skip("this OS has no /dev/stdout") + } + } + ran := false + cmd := newRootCommand(t.Context()) + cmd.AddCommand(protocolTestCommand(&ran)) + cmd.SetErr(io.Discard) + + err := execute(t.Context(), cmd, []string{"serve", "--log-file", logFile, "--log-level", "debug"}) + require.ErrorContains(t, err, "writes protocol messages to stdout") + assert.False(t, ran) + }) + } +} + +// TestExecuteClosesLogFileAndRestoresLogger checks that once Execute returns, the file the command logged to is closed +// and the default logger writes elsewhere. It also checks the panic path, where the command never returns. +func TestExecuteClosesLogFileAndRestoresLogger(t *testing.T) { + prev := slog.Default() + t.Cleanup(func() { slog.SetDefault(prev) }) + for _, panics := range []bool{false, true} { + t.Run(fmt.Sprintf("panics=%v", panics), func(t *testing.T) { + var slot *logFileSlot + cmd := newRootCommand(t.Context()) + cmd.AddCommand(&cobra.Command{ + Use: "serve", + RunE: func(cmd *cobra.Command, _ []string) error { + var ok bool + slot, ok = cmd.Context().Value(logFileKey{}).(*logFileSlot) + require.True(t, ok) + if panics { + panic("boom") + } + return nil + }, + }) + logFile := filepath.Join(t.TempDir(), "run.log") + cmd.SetErr(io.Discard) + + err := execute(t.Context(), cmd, []string{"serve", "--log-file", logFile}) + assert.Equal(t, panics, err != nil) + assert.Equal(t, prev, slog.Default()) + require.NotNil(t, slot) + assert.ErrorIs(t, slot.file.Close(), os.ErrClosed) + }) + } +} + +func TestExecuteAllowsFileLogForProtocolCommand(t *testing.T) { + useSeparateStderr(t) + logFile := filepath.Join(t.TempDir(), "serve.log") + ran := false + cmd := newRootCommand(t.Context()) + cmd.AddCommand(protocolTestCommand(&ran)) + cmd.SetErr(io.Discard) + + require.NoError(t, execute(t.Context(), cmd, []string{"serve", "--log-file", logFile, "--log-level", "debug"})) + assert.True(t, ran) +} + +// TestWritesToStdout checks that a path that is stdout under another name is refused. stderr is never refused. The +// streams are pipes of the test's own, not the process's. +func TestWritesToStdout(t *testing.T) { + stdout := newPipe(t) + stderr := newPipe(t) + other := newPipe(t) + file, err := os.Create(filepath.Join(t.TempDir(), "run.log")) + require.NoError(t, err) + t.Cleanup(func() { file.Close() }) + null, err := os.OpenFile(os.DevNull, os.O_WRONLY, 0) + require.NoError(t, err) + t.Cleanup(func() { null.Close() }) + + tests := []struct { + name string + w io.Writer + want bool + }{ + {name: "stdout", w: stdout, want: true}, + {name: "stderr", w: stderr, want: false}, + {name: "another pipe", w: other, want: false}, + {name: "regular file", w: file, want: false}, + {name: "null device", w: null, want: false}, + {name: "not a file", w: io.Discard, want: false}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ok := writesToStdout(tc.w, stdout, stderr) + assert.Equal(t, tc.want, ok) + }) + } +} + +// TestWritesToStdoutAllowsStderrFile checks that when stdout and stderr are one file, as on a terminal, a log file that +// is that file is not refused: the protocol already shares it with the diagnostics. A second handle to it is that file. +func TestWritesToStdoutAllowsStderrFile(t *testing.T) { + path := filepath.Join(t.TempDir(), "terminal.txt") + shared, err := os.Create(path) + require.NoError(t, err) + t.Cleanup(func() { shared.Close() }) + second, err := os.OpenFile(path, os.O_WRONLY, 0) + require.NoError(t, err) + t.Cleanup(func() { second.Close() }) + + ok := writesToStdout(second, shared, shared) + assert.False(t, ok) + + ok = writesToStdout(shared, shared, shared) + assert.False(t, ok) +} + +// TestWritesToStdoutIgnoresStatFailure checks that a stream that cannot be stat'ed is not stdout: a closed stderr does +// not stop a protocol command, and a closed stdout is not the file a log file names. +func TestWritesToStdoutIgnoresStatFailure(t *testing.T) { + stdout := newPipe(t) + closed := newPipe(t) + require.NoError(t, closed.Close()) + file, err := os.Create(filepath.Join(t.TempDir(), "run.log")) + require.NoError(t, err) + t.Cleanup(func() { file.Close() }) + + ok := writesToStdout(stdout, stdout, closed) + assert.True(t, ok, "stdout is still stdout when stderr is closed") + + ok = writesToStdout(file, stdout, closed) + assert.False(t, ok) + + ok = writesToStdout(file, closed, stdout) + assert.False(t, ok) +} + +// TestExecuteRunsProtocolCommandWithClosedStderr checks that a protocol command without a log file runs when stderr is +// closed, as `2>&-` leaves it. +func TestExecuteRunsProtocolCommandWithClosedStderr(t *testing.T) { + closed, err := os.Create(filepath.Join(t.TempDir(), "stderr.txt")) + require.NoError(t, err) + require.NoError(t, closed.Close()) + prev := os.Stderr + os.Stderr = closed + t.Cleanup(func() { os.Stderr = prev }) + + ran := false + cmd := newRootCommand(t.Context()) + cmd.AddCommand(protocolTestCommand(&ran)) + cmd.SetErr(io.Discard) + + require.NoError(t, execute(t.Context(), cmd, []string{"serve"})) + assert.True(t, ran) +} + +// newPipe returns the write end of a pipe that the test closes. +func newPipe(t *testing.T) *os.File { + t.Helper() + pr, pw, err := os.Pipe() + require.NoError(t, err) + t.Cleanup(func() { + pr.Close() + pw.Close() + }) + return pw +} + +func TestCarriesProtocol(t *testing.T) { + assert.True(t, carriesProtocol(protocolTestCommand(new(bool)))) + assert.False(t, carriesProtocol(&cobra.Command{Use: "serve"})) +} diff --git a/cmd/root/stdout_protocol_windows.go b/cmd/root/stdout_protocol_windows.go new file mode 100644 index 00000000000..734761fd03b --- /dev/null +++ b/cmd/root/stdout_protocol_windows.go @@ -0,0 +1,10 @@ +package root + +import "os" + +// sameFileInfo reports whether a and b describe the same file. On Windows os.SameFile reports any two pipes or +// character devices as the same file, since their file ids are zero. Only regular files are compared by identity, so +// a pipe or a null device is never taken for stdout. +func sameFileInfo(a, b os.FileInfo) bool { + return a.Mode().IsRegular() && b.Mode().IsRegular() && os.SameFile(a, b) +} diff --git a/libs/aitools/mcp/binding.go b/libs/aitools/mcp/binding.go index 3ac9e145ecf..1bc7382ed1f 100644 --- a/libs/aitools/mcp/binding.go +++ b/libs/aitools/mcp/binding.go @@ -17,10 +17,6 @@ import ( "github.com/databricks/databricks-sdk-go/config" ) -// bindExitCode is the exit status for binding failures, so callers can tell them apart -// from other errors. -const bindExitCode = 2 - // Commands that binding messages tell the user to run. const ( authLoginCommand = "databricks auth login" @@ -63,11 +59,6 @@ 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 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 @@ -95,7 +86,11 @@ func Bind(ctx context.Context, opts BindOptions) (*config.Config, *Entry, error) 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 := AuthenticateBound(ctx, cfg, opts.Profile); err != nil { + pinnedFile := "" + if entry != nil && entry.ConfigFile != "" { + pinnedFile = filepath.ToSlash(configFile) + } + if err := AuthenticateBound(ctx, cfg, opts.Profile, pinnedFile); err != nil { return nil, nil, err } return cfg, entry, nil @@ -119,8 +114,9 @@ func BoundConfig(profileName, configFile string) (*config.Config, error) { } // AuthenticateBound checks that cfg, from BoundConfig for profileName, has a credential. A credential the profile cannot -// provide is AUTH_FAILED, with the login command for the profile. -func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName string) error { +// provide is AUTH_FAILED, with the login command for the profile. pinnedFile is the config file an entry pins the +// profile to, or "" when there is none; the login command names it. +func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName, pinnedFile string) error { // 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) @@ -128,7 +124,7 @@ func AuthenticateBound(ctx context.Context, cfg *config.Config, profileName stri return uncategorizedError(err) } if err := cfg.Authenticate(req); err != nil { - return AuthError(profileName, err) + return AuthErrorIn(profileName, pinnedFile, err) } return nil } @@ -157,6 +153,21 @@ func CheckBinding(ctx context.Context, state *State, service, profileName, confi return entry, nil } +// CheckUnrecordedBinding is CheckBinding for a server with no install record. Nothing records the host the server was +// bound to, so cfg, the config Bind resolved at start, stands in for the record. The messages name no record to remove, +// since there is none. +func CheckUnrecordedBinding(service, profileName, configFile string, cfg *config.Config) error { + p, err := profileFor(service, profileName, configFile, nil) + if err != nil { + return err + } + bound := Entry{Host: cfg.Host, WorkspaceID: cfg.WorkspaceID} + if drift := driftOf(bound, p); drift != nil { + return unrecordedDriftError(service, profileName, bound, *drift) + } + return nil +} + // profileFor returns the profile profileName in configFile, or the PROFILE_NOT_FOUND error that says it // is gone. name and entry describe the state entry the profile serves; entry is nil without one. func profileFor(name, profileName, configFile string, entry *Entry) (profile.Profile, error) { @@ -258,6 +269,13 @@ func driftError(name, profileName string, entry Entry, drift Drift) error { return &BindError{Category: BindProfileHostChanged, message: message} } +// unrecordedDriftError is the PROFILE_HOST_CHANGED error for a server with no install record. A restart binds the server +// to the profile's current host, as the startup check does without a record. +func unrecordedDriftError(service, profileName string, bound Entry, drift Drift) error { + message := DriftMessage("MCP server "+service, bound, profileName, drift) + ". Restart the MCP server in your agent to use the current host" + 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 { @@ -269,10 +287,26 @@ func configFileError(name, profileName, recordedPath, currentPath string) error // 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, shellquote.BashArg(profileName)) + return AuthErrorIn(profileName, "", cause) +} + +// AuthErrorIn is AuthError for a profile pinned to configFile. The login command names configFile, since the login must +// write that file; pass "" when the login's default file is that file. +func AuthErrorIn(profileName, configFile string, cause error) error { + message := fmt.Sprintf("%s\nRun: %s", cause, LoginCommand(profileName, configFile)) return &BindError{Category: BindAuthFailed, message: message, cause: cause} } +// LoginCommand returns the command that logs in profileName. configFile is the file the login must write, passed as +// DATABRICKS_CONFIG_FILE; pass "" when the login's default file is that file. +func LoginCommand(profileName, configFile string) string { + command := authLoginCommand + " --profile " + shellquote.BashArg(profileName) + if configFile != "" { + command = "DATABRICKS_CONFIG_FILE=" + shellquote.BashArg(configFile) + " " + command + } + return command +} + // 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. Service names are compared without case, as Unity Catalog // names are. Both sides are compared as resolved paths, so an entry with no pinned file matches the default file. diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 86986184874..f4a49002de3 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -14,6 +14,7 @@ import ( "github.com/databricks/cli/libs/aitools/mcp" "github.com/databricks/cli/libs/env" "github.com/databricks/cli/libs/shellquote" + "github.com/databricks/databricks-sdk-go/config" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -297,7 +298,7 @@ func TestBindNeverUsesEnvTokenForProfileWithoutToken(t *testing.T) { Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindAuthFailed) - assert.Contains(t, err.Error(), "\nRun: databricks auth login --profile logfood") + assert.Contains(t, err.Error(), "\nRun: "+mcp.LoginCommand(bindingTestProfile, filepath.ToSlash(resolvedConfigFile(t, cfgPath)))) assert.NotContains(t, err.Error(), "dapi-env-token") assert.Error(t, errors.Unwrap(err)) } @@ -354,7 +355,7 @@ func TestBindTokenCannotBeMinted(t *testing.T) { Stderr: &bytes.Buffer{}, }) requireBindError(t, err, mcp.BindAuthFailed) - assert.Contains(t, err.Error(), "\nRun: databricks auth login --profile logfood") + assert.Contains(t, err.Error(), "\nRun: "+mcp.LoginCommand(bindingTestProfile, filepath.ToSlash(resolvedConfigFile(t, cfgPath)))) assert.Error(t, errors.Unwrap(err)) } @@ -391,7 +392,6 @@ func requireBindError(t *testing.T, err error, want mcp.BindCategory) { var bindErr *mcp.BindError require.ErrorAs(t, err, &bindErr) assert.Equal(t, want, bindErr.Category) - assert.Equal(t, 2, bindErr.ExitCode()) } // bindingTestDefaultConfig writes contents to ~/.databrickscfg in a fresh home directory, with @@ -878,3 +878,53 @@ func TestCheckBindingChecksProfileWithoutEntry(t *testing.T) { }) } } + +// TestCheckUnrecordedBindingWordsChangesWithoutRecord checks the runtime check for a server with no install record. Its +// messages name no record to remove, and the missing-profile message is the one the startup check gives. +func TestCheckUnrecordedBindingWordsChangesWithoutRecord(t *testing.T) { + tests := []struct { + name string + contents string + host string // the host the server was bound to at start + category mcp.BindCategory + want func(configFile string) string + }{ + { + name: "profile missing", + contents: "[other]\nhost = https://other.test\n", + host: bindingTestHost, + category: mcp.BindProfileNotFound, + want: func(configFile string) string { + return fmt.Sprintf(`MCP server system.ai.github needs Databricks profile "logfood", which does not exist in %s. Set it up with: databricks auth login --profile logfood`, configFile) + }, + }, + { + name: "host changed", + contents: bindingTestProfileContents(bindingTestToken), + host: "https://adb-999.test", + category: mcp.BindProfileHostChanged, + want: func(string) string { + return `MCP server system.ai.github was set up for https://adb-999.test, but profile "logfood" now points at https://adb-123.test. Restart the MCP server in your agent to use the current host` + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + cfgPath := bindingTestConfig(t, tt.contents) + resolved := bindingTestResolved(t, cfgPath) + + err := mcp.CheckUnrecordedBinding(bindingTestService, bindingTestProfile, resolved, &config.Config{Host: tt.host}) + requireBindError(t, err, tt.category) + assert.Equal(t, tt.want(filepath.ToSlash(resolved)), err.Error()) + }) + } +} + +// resolvedConfigFile returns the config file path that Bind names in its hints: symlinks in path are resolved, as +// ResolveConfigFile does (on macOS the temporary directory is under a symlink). +func resolvedConfigFile(t *testing.T, path string) string { + t.Helper() + resolved, err := mcp.ResolveConfigFile(t.Context(), path) + require.NoError(t, err) + return resolved +} diff --git a/libs/aitools/mcp/proxy/bridge.go b/libs/aitools/mcp/proxy/bridge.go index cdbb111d062..967778844a8 100644 --- a/libs/aitools/mcp/proxy/bridge.go +++ b/libs/aitools/mcp/proxy/bridge.go @@ -51,6 +51,9 @@ var ( errNoResponse = errors.New("upstream sent no response") // errUpstreamTooLarge is the error for an upstream message over maxLineBytes, whether it is a body or an event. errUpstreamTooLarge = fmt.Errorf("an upstream message exceeds the %d MiB limit", maxLineBytes>>20) + // ErrSkipRequest is returned by Authenticate to leave the session DELETE unsent, which needs no answer. Any other + // request that returns it fails like any other. + ErrSkipRequest = errors.New("request skipped") ) // Hooks intercepts upstream messages before they are written to stdout. @@ -69,7 +72,7 @@ type Bridge struct { Err io.Writer Endpoint string // Authenticate sets the Authorization header on each upstream request. It is required and must not be nil, since - // every upstream request calls it. + // every upstream request calls it. It may return ErrSkipRequest for the session DELETE (see ErrSkipRequest). Authenticate func(*http.Request) error // Headers are set on every upstream request. Headers map[string]string @@ -318,9 +321,12 @@ func (c *conn) deleteSession(ctx context.Context, sent upstreamState) { } } -// sendDelete sends the DELETE that ends the session sent. +// sendDelete sends the DELETE that ends the session sent. A DELETE that Authenticate skips (ErrSkipRequest) is not sent. func (c *conn) sendDelete(ctx context.Context, sent upstreamState) error { req, err := c.newRequest(ctx, http.MethodDelete, nil, sent) + if errors.Is(err, ErrSkipRequest) { + return nil + } if err != nil { return err } diff --git a/libs/aitools/mcp/proxy/bridge_test.go b/libs/aitools/mcp/proxy/bridge_test.go index dc9ab6b92f1..fb1b07a78c6 100644 --- a/libs/aitools/mcp/proxy/bridge_test.go +++ b/libs/aitools/mcp/proxy/bridge_test.go @@ -1928,6 +1928,32 @@ func TestRunRecordsSessionOnlyFromInitializeResult(t *testing.T) { assert.Empty(t, deletedSessions(fake), "no session was started, so none is deleted") } +// Authenticate returns ErrSkipRequest for the session DELETE: no DELETE is sent, and nothing is logged, since the +// session is over. +func TestRunSkipsSessionDeleteWhenAuthenticateSkipsIt(t *testing.T) { + srv, fake := newFakeEndpoint(t, func(w http.ResponseWriter, r *http.Request) { + if r.Method == http.MethodDelete { + w.WriteHeader(http.StatusOK) + return + } + w.Header().Set("Mcp-Session-Id", "s1") + answer(w, r) + }) + b := bridgeFor(srv.URL) + authenticate := b.Authenticate + b.Authenticate = func(r *http.Request) error { + if r.Method == http.MethodDelete { + return ErrSkipRequest + } + return authenticate(r) + } + + _, errOut := runAll(t, b, `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`+"\n") + + assert.Empty(t, deletedSessions(fake)) + assert.Empty(t, errOut) +} + // deletedSessions returns the Mcp-Session-Id of each DELETE the fake endpoint received, in order. func deletedSessions(fake *fakeEndpoint) []string { var sessions []string diff --git a/libs/aitools/mcp/proxy/run.go b/libs/aitools/mcp/proxy/run.go new file mode 100644 index 00000000000..5aa60d6322c --- /dev/null +++ b/libs/aitools/mcp/proxy/run.go @@ -0,0 +1,68 @@ +package proxy + +import ( + "context" + "fmt" + "io" + "maps" + "net/http" + + "github.com/databricks/cli/libs/auth" + "github.com/databricks/databricks-sdk-go/config" + "github.com/databricks/databricks-sdk-go/useragent" +) + +// User-Agent extras of a proxied session. useragent.FromContext carries cli/ but not +// agent/, so Run adds that from useragent.AgentProvider. The extras identify the proxy and +// the calling agent. +const ( + proxyAgentKey = "aitools-mcp" + clientAgentKey = "aitools-mcp-client" +) + +// Options is one `serve` session. The binding checks (mcp.Bind) have already passed. +type Options struct { + In io.Reader + Out io.Writer + Err io.Writer + // Endpoint is the service URL, as mcp.ServiceURL builds it. + Endpoint string + // Config is the bound profile's config. Its host is named in the instructions, and its + // workspace ID is sent as the routing header. + Config *config.Config + // Authenticate sets the Authorization header on each upstream request. + Authenticate func(*http.Request) error + // Profile is the bound profile's name, named in the instructions. + Profile string + // Agent is the registry name of the calling agent, or "" when --agent was not given. + Agent string + // Transport performs the upstream requests. Nil means http.DefaultTransport, as in Bridge. + Transport http.RoundTripper +} + +// Run bridges opts.In to the service endpoint until In closes or ctx is done. It returns an error +// only when reading In fails. +func Run(ctx context.Context, opts Options) error { + ctx = useragent.InContext(ctx, proxyAgentKey, "proxy") + if opts.Agent != "" { + ctx = useragent.InContext(ctx, clientAgentKey, opts.Agent) + } + if agent := useragent.AgentProvider(); agent != "" { + ctx = useragent.InContext(ctx, useragent.AgentKey, agent) + } + + headers := map[string]string{"User-Agent": useragent.FromContext(ctx)} + maps.Copy(headers, auth.WorkspaceIDHeaders(opts.Config)) + + b := &Bridge{ + In: opts.In, + Out: opts.Out, + Err: opts.Err, + Endpoint: opts.Endpoint, + Authenticate: opts.Authenticate, + Headers: headers, + Transport: opts.Transport, + Instructions: fmt.Sprintf("This server is bound to Databricks workspace %s (CLI profile \"%s\"). Its tools act on that workspace only.", opts.Config.Host, opts.Profile), + } + return b.Run(ctx) +} diff --git a/libs/aitools/mcp/proxy/run_test.go b/libs/aitools/mcp/proxy/run_test.go new file mode 100644 index 00000000000..852298a6df0 --- /dev/null +++ b/libs/aitools/mcp/proxy/run_test.go @@ -0,0 +1,91 @@ +package proxy + +import ( + "io" + "net/http" + "strings" + "testing" + + "github.com/databricks/databricks-sdk-go/config" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const initializeResult = `{"protocolVersion":"2025-06-18","capabilities":{"tools":{"listChanged":false}},` + + `"serverInfo":{"name":"genie_chat"},"instructions":"Base."}` + +// answerInitialize replies to initialize with initializeResult and to anything else with an empty result. +func answerInitialize(w http.ResponseWriter, r *http.Request) { + body, _ := io.ReadAll(r.Body) + m, _ := parseMessage(body) + w.Header().Set("Content-Type", "application/json") + if m.Method == "initialize" { + _, _ = io.WriteString(w, `{"jsonrpc":"2.0","id":1,"result":`+initializeResult+`}`) + return + } + answer(w, r) +} + +// runOptions returns Options that send input from stdin to endpoint as profile logfood and agent claude-code, with +// testToken as the authorization. It also returns the buffers that receive stdout and stderr. +func runOptions(t *testing.T, endpoint string, cfg *config.Config, input string) (Options, *syncBuffer, *syncBuffer) { + t.Helper() + var out, errOut syncBuffer + return Options{ + In: strings.NewReader(input), + Out: &out, + Err: &errOut, + Endpoint: endpoint, + Config: cfg, + Authenticate: func(r *http.Request) error { + r.Header.Set("Authorization", testToken) + return nil + }, + Profile: "logfood", + Agent: "claude-code", + }, &out, &errOut +} + +func TestRunSendsProxyUserAgentAndRoutingHeader(t *testing.T) { + srv, fake := newFakeEndpoint(t, answerInitialize) + cfg := &config.Config{Host: "https://workspace.test", WorkspaceID: "1965859176160743"} + opts, _, _ := runOptions(t, srv.URL, cfg, `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`+"\n") + + require.NoError(t, Run(t.Context(), opts)) + + reqs := fake.requests() + require.Len(t, reqs, 1) + ua := reqs[0].Header.Get("User-Agent") + assert.Contains(t, ua, "aitools-mcp/proxy") + assert.Contains(t, ua, "aitools-mcp-client/claude-code") + assert.Equal(t, "1965859176160743", reqs[0].Header.Get("X-Databricks-Workspace-Id")) + assert.Equal(t, testToken, reqs[0].Header.Get("Authorization")) +} + +func TestRunOmitsRoutingHeaderWithoutWorkspaceID(t *testing.T) { + srv, fake := newFakeEndpoint(t, answerInitialize) + opts, _, _ := runOptions(t, srv.URL, &config.Config{Host: "https://workspace.test"}, `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`+"\n") + opts.Agent = "" + + require.NoError(t, Run(t.Context(), opts)) + + reqs := fake.requests() + require.Len(t, reqs, 1) + assert.Empty(t, reqs[0].Header.Values("X-Databricks-Workspace-Id")) + assert.NotContains(t, reqs[0].Header.Get("User-Agent"), "aitools-mcp-client") +} + +func TestRunNamesWorkspaceAndProfileInInstructions(t *testing.T) { + srv, _ := newFakeEndpoint(t, answerInitialize) + opts, out, _ := runOptions(t, srv.URL, &config.Config{Host: "https://workspace.test"}, + `{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`+"\n") + + require.NoError(t, Run(t.Context(), opts)) + + lines := outLines(out.String()) + require.Len(t, lines, 1) + reply := parseReply(t, lines[0]) + assert.Equal(t, + "Base.\n\nThis server is bound to Databricks workspace https://workspace.test (CLI profile \"logfood\"). Its tools act on that workspace only.", + reply.Result["instructions"]) +} diff --git a/libs/testserver/handlers.go b/libs/testserver/handlers.go index 970a9560656..e21c6f0ea7d 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -981,6 +981,10 @@ func AddDefaultHandlers(server *Server) { return MapDelete(req.Workspace, req.Workspace.McpServices, req.Vars["name"]) }) + server.Handle("POST", "/ai-gateway/mcp-services/{name}", func(req Request) any { + return req.Workspace.McpEndpoint(req, req.Vars["name"]) + }) + // Feature Engineering features: server.Handle("POST", "/api/2.0/feature-engineering/features", func(req Request) any { return req.Workspace.FeaturesCreate(req) diff --git a/libs/testserver/mcp_endpoint.go b/libs/testserver/mcp_endpoint.go new file mode 100644 index 00000000000..52b600370b1 --- /dev/null +++ b/libs/testserver/mcp_endpoint.go @@ -0,0 +1,133 @@ +package testserver + +import ( + "encoding/json" + "net/http" + "strings" + + "github.com/databricks/databricks-sdk-go/service/catalog" +) + +// JSON-RPC codes the fake endpoint answers with. +const ( + rpcCodeParseError = -32700 + rpcCodeMethodNotFound = -32601 + rpcCodeInvalidParams = -32602 + // rpcCodeSignInRequired is the gateway's answer to a service that needs a credential the + // caller does not have. + rpcCodeSignInRequired = -32006 +) + +const ( + jsonRPCVersion = "2.0" + fakeMcpProtocolVersion = "2025-06-18" + fakeMcpInstructions = "Fake Databricks MCP service for tests." + fakeMcpEchoTool = "echo" +) + +// mcpRPC is the part of a client message the fake reads: the id, the method, and the tool call's name and arguments. +type mcpRPC struct { + ID json.RawMessage `json:"id,omitempty"` + Method string `json:"method"` + Params struct { + Name string `json:"name"` + Arguments map[string]any `json:"arguments"` + } `json:"params"` +} + +// McpEndpoint fakes POST /ai-gateway/mcp-services/{name}, a streamable HTTP MCP server with one +// tool, echo. A name the workspace has no service for is 404. A service stored with an OAuth source +// connection is external: no credential is ever mapped to it here, so tools/list and tools/call +// answer -32006 as the gateway does before sign-in. Notifications and client responses, which have +// no method or no id, get 202 with no body. +func (s *FakeWorkspace) McpEndpoint(req Request, name string) Response { + defer s.LockUnlock()() + + service, ok := s.mcpServiceNamed(name) + if !ok { + return Response{StatusCode: http.StatusNotFound} + } + var msg mcpRPC + if err := json.Unmarshal(req.Body, &msg); err != nil { + return Response{StatusCode: http.StatusBadRequest, Body: rpcError(nil, rpcCodeParseError, "Parse error")} + } + if msg.ID == nil || msg.Method == "" { + return Response{StatusCode: http.StatusAccepted} + } + + external := mcpServiceIsExternal(service) + switch msg.Method { + case "initialize": + return rpcResult(msg.ID, map[string]any{ + "protocolVersion": fakeMcpProtocolVersion, + "capabilities": map[string]any{"tools": map[string]any{"listChanged": true}}, + "serverInfo": map[string]any{"name": "fake_" + name}, + "instructions": fakeMcpInstructions, + }) + case "tools/list": + if external { + return signInRequired(msg.ID) + } + return rpcResult(msg.ID, map[string]any{"tools": []any{ + map[string]any{ + "name": fakeMcpEchoTool, + "description": "Returns the text argument", + "inputSchema": map[string]any{ + "type": "object", + "properties": map[string]any{"text": map[string]any{"type": "string"}}, + }, + }, + }}) + case "tools/call": + if external { + return signInRequired(msg.ID) + } + if msg.Params.Name != fakeMcpEchoTool { + return Response{Body: rpcError(msg.ID, rpcCodeInvalidParams, "Unknown tool: "+msg.Params.Name)} + } + text, _ := msg.Params.Arguments["text"].(string) + return rpcResult(msg.ID, map[string]any{ + "content": []any{map[string]any{"type": "text", "text": text}}, + "isError": false, + }) + default: + return Response{Body: rpcError(msg.ID, rpcCodeMethodNotFound, "Method not found: "+msg.Method)} + } +} + +// mcpServiceNamed returns the stored service with the given full name. Unity Catalog names are case-insensitive, so the +// match ignores case, as McpServicesList does. +func (s *FakeWorkspace) mcpServiceNamed(name string) (catalog.McpService, bool) { + for key, service := range s.McpServices { + if strings.EqualFold(key, name) { + return service, true + } + } + return catalog.McpService{}, false +} + +// mcpServiceIsExternal reports whether the service takes an OAuth source connection. +func mcpServiceIsExternal(ms catalog.McpService) bool { + return ms.Config != nil && ms.Config.SourceConnection != nil && + ms.Config.SourceConnection.Options["oauth_provider"] != "" +} + +// rpcResult is the response to the request with id, carrying result. +func rpcResult(id json.RawMessage, result any) Response { + return Response{Body: map[string]any{"jsonrpc": jsonRPCVersion, "id": id, "result": result}} +} + +// rpcError is the error response to the request with id. +func rpcError(id json.RawMessage, code int, message string) map[string]any { + return map[string]any{ + "jsonrpc": jsonRPCVersion, + "id": id, + "error": map[string]any{"code": code, "message": message, "data": nil}, + } +} + +// signInRequired is the answer to a request for an external service, which the gateway gives before sign-in. +func signInRequired(id json.RawMessage) Response { + return Response{Body: rpcError(id, rpcCodeSignInRequired, + "The MCP service request was rejected (HTTP 401). Sign-in is required.")} +} diff --git a/libs/testserver/mcp_endpoint_test.go b/libs/testserver/mcp_endpoint_test.go new file mode 100644 index 00000000000..8c17f3ca621 --- /dev/null +++ b/libs/testserver/mcp_endpoint_test.go @@ -0,0 +1,96 @@ +package testserver + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// mcpEndpointTestCall posts one JSON-RPC message to the fake endpoint for name and decodes the reply. +func mcpEndpointTestCall(t *testing.T, workspace *FakeWorkspace, name, message string) map[string]any { + t.Helper() + response := workspace.McpEndpoint(Request{Body: []byte(message)}, name) + require.Equal(t, 0, response.StatusCode) + encoded, err := json.Marshal(response.Body) + require.NoError(t, err) + var reply map[string]any + require.NoError(t, json.Unmarshal(encoded, &reply)) + return reply +} + +func TestMcpEndpointAnswersInternalService(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + const name = "system.ai.genie_one_mcp" + mcpServicesTestSeed(workspace, name, "Genie", "") + + reply := mcpEndpointTestCall(t, workspace, name, `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`) + assert.Nil(t, reply["error"]) + result := reply["result"].(map[string]any) + assert.Equal(t, "echo", result["tools"].([]any)[0].(map[string]any)["name"]) + + reply = mcpEndpointTestCall(t, workspace, name, + `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"echo","arguments":{"text":"hi"}}}`) + content := reply["result"].(map[string]any)["content"].([]any) + assert.Equal(t, "hi", content[0].(map[string]any)["text"]) +} + +// TestMcpEndpointMatchesServiceNameWithoutCase checks that the endpoint matches a service name whatever its case, as +// Unity Catalog names are case-insensitive and the listing already matches them that way. +func TestMcpEndpointMatchesServiceNameWithoutCase(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + mcpServicesTestSeed(workspace, "system.ai.genie_one_mcp", "Genie", "") + + reply := mcpEndpointTestCall(t, workspace, "SYSTEM.AI.Genie_One_MCP", `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`) + assert.Nil(t, reply["error"]) + assert.NotNil(t, reply["result"]) +} + +func TestMcpEndpointRejectsExternalServiceWithoutCredential(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + const name = "system.ai.github" + mcpServicesTestSeed(workspace, name, "GitHub", mcpServicesTestGitHubProvider) + + reply := mcpEndpointTestCall(t, workspace, name, `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`) + errBody := reply["error"].(map[string]any) + assert.EqualValues(t, -32006, errBody["code"]) + assert.Nil(t, reply["result"]) +} + +// TestMcpEndpointUnknownServiceIsNotFound checks that a name no service has is 404, whatever the message. The name is +// one letter short of the seeded service, so it also shows that the endpoint matches whole names, not prefixes. +func TestMcpEndpointUnknownServiceIsNotFound(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + mcpServicesTestSeed(workspace, "system.ai.genie_one_mcp", "Genie", "") + + for _, body := range []string{ + `{"jsonrpc":"2.0","id":1,"method":"tools/list"}`, + `{"jsonrpc":"2.0","method":"notifications/initialized"}`, + } { + response := workspace.McpEndpoint(Request{Body: []byte(body)}, "system.ai.genie_one_mc") + assert.Equal(t, 404, response.StatusCode, body) + assert.Nil(t, response.Body) + } +} + +// TestMcpEndpointNoBodyForNotificationsAndResponses checks that a notification, and a client's answer to a request from +// the server (an id and no method), get 202 with no body. +func TestMcpEndpointNoBodyForNotificationsAndResponses(t *testing.T) { + tests := []struct { + name string + body string + }{ + {name: "notification", body: `{"jsonrpc":"2.0","method":"notifications/initialized"}`}, + {name: "client response", body: `{"jsonrpc":"2.0","id":7,"result":{}}`}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + workspace := NewFakeWorkspace("http://test", "dbapi123") + mcpServicesTestSeed(workspace, "system.ai.genie_one_mcp", "Genie", "") + response := workspace.McpEndpoint(Request{Body: []byte(tc.body)}, "system.ai.genie_one_mcp") + assert.Equal(t, 202, response.StatusCode) + assert.Nil(t, response.Body) + }) + } +} diff --git a/main.go b/main.go index 0b4e8e48a0b..a08ab8c2ec9 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "github.com/databricks/cli/cmd" "github.com/databricks/cli/cmd/aitools" + "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/libs/aitools/autoinstall" // Registers a disk-cached HostMetadataResolver factory on the SDK so every @@ -33,6 +34,6 @@ func main() { ctx := context.Background() err := cmd.New(ctx).Execute(ctx, os.Args[1:]) if err != nil { - os.Exit(1) + os.Exit(root.ExitStatus(err)) } }