Skip to content

fix(client): prevent panics #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2025
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
13 changes: 8 additions & 5 deletions client/stdio.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,13 @@ func NewStdioMCPClient(

// GetStderr returns a reader for the stderr output of the subprocess.
// This can be used to capture error messages or logs from the subprocess.
//
// Note: This method only works with stdio transport, or it will panic.
func GetStderr(c *Client) io.Reader {
func GetStderr(c *Client) (io.Reader, bool) {
t := c.GetTransport()
stdio := t.(*transport.Stdio)
return stdio.Stderr()

stdio, ok := t.(*transport.Stdio)
if !ok {
return nil, false
}

return stdio.Stderr(), true
}
8 changes: 7 additions & 1 deletion client/stdio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ func TestStdioMCPClient(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
dec := json.NewDecoder(GetStderr(client))

stderr, ok := GetStderr(client)
if !ok {
return
}

dec := json.NewDecoder(stderr)
for {
var record map[string]any
if err := dec.Decode(&record); err != nil {
Expand Down