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..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-binding-errors/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood 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..14931a34324 --- /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 +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 +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..0369be633c5 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/.databrickscfg @@ -0,0 +1,3 @@ +[logfood] +host = $DATABRICKS_HOST +token = test-token-logfood 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..eba527069a2 --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/output.txt @@ -0,0 +1,23 @@ + +=== 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 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..9a16ce3999c --- /dev/null +++ b/acceptance/cmd/aitools/mcp/serve-passthrough/script @@ -0,0 +1,25 @@ +# 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 diff --git a/cmd/aitools/mcp.go b/cmd/aitools/mcp.go index 69a7f872166..b275848610e 100644 --- a/cmd/aitools/mcp.go +++ b/cmd/aitools/mcp.go @@ -39,6 +39,7 @@ func NewMcpCmd() *cobra.Command { cmd.AddCommand(NewMcpAddCmd()) cmd.AddCommand(NewMcpListCmd()) cmd.AddCommand(NewMcpRemoveCmd()) + cmd.AddCommand(NewMcpServeCmd()) return cmd } diff --git a/cmd/aitools/mcp_add_test.go b/cmd/aitools/mcp_add_test.go index 6169d390538..1cad4af0ea6 100644 --- a/cmd/aitools/mcp_add_test.go +++ b/cmd/aitools/mcp_add_test.go @@ -882,6 +882,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..dd5446eb05e --- /dev/null +++ b/cmd/aitools/mcp_serve.go @@ -0,0 +1,287 @@ +package aitools + +import ( + "context" + "crypto/tls" + "encoding/json" + "errors" + "fmt" + "io" + "net" + "net/http" + "os" + "os/signal" + "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/u2m" + "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 +} + +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. +// +// Known limitation: the binding is checked only when serve starts. If the profile is pointed at another host during +// the session, for example by `databricks auth login --profile P --host B`, serve keeps sending the profile's token, +// which is now B's, to the endpoint of the host it was bound to. +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, _, err := mcp.Bind(cmd.Context(), mcp.BindOptions{ + Profile: profileName, + Service: f.service, + State: state, + Stderr: cmd.ErrOrStderr(), + }) + if err != nil { + return serveFailure(cmd, err, serveBindStatus) + } + + return serveProxy(cmd, proxy.Options{ + In: cmd.InOrStdin(), + Out: cmd.OutOrStdout(), + Err: cmd.ErrOrStderr(), + Endpoint: mcp.ServiceURL(cfg.Host, f.service), + Config: cfg, + Authenticate: cfg.Authenticate, + Profile: profileName, + Agent: serveAgent(f.agent, cmd.ErrOrStderr()), + Transport: serveTransport(cfg), + }) +} + +// serveProxy bridges opts to the service until stdin closes or a signal arrives. Authenticate goes through a +// refreshGuard, so a rejected token stops the proxy; see proxyResult. +func serveProxy(cmd *cobra.Command, opts proxy.Options) error { + ctx, stop := serveSignalContext(cmd.Context()) + defer stop() + refresh := &refreshGuard{authenticate: opts.Authenticate, profile: opts.Profile, 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(), "stopped after a token refresh failure for profile %s\n", 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 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.Fprintln(cmd.ErrOrStderr(), err) + return exitAlreadyPrinted(err, status) +} + +// 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. 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 rejection. +type refreshGuard struct { + authenticate func(*http.Request) error + profile string + 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 !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, then restart the MCP server in your agent", mcp.AuthError(g.profile, err)) + g.mu.Lock() + first := g.err == nil + if first { + g.err = authErr + } + g.mu.Unlock() + if !first { + return g.stoppedError() + } + g.cancel() + return authErr +} + +// stoppedError is the error for a request that arrives after the refresh failure. It does not repeat the login hint. +func (g *refreshGuard) stoppedError() error { + return fmt.Errorf("the session is stopping after a token refresh failure for profile %s", g.profile) +} + +// 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, 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 + } + // 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..a49f8f85693 --- /dev/null +++ b/cmd/aitools/mcp_serve_test.go @@ -0,0 +1,741 @@ +package aitools + +import ( + "bufio" + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "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/cmdio" + "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'") +} + +// TestRefreshGuardTransientFailureFailsOnlyThatRequest checks that a token failure which is not a rejection fails that +// request with the login hint and nothing else. 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) +} + +// 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) { + oauthResponse := func(status int, body string) error { + return &httpclient.HttpError{Response: &http.Response{StatusCode: status}, Message: body} + } + 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}, + } + 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, cause.Error()+"\n", stderr.String()) + assert.ErrorIs(t, err, root.ErrAlreadyPrinted) + assert.Equal(t, 2, root.ExitStatus(err)) + assert.ErrorIs(t, err, cause) +} + +// 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, "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, then restart the MCP server in your agent" + refreshStoppedLine = "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 with the 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()) +} + +// 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) + } + } +} diff --git a/cmd/aitools/mcp_test.go b/cmd/aitools/mcp_test.go index 6625a722351..e4fa4036c4d 100644 --- a/cmd/aitools/mcp_test.go +++ b/cmd/aitools/mcp_test.go @@ -31,7 +31,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 NewAitoolsCmd().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 59b9777ef69..064f8771990 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" @@ -66,7 +68,7 @@ func New(ctx context.Context) *cobra.Command { } // Configure default logger. - ctx, err = logFlags.initializeContext(ctx) + ctx, err = logFlags.initializeContext(ctx, cmd) if err != nil { return err } @@ -76,7 +78,7 @@ func New(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) @@ -154,6 +156,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) { @@ -170,6 +186,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 @@ -179,21 +197,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 ce1584eacd4..b4e68f32097 100644 --- a/cmd/root/root_test.go +++ b/cmd/root/root_test.go @@ -3,7 +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" @@ -177,3 +183,61 @@ func TestExecuteErrAlreadyPrintedNotEnriched(t *testing.T) { require.Error(t, err) assert.Empty(t, stderr.String()) } + +func TestExecuteLogsExitStatus(t *testing.T) { + keepDefaultLogger(t) + logFile := filepath.Join(t.TempDir(), "run.log") + cmd := New(t.Context()) + cmd.AddCommand(&cobra.Command{ + Use: "fail", + RunE: func(*cobra.Command, []string) error { + return &ExitStatusError{Status: 2, Err: errors.New("binding failed")} + }, + }) + cmd.SetArgs([]string{"fail", "--log-file", logFile, "--log-level", "info", "--log-format", "json"}) + cmd.SetErr(io.Discard) + + err := Execute(t.Context(), cmd) + 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 := New(t.Context()) + cmd.AddCommand(&cobra.Command{ + Use: "ok", + RunE: func(*cobra.Command, []string) error { return nil }, + }) + cmd.SetArgs([]string{"ok", "--log-file", logFile, "--log-level", "debug"}) + cmd.SetErr(io.Discard) + require.NoError(t, Execute(t.Context(), cmd)) + + 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..727620841df 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,18 @@ 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..67c66ae2f10 --- /dev/null +++ b/cmd/root/stdout_protocol_test.go @@ -0,0 +1,224 @@ +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 := New(t.Context()) + cmd.AddCommand(protocolTestCommand(&ran)) + cmd.SetArgs([]string{"serve", "--log-file", logFile, "--log-level", "debug"}) + cmd.SetErr(io.Discard) + + err := Execute(t.Context(), cmd) + 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 := New(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.SetArgs([]string{"serve", "--log-file", logFile}) + cmd.SetErr(io.Discard) + + err := Execute(t.Context(), cmd) + 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 := New(t.Context()) + cmd.AddCommand(protocolTestCommand(&ran)) + cmd.SetArgs([]string{"serve", "--log-file", logFile, "--log-level", "debug"}) + cmd.SetErr(io.Discard) + + require.NoError(t, Execute(t.Context(), cmd)) + 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 := New(t.Context()) + cmd.AddCommand(protocolTestCommand(&ran)) + cmd.SetArgs([]string{"serve"}) + cmd.SetErr(io.Discard) + + require.NoError(t, Execute(t.Context(), cmd)) + 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 20f68088306..a886f378f23 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" @@ -62,11 +58,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 diff --git a/libs/aitools/mcp/binding_test.go b/libs/aitools/mcp/binding_test.go index 2d58a95d138..af26cead5fb 100644 --- a/libs/aitools/mcp/binding_test.go +++ b/libs/aitools/mcp/binding_test.go @@ -391,7 +391,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 diff --git a/libs/aitools/mcp/proxy/bridge.go b/libs/aitools/mcp/proxy/bridge.go index bc3357e7619..5e22aa33bfa 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. @@ -68,7 +71,8 @@ type Bridge struct { Out io.Writer Err io.Writer Endpoint string - // Authenticate sets the Authorization header on each upstream request. + // Authenticate sets the Authorization header on each upstream request. 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 @@ -305,9 +309,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 0c3e5e09556..89fe538b7a8 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 00d3e1fc9af..9e91a98949b 100644 --- a/libs/testserver/handlers.go +++ b/libs/testserver/handlers.go @@ -939,6 +939,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 6c4dddd40b6..9e75a969ce8 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,6 @@ func main() { ctx := context.Background() err := root.Execute(ctx, cmd.New(ctx)) if err != nil { - os.Exit(1) + os.Exit(root.ExitStatus(err)) } }