internal/mcp: export the Transport interface; add a Server example

Export the Transport interface, as there's no reason users shouldn't be
able to define their own transports: they are just bidirectional
streams.

Also, add a godoc example for basic Server usage.

Finally, remove some TODOs that existed due to a bad merge: they are
related to the (as yet unsubmitted) streamable HTTP transport.

Change-Id: Id669793fc80630e667d50611fe2876d09125a025
Reviewed-on: https://go-review.googlesource.com/c/tools/+/668675
TryBot-Bypass: Robert Findley <rfindley@google.com>
Reviewed-by: Sam Thanawalla <samthanawalla@google.com>
Auto-Submit: Robert Findley <rfindley@google.com>
This commit is contained in:
Rob Findley 2025-04-28 20:10:20 +00:00 • committed by Robert Findley
commit 3b86fb5601

View file

@ -19,9 +19,6 @@ import (
"github.com/modelcontextprotocol/go-sdk/internal/xcontext"
)
// A JSONRPC2 error is an error defined by the JSONRPC2 spec.
type JSONRPC2Error = jsonrpc2.WireError
// ErrConnectionClosed is returned when sending a message to a connection that
// is closed or in the process of closing.
var ErrConnectionClosed = errors.New("connection closed")
@ -32,15 +29,15 @@ var ErrConnectionClosed = errors.New("connection closed")
// Transports should be used for at most one call to [Server.Connect] or
// [Client.Connect].
type Transport interface {
// connect returns the logical stream.
// Connect returns the logical stream.
//
// It is called exactly once by [connect].
connect(ctx context.Context) (stream, error)
// It is called exactly once by [Connect].
Connect(ctx context.Context) (Stream, error)
}
// A stream is an abstract bidirectional jsonrpc2 stream.
// A Stream is an abstract bidirectional jsonrpc2 Stream.
// It is used by [connect] to establish a [jsonrpc2.Connection].
type stream interface {
type Stream interface {
jsonrpc2.Reader
jsonrpc2.Writer
io.Closer
@ -59,7 +56,7 @@ type IOTransport struct {
rwc io.ReadWriteCloser
}
func (t *IOTransport) connect(context.Context) (stream, error) {
func (t *IOTransport) Connect(context.Context) (Stream, error) {
return newIOStream(t.rwc), nil
}
@ -94,7 +91,7 @@ func connect[H handler](ctx context.Context, t Transport, opts *ConnectionOption
}
var zero H
stream, err := t.connect(ctx)
stream, err := t.Connect(ctx)
if err != nil {
return zero, err
}
@ -275,8 +272,8 @@ func newIOStream(rwc io.ReadWriteCloser) *ioStream {
}
}
// connect returns the receiver, as a streamTransport is a logical stream.
func (t *ioStream) connect(ctx context.Context) (stream, error) {
// Connect returns the receiver, as a streamTransport is a logical stream.
func (t *ioStream) Connect(ctx context.Context) (Stream, error) {
return t, nil
}