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

Function TestSSEServer

mcp/sse_test.go:21–127  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

19)
20
21func TestSSEServer(t *testing.T) {
22 for _, closeServerFirst := range []bool{false, true} {
23 t.Run(fmt.Sprintf("closeServerFirst=%t", closeServerFirst), func(t *testing.T) {
24 ctx := context.Background()
25 server := NewServer(testImpl, nil)
26 AddTool(server, &Tool{Name: "greet"}, sayHi)
27
28 sseHandler := NewSSEHandler(func(*http.Request) *Server { return server }, nil)
29
30 serverSessions := make(chan *ServerSession, 1)
31 sseHandler.onConnection = func(ss *ServerSession) {
32 select {
33 case serverSessions <- ss:
34 default:
35 }
36 }
37 httpServer := httptest.NewServer(sseHandler)
38 defer httpServer.Close()
39
40 var customClientUsed int64
41 customClient := &http.Client{
42 Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
43 atomic.AddInt64(&customClientUsed, 1)
44 return http.DefaultTransport.RoundTrip(req)
45 }),
46 }
47
48 clientTransport := &SSEClientTransport{
49 Endpoint: httpServer.URL,
50 HTTPClient: customClient,
51 }
52
53 c := NewClient(testImpl, nil)
54 cs, err := c.Connect(ctx, clientTransport, nil)
55 if err != nil {
56 t.Fatal(err)
57 }
58 if err := cs.Ping(ctx, nil); err != nil {
59 t.Fatal(err)
60 }
61 ss := <-serverSessions
62 gotHi, err := cs.CallTool(ctx, &CallToolParams{
63 Name: "greet",
64 Arguments: map[string]any{"Name": "user"},
65 })
66 if err != nil {
67 t.Fatal(err)
68 }
69 wantHi := &CallToolResult{
70 Content: []Content{
71 &TextContent{Text: "hi user"},
72 },
73 }
74 if diff := cmp.Diff(wantHi, gotHi, ctrCmpOpts...); diff != "" {
75 t.Errorf("tools/call 'greet' mismatch (-want +got):\n%s", diff)
76 }
77
78 // Verify that customClient was used

Callers

nothing calls this directly

Calls 13

ConnectMethod · 0.95
NewServerFunction · 0.85
AddToolFunction · 0.85
NewSSEHandlerFunction · 0.85
roundTripperFuncFuncType · 0.85
NewClientFunction · 0.85
RunMethod · 0.80
CallToolMethod · 0.80
CloseMethod · 0.65
PingMethod · 0.65
RoundTripMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…