internal/mcp: add batching support

Update the jsonrpc2 MCP framer to support both incoming and outgoing
batches. In order to achieve this, we must correlate the framed Reader
and Writer, which is not explicitly supported by the jsonrpc2 API, but
does work. A note is left to revisit the Framer interface.

Change-Id: If060cb5822da067833db20d58f5f112a0528da91
Reviewed-on: https://go-review.googlesource.com/c/tools/+/667295
Reviewed-by: Jonathan Amsterdam <jba@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Rob Findley 2025-04-22 13:28:16 +00:00 • committed by Robert Findley
commit 1204b93a85

View file

@ -142,6 +142,7 @@ func TestServerClosing(t *testing.T) {
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
@ -155,7 +156,36 @@ func TestServerClosing(t *testing.T) {
}
cc.Close()
wg.Wait()
if _, err = sc.CallTool(ctx, "greet", hiParams{"user"}); !errors.Is(err, mcp.ErrConnectionClosed) {
if _, err := sc.CallTool(ctx, "greet", hiParams{"user"}); !errors.Is(err, mcp.ErrConnectionClosed) {
t.Errorf("after disconnection, got error %v, want EOF", err)
}
}
func TestBatching(t *testing.T) {
ctx := context.Background()
ct, st := mcp.NewLocalTransport()
s := mcp.NewServer("testServer", "v1.0.0", nil)
_, err := s.Connect(ctx, st, nil)
if err != nil {
t.Fatal(err)
}
c := mcp.NewClient("testClient", "v1.0.0", nil)
opts := new(mcp.ConnectionOptions)
mcp.BatchSize(opts, 2)
sc, err := c.Connect(ctx, ct, opts)
if err != nil {
t.Fatal(err)
}
defer sc.Close()
errs := make(chan error, 2)
for range 2 {
go func() {
_, err := sc.ListTools(ctx)
errs <- err
}()
}
}