Skip to content

Commit e8685b3

Browse files
authored
mcp: don't close stdout when shutting down the stdio transport (#562)
We need to close stdin when the stdio transport is shut down, to unblock its read loop. However, we don't technically need to close stdout. Fixes #548
1 parent 9348d33 commit e8685b3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

mcp/transport.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,16 @@ type StdioTransport struct{}
9090

9191
// Connect implements the [Transport] interface.
9292
func (*StdioTransport) Connect(context.Context) (Connection, error) {
93-
return newIOConn(rwc{os.Stdin, os.Stdout}), nil
93+
return newIOConn(rwc{os.Stdin, nopCloserWriter{os.Stdout}}), nil
9494
}
9595

96+
// nopCloserWriter is an io.WriteCloser with a trivial Close method.
97+
type nopCloserWriter struct {
98+
io.Writer
99+
}
100+
101+
func (nopCloserWriter) Close() error { return nil }
102+
96103
// An IOTransport is a [Transport] that communicates over separate
97104
// io.ReadCloser and io.WriteCloser using newline-delimited JSON.
98105
type IOTransport struct {
@@ -107,6 +114,9 @@ func (t *IOTransport) Connect(context.Context) (Connection, error) {
107114

108115
// An InMemoryTransport is a [Transport] that communicates over an in-memory
109116
// network connection, using newline-delimited JSON.
117+
//
118+
// InMemoryTransports should be constructed using [NewInMemoryTransports],
119+
// which returns two transports connected to each other.
110120
type InMemoryTransport struct {
111121
rwc io.ReadWriteCloser
112122
}

0 commit comments

Comments
 (0)