Skip to content

Commit 14e99ea

Browse files
committed
fix(client): prevent panics
The type assertation should be checked to prevent panicing allowing the caller to handle gracefully.
1 parent 781b732 commit 14e99ea

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

client/stdio.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ func NewStdioMCPClient(
3131

3232
// GetStderr returns a reader for the stderr output of the subprocess.
3333
// This can be used to capture error messages or logs from the subprocess.
34-
//
35-
// Note: This method only works with stdio transport, or it will panic.
36-
func GetStderr(c *Client) io.Reader {
34+
func GetStderr(c *Client) (io.Reader, bool) {
3735
t := c.GetTransport()
38-
stdio := t.(*transport.Stdio)
39-
return stdio.Stderr()
36+
37+
stdio, ok := t.(*transport.Stdio)
38+
if !ok {
39+
return nil, false
40+
}
41+
42+
return stdio.Stderr(), true
4043
}

client/stdio_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@ func TestStdioMCPClient(t *testing.T) {
4747
wg.Add(1)
4848
go func() {
4949
defer wg.Done()
50-
dec := json.NewDecoder(GetStderr(client))
50+
51+
stderr, ok := GetStderr(client)
52+
if !ok {
53+
return
54+
}
55+
56+
dec := json.NewDecoder(stderr)
5157
for {
5258
var record map[string]any
5359
if err := dec.Decode(&record); err != nil {

0 commit comments

Comments
 (0)