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

Function Example_lifecycle

mcp/mcp_example_test.go:19–54  ·  view source on GitHub ↗

!+lifecycle

()

Source from the content-addressed store, hash-verified

17// !+lifecycle
18
19func Example_lifecycle() {
20 ctx := context.Background()
21
22 // Create a client and server.
23 // Wait for the client to initialize the session.
24 client := mcp.NewClient(&mcp.Implementation{Name: "client", Version: "v0.0.1"}, nil)
25 server := mcp.NewServer(&mcp.Implementation{Name: "server", Version: "v0.0.1"}, &mcp.ServerOptions{
26 InitializedHandler: func(context.Context, *mcp.InitializedRequest) {
27 fmt.Println("initialized!")
28 },
29 })
30
31 // Connect the server and client using in-memory transports.
32 //
33 // Connect the server first so that it's ready to receive initialization
34 // messages from the client.
35 t1, t2 := mcp.NewInMemoryTransports()
36 serverSession, err := server.Connect(ctx, t1, nil)
37 if err != nil {
38 log.Fatal(err)
39 }
40 clientSession, err := client.Connect(ctx, t2, nil)
41 if err != nil {
42 log.Fatal(err)
43 }
44
45 // Now shut down the session by closing the client, and waiting for the
46 // server session to end.
47 if err := clientSession.Close(); err != nil {
48 log.Fatal(err)
49 }
50 if err := serverSession.Wait(); err != nil {
51 log.Fatal(err)
52 }
53 // Output: initialized!
54}
55
56// !-lifecycle
57

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.95
ConnectMethod · 0.95
NewClientFunction · 0.92
NewServerFunction · 0.92
NewInMemoryTransportsFunction · 0.92
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…