|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/mark3labs/mcp-go/mcp" |
| 8 | + "github.com/mark3labs/mcp-go/server" |
| 9 | +) |
| 10 | + |
| 11 | +func TestStateRestoration(t *testing.T) { |
| 12 | + mcpServer := server.NewMCPServer( |
| 13 | + "test-server", |
| 14 | + "1.0.0", |
| 15 | + server.WithToolCapabilities(true), |
| 16 | + ) |
| 17 | + |
| 18 | + // First client lifecycle |
| 19 | + client1, err := NewInProcessClient(mcpServer) |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("Failed to create first client: %v", err) |
| 22 | + } |
| 23 | + |
| 24 | + ctx := context.Background() |
| 25 | + |
| 26 | + if err := client1.Start(ctx); err != nil { |
| 27 | + t.Fatalf("Failed to start first client: %v", err) |
| 28 | + } |
| 29 | + |
| 30 | + initRequest := mcp.InitializeRequest{} |
| 31 | + initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION |
| 32 | + initRequest.Params.ClientInfo = mcp.Implementation{ |
| 33 | + Name: "test-client", |
| 34 | + Version: "1.0.0", |
| 35 | + } |
| 36 | + |
| 37 | + if _, err := client1.Initialize(ctx, initRequest); err != nil { |
| 38 | + t.Fatalf("Failed to initialize first client: %v", err) |
| 39 | + } |
| 40 | + |
| 41 | + if err := client1.Ping(ctx); err != nil { |
| 42 | + t.Fatalf("Ping on first client failed: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + exportedState := client1.ExportState() |
| 46 | + |
| 47 | + if err := client1.Close(); err != nil { |
| 48 | + t.Fatalf("Failed to close first client: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + // Second client lifecycle (state restoration) |
| 52 | + client2, err := NewInProcessClient(mcpServer) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("Failed to create second client: %v", err) |
| 55 | + } |
| 56 | + defer client2.Close() |
| 57 | + |
| 58 | + if err := client2.Start(ctx); err != nil { |
| 59 | + t.Fatalf("Failed to start second client: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + if err := client2.ImportState(ctx, exportedState); err != nil { |
| 63 | + t.Fatalf("Failed to import state into second client: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + if err := client2.Ping(ctx); err != nil { |
| 67 | + t.Fatalf("Ping on restored client failed: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + restoredState := client2.ExportState() |
| 71 | + |
| 72 | + if !restoredState.Initialized { |
| 73 | + t.Errorf("Expected restored client to be initialized") |
| 74 | + } |
| 75 | + |
| 76 | + if restoredState.RequestID <= exportedState.RequestID { |
| 77 | + t.Errorf("Expected request ID to increase after restoration; before=%d, after=%d", exportedState.RequestID, restoredState.RequestID) |
| 78 | + } |
| 79 | +} |
0 commit comments