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

Function ExampleSSEHandler

mcp/sse_example_test.go:30–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28}
29
30func ExampleSSEHandler() {
31 server := mcp.NewServer(&mcp.Implementation{Name: "adder", Version: "v0.0.1"}, nil)
32 mcp.AddTool(server, &mcp.Tool{Name: "add", Description: "add two numbers"}, Add)
33
34 handler := mcp.NewSSEHandler(func(*http.Request) *mcp.Server { return server }, nil)
35 httpServer := httptest.NewServer(handler)
36 defer httpServer.Close()
37
38 ctx := context.Background()
39 transport := &mcp.SSEClientTransport{Endpoint: httpServer.URL}
40 client := mcp.NewClient(&mcp.Implementation{Name: "test", Version: "v1.0.0"}, nil)
41 cs, err := client.Connect(ctx, transport, nil)
42 if err != nil {
43 log.Fatal(err)
44 }
45 defer cs.Close()
46
47 res, err := cs.CallTool(ctx, &mcp.CallToolParams{
48 Name: "add",
49 Arguments: map[string]any{"x": 1, "y": 2},
50 })
51 if err != nil {
52 log.Fatal(err)
53 }
54 fmt.Println(res.Content[0].(*mcp.TextContent).Text)
55
56 // Output: 3
57}

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.95
NewServerFunction · 0.92
AddToolFunction · 0.92
NewSSEHandlerFunction · 0.92
NewClientFunction · 0.92
CallToolMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…