Skip to content

fix(mcptoolset): let a CommandTransport reconnect - #2115

Merged
wolo-lab merged 7 commits into
google:mainfrom
RyoKusnadi:fix/mcptoolset-fresh-command-per-connect
Nov 7, 2026
Merged

wolo-lab merged 7 commits into
google:mainfrom
RyoKusnadi:fix/mcptoolset-fresh-command-per-connect

Conversation

@RyoKusnadi

@RyoKusnadi RyoKusnadi commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Linked issue

Problem:
mcptoolset hands the same mcp.CommandTransport to every Connect. Its *exec.Cmd can be started only once, so if the first connect is cut short, or the server dies and the automatic reconnect runs, every later attempt fails with exec: Stdout already set until the app restarts.

Solution:
buildTransport wraps a *mcp.CommandTransport (with a non-nil Command) in a small transport. The first Connect uses the caller's *exec.Cmd unchanged. Each later Connect runs a fresh exec.Cmd built from settings copied at New (Path, Args, Env, Dir, Stdin, Stderr, ExtraFiles, SysProcAttr, WaitDelay, Err). The slices are cloned, and the rest of the CommandTransport (such as TerminateDuration) is copied as a whole. A nil Command is passed through, so it behaves as before. Starting a fresh process on reconnect matches adk-python, which builds a new stdio client from the stored server parameters.

Behavior change

Reconnecting to a stdio server now starts a new process instead of failing. The Config.Transport and buildTransport docs now say how callers can notice the wrapping:

  • The transport is copied at New, so changing it afterwards (for example TerminateDuration) has no effect, even on the first connect.
  • The copy can't carry an exec.CommandContext context, so a server built with CommandContext is started again after that context is cancelled, and a server stopped by killing cmd.Process comes back on the next tool call. Cancel is left out too, since Start rejects a Cancel without a context.
  • A CommandTransport inside a wrapper such as *mcp.LoggingTransport is used as given, so it still can't reconnect.

Testing Plan

Unit Tests:

  • All unit tests pass locally. (go vet and go test -race ./tool/mcptoolset/... on go1.27.1, darwin/arm64)

With your source change reverted and your tests kept, which test fails?

  • TestCommandTransportReconnect: Connect #2 error = exec: Stdout already set. It connects three times, and the helper process writes its working directory to the caller's Stderr on each start, so the test also fails if a reconnect loses Path, Args, Env, Dir or Stderr.
  • TestCommandTransportReconnectKeepsSettings checks every copied field and TerminateDuration on the reconnect command, and that each reconnect gets its own copy. Removing any one field from newCommandTransport fails it (checked for Env, Dir, Stderr, SysProcAttr and WaitDelay).
  • TestCommandTransportSpecIsIndependent covers the cloned slices (before: spec.Args = [server --changed]).

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

An exec.Cmd can only be started once, so after the first connect attempt
(failed or not) every reconnect failed with 'exec: Stdout already set'.
Use the caller's command for the first Connect and a fresh copy of its
settings for each later one.

Signed-off-by: Ryo Kusnadi <[email protected]>
@google-cla

google-cla Bot commented Nov 2, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In tool/mcptoolset/set.go:

  1. Line 187: When constructing the reconnect transport:
return (&mcp.CommandTransport{Command: &cmd, TerminateDuration: t.first.TerminateDuration}).Connect(ctx)

MaxLineLength is not copied over (MaxLineLength: t.first.MaxLineLength). If a caller configured a custom inbound frame buffer cap on the initial transport, that setting is dropped on every reconnect and silently falls back to 0 (DefaultMaxLineLength).

  1. Lines 164–174: spec: exec.Cmd{...} performs a shallow copy of the command's slice fields (Args, Env, ExtraFiles). Cloning the slices (e.g. append([]string(nil), src.Args...)) prevents the original and reconnect commands from sharing underlying slice capacity.

@RyoKusnadi

RyoKusnadi commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @mangeshraut712! Both points are handled in 8917989.

  1. MaxLineLength: in the SDK version this repo uses (go-sdk v1.8.0) mcp.CommandTransport only has Command and TerminateDuration. MaxLineLength exists on StdioTransport and IOTransport, so I can't set it here. Your worry still applies to future versions, so the reconnect now copies the whole transport and only swaps Command. Any field the SDK adds later carries over on its own.
  2. Shared slices: good catch. Args, Env and ExtraFiles are now cloned when the settings are saved. I added TestCommandTransportSpecIsIndependent, which failed before (spec.Args = [server --changed]) and passes now. This only matters if someone edits those slices after New, so it is a small safety fix.

I also updated the PR description. go vet and go test -race ./tool/mcptoolset/... pass. Could a maintainer take a look when there's time? Thanks!

@RyoKusnadi

Copy link
Copy Markdown
Contributor Author

@mangeshraut712 friendly ping on this one when you have a moment. Happy to change anything.

@wolo-lab wolo-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must change before merge:

  • The Config.Transport doc needs updating (details below).
  • A test that a reconnect starts the caller's command with its settings (details inline).

The Config.Transport doc still says other transport types "are used as given". That is no longer true for *mcp.CommandTransport. With a non-nil Command it is now wrapped when New is called, and callers can see that in three ways, so it deserves a short paragraph there:

  • The transport is copied at New, so changing TerminateDuration afterwards has no effect, even on the first connect.
  • Reconnects start a copy of the command taken at New. The copy can't carry an exec.CommandContext context, so a server built with CommandContext is started again after that context is cancelled, and the new process ignores it. The same goes for a caller who stops the server by killing cmd.Process: the next tool call brings it back.
  • A CommandTransport inside a wrapper such as *mcp.LoggingTransport is still used as given, so it still can't reconnect.

The buildTransport doc needs the same update.

The fix itself works. I ran the reproduction from #2111 against this branch, and both cases (a first connect that times out, and a server killed after a successful run) now recover on the next run. On main both still fail with exec: Stdout already set. Starting a fresh process on reconnect also matches adk-python, which creates a new stdio client from the stored server parameters when it replaces a dead session.

Comment thread tool/mcptoolset/set.go Resolved
Comment thread tool/mcptoolset/set.go Resolved
@RyoKusnadi

Copy link
Copy Markdown
Contributor Author

Thanks for running the repro against both cases. All of it is in f33f8c2:

  • Config.Transport and buildTransport docs now spell out the wrapping and the three ways a caller can notice it (copied at New, no CommandContext context on reconnect, wrapped transports used as given).
  • TestCommandTransportReconnect now sets Dir and a file Stderr, and the helper writes its working directory to stderr on every start. So a reconnect that loses the env var (or Path, Args, Dir, Stderr) fails instead of passing quietly.
  • New TestCommandTransportReconnectKeepsSettings checks every copied field plus TerminateDuration on the reconnect command. I dropped Env, Dir, Stderr, SysProcAttr and WaitDelay one at a time to make sure each one fails it.
  • Fixed both comments: nil Command now says it "behaves as before", and the Cancel note says why it really is left out.

Updated the description too. @wolo-lab mind taking another look?

@wolo-lab

wolo-lab commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

We're close to having this merged. Both items from the last round are addressed, and the new tests catch each copied setting being dropped.

Must change before merge: golangci-lint run reports two issues from the last commit.

  • transport_config_test.go#L291: the error from defer stderr.Close() is unchecked (errcheck).
  • set.go#L257: cancelled should be canceled (misspell). That spelling came from my wording in the last review, sorry about that.

Optional: TestCommandTransportSpecIsIndependent checks the Args and Env clones but not ExtraFiles.

Once lint is clean and CI is green, this is ready to merge.

@RyoKusnadi

RyoKusnadi commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

Both lint issues are fixed in 7b20941 (no worries on the spelling, I should have run the linter). Also added the ExtraFiles check to TestCommandTransportSpecIsIndependent in aefae61, and confirmed it fails if the clone is removed. @wolo-lab mind taking another look?

@wolo-lab wolo-lab left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@wolo-lab
wolo-lab merged commit 1a84f04 into google:main Nov 7, 2026
15 checks passed
@RyoKusnadi
RyoKusnadi deleted the fix/mcptoolset-fresh-command-per-connect branch November 7, 2026 09:26
wolo-lab added a commit that referenced this pull request Nov 16, 2026
…tifactstool fixes (#1961, #2120, #2115, #2283, #2307) (#2356)

* fix(functiontool): preserve streaming consumer panics (#1961)

(cherry picked from commit 1af51a3)

* fix(functiontool): keep tool panics out of the model's function response (#2120)

(cherry picked from commit e83ab5b)

* fix(mcptoolset): let a CommandTransport reconnect (#2115)

(cherry picked from commit 1a84f04)

* fix(skilltoolset): list skills under "result" and trim SKILL.md instructions (#2283)

(cherry picked from commit 8ed375d)

* fix(loadartifactstool): return an error result for non-string names (#2307)

(cherry picked from commit f1f0d2d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mcptoolset: a stdio MCP server that dies, or whose first connect times out, leaves the toolset failing with "exec: Stdout already set" until restart

3 participants