|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "log/slog" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "dev.gaijin.team/go/golib/e" |
| 14 | + "github.com/modelcontextprotocol/go-sdk/jsonrpc" |
| 15 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +const receiveDeadline = 10 * time.Second |
| 21 | + |
| 22 | +// observedTransport wraps the client side of an in-memory pair and copies |
| 23 | +// every channel notification the client reads into a buffered channel; the |
| 24 | +// SDK client itself drops the method as undispatchable. |
| 25 | +type observedTransport struct { |
| 26 | + inner mcp.Transport |
| 27 | + notifications chan *jsonrpc.Request |
| 28 | +} |
| 29 | + |
| 30 | +func (t *observedTransport) Connect(ctx context.Context) (mcp.Connection, error) { |
| 31 | + conn, err := t.inner.Connect(ctx) |
| 32 | + if err != nil { |
| 33 | + return nil, e.NewFrom("connect inner transport", err) |
| 34 | + } |
| 35 | + |
| 36 | + return &observedConn{Connection: conn, notifications: t.notifications}, nil |
| 37 | +} |
| 38 | + |
| 39 | +type observedConn struct { |
| 40 | + mcp.Connection |
| 41 | + |
| 42 | + notifications chan *jsonrpc.Request |
| 43 | +} |
| 44 | + |
| 45 | +func (c *observedConn) Read(ctx context.Context) (jsonrpc.Message, error) { |
| 46 | + msg, err := c.Connection.Read(ctx) |
| 47 | + if err != nil { |
| 48 | + // Read errors pass through unwrapped: the SDK detects io.EOF on them. |
| 49 | + return msg, err //nolint:wrapcheck |
| 50 | + } |
| 51 | + |
| 52 | + if req, ok := msg.(*jsonrpc.Request); ok && req.Method == channelMethod { |
| 53 | + c.notifications <- req |
| 54 | + } |
| 55 | + |
| 56 | + return msg, nil |
| 57 | +} |
| 58 | + |
| 59 | +// channelFixture is a connected server/client pair with the production |
| 60 | +// capture transport on the server side and an observing transport on the |
| 61 | +// client side. |
| 62 | +type channelFixture struct { |
| 63 | + emitter *channelEmitter |
| 64 | + session *mcp.ClientSession |
| 65 | + notifications chan *jsonrpc.Request |
| 66 | + logs *bytes.Buffer |
| 67 | +} |
| 68 | + |
| 69 | +func newChannelFixture(t *testing.T) *channelFixture { |
| 70 | + t.Helper() |
| 71 | + |
| 72 | + serverTransport, clientTransport := mcp.NewInMemoryTransports() |
| 73 | + |
| 74 | + capture := &captureTransport{inner: serverTransport} |
| 75 | + observed := &observedTransport{ |
| 76 | + inner: clientTransport, |
| 77 | + notifications: make(chan *jsonrpc.Request, 64), |
| 78 | + } |
| 79 | + |
| 80 | + srv := mcp.NewServer(&mcp.Implementation{Name: "test", Version: "0"}, nil) |
| 81 | + |
| 82 | + type echoArgs struct { |
| 83 | + Text string `json:"text"` |
| 84 | + } |
| 85 | + |
| 86 | + mcp.AddTool(srv, &mcp.Tool{Name: "echo", Description: "echo the input back"}, |
| 87 | + func(_ context.Context, _ *mcp.CallToolRequest, args echoArgs) (*mcp.CallToolResult, any, error) { |
| 88 | + return &mcp.CallToolResult{ |
| 89 | + Content: []mcp.Content{&mcp.TextContent{Text: args.Text}}, |
| 90 | + }, nil, nil |
| 91 | + }) |
| 92 | + |
| 93 | + _, err := srv.Connect(t.Context(), capture, nil) |
| 94 | + require.NoError(t, err) |
| 95 | + |
| 96 | + client := mcp.NewClient(&mcp.Implementation{Name: "probe", Version: "0"}, nil) |
| 97 | + |
| 98 | + session, err := client.Connect(t.Context(), observed, nil) |
| 99 | + require.NoError(t, err) |
| 100 | + t.Cleanup(func() { _ = session.Close() }) |
| 101 | + |
| 102 | + logs := &bytes.Buffer{} |
| 103 | + |
| 104 | + return &channelFixture{ |
| 105 | + emitter: &channelEmitter{transport: capture, lgr: slog.New(slog.NewTextHandler(logs, nil))}, |
| 106 | + session: session, |
| 107 | + notifications: observed.notifications, |
| 108 | + logs: logs, |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (f *channelFixture) receive(t *testing.T) *jsonrpc.Request { |
| 113 | + t.Helper() |
| 114 | + |
| 115 | + select { |
| 116 | + case req := <-f.notifications: |
| 117 | + return req |
| 118 | + case <-time.After(receiveDeadline): |
| 119 | + t.Fatal("no channel notification before timeout") |
| 120 | + |
| 121 | + return nil |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func Test_ChannelEmitter_Emit(t *testing.T) { |
| 126 | + t.Parallel() |
| 127 | + |
| 128 | + t.Run("notification reaches the client with method and params intact", func(t *testing.T) { |
| 129 | + t.Parallel() |
| 130 | + |
| 131 | + f := newChannelFixture(t) |
| 132 | + |
| 133 | + content := `<review_activity change="123" status="NEW"/>` |
| 134 | + require.NoError(t, f.emitter.Emit(t.Context(), content, map[string]string{"change": "123"})) |
| 135 | + |
| 136 | + req := f.receive(t) |
| 137 | + |
| 138 | + assert.False(t, req.IsCall(), "channel notification must carry no request ID") |
| 139 | + assert.Equal(t, channelMethod, req.Method) |
| 140 | + |
| 141 | + var params channelParams |
| 142 | + |
| 143 | + require.NoError(t, json.Unmarshal(req.Params, ¶ms)) |
| 144 | + assert.Equal(t, content, params.Content) |
| 145 | + assert.Equal(t, map[string]string{"change": "123"}, params.Meta) |
| 146 | + }) |
| 147 | + |
| 148 | + t.Run("emissions survive concurrent tool traffic", func(t *testing.T) { |
| 149 | + t.Parallel() |
| 150 | + |
| 151 | + const emissions = 20 |
| 152 | + |
| 153 | + f := newChannelFixture(t) |
| 154 | + |
| 155 | + var wg sync.WaitGroup |
| 156 | + |
| 157 | + wg.Go(func() { |
| 158 | + for i := range emissions { |
| 159 | + content := fmt.Sprintf("event-%d", i) |
| 160 | + if err := f.emitter.Emit(t.Context(), content, map[string]string{"change": "42"}); err != nil { |
| 161 | + t.Errorf("emit %d: %v", i, err) |
| 162 | + |
| 163 | + return |
| 164 | + } |
| 165 | + } |
| 166 | + }) |
| 167 | + |
| 168 | + wg.Go(func() { |
| 169 | + for i := range emissions { |
| 170 | + res, err := f.session.CallTool(t.Context(), &mcp.CallToolParams{ |
| 171 | + Name: "echo", |
| 172 | + Arguments: map[string]any{"text": fmt.Sprintf("call-%d", i)}, |
| 173 | + }) |
| 174 | + if err != nil || res.IsError { |
| 175 | + t.Errorf("tool call %d failed: %v", i, err) |
| 176 | + |
| 177 | + return |
| 178 | + } |
| 179 | + } |
| 180 | + }) |
| 181 | + |
| 182 | + wg.Wait() |
| 183 | + |
| 184 | + for range emissions { |
| 185 | + req := f.receive(t) |
| 186 | + assert.Equal(t, channelMethod, req.Method) |
| 187 | + } |
| 188 | + }) |
| 189 | + |
| 190 | + t.Run("invalid meta keys dropped and named", func(t *testing.T) { |
| 191 | + t.Parallel() |
| 192 | + |
| 193 | + f := newChannelFixture(t) |
| 194 | + |
| 195 | + meta := map[string]string{"change": "123", "bad-key": "x", "worse key": "y"} |
| 196 | + require.NoError(t, f.emitter.Emit(t.Context(), "content", meta)) |
| 197 | + |
| 198 | + var params channelParams |
| 199 | + |
| 200 | + require.NoError(t, json.Unmarshal(f.receive(t).Params, ¶ms)) |
| 201 | + |
| 202 | + assert.Equal(t, map[string]string{"change": "123"}, params.Meta) |
| 203 | + assert.Contains(t, f.logs.String(), "bad-key") |
| 204 | + assert.Contains(t, f.logs.String(), "worse key") |
| 205 | + }) |
| 206 | + |
| 207 | + t.Run("emission before the session connects is dropped with a log", func(t *testing.T) { |
| 208 | + t.Parallel() |
| 209 | + |
| 210 | + logs := &bytes.Buffer{} |
| 211 | + emitter := &channelEmitter{ |
| 212 | + transport: &captureTransport{inner: nil}, |
| 213 | + lgr: slog.New(slog.NewTextHandler(logs, nil)), |
| 214 | + } |
| 215 | + |
| 216 | + require.NoError(t, emitter.Emit(t.Context(), "content", nil)) |
| 217 | + assert.Contains(t, logs.String(), "session not connected") |
| 218 | + }) |
| 219 | +} |
0 commit comments