Skip to content

Commit adebac9

Browse files
committed
mcp: add IOTransport for separate reader/writer
Enable MCP communication over arbitrary I/O streams beyond stdin/stdout, supporting custom transport implementations.
1 parent 392f719 commit adebac9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

mcp/transport.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@ func NewStdioTransport() *StdioTransport {
103103
return &StdioTransport{}
104104
}
105105

106+
// A StdioTransport is a [Transport] that communicates over stdin/stdout using
107+
// newline-delimited JSON.
108+
type IOTransport struct {
109+
reader io.ReadCloser
110+
writer io.WriteCloser
111+
}
112+
113+
// Connect implements the [Transport] interface.
114+
func (t *IOTransport) Connect(context.Context) (Connection, error) {
115+
return newIOConn(rwc{t.reader, t.writer}), nil
116+
}
117+
118+
// NewIOTransport constructs a transport that communicates over
119+
// io.ReadCloser and io.WriteCloser.
120+
//
121+
//go:fix inline
122+
func NewIOTransport(reader io.ReadCloser, writer io.WriteCloser) *IOTransport {
123+
return &IOTransport{
124+
reader: reader,
125+
writer: writer,
126+
}
127+
}
128+
106129
// An InMemoryTransport is a [Transport] that communicates over an in-memory
107130
// network connection, using newline-delimited JSON.
108131
type InMemoryTransport struct {

0 commit comments

Comments
 (0)