Repository navigation
fix(mcptoolset): let a CommandTransport reconnect - #2115
Conversation
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]>
|
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
left a comment
There was a problem hiding this comment.
In tool/mcptoolset/set.go:
- 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).
- 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.
|
Thanks for the review, @mangeshraut712! Both points are handled in 8917989.
I also updated the PR description. |
… on reconnect Signed-off-by: Ryo Kusnadi <[email protected]>
|
@mangeshraut712 friendly ping on this one when you have a moment. Happy to change anything. |
wolo-lab
left a comment
There was a problem hiding this comment.
Must change before merge:
- The
Config.Transportdoc 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 changingTerminateDurationafterwards has no effect, even on the first connect. - Reconnects start a copy of the command taken at
New. The copy can't carry anexec.CommandContextcontext, so a server built withCommandContextis started again after that context is cancelled, and the new process ignores it. The same goes for a caller who stops the server by killingcmd.Process: the next tool call brings it back. - A
CommandTransportinside a wrapper such as*mcp.LoggingTransportis 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.
…t settings Signed-off-by: Ryo Kusnadi <[email protected]>
|
Thanks for running the repro against both cases. All of it is in f33f8c2:
Updated the description too. @wolo-lab mind taking another look? |
|
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:
Optional: Once lint is clean and CI is green, this is ready to merge. |
Signed-off-by: Ryo Kusnadi <[email protected]>
…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)
Linked issue
Problem:
mcptoolsethands the samemcp.CommandTransportto everyConnect. Its*exec.Cmdcan 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 withexec: Stdout already setuntil the app restarts.Solution:
buildTransportwraps a*mcp.CommandTransport(with a non-nilCommand) in a small transport. The firstConnectuses the caller's*exec.Cmdunchanged. Each laterConnectruns a freshexec.Cmdbuilt from settings copied atNew(Path, Args, Env, Dir, Stdin, Stderr, ExtraFiles, SysProcAttr, WaitDelay, Err). The slices are cloned, and the rest of theCommandTransport(such asTerminateDuration) is copied as a whole. A nilCommandis 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.TransportandbuildTransportdocs now say how callers can notice the wrapping:New, so changing it afterwards (for exampleTerminateDuration) has no effect, even on the first connect.exec.CommandContextcontext, so a server built withCommandContextis started again after that context is cancelled, and a server stopped by killingcmd.Processcomes back on the next tool call.Cancelis left out too, sinceStartrejects aCancelwithout a context.CommandTransportinside a wrapper such as*mcp.LoggingTransportis used as given, so it still can't reconnect.Testing Plan
Unit Tests:
go vetandgo 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'sStderron each start, so the test also fails if a reconnect loses Path, Args, Env, Dir or Stderr.TestCommandTransportReconnectKeepsSettingschecks every copied field andTerminateDurationon the reconnect command, and that each reconnect gets its own copy. Removing any one field fromnewCommandTransportfails it (checked for Env, Dir, Stderr, SysProcAttr and WaitDelay).TestCommandTransportSpecIsIndependentcovers the cloned slices (before:spec.Args = [server --changed]).Checklist