mcp-go icon indicating copy to clipboard operation
mcp-go copied to clipboard

bug: panic, nil pointer dereference in Stdio readResponses

Open xanarry opened this issue 7 months ago • 10 comments

Description

I need some help with a panic issue when using the stdio MCP client. I encountered the following panic

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0x5c27bd]

goroutine 27837 [running]:
bufio.(*Reader).ReadSlice(0x0, 0xa)
        /usr/local/go/src/bufio/bufio.go:355 +0x3d
bufio.(*Reader).collectFragments(0x0, 0xa)
        /usr/local/go/src/bufio/bufio.go:455 +0x70
bufio.(*Reader).ReadString(0x0?, 0x0?)
        /usr/local/go/src/bufio/bufio.go:502 +0x1f
github.com/mark3labs/mcp-go/client/transport.(*Stdio).readResponses(0xc000f40240)
        /Users/xanarry/go/pkg/mod/github.com/mark3labs/[email protected]/client/transport/stdio.go:170 +0x53
github.com/mark3labs/mcp-go/client/transport.(*Stdio).Start.func1()
        /Users/xanarry/go/pkg/mod/github.com/mark3labs/[email protected]/client/transport/stdio.go:79 +0x2f
created by github.com/mark3labs/mcp-go/client/transport.(*Stdio).Start in goroutine 27955
        /Users/xanarry/go/pkg/mod/github.com/mark3labs/[email protected]/client/transport/stdio.go:77 +0x8c

my mcp-go version is v0.29.0

I have checked my own code and the MCP client initialization logic, but I couldn't find any obvious reason for c.stdout to be nil at this point. My guess is that this happens when the MCP server subprocess exits unexpectedly, and the client tries to read from a closed or nil pipe. Should there be a panic recovery or more defensive check in the readResponses goroutine to avoid crashing the whole process? Or is there any recommended way to handle this scenario gracefully? Any advice or fix would be appreciated. Thank you!

xanarry avatar May 31 '25 16:05 xanarry

I have tried to kill stdio mcp server process all the time, but I can not trigger this pannic, anybody help

xanarry avatar Jun 01 '25 02:06 xanarry

@xanarry There has been some breaking changes recently but some bugs were also resolved which could be the issue. Could you try the latest release of the SDK? It should not have the issue.

pottekkat avatar Jun 01 '25 10:06 pottekkat

@pottekkat thanks, I have updated it from v0.29.0 to v0.31.0, and continue to observe whether the problem will recur.

xanarry avatar Jun 02 '25 01:06 xanarry

Great, thanks. You can reopen this issue if you still have problems.

pottekkat avatar Jun 02 '25 02:06 pottekkat

@pottekkat hello. pottekkat, I have updated sdk to v0.31.0, but it still quit without any warning signs today, the same panic error

xanarry avatar Jun 06 '25 06:06 xanarry

I met the same problem.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x685bf0]

goroutine 14 [running]:
github.com/mark3labs/mcp-go/server.(*MCPServer).HandleMessage(0xc0001bc000, {0x81b448?, 0xc000194460?}, {0xc00001e300, 0x74, 0x80})
        github.com/mark3labs/[email protected]/server/request_handler.go:279 +0x17d0
github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage.func1({0x81b448?, 0xc000194460?})
        github.com/mark3labs/[email protected]/server/sse.go:574 +0x89
created by github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage in goroutine 18
        github.com/mark3labs/[email protected]/server/sse.go:570 +0x47c

My code:

sse := server.NewSSEServer(s)
sse.Start(":9090")

My mcp-go version:

github.com/mark3labs/mcp-go v0.43.0

Please let me know when this issue is fixed.

shmng avatar Nov 07 '25 06:11 shmng

I met the same problem.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x685bf0]

goroutine 14 [running]:
github.com/mark3labs/mcp-go/server.(*MCPServer).HandleMessage(0xc0001bc000, {0x81b448?, 0xc000194460?}, {0xc00001e300, 0x74, 0x80})
        github.com/mark3labs/[email protected]/server/request_handler.go:279 +0x17d0
github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage.func1({0x81b448?, 0xc000194460?})
        github.com/mark3labs/[email protected]/server/sse.go:574 +0x89
created by github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage in goroutine 18
        github.com/mark3labs/[email protected]/server/sse.go:570 +0x47c

My code:

sse := server.NewSSEServer(s)
sse.Start(":9090")

My mcp-go version:

github.com/mark3labs/mcp-go v0.43.0

Please let me know when this issue is fixed.

To my disappointment, server.WithRecovery() didn't work because the program crashed and exited after the NPE error. If the program hadn't crashed and exited, this wouldn't be considered a bug.

shmng avatar Nov 07 '25 06:11 shmng

I met the same problem.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x685bf0]

goroutine 14 [running]:
github.com/mark3labs/mcp-go/server.(*MCPServer).HandleMessage(0xc0001bc000, {0x81b448?, 0xc000194460?}, {0xc00001e300, 0x74, 0x80})
        github.com/mark3labs/[email protected]/server/request_handler.go:279 +0x17d0
github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage.func1({0x81b448?, 0xc000194460?})
        github.com/mark3labs/[email protected]/server/sse.go:574 +0x89
created by github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage in goroutine 18
        github.com/mark3labs/[email protected]/server/sse.go:570 +0x47c

My code:

sse := server.NewSSEServer(s)
sse.Start(":9090")

My mcp-go version: github.com/mark3labs/mcp-go v0.43.0 Please let me know when this issue is fixed.

To my disappointment, server.WithRecovery() didn't work because the program crashed and exited after the NPE error. If the program hadn't crashed and exited, this wouldn't be considered a bug.

@pottekkat hello. pottekkat,The issue lies in the instantiation of this result - it's a nil value, and directly dereferencing it with * in the code caused this problem.

In github.com/mark3labs/[email protected]/server/request_handler.go:279

Image

shmng avatar Nov 07 '25 06:11 shmng

I met the same problem.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x685bf0]

goroutine 14 [running]:
github.com/mark3labs/mcp-go/server.(*MCPServer).HandleMessage(0xc0001bc000, {0x81b448?, 0xc000194460?}, {0xc00001e300, 0x74, 0x80})
        github.com/mark3labs/[email protected]/server/request_handler.go:279 +0x17d0
github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage.func1({0x81b448?, 0xc000194460?})
        github.com/mark3labs/[email protected]/server/sse.go:574 +0x89
created by github.com/mark3labs/mcp-go/server.(*SSEServer).handleMessage in goroutine 18
        github.com/mark3labs/[email protected]/server/sse.go:570 +0x47c

My code:

sse := server.NewSSEServer(s)
sse.Start(":9090")

My mcp-go version: github.com/mark3labs/mcp-go v0.43.0 Please let me know when this issue is fixed.

To my disappointment, server.WithRecovery() didn't work because the program crashed and exited after the NPE error. If the program hadn't crashed and exited, this wouldn't be considered a bug.

@pottekkat hello. pottekkat,The issue lies in the instantiation of this result - it's a nil value, and directly dereferencing it with * in the code caused this problem.

In github.com/mark3labs/[email protected]/server/request_handler.go:279

Image

Yes, I've encountered the same issue. Do you have any solutions in mind? Mcp-go is quite a useful framework, but its stability still needs improvement.

dusbot avatar Nov 07 '25 06:11 dusbot

According to my tests, this issue still exists in the latest version.

Back-Pack-Mack avatar Nov 07 '25 06:11 Back-Pack-Mack