Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[logfood]
host = $DATABRICKS_HOST
token = test-token-logfood-$UNIQUE_NAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
41 changes: 41 additions & 0 deletions acceptance/cmd/aitools/mcp/serve-binding-errors/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

=== The bound profile is not in the config file

>>> errcode [CLI] aitools mcp serve --profile gone --service system.ai.genie_one_mcp
Error: MCP server system.ai.genie_one_mcp needs Databricks profile "gone", which does not exist in [TEST_TMP_DIR]/.databrickscfg. Set it up with: databricks auth login --profile gone

Exit code: 2
stdout bytes: 0

=== The profile now points at another host than the one recorded

>>> errcode [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp
Error: MCP server genie_one_mcp-logfood was set up for https://other.test, but profile "logfood" now points at [DATABRICKS_URL]. Run: databricks aitools mcp remove --names genie_one_mcp-logfood and add it again

Exit code: 2
stdout bytes: 0

=== Flags are required and validated before the binding checks

>>> errcode [CLI] aitools mcp serve --service system.ai.genie_one_mcp
Error: --profile is required

Exit code: 1

>>> errcode [CLI] aitools mcp serve --profile logfood --service github
Error: --service needs a full service name such as system.ai.github

Exit code: 1

>>> errcode [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --output json
Error: --output does not apply to serve: its stdout carries the MCP protocol

Exit code: 1

=== A log file on stdout is refused before anything is logged

>>> errcode [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --log-file stdout --log-level debug
Error: databricks aitools mcp serve writes protocol messages to stdout, so its logs cannot go to stdout; use --log-file stderr or a file

Exit code: 1
stdout bytes: 0
Original file line number Diff line number Diff line change
@@ -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"}}}
55 changes: 55 additions & 0 deletions acceptance/cmd/aitools/mcp/serve-binding-errors/script
Original file line number Diff line number Diff line change
@@ -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 <<JSON
{
"schema_version": 3,
"mcp": {
"servers": [
{
"name": "genie_one_mcp-logfood",
"service": "system.ai.genie_one_mcp",
"profile": "logfood",
"host": "https://other.test",
"workspace_id": "",
"sign_in_required": false,
"command": "",
"config_file": "$cfgdir/.databrickscfg",
"agents": {},
"installed_at": "2026-01-01T00:00:00Z",
"cli_version": "0.0.0"
}
]
}
}
JSON

title "The profile now points at another host than the one recorded"
trace errcode $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp < requests.ndjson > 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 ' ')"
3 changes: 3 additions & 0 deletions acceptance/cmd/aitools/mcp/serve-passthrough/.databrickscfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[logfood]
host = $DATABRICKS_HOST
token = test-token-logfood-$UNIQUE_NAME
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Cloud = false
32 changes: 32 additions & 0 deletions acceptance/cmd/aitools/mcp/serve-passthrough/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

=== Stdout holds one JSON-RPC message per line; the notification gets no reply
{"id":1,"instructions":"Fake Databricks MCP service for tests.\n\nThis server is bound to Databricks workspace [DATABRICKS_URL] (CLI profile \"logfood\"). Its tools act on that workspace only.","tools":[],"text":null,"error":null}
{"id":2,"instructions":null,"tools":["echo"],"text":null,"error":null}
{"id":3,"instructions":null,"tools":[],"text":"hello","error":null}

=== Diagnostics go to stderr only

>>> cat LOG.stderr

>>> [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code
No install record for system.ai.genie_one_mcp on profile "logfood"; using its current host [DATABRICKS_URL]

=== An agent this binary does not know is accepted
{"id":1,"instructions":"Fake Databricks MCP service for tests.\n\nThis server is bound to Databricks workspace [DATABRICKS_URL] (CLI profile \"logfood\"). Its tools act on that workspace only.","tools":[],"text":null,"error":null}
{"id":2,"instructions":null,"tools":["echo"],"text":null,"error":null}
{"id":3,"instructions":null,"tools":[],"text":"hello","error":null}

>>> cat LOG.future

>>> [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent future-agent
No install record for system.ai.genie_one_mcp on profile "logfood"; using its current host [DATABRICKS_URL]
unknown agent "future-agent"; it is left out of the User-Agent

=== Debug level logs one line per upstream request

>>> [CLI] aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code --log-file LOG.debug --log-level debug
No install record for system.ai.genie_one_mcp on profile "logfood"; using its current host [DATABRICKS_URL]
upstream request http_method=POST rpc_method=initialize rpc_id=1 status=200 duration=DUR session_sent=false session_received=false session_replaced=false
upstream request http_method=POST rpc_method=notifications/initialized rpc_id= status=202 duration=DUR session_sent=false session_received=false session_replaced=false
upstream request http_method=POST rpc_method=tools/call rpc_id=3 status=200 duration=DUR session_sent=false session_received=false session_replaced=false
upstream request http_method=POST rpc_method=tools/list rpc_id=2 status=200 duration=DUR session_sent=false session_received=false session_replaced=false
Original file line number Diff line number Diff line change
@@ -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"}}}
31 changes: 31 additions & 0 deletions acceptance/cmd/aitools/mcp/serve-passthrough/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Isolate HOME so the proxy finds no state file and no agent config, as on a fresh machine.
sethome home
envsubst < .databrickscfg > out && mv out .databrickscfg
export DATABRICKS_CONFIG_FILE=.databrickscfg

# DATABRICKS_HOST points elsewhere; the proxy must still talk to the profile's host.
export DATABRICKS_HOST=https://elsewhere.test

# The service the proxy serves, created in the fake workspace. The fake answers 404 for a service it does not have.
MSYS_NO_PATHCONV=1 $CLI api post "/api/2.1/unity-catalog/mcp-services?parent=schemas/system.ai&mcp_service_id=genie_one_mcp" --profile logfood --json '{"comment": "Ask questions about your data"}' &> LOG.create-genie_one_mcp-logfood

# Each stdout line must be one complete JSON value, so a pretty-printed or doubled-up message fails the filter. Responses
# may arrive in any order, so they are sorted by id.
frames='split("\n") | map(select(length > 0) | fromjson) | sort_by(.id)[] | {id, instructions: .result.instructions, tools: [.result.tools[]?.name], text: .result.content[0].text, error}'

title "Stdout holds one JSON-RPC message per line; the notification gets no reply"
trace $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code < requests.ndjson 2> LOG.stderr | jq -Rsc "$frames"

title "Diagnostics go to stderr only"
trace cat LOG.stderr

# An agent this binary does not know still serves; it is left out of the User-Agent, with one line on stderr.
title "An agent this binary does not know is accepted"
trace $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent future-agent < requests.ndjson 2> LOG.future | jq -Rsc "$frames"
trace cat LOG.future

# At debug level each upstream request is logged once: method, id, status, session flags. The timing is masked, and no
# body or header value is in the line, so the tool argument and the session id stay out of the golden.
title "Debug level logs one line per upstream request"
trace $CLI aitools mcp serve --profile logfood --service system.ai.genie_one_mcp --agent claude-code --log-file LOG.debug --log-level debug < requests.ndjson > /dev/null
grep 'Debug: upstream request' LOG.debug | sed -e 's/^.*Debug: //' -e 's/ pid=[0-9]*//' -e 's/duration=[^ ]*/duration=DUR/' | sort
1 change: 1 addition & 0 deletions cmd/aitools/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Use 'add' to install them into coding agents, bound to a Databricks profile.`,
cmd.AddCommand(NewMcpAddCmd())
cmd.AddCommand(NewMcpListCmd())
cmd.AddCommand(NewMcpRemoveCmd())
cmd.AddCommand(NewMcpServeCmd())
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/aitools/mcp_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ func profileClient(ctx context.Context, name, configFile string) (*databricks.Wo
if err != nil {
return nil, err
}
if err := mcp.AuthenticateBound(ctx, cfg, name); err != nil {
if err := mcp.AuthenticateBound(ctx, cfg, name, ""); err != nil {
return nil, err
}
return w, nil
Expand Down
22 changes: 22 additions & 0 deletions cmd/aitools/mcp_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,28 @@ func TestMcpAddRejectedTokenIsAuthFailed(t *testing.T) {
assert.NoFileExists(t, filepath.Join(home, ".claude.json"))
}

// TestMcpAddBindingFailureExitsOne checks that a binding failure is an ordinary failure of add: it exits 1 in text and
// in JSON mode. Exit 2 is serve's status for binding failures, which an agent restarts on.
func TestMcpAddBindingFailureExitsOne(t *testing.T) {
home := withHome(t)
withDatabricksBinary(t)
server := noRequestServer(t)
writeHomeConfig(t, home, "[logfood]\nhost = "+server.URL+"\nauth_type = pat\ntoken = dapi-logfood\n")
require.NoError(t, mcp.SaveState(t.Context(), &mcp.State{Servers: []mcp.Entry{{
Name: "github-logfood", Service: "system.ai.github", Profile: "logfood", Host: "https://adb-123.test",
}}}))
args := []string{"--profile", "logfood", "--names", "github", "--agents", "claude-code"}

_, err := newAddTestCmd(t, args...)
require.Error(t, err)
assert.Equal(t, protos.AitoolsErrorCategory(mcp.BindProfileHostChanged), errorCategory(err))
assert.Equal(t, 1, root.ExitStatus(err))

_, err = newAddTestCmd(t, append(args, "--output", "json")...)
require.ErrorIs(t, err, root.ErrAlreadyPrinted)
assert.Equal(t, 1, root.ExitStatus(err))
}

// TestMcpAddRefusesBinaryNotNamedDatabricks checks that the binary must be databricks: an argv[0] with another name, not on PATH, is refused, not replaced.
func TestMcpAddRefusesBinaryNotNamedDatabricks(t *testing.T) {
home := withHome(t)
Expand Down
Loading
Loading