MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestStreamableConcurrentHandling

Function TestStreamableConcurrentHandling

mcp/streamable_test.go:242–289  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

240}
241
242func TestStreamableConcurrentHandling(t *testing.T) {
243 // This test checks that the streamable server and client transports can
244 // communicate.
245 type count struct {
246 Count int
247 }
248
249 var mu sync.Mutex
250 counts := make(map[string]int)
251
252 server := NewServer(testImpl, nil)
253 AddTool(server, &Tool{Name: "inc"}, func(ctx context.Context, req *CallToolRequest, _ any) (*CallToolResult, count, error) {
254 id := req.Session.ID()
255 mu.Lock()
256 defer mu.Unlock()
257 c := counts[id]
258 counts[id] = c + 1
259 return nil, count{c}, nil
260 })
261 handler := NewStreamableHTTPHandler(func(req *http.Request) *Server { return server }, nil)
262 httpServer := httptest.NewServer(mustNotPanic(t, handler))
263 defer httpServer.Close()
264
265 ctx := context.Background()
266 client := NewClient(testImpl, nil)
267 var wg sync.WaitGroup
268 for range 100 {
269 wg.Go(func() {
270 clientSession, err := client.Connect(ctx, &StreamableClientTransport{Endpoint: httpServer.URL}, nil)
271 if err != nil {
272 t.Errorf("Connect failed: %v", err)
273 return
274 }
275 defer clientSession.Close()
276 for i := range 10 {
277 res, err := clientSession.CallTool(ctx, &CallToolParams{Name: "inc"})
278 if err != nil {
279 t.Errorf("CallTool failed: %v", err)
280 return
281 }
282 if got := int(res.StructuredContent.(map[string]any)["Count"].(float64)); got != i {
283 t.Errorf("got count %d, want %d", got, i)
284 }
285 }
286 })
287 }
288 wg.Wait()
289}
290
291func TestStreamableServerShutdown(t *testing.T) {
292 ctx := context.Background()

Callers

nothing calls this directly

Calls 10

ConnectMethod · 0.95
NewServerFunction · 0.85
AddToolFunction · 0.85
NewStreamableHTTPHandlerFunction · 0.85
mustNotPanicFunction · 0.85
NewClientFunction · 0.85
CallToolMethod · 0.80
IDMethod · 0.65
CloseMethod · 0.65
WaitMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…