Skip to content

Commit 74c1760

Browse files
committed
guard loggingtransport
1 parent a1d6ed4 commit 74c1760

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

mcp/transport.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,42 +212,54 @@ func (t *LoggingTransport) Connect(ctx context.Context) (Connection, error) {
212212
if err != nil {
213213
return nil, err
214214
}
215-
return &loggingConn{delegate, t.Writer}, nil
215+
return &loggingConn{delegate: delegate, w: t.Writer}, nil
216216
}
217217

218218
type loggingConn struct {
219219
delegate Connection
220-
w io.Writer
220+
221+
mu sync.Mutex
222+
w io.Writer
221223
}
222224

223225
func (c *loggingConn) SessionID() string { return c.delegate.SessionID() }
224226

225227
// Read is a stream middleware that logs incoming messages.
226228
func (s *loggingConn) Read(ctx context.Context) (jsonrpc.Message, error) {
227229
msg, err := s.delegate.Read(ctx)
230+
228231
if err != nil {
232+
s.mu.Lock()
229233
fmt.Fprintf(s.w, "read error: %v", err)
234+
s.mu.Unlock()
230235
} else {
231236
data, err := jsonrpc2.EncodeMessage(msg)
237+
s.mu.Lock()
232238
if err != nil {
233239
fmt.Fprintf(s.w, "LoggingTransport: failed to marshal: %v", err)
234240
}
235241
fmt.Fprintf(s.w, "read: %s\n", string(data))
242+
s.mu.Unlock()
236243
}
244+
237245
return msg, err
238246
}
239247

240248
// Write is a stream middleware that logs outgoing messages.
241249
func (s *loggingConn) Write(ctx context.Context, msg jsonrpc.Message) error {
242250
err := s.delegate.Write(ctx, msg)
243251
if err != nil {
252+
s.mu.Lock()
244253
fmt.Fprintf(s.w, "write error: %v", err)
254+
s.mu.Unlock()
245255
} else {
246256
data, err := jsonrpc2.EncodeMessage(msg)
257+
s.mu.Lock()
247258
if err != nil {
248259
fmt.Fprintf(s.w, "LoggingTransport: failed to marshal: %v", err)
249260
}
250261
fmt.Fprintf(s.w, "write: %s\n", string(data))
262+
s.mu.Unlock()
251263
}
252264
return err
253265
}

0 commit comments

Comments
 (0)