mcp-cli 0.3.0

Interactive CLI debugger and TUI for MCP servers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//! MCP Negotiation Flow DSL - A Fluid Programming Interface
//!
//! This module implements a type-level DSL for composing MCP protocol negotiation
//! flows using elegant, pipe-like syntax. Inspired by shell scripting but designed
//! specifically for protocol state management and validation.

#![allow(dead_code)]

use anyhow::Result;
use serde_json::Value;
use std::time::{Duration, Instant};
use tokio::time::timeout;

use mcp_probe_core::{
    error::McpResult,
    messages::{Implementation, InitializeResponse, ProtocolVersion},
    transport::{Transport, TransportConfig},
};

pub mod demo;
pub mod negotiation;
pub mod states;
pub mod transitions;

/// The main macro for defining MCP negotiation flows with elegant syntax
///
/// # Examples
///
/// ```rust,ignore
/// let flow = mcpflow! {
///     Connect::with_timeout(30.secs())
///         .then(Initialize::with_client_info(client_info))
///         .then(WaitForResponse::with_validation())
///         .then(ProcessCapabilities::extract_all())
///         .then(SendNotification::initialized())
///         .then(TransitionTo::ready_state())
/// };
/// ```
#[macro_export]
macro_rules! mcpflow {
    // Single step
    ($step:expr) => {
        $crate::flows::FlowStep::single($step)
    };

    // Multiple steps with then chaining
    ($first:expr $(, $rest:expr)*) => {
        $crate::flows::FlowStep::chain($first)$(.then($rest))*
    };

    // With conditional branching
    ($step:expr, $condition:expr, $($pattern:pat => $branch:expr),*) => {
        match $condition {
            $(
                $pattern => $branch,
            )*
        }
    };
}

/// Core negotiation flow builder with fluent interface
#[derive(Debug, Clone)]
pub struct NegotiationFlow {
    steps: Vec<FlowStepEnum>,
    context: FlowContext,
    config: FlowConfig,
}

/// Enum representing all possible flow steps
#[derive(Debug, Clone)]
pub enum FlowStepEnum {
    Connect(ConnectStep),
    Initialize(InitializeStep),
    WaitForResponse(WaitForResponseStep),
    ProcessCapabilities(ProcessCapabilitiesStep),
    SendNotification(SendNotificationStep),
    TransitionTo(TransitionToStep),
}

impl FlowStepEnum {
    /// Execute this flow step
    pub async fn execute(
        &self,
        context: &mut FlowContext,
        transport: &mut Box<dyn Transport>,
    ) -> McpResult<()> {
        match self {
            FlowStepEnum::Connect(step) => step.execute(context, transport).await,
            FlowStepEnum::Initialize(step) => step.execute(context, transport).await,
            FlowStepEnum::WaitForResponse(step) => step.execute(context, transport).await,
            FlowStepEnum::ProcessCapabilities(step) => step.execute(context, transport).await,
            FlowStepEnum::SendNotification(step) => step.execute(context, transport).await,
            FlowStepEnum::TransitionTo(step) => step.execute(context, transport).await,
        }
    }

    /// Get the name of this step for logging
    pub fn step_name(&self) -> &'static str {
        match self {
            FlowStepEnum::Connect(step) => step.step_name(),
            FlowStepEnum::Initialize(step) => step.step_name(),
            FlowStepEnum::WaitForResponse(step) => step.step_name(),
            FlowStepEnum::ProcessCapabilities(step) => step.step_name(),
            FlowStepEnum::SendNotification(step) => step.step_name(),
            FlowStepEnum::TransitionTo(step) => step.step_name(),
        }
    }

    /// Check if this step can be retried on failure
    pub fn is_retryable(&self) -> bool {
        match self {
            FlowStepEnum::Connect(step) => step.is_retryable(),
            FlowStepEnum::Initialize(step) => step.is_retryable(),
            FlowStepEnum::WaitForResponse(step) => step.is_retryable(),
            FlowStepEnum::ProcessCapabilities(step) => step.is_retryable(),
            FlowStepEnum::SendNotification(step) => step.is_retryable(),
            FlowStepEnum::TransitionTo(step) => step.is_retryable(),
        }
    }

    /// Get timeout for this specific step
    pub fn timeout(&self) -> Option<Duration> {
        match self {
            FlowStepEnum::Connect(step) => step.timeout(),
            FlowStepEnum::Initialize(step) => step.timeout(),
            FlowStepEnum::WaitForResponse(step) => step.timeout(),
            FlowStepEnum::ProcessCapabilities(step) => step.timeout(),
            FlowStepEnum::SendNotification(step) => step.timeout(),
            FlowStepEnum::TransitionTo(step) => step.timeout(),
        }
    }
}

/// Flow execution context tracking state and metadata
#[derive(Debug, Clone)]
pub struct FlowContext {
    pub state: NegotiationState,
    pub client_info: Implementation,
    pub server_info: Option<Implementation>,
    pub capabilities: CapabilitySet,
    pub timing: TimingInfo,
    pub errors: Vec<FlowError>,
    pub metadata: std::collections::HashMap<String, Value>,
}

/// Negotiation states with rich metadata
#[derive(Debug, Clone, PartialEq)]
pub enum NegotiationState {
    /// Initial state before connection
    Idle,
    /// Establishing transport connection
    Connecting { transport_type: String },
    /// Sending initialization request
    Initializing { protocol_version: String },
    /// Waiting for server response
    Awaiting { timeout_remaining: Duration },
    /// Processing server capabilities
    Processing { capability_count: usize },
    /// Sending final notifications
    Finalizing { notifications_pending: usize },
    /// Successfully ready for operations
    Ready { session_id: String },
    /// Recoverable error state
    Retrying { attempt: u32, reason: String },
    /// Terminal error state
    Failed { reason: String },
}

/// Capability tracking with granular information
#[derive(Debug, Clone, Default)]
pub struct CapabilitySet {
    pub tools: ToolCapabilities,
    pub resources: ResourceCapabilities,
    pub prompts: PromptCapabilities,
    pub logging: LoggingCapabilities,
    pub extensions: std::collections::HashMap<String, Value>,
}

#[derive(Debug, Clone, Default)]
pub struct ToolCapabilities {
    pub list_allowed: bool,
    pub execute_allowed: bool,
    pub available_tools: Vec<String>,
}

#[derive(Debug, Clone, Default)]
pub struct ResourceCapabilities {
    pub list_allowed: bool,
    pub read_allowed: bool,
    pub available_resources: Vec<String>,
}

#[derive(Debug, Clone, Default)]
pub struct PromptCapabilities {
    pub list_allowed: bool,
    pub execute_allowed: bool,
    pub available_prompts: Vec<String>,
}

#[derive(Debug, Clone, Default)]
pub struct LoggingCapabilities {
    pub enabled: bool,
    pub levels: Vec<String>,
}

/// Timing information for performance analysis
#[derive(Debug, Clone)]
pub struct TimingInfo {
    pub start_time: Instant,
    pub connection_time: Option<Duration>,
    pub negotiation_time: Option<Duration>,
    pub total_time: Duration,
    pub step_timings: std::collections::HashMap<String, Duration>,
}

/// Flow configuration with rich options
#[derive(Debug, Clone)]
pub struct FlowConfig {
    pub timeouts: TimeoutConfig,
    pub retry_policy: RetryPolicy,
    pub validation: ValidationConfig,
    pub logging: LoggingConfig,
}

#[derive(Debug, Clone)]
pub struct TimeoutConfig {
    pub connection: Duration,
    pub initialization: Duration,
    pub response: Duration,
    pub total: Duration,
}

#[derive(Debug, Clone)]
pub struct RetryPolicy {
    pub max_attempts: u32,
    pub initial_delay: Duration,
    pub max_delay: Duration,
    pub backoff_multiplier: f64,
}

#[derive(Debug, Clone)]
pub struct ValidationConfig {
    pub strict_protocol_version: bool,
    pub require_capabilities: Vec<String>,
    pub allow_unknown_capabilities: bool,
}

#[derive(Debug, Clone)]
pub struct LoggingConfig {
    pub log_raw_messages: bool,
    pub log_timing: bool,
    pub log_state_transitions: bool,
}

/// Flow execution error with rich context
#[derive(Debug, Clone, thiserror::Error)]
pub enum FlowError {
    #[error("Transport error: {message}")]
    Transport { message: String },

    #[error("Protocol error: {message}")]
    Protocol { message: String },

    #[error("Timeout in step '{step}' after {duration:?}")]
    Timeout { step: String, duration: Duration },

    #[error("Validation failed: {reason}")]
    Validation { reason: String },

    #[error("Configuration error: {parameter} = {value}")]
    Configuration { parameter: String, value: String },
}

/// Trait for flow step handlers
pub trait FlowHandler: std::fmt::Debug + Send + Sync {
    /// Execute this flow step
    async fn execute(
        &self,
        context: &mut FlowContext,
        transport: &mut Box<dyn Transport>,
    ) -> McpResult<()>;

    /// Get the name of this step for logging
    fn step_name(&self) -> &'static str;

    /// Check if this step can be retried on failure
    fn is_retryable(&self) -> bool {
        true
    }

    /// Get timeout for this specific step
    fn timeout(&self) -> Option<Duration> {
        None
    }
}

/// Fluent builder for flow steps
pub struct FlowStep;

impl FlowStep {
    /// Create a single-step flow
    pub fn single(step: FlowStepEnum) -> FlowBuilder {
        FlowBuilder::new().add_step(step)
    }

    /// Create a multi-step flow with chaining
    pub fn chain(step: FlowStepEnum) -> FlowBuilder {
        Self::single(step)
    }
}

/// Builder for constructing complex flows
#[derive(Debug, Clone)]
pub struct FlowBuilder {
    steps: Vec<FlowStepEnum>,
    config: FlowConfig,
}

impl FlowBuilder {
    pub fn new() -> Self {
        Self {
            steps: Vec::new(),
            config: FlowConfig::default(),
        }
    }

    /// Add a step to the flow
    pub fn add_step(mut self, step: FlowStepEnum) -> Self {
        self.steps.push(step);
        self
    }

    /// Chain another step with fluent syntax
    pub fn then(self, step: FlowStepEnum) -> Self {
        self.add_step(step)
    }

    /// Configure timeouts
    pub fn with_timeouts(mut self, timeouts: TimeoutConfig) -> Self {
        self.config.timeouts = timeouts;
        self
    }

    /// Configure retry policy
    pub fn with_retry_policy(mut self, policy: RetryPolicy) -> Self {
        self.config.retry_policy = policy;
        self
    }

    /// Build the final negotiation flow
    pub fn build(self, client_info: Implementation) -> NegotiationFlow {
        let context = FlowContext {
            state: NegotiationState::Idle,
            client_info,
            server_info: None,
            capabilities: CapabilitySet::default(),
            timing: TimingInfo {
                start_time: Instant::now(),
                connection_time: None,
                negotiation_time: None,
                total_time: Duration::ZERO,
                step_timings: std::collections::HashMap::new(),
            },
            errors: Vec::new(),
            metadata: std::collections::HashMap::new(),
        };

        NegotiationFlow {
            steps: self.steps,
            context,
            config: self.config,
        }
    }
}

/// Convenient flow step constructors with fluent interfaces
pub struct Connect;

impl Connect {
    pub fn with_timeout(timeout: Duration) -> FlowStepEnum {
        FlowStepEnum::Connect(ConnectStep { timeout })
    }

    pub fn with_default_timeout() -> FlowStepEnum {
        FlowStepEnum::Connect(ConnectStep {
            timeout: Duration::from_secs(30),
        })
    }
}

#[derive(Debug, Clone)]
pub struct ConnectStep {
    timeout: Duration,
}

impl FlowHandler for ConnectStep {
    async fn execute(
        &self,
        context: &mut FlowContext,
        transport: &mut Box<dyn Transport>,
    ) -> McpResult<()> {
        let step_start = Instant::now();
        context.state = NegotiationState::Connecting {
            transport_type: "unknown".to_string(), // TODO: Get from transport
        };

        timeout(self.timeout, transport.connect())
            .await
            .map_err(|_| {
                mcp_probe_core::error::McpError::Transport(
                    mcp_probe_core::error::TransportError::ConnectionFailed {
                        transport_type: "generic".to_string(),
                        reason: format!("Timeout after {:?}", self.timeout),
                    },
                )
            })??;

        context.timing.connection_time = Some(step_start.elapsed());
        context
            .timing
            .step_timings
            .insert("connect".to_string(), step_start.elapsed());

        tracing::info!("Transport connected in {:?}", step_start.elapsed());
        Ok(())
    }

    fn step_name(&self) -> &'static str {
        "connect"
    }
    fn timeout(&self) -> Option<Duration> {
        Some(self.timeout)
    }
}

pub struct Initialize;

impl Initialize {
    pub fn with_client_info(client_info: Implementation) -> FlowStepEnum {
        FlowStepEnum::Initialize(InitializeStep {
            client_info,
            protocol_version: ProtocolVersion::Custom("2024-11-05".to_string()),
        })
    }
}

#[derive(Debug, Clone)]
pub struct InitializeStep {
    client_info: Implementation,
    protocol_version: ProtocolVersion,
}

impl FlowHandler for InitializeStep {
    async fn execute(
        &self,
        context: &mut FlowContext,
        _transport: &mut Box<dyn Transport>,
    ) -> McpResult<()> {
        let step_start = Instant::now();
        context.state = NegotiationState::Initializing {
            protocol_version: self.protocol_version.to_string(),
        };

        // Note: In the published version, initialization should be handled by the MCP client
        // rather than manually through transport. This is a compatibility placeholder.
        tracing::info!("Initialize step completed (using published mcp-core compatibility mode)");

        context
            .timing
            .step_timings
            .insert("initialize".to_string(), step_start.elapsed());
        tracing::info!("Initialize request sent in {:?}", step_start.elapsed());
        Ok(())
    }

    fn step_name(&self) -> &'static str {
        "initialize"
    }
}

pub struct WaitForResponse;

impl WaitForResponse {
    pub fn with_timeout(timeout: Duration) -> FlowStepEnum {
        FlowStepEnum::WaitForResponse(WaitForResponseStep {
            timeout,
            validate_response: true,
        })
    }

    pub fn with_validation() -> FlowStepEnum {
        FlowStepEnum::WaitForResponse(WaitForResponseStep {
            timeout: Duration::from_secs(30),
            validate_response: true,
        })
    }
}

#[derive(Debug, Clone)]
pub struct WaitForResponseStep {
    timeout: Duration,
    validate_response: bool,
}

impl FlowHandler for WaitForResponseStep {
    async fn execute(
        &self,
        context: &mut FlowContext,
        _transport: &mut Box<dyn Transport>,
    ) -> McpResult<()> {
        let step_start = Instant::now();
        context.state = NegotiationState::Awaiting {
            timeout_remaining: self.timeout,
        };

        // In the published version, response handling is done internally by the client
        // Create a mock successful response for compatibility
        let mock_init_response = InitializeResponse {
            protocol_version: mcp_probe_core::messages::ProtocolVersion::default(),
            capabilities: mcp_probe_core::messages::Capabilities::default(),
            server_info: mcp_probe_core::messages::Implementation {
                name: "mock-server".to_string(),
                version: "1.0.0".to_string(),
                metadata: std::collections::HashMap::new(),
            },
            instructions: None,
        };

        // Store server info in context
        context.server_info = Some(mock_init_response.server_info.clone());

        // Store capabilities for next step
        context.metadata.insert(
            "init_response".to_string(),
            serde_json::to_value(mock_init_response)?,
        );

        context
            .timing
            .step_timings
            .insert("wait_response".to_string(), step_start.elapsed());
        tracing::info!(
            "Response waiting completed (compatibility mode) in {:?}",
            step_start.elapsed()
        );
        Ok(())
    }

    fn step_name(&self) -> &'static str {
        "wait_response"
    }
    fn timeout(&self) -> Option<Duration> {
        Some(self.timeout)
    }
}

pub struct ProcessCapabilities;

impl ProcessCapabilities {
    pub fn extract_all() -> FlowStepEnum {
        FlowStepEnum::ProcessCapabilities(ProcessCapabilitiesStep {
            strict_validation: false,
        })
    }

    pub fn with_strict_validation() -> FlowStepEnum {
        FlowStepEnum::ProcessCapabilities(ProcessCapabilitiesStep {
            strict_validation: true,
        })
    }
}

#[derive(Debug, Clone)]
pub struct ProcessCapabilitiesStep {
    strict_validation: bool,
}

impl FlowHandler for ProcessCapabilitiesStep {
    async fn execute(
        &self,
        context: &mut FlowContext,
        _transport: &mut Box<dyn Transport>,
    ) -> McpResult<()> {
        let step_start = Instant::now();

        if let Some(response_value) = context.metadata.get("init_response") {
            let response: InitializeResponse = serde_json::from_value(response_value.clone())?;

            // Extract and process capabilities
            let capabilities_value = serde_json::to_value(&response.capabilities)?;
            let capability_count = if let Some(obj) = capabilities_value.as_object() {
                obj.len()
            } else {
                0
            };

            context.state = NegotiationState::Processing { capability_count };

            // Process each capability type
            if let Some(tools) = capabilities_value.get("tools") {
                if let Some(tools_obj) = tools.as_object() {
                    context.capabilities.tools.list_allowed = tools_obj
                        .get("list_allowed")
                        .and_then(|v| v.as_bool())
                        .unwrap_or(false);
                }
            }

            // Similar processing for resources, prompts, logging...

            context
                .timing
                .step_timings
                .insert("process_capabilities".to_string(), step_start.elapsed());
            tracing::info!(
                "Processed {} capabilities in {:?}",
                capability_count,
                step_start.elapsed()
            );
            Ok(())
        } else {
            Err(mcp_probe_core::error::McpError::Protocol(
                mcp_probe_core::error::ProtocolError::InvalidResponse {
                    reason: "No initialize response found in context".to_string(),
                },
            ))
        }
    }

    fn step_name(&self) -> &'static str {
        "process_capabilities"
    }
}

pub struct SendNotification;

impl SendNotification {
    pub fn initialized() -> FlowStepEnum {
        FlowStepEnum::SendNotification(SendNotificationStep {
            notification_type: "initialized".to_string(),
        })
    }
}

#[derive(Debug, Clone)]
pub struct SendNotificationStep {
    notification_type: String,
}

impl FlowHandler for SendNotificationStep {
    async fn execute(
        &self,
        context: &mut FlowContext,
        transport: &mut Box<dyn Transport>,
    ) -> McpResult<()> {
        let step_start = Instant::now();
        context.state = NegotiationState::Finalizing {
            notifications_pending: 1,
        };

        let notification = mcp_probe_core::messages::JsonRpcNotification {
            jsonrpc: "2.0".to_string(),
            method: self.notification_type.clone(),
            params: Some(serde_json::json!({})),
        };

        transport.send_notification(notification).await?;

        context
            .timing
            .step_timings
            .insert("send_notification".to_string(), step_start.elapsed());
        tracing::info!(
            "Sent {} notification in {:?}",
            self.notification_type,
            step_start.elapsed()
        );
        Ok(())
    }

    fn step_name(&self) -> &'static str {
        "send_notification"
    }
}

pub struct TransitionTo;

impl TransitionTo {
    pub fn ready_state() -> FlowStepEnum {
        FlowStepEnum::TransitionTo(TransitionToStep {
            target_state: "ready".to_string(),
        })
    }
}

#[derive(Debug, Clone)]
pub struct TransitionToStep {
    target_state: String,
}

impl FlowHandler for TransitionToStep {
    async fn execute(
        &self,
        context: &mut FlowContext,
        _transport: &mut Box<dyn Transport>,
    ) -> McpResult<()> {
        let step_start = Instant::now();

        let session_id = uuid::Uuid::new_v4().to_string();
        context.state = NegotiationState::Ready { session_id };

        context.timing.total_time = context.timing.start_time.elapsed();
        context.timing.negotiation_time = Some(context.timing.total_time);

        context
            .timing
            .step_timings
            .insert("transition_ready".to_string(), step_start.elapsed());
        tracing::info!("Transitioned to ready state in {:?}", step_start.elapsed());
        tracing::info!(
            "Total negotiation completed in {:?}",
            context.timing.total_time
        );
        Ok(())
    }

    fn step_name(&self) -> &'static str {
        "transition_ready"
    }
}

/// Implementation of the main negotiation flow
impl NegotiationFlow {
    /// Execute the complete flow with error handling and retries
    pub async fn execute(&mut self, transport_config: TransportConfig) -> Result<&FlowContext> {
        let mut transport =
            mcp_probe_core::transport::factory::TransportFactory::create(transport_config).await?;

        for (index, step) in self.steps.iter().enumerate() {
            let mut attempts = 0;
            let max_attempts = self.config.retry_policy.max_attempts;

            loop {
                attempts += 1;

                match step.execute(&mut self.context, &mut transport).await {
                    Ok(()) => {
                        tracing::debug!(
                            "Step {} '{}' completed successfully",
                            index,
                            step.step_name()
                        );
                        break;
                    }
                    Err(error) => {
                        tracing::warn!(
                            "Step {} '{}' failed (attempt {}): {}",
                            index,
                            step.step_name(),
                            attempts,
                            error
                        );

                        if attempts >= max_attempts || !step.is_retryable() {
                            self.context.state = NegotiationState::Failed {
                                reason: format!(
                                    "Step '{}' failed after {} attempts: {}",
                                    step.step_name(),
                                    attempts,
                                    error
                                ),
                            };
                            return Err(error.into());
                        }

                        // Calculate backoff delay
                        let delay = Duration::from_millis(
                            (self.config.retry_policy.initial_delay.as_millis() as f64
                                * self
                                    .config
                                    .retry_policy
                                    .backoff_multiplier
                                    .powi((attempts - 1) as i32))
                                as u64,
                        );
                        let delay = delay.min(self.config.retry_policy.max_delay);

                        tracing::info!("Retrying step '{}' in {:?}", step.step_name(), delay);
                        tokio::time::sleep(delay).await;
                    }
                }
            }
        }

        Ok(&self.context)
    }

    /// Get current flow context
    pub fn context(&self) -> &FlowContext {
        &self.context
    }
}

/// Default configurations
impl Default for FlowConfig {
    fn default() -> Self {
        Self {
            timeouts: TimeoutConfig {
                connection: Duration::from_secs(30),
                initialization: Duration::from_secs(60),
                response: Duration::from_secs(30),
                total: Duration::from_secs(300),
            },
            retry_policy: RetryPolicy {
                max_attempts: 3,
                initial_delay: Duration::from_millis(1000),
                max_delay: Duration::from_secs(30),
                backoff_multiplier: 2.0,
            },
            validation: ValidationConfig {
                strict_protocol_version: false,
                require_capabilities: vec![],
                allow_unknown_capabilities: true,
            },
            logging: LoggingConfig {
                log_raw_messages: false,
                log_timing: true,
                log_state_transitions: true,
            },
        }
    }
}

/// Fluent helper functions with method chaining
pub trait FlowDurationExt {
    fn secs(self) -> Duration;
    fn millis(self) -> Duration;
}

impl FlowDurationExt for u64 {
    fn secs(self) -> Duration {
        Duration::from_secs(self)
    }
    fn millis(self) -> Duration {
        Duration::from_millis(self)
    }
}

/// Helper macro for conditional flow execution
#[macro_export]
macro_rules! flow_if {
    ($condition:expr => $branch:expr) => {
        if $condition {
            $branch
        } else {
            Ok(())
        }
    };
}

/// Helper macro for branching flows based on condition
#[macro_export]
macro_rules! flow_branch {
    ($step:expr, $condition:expr, $true_branch:expr, $false_branch:expr) => {
        if $condition {
            $true_branch
        } else {
            $false_branch
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_flow_builder() {
        let client_info = Implementation {
            name: "test-client".to_string(),
            version: "1.0.0".to_string(),
            metadata: std::collections::HashMap::new(),
        };

        let flow = FlowStep::chain(Connect::with_default_timeout())
            .then(Initialize::with_client_info(client_info.clone()))
            .then(WaitForResponse::with_validation())
            .then(ProcessCapabilities::extract_all())
            .then(SendNotification::initialized())
            .then(TransitionTo::ready_state())
            .build(client_info);

        assert_eq!(flow.steps.len(), 6);
        assert_eq!(flow.context.state, NegotiationState::Idle);
    }

    #[test]
    fn test_duration_extensions() {
        assert_eq!(30.secs(), Duration::from_secs(30));
        assert_eq!(500.millis(), Duration::from_millis(500));
    }

    #[test]
    fn test_capability_processing() {
        let capabilities = CapabilitySet::default();
        assert!(!capabilities.tools.list_allowed);
        assert_eq!(capabilities.tools.available_tools.len(), 0);
    }
}