!+lifecycle
()
| 17 | // !+lifecycle |
| 18 | |
| 19 | func 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 |
nothing calls this directly
no test coverage detected
searching dependent graphs…