Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
87dc589
Add aitools mcp serve, the stdio proxy for managed MCP servers
simonfaltum Nov 12, 2026
085d812
Reject --log-file stdout on aitools mcp serve
simonfaltum Nov 12, 2026
fc5e64a
Make the serve-binding-errors byte count portable
simonfaltum Nov 12, 2026
93b817c
Adapt serve acceptance to the shared state file and binding messages
simonfaltum Nov 12, 2026
122ede9
Set a 10 s connect timeout on the serve upstream transport
simonfaltum Nov 13, 2026
18e4255
Update serve binding-error goldens for the named-server messages
simonfaltum Nov 13, 2026
63b031c
cmd/root: take the exit status from errors that set one
simonfaltum Nov 13, 2026
9d955f7
cmd/root: refuse stdout logs for commands whose stdout is a protocol
simonfaltum Nov 13, 2026
55fe36f
serve: keep the session through cancelled requests and the shutdown D…
simonfaltum Nov 13, 2026
1386c6a
serve acceptance: LOG files, native config path, unknown agent accepted
simonfaltum Nov 13, 2026
99be6e0
cmd/root: check the stdout log file by identity; exit status by named…
simonfaltum Nov 13, 2026
3ba8ad4
cmd/root: refuse a stdout log before the logger is installed
simonfaltum Nov 13, 2026
3129734
serve: exit 2 for binding and token failures, print each once, check …
simonfaltum Nov 13, 2026
86426ec
cmd/root: check stdout against the streams it is given
simonfaltum Nov 13, 2026
2a1f69a
cmd/root: compare stdout by identity only for regular files on Windows
simonfaltum Nov 13, 2026
17b10b8
cmd/root: allow a log file that is stderr when stdout is the same file
simonfaltum Nov 13, 2026
fe35f46
cmd/root: close the log file and restore the logger when Execute returns
simonfaltum Nov 13, 2026
9517f02
libs/aitools/mcp/proxy: let Authenticate skip the session DELETE
simonfaltum Nov 13, 2026
d741032
cmd/aitools: stop a refresh-failed serve cleanly and say why
simonfaltum Nov 13, 2026
e5f3d6e
libs/testserver: answer 404 for an MCP service the workspace does not…
simonfaltum Nov 13, 2026
dae5057
cmd/root: test the stale-version skip for protocol commands; simplify…
simonfaltum Nov 13, 2026
9197239
cmd/aitools: serve takes add's flag names from mcpconfig and inlines …
simonfaltum Nov 13, 2026
1c5477a
acceptance: show the serve command line in its goldens
simonfaltum Nov 13, 2026
86087f6
cmd/root: restore the standard log writer and flags after Execute
simonfaltum Nov 13, 2026
c4d8d90
cmd/root: treat a stream that cannot be stat'ed as not stdout
simonfaltum Nov 13, 2026
9a26a2d
libs/testserver: match MCP service names without regard to case
simonfaltum Nov 13, 2026
47ed485
acceptance: check serve's stdout framing line by line
simonfaltum Nov 13, 2026
b01d95b
cmd/aitools, cmd/root, libs/testserver: refactor serve tests and helpers
simonfaltum Nov 13, 2026
130ee61
cmd/aitools, cmd/root: document serve's binding limitation and the Wi…
simonfaltum Nov 13, 2026
6d9f746
cmd/root: allow the standard log import in the logger restore
simonfaltum Nov 13, 2026
70974a6
aitools mcp serve: fail only the request on a transient token failure
simonfaltum Nov 13, 2026
1d1e8f2
aitools mcp serve: no login hint for a transient token failure
simonfaltum Nov 13, 2026
760b867
aitools mcp serve: print binding and auth failures with the Error prefix
simonfaltum Nov 16, 2026
69fa1cd
aitools mcp serve: log each upstream request at debug level
simonfaltum Nov 16, 2026
b02d29d
aitools mcp serve: fail a sign-in redirect with the login command
simonfaltum Nov 16, 2026
53856a5
cmd/root: keep the DBR comment in the stale-version skip
simonfaltum Nov 17, 2026
f2342da
aitools mcp serve: name the config file and restart in the login hints
simonfaltum Nov 17, 2026
7d36299
aitools mcp serve: pin the config file in every login hint
simonfaltum Nov 17, 2026
b52879a
aitools mcp: resolve the config path in the pinned login hint tests
simonfaltum Nov 17, 2026
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
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
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