Debian Codegraph @

debian/go/src/github.com/modelcontextprotocol/go-sdk/mcp/transport_test.go

raw symbols SCIP
1 // Copyright 2025 The Go MCP SDK Authors. All rights reserved.
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mcp
6
7 import (
8 "context"
9 "io"
10 "strings"
11 "testing"
12
13 "github.com/modelcontextprotocol/go-sdk/internal/jsonrpc2"
14 "github.com/modelcontextprotocol/go-sdk/jsonrpc"
15 )
16
17 func TestBatchFraming(t *testing.T) {
18 // This test checks that the ndjsonFramer can read and write JSON batches.
19 //
20 // The framer is configured to write a batch size of 2, and we confirm that
21 // nothing is sent over the wire until the second write, at which point both
22 // messages become available.
23 ctx := context.Background()
24
25 r, w := io.Pipe()
26 tport := newIOConn(rwc{r, w})
27 tport.outgoingBatch = make([]jsonrpc.Message, 0, 2)
28 t.Cleanup(func() { tport.Close() })
29
30 // Read the two messages into a channel, for easy testing later.
31 read := make(chan jsonrpc.Message)
32 go func() {
33 for range 2 {
34 msg, _ := tport.Read(ctx)
35 read <- msg
36 }
37 }()
38
39 // The first write should not yet be observed by the reader.
40 tport.Write(ctx, &jsonrpc.Request{ID: jsonrpc2.Int64ID(1), Method: "test"})
41 select {
42 case got := <-read:
43 t.Fatalf("after one write, got message %v", got)
44 default:
45 }
46
47 // ...but the second write causes both messages to be observed.
48 tport.Write(ctx, &jsonrpc.Request{ID: jsonrpc2.Int64ID(2), Method: "test"})
49 for _, want := range []int64{1, 2} {
50 got := <-read
51 if got := got.(*jsonrpc.Request).ID.Raw(); got != want {
52 t.Errorf("got message #%d, want #%d", got, want)
53 }
54 }
55 }
56
57 func TestIOConnRead(t *testing.T) {
58 tests := []struct {
59 name string
60 input string
61 want string
62 protocolVersion string
63 }{
64 {
65 name: "valid json input",
66 input: `{"jsonrpc":"2.0","id":1,"method":"test","params":{}}`,
67 want: "",
68 },
69 {
70 name: "newline at the end of first valid json input",
71 input: `{"jsonrpc":"2.0","id":1,"method":"test","params":{}}
72 `,
73 want: "",
74 },
75 {
76 name: "bad data at the end of first valid json input",
77 input: `{"jsonrpc":"2.0","id":1,"method":"test","params":{}},`,
78 want: "invalid trailing data at the end of stream",
79 },
80 {
81 name: "batching unknown protocol",
82 input: `[{"jsonrpc":"2.0","id":1,"method":"test1"},{"jsonrpc":"2.0","id":2,"method":"test2"}]`,
83 want: "",
84 protocolVersion: "",
85 },
86 {
87 name: "windows newline at the end of first valid json input",
88 input: "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"test\",\"params\":{}}\r\n",
89 want: "",
90 },
91 {
92 name: "batching old protocol",
93 input: `[{"jsonrpc":"2.0","id":1,"method":"test1"},{"jsonrpc":"2.0","id":2,"method":"test2"}]`,
94 want: "",
95 protocolVersion: protocolVersion20241105,
96 },
97 {
98 name: "batching new protocol",
99 input: `[{"jsonrpc":"2.0","id":1,"method":"test1"},{"jsonrpc":"2.0","id":2,"method":"test2"}]`,
100 want: "JSON-RPC batching is not supported in 2025-06-18 and later (request version: 2025-06-18)",
101 protocolVersion: protocolVersion20250618,
102 },
103 }
104 for _, tt := range tests {
105 t.Run(tt.name, func(t *testing.T) {
106 tr := newIOConn(rwc{
107 rc: io.NopCloser(strings.NewReader(tt.input)),
108 })
109 t.Cleanup(func() { tr.Close() })
110 if tt.protocolVersion != "" {
111 tr.sessionUpdated(ServerSessionState{
112 InitializeParams: &InitializeParams{
113 ProtocolVersion: tt.protocolVersion,
114 },
115 })
116 }
117 _, err := tr.Read(context.Background())
118 if err == nil && tt.want != "" {
119 t.Errorf("ioConn.Read() got nil error but wanted %v", tt.want)
120 }
121 if err != nil && err.Error() != tt.want {
122 t.Errorf("ioConn.Read() = %v, want %v", err.Error(), tt.want)
123 }
124 })
125 }
126 }