mcp: add Session.ID (#67)

Support retrieving the session ID from client and server sessions.

Fixes #65.
This commit is contained in:
Jonathan Amsterdam 2025-07-01 13:49:23 -04:00 • committed by GitHub
commit 9b6327b3b7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -50,6 +50,7 @@ type Connection interface {
Read(context.Context) (JSONRPCMessage, error)
Write(context.Context, JSONRPCMessage) error
Close() error // may be called concurrently by both peers
SessionID() string
}
// A StdioTransport is a [Transport] that communicates over stdin/stdout using
@ -94,6 +95,7 @@ type binder[T handler] interface {
type handler interface {
handle(ctx context.Context, req *JSONRPCRequest) (any, error)
setConn(Connection)
}
func connect[H handler](ctx context.Context, t Transport, b binder[H]) (H, error) {
@ -124,6 +126,7 @@ func connect[H handler](ctx context.Context, t Transport, b binder[H]) (H, error
},
})
assert(preempter.conn != nil, "unbound preempter")
h.setConn(conn)
return h, nil
}
@ -200,6 +203,8 @@ type loggingConn struct {
w io.Writer
}
func (c *loggingConn) SessionID() string { return c.delegate.SessionID() }
// loggingReader is a stream middleware that logs incoming messages.
func (s *loggingConn) Read(ctx context.Context) (JSONRPCMessage, error) {
msg, err := s.delegate.Read(ctx)
@ -285,6 +290,8 @@ func newIOConn(rwc io.ReadWriteCloser) *ioConn {
}
}
func (c *ioConn) SessionID() string { return "" }
// addBatch records a msgBatch for an incoming batch payload.
// It returns an error if batch is malformed, containing previously seen IDs.
//