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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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
#!/usr/bin/env python3
"""End-to-End Testing for Real MCP Execution Flow.

This module implements comprehensive end-to-end testing for Task 65: MVP4 Sprint 3 - End to End Testing.
It covers the complete flow from user query to real MCP tool execution, including:

- Full workflow: Query → Planning → Input Collection → Real MCP Execution
- Live MCP server communication testing
- Error handling and fallback scenarios
- Performance and reliability testing
- Integration testing across all system components

This test suite validates that the complete KGraph-MCP system works end-to-end
with real MCP servers running on localhost.
"""

import time
from typing import Any
from unittest.mock import Mock, patch

import pytest
import requests
from fastapi.testclient import TestClient

from agents.executor import McpExecutorAgent
from app import app, app_with_ui, initialize_agent_system
from kg_services.ontology import MCPPrompt, MCPTool, PlannedStep


class TestE2EMcpExecutionFlow:
    """Test complete end-to-end MCP execution workflows."""

    @pytest.fixture
    def client(self):
        """Provide test client with initialized system."""
        return TestClient(app)

    @pytest.fixture
    def initialized_system(self):
        """Provide fully initialized system with real agents."""
        import app as app_module

        planner, executor = initialize_agent_system()
        app_module.planner_agent = planner
        app_module.executor_agent = executor

        return {
            "planner": planner,
            "executor": executor,
            "client": TestClient(app_with_ui)
        }

    @pytest.fixture
    def mcp_executor(self):
        """Provide McpExecutorAgent for direct testing."""
        return McpExecutorAgent()

    def test_complete_sentiment_analysis_mcp_flow(self, initialized_system):
        """Test complete flow: query → plan → real MCP execution for sentiment analysis."""
        client = initialized_system["client"]

        # Skip if system not initialized
        if initialized_system["planner"] is None:
            pytest.skip("Agent system not initialized - missing data files or API keys")

        # Step 1: Submit sentiment analysis query
        plan_request = {
            "query": "I need to analyze the sentiment of customer feedback about our new product",
            "top_k": 3
        }

        response = client.post("/api/plan/generate", json=plan_request)

        # Handle case where system is not properly initialized (503 error)
        if response.status_code == 503:
            pytest.skip("Agent system not available - check initialization and API keys")

        assert response.status_code == 200

        plan_data = response.json()

        # Check if response has error status due to missing embeddings
        if "detail" in plan_data:
            pytest.skip(f"System error: {plan_data['detail']}")

        assert plan_data["status"] == "success"
        assert len(plan_data["planned_steps"]) > 0

        # Step 2: Find sentiment analysis tool in the plan
        sentiment_step = None
        for step in plan_data["planned_steps"]:
            tool_name = step["tool"]["name"].lower()
            tool_desc = step["tool"]["description"].lower()
            if "sentiment" in tool_name or "sentiment" in tool_desc:
                sentiment_step = step
                break

        assert sentiment_step is not None, "No sentiment analysis tool found in plan"

        # Step 3: Verify the tool is configured for real MCP execution
        tool_info = sentiment_step["tool"]
        assert tool_info.get("execution_type") == "remote_mcp_gradio"
        assert tool_info.get("mcp_endpoint_url") is not None

        # Step 4: Test real MCP execution (if server is available)
        if self._is_mcp_server_available(tool_info["mcp_endpoint_url"]):
            # Create PlannedStep for execution
            planned_step = self._create_planned_step_from_api_response(sentiment_step)

            # Prepare realistic input
            test_inputs = {
                "input_text": "This new product is absolutely amazing! I love how easy it is to use and the quality is outstanding. Highly recommend!"
            }

            # Execute with real MCP
            executor = initialized_system["executor"]
            if hasattr(executor, "execute_plan_step"):
                result = executor.execute_plan_step(planned_step, test_inputs)

                # Verify successful real MCP execution
                assert result["status"] == "success_live_mcp"
                assert "tool_specific_output" in result
                assert result["execution_mode"] == "live_mcp"
                assert "mcp_endpoint" in result

                # Verify output contains sentiment analysis results
                output = result["tool_specific_output"]
                assert output is not None
                assert len(output) > 0

                print(f"✅ Real MCP Sentiment Analysis Result: {output[:200]}...")
            else:
                pytest.skip("Executor doesn't support real MCP execution")
        else:
            pytest.skip(f"MCP server not available at {tool_info['mcp_endpoint_url']}")

    def test_complete_text_summarization_mcp_flow(self, initialized_system):
        """Test complete flow: query → plan → real MCP execution for text summarization."""
        client = initialized_system["client"]

        if initialized_system["planner"] is None:
            pytest.skip("Agent system not initialized")

        # Step 1: Submit summarization query
        plan_request = {
            "query": "I need to summarize a long technical document for my team meeting",
            "top_k": 3
        }

        response = client.post("/api/plan/generate", json=plan_request)

        # Handle case where system is not properly initialized
        if response.status_code == 503:
            pytest.skip("Agent system not available - check initialization and API keys")

        assert response.status_code == 200

        plan_data = response.json()

        # Check if response has error status
        if "detail" in plan_data:
            pytest.skip(f"System error: {plan_data['detail']}")

        assert plan_data["status"] == "success"

        # Step 2: Find text summarization tool
        summarizer_step = None
        for step in plan_data["planned_steps"]:
            tool_name = step["tool"]["name"].lower()
            tool_desc = step["tool"]["description"].lower()
            if "summar" in tool_name or "summar" in tool_desc:
                summarizer_step = step
                break

        assert summarizer_step is not None, "No text summarization tool found in plan"

        # Step 3: Test real MCP execution if server available
        tool_info = summarizer_step["tool"]
        if (tool_info.get("execution_type") == "remote_mcp_gradio" and
            self._is_mcp_server_available(tool_info["mcp_endpoint_url"])):

            planned_step = self._create_planned_step_from_api_response(summarizer_step)

            # Prepare realistic long text input
            long_text = """
            Artificial Intelligence (AI) has revolutionized numerous industries and continues to shape the future of technology. 
            Machine learning algorithms, particularly deep learning neural networks, have achieved remarkable breakthroughs in 
            computer vision, natural language processing, and predictive analytics. These advancements have enabled applications 
            ranging from autonomous vehicles and medical diagnosis to personalized recommendations and automated customer service.
            
            The integration of AI into business processes has led to increased efficiency, reduced costs, and improved decision-making 
            capabilities. Companies across various sectors are leveraging AI to optimize operations, enhance customer experiences, 
            and gain competitive advantages. However, the rapid adoption of AI also raises important considerations regarding ethics, 
            privacy, and the future of work.
            
            As AI technology continues to evolve, it is crucial for organizations to develop comprehensive strategies for responsible 
            AI implementation, ensuring that these powerful tools are used to benefit society while mitigating potential risks and 
            challenges. The future of AI promises even more sophisticated applications and transformative impacts across all aspects 
            of human life and business operations.
            """

            test_inputs = {
                "text": long_text.strip(),
                "max_length": "100",
                "min_length": "50"
            }

            executor = initialized_system["executor"]
            if hasattr(executor, "execute_plan_step"):
                result = executor.execute_plan_step(planned_step, test_inputs)

                # Handle cold start scenarios gracefully - accept both live success and simulation fallback
                if result["status"] == "success_live_mcp":
                    # Preferred: Live MCP execution succeeded
                    assert "tool_specific_output" in result
                    assert result["execution_mode"] == "live_mcp"
                    output = result["tool_specific_output"]
                    assert output is not None
                    assert len(output) > 0
                    print(f"✅ Real MCP Summarization Result: {output[:200]}...")
                elif result["status"] == "simulated_success":
                    # Acceptable: Fell back to simulation due to cold start/timeout
                    assert "tool_specific_output" in result
                    assert result["execution_mode"] == "simulated"
                    output = result["tool_specific_output"]
                    assert output is not None
                    assert len(output) > 0
                    print(f"✅ Simulated Summarization Result (cold start fallback): {output[:200]}...")
                elif result["status"].startswith("error_live_mcp") and "timeout" in result.get("message", "").lower():
                    # Acceptable: HuggingFace Space cold start timeout - this is normal for serverless deployments
                    print(f"✅ Expected HuggingFace Space cold start timeout: {result.get('message', 'timeout')[:100]}...")
                    pytest.skip("HuggingFace Space experiencing cold start timeout - normal for serverless deployments")
                else:
                    # Unexpected error
                    pytest.fail(f"Unexpected execution result: {result['status']} - {result.get('message', 'No message')}")

                # Verify summary output characteristics (regardless of execution mode)
                output = result["tool_specific_output"]
                assert len(output) < len(long_text)  # Should be shorter than input
            else:
                pytest.skip("Executor doesn't support real MCP execution")
        else:
            pytest.skip("MCP server not available or not configured for real execution")

    def test_mcp_execution_error_handling(self, mcp_executor):
        """Test error handling in real MCP execution scenarios."""
        # Create a test tool with invalid endpoint
        invalid_tool = MCPTool(
            tool_id="test_invalid",
            name="Invalid Test Tool",
            description="Tool for testing error handling",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="remote_mcp_gradio",
            mcp_endpoint_url="http://localhost:9999/invalid",
            timeout_seconds=5
        )

        test_prompt = MCPPrompt(
            prompt_id="test_prompt",
            name="Test Prompt",
            description="Test prompt",
            target_tool_id="test_invalid",
            template_string="Test: {{input}}",
            input_variables=["input"]
        )

        planned_step = PlannedStep(
            tool=invalid_tool,
            prompt=test_prompt,
            relevance_score=0.9
        )

        test_inputs = {"input": "test data"}

        # Execute and expect error handling
        result = mcp_executor.execute_plan_step(planned_step, test_inputs)

        # Verify error response structure
        assert result["status"].startswith("error_")
        assert "error_information" in result
        assert "recovery_suggestions" in result["error_information"]
        assert result["execution_mode"] == "live_mcp_failed"

        # Verify error categorization
        error_info = result["error_information"]
        assert error_info["error_category"] in ["network", "server_error", "connection"]
        assert isinstance(error_info["recovery_suggestions"], list)
        assert len(error_info["recovery_suggestions"]) > 0

    def test_mcp_execution_timeout_handling(self, mcp_executor):
        """Test timeout handling in MCP execution."""
        # Create tool with very short timeout
        timeout_tool = MCPTool(
            tool_id="test_timeout",
            name="Timeout Test Tool",
            description="Tool for testing timeout handling",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="remote_mcp_gradio",
            mcp_endpoint_url="http://httpbin.org/delay/10",  # 10 second delay
            timeout_seconds=1  # 1 second timeout
        )

        test_prompt = MCPPrompt(
            prompt_id="timeout_prompt",
            name="Timeout Test Prompt",
            description="Test timeout prompt",
            target_tool_id="test_timeout",
            template_string="Test: {{input}}",
            input_variables=["input"]
        )

        planned_step = PlannedStep(
            tool=timeout_tool,
            prompt=test_prompt,
            relevance_score=0.9
        )

        test_inputs = {"input": "test data"}

        # Execute and expect timeout error
        result = mcp_executor.execute_plan_step(planned_step, test_inputs)

        # Verify timeout error handling
        assert result["status"] == "error_live_mcp_timeout"
        assert "timeout" in result["message"].lower()
        assert result["error_information"]["error_category"] == "network"

        # Verify timeout-specific recovery suggestions
        suggestions = result["error_information"]["recovery_suggestions"]
        assert any("timeout" in suggestion.lower() for suggestion in suggestions)

    def test_mcp_execution_retry_mechanism(self, mcp_executor):
        """Test retry mechanism for MCP execution failures."""
        with patch("requests.Session.post") as mock_post:
            # Configure mock to fail twice then succeed
            mock_response_fail = Mock()
            mock_response_fail.raise_for_status.side_effect = requests.exceptions.HTTPError(
                response=Mock(status_code=503, text="Service Unavailable")
            )
            mock_response_fail.status_code = 503
            mock_response_fail.text = "Service Unavailable"

            mock_response_success = Mock()
            mock_response_success.raise_for_status.return_value = None
            mock_response_success.json.return_value = {"data": ["Success after retry"]}
            mock_response_success.status_code = 200

            # First two calls fail, third succeeds
            mock_post.side_effect = [
                mock_response_fail,
                mock_response_fail,
                mock_response_success
            ]

            # Create test tool
            retry_tool = MCPTool(
                tool_id="test_retry",
                name="Retry Test Tool",
                description="Tool for testing retry mechanism",
                tags=["test"],
                invocation_command_stub="test",
                execution_type="remote_mcp_gradio",
                mcp_endpoint_url="http://localhost:7860/test",
                timeout_seconds=30
            )

            test_prompt = MCPPrompt(
                prompt_id="retry_prompt",
                name="Retry Test Prompt",
                description="Test retry prompt",
                target_tool_id="test_retry",
                template_string="Test: {{input}}",
                input_variables=["input"]
            )

            planned_step = PlannedStep(
                tool=retry_tool,
                prompt=test_prompt,
                relevance_score=0.9
            )

            test_inputs = {"input": "test data"}

            # Execute with retry
            result = mcp_executor.execute_plan_step(planned_step, test_inputs)

            # Verify successful execution after retries
            assert result["status"] == "success_live_mcp"
            assert result["attempts_made"] == 3
            assert mock_post.call_count == 3

    def test_fallback_to_simulation(self, mcp_executor):
        """Test fallback to simulation when MCP execution fails."""
        # Create tool configured for simulation
        sim_tool = MCPTool(
            tool_id="test_simulation",
            name="Simulation Test Tool",
            description="Tool for testing simulation fallback",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="simulated"  # Configured for simulation
        )

        test_prompt = MCPPrompt(
            prompt_id="sim_prompt",
            name="Simulation Test Prompt",
            description="Test simulation prompt",
            target_tool_id="test_simulation",
            template_string="Test: {{input}}",
            input_variables=["input"]
        )

        planned_step = PlannedStep(
            tool=sim_tool,
            prompt=test_prompt,
            relevance_score=0.9
        )

        test_inputs = {"input": "test data"}

        # Execute simulation
        result = mcp_executor.execute_plan_step(planned_step, test_inputs)

        # Verify simulation execution (handle random error simulation)
        assert result["status"] in ["simulated_success", "simulated_error_timeout", "simulated_error_rate_limit",
                                   "simulated_error_invalid_input", "simulated_error_service_unavailable",
                                   "simulated_error_authentication_failed"]
        assert result["execution_mode"] in ["simulated", "simulated_error"]

        # If successful, check output content
        if result["status"] == "simulated_success":
            assert "tool_specific_output" in result
            assert result["tool_specific_output"] is not None

    def test_input_parameter_ordering(self, mcp_executor):
        """Test that input parameters are correctly ordered for MCP calls."""
        with patch("requests.Session.post") as mock_post:
            mock_response = Mock()
            mock_response.raise_for_status.return_value = None
            mock_response.json.return_value = {"data": ["Parameter order test result"]}
            mock_post.return_value = mock_response

            # Create tool with specific parameter order
            ordered_tool = MCPTool(
                tool_id="test_order",
                name="Parameter Order Test Tool",
                description="Tool for testing parameter ordering",
                tags=["test"],
                invocation_command_stub="test",
                execution_type="remote_mcp_gradio",
                mcp_endpoint_url="http://localhost:7860/test",
                input_parameter_order=["text", "max_length", "min_length"],
                timeout_seconds=30
            )

            test_prompt = MCPPrompt(
                prompt_id="order_prompt",
                name="Order Test Prompt",
                description="Test parameter order prompt",
                target_tool_id="test_order",
                template_string="Summarize: {{text}} with max {{max_length}} and min {{min_length}}",
                input_variables=["text", "max_length", "min_length"]
            )

            planned_step = PlannedStep(
                tool=ordered_tool,
                prompt=test_prompt,
                relevance_score=0.9
            )

            test_inputs = {
                "min_length": "50",
                "text": "Test document content",
                "max_length": "150"
            }

            # Execute
            result = mcp_executor.execute_plan_step(planned_step, test_inputs)

            # Verify successful execution
            assert result["status"] == "success_live_mcp"

            # Verify parameter order in the call
            call_args = mock_post.call_args
            payload = call_args[1]["json"]
            expected_order = ["Test document content", 150, 50]  # Numeric parameters should be integers
            assert payload["data"] == expected_order

    def test_performance_requirements(self, initialized_system):
        """Test that end-to-end execution meets performance requirements."""
        client = initialized_system["client"]

        if initialized_system["planner"] is None:
            pytest.skip("Agent system not initialized")

        # Test response time for planning
        start_time = time.time()

        plan_request = {
            "query": "I need sentiment analysis for customer reviews",
            "top_k": 3
        }

        response = client.post("/api/plan/generate", json=plan_request)
        planning_time = time.time() - start_time

        # Handle system not available
        if response.status_code == 503:
            pytest.skip("Agent system not available - check initialization and API keys")

        assert response.status_code == 200
        assert planning_time < 2.0  # Planning should complete within 2 seconds

        plan_data = response.json()

        # Check if response has error status
        if "detail" in plan_data:
            pytest.skip(f"System error: {plan_data['detail']}")

        assert plan_data["status"] == "success"

        print(f"✅ Planning completed in {planning_time:.2f}s")

    def test_mcp_executor_direct_testing(self, mcp_executor):
        """Test MCP executor directly without requiring external APIs."""
        # Create a comprehensive test that works without external dependencies

        # Test 1: Simulated execution
        sim_tool = MCPTool(
            tool_id="test_sentiment_sim",
            name="Test Sentiment Analyzer",
            description="Test sentiment analysis tool",
            tags=["sentiment", "test"],
            invocation_command_stub="test_sentiment",
            execution_type="simulated"
        )

        sim_prompt = MCPPrompt(
            prompt_id="test_sentiment_prompt",
            name="Test Sentiment Prompt",
            description="Test sentiment analysis prompt",
            target_tool_id="test_sentiment_sim",
            template_string="Analyze sentiment: {{text}}",
            input_variables=["text"]
        )

        planned_step = PlannedStep(
            tool=sim_tool,
            prompt=sim_prompt,
            relevance_score=0.95
        )

        test_inputs = {"text": "This product is amazing and I love it!"}

        # Execute simulation
        result = mcp_executor.execute_plan_step(planned_step, test_inputs)

        # Verify simulation results (handle random error simulation)
        assert result["status"] in ["simulated_success", "simulated_error_timeout", "simulated_error_rate_limit",
                                   "simulated_error_invalid_input", "simulated_error_service_unavailable",
                                   "simulated_error_authentication_failed"]
        assert result["execution_mode"] in ["simulated", "simulated_error"]
        assert result["tool_id_used"] == "test_sentiment_sim"
        assert result["tool_name_used"] == "Test Sentiment Analyzer"

        # If successful, check output content
        if result["status"] == "simulated_success":
            assert "tool_specific_output" in result
            assert result["tool_specific_output"] is not None
            assert "sentiment" in result["tool_specific_output"].lower()

        print(f"✅ Simulated execution test passed (status: {result['status']})")

        # Test 2: Error handling for unreachable MCP endpoint
        mcp_tool_unreachable = MCPTool(
            tool_id="test_mcp_unreachable",
            name="MCP Tool Unreachable",
            description="MCP tool with unreachable endpoint",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="remote_mcp_gradio",
            mcp_endpoint_url="http://localhost:9999/unreachable",  # Unreachable endpoint
            timeout_seconds=5
        )

        # Create a matching prompt for the unreachable tool
        mcp_prompt = MCPPrompt(
            prompt_id="test_mcp_unreachable_prompt",
            name="Test MCP Unreachable Prompt",
            description="Test prompt for unreachable MCP endpoint",
            target_tool_id="test_mcp_unreachable",  # Match the tool_id
            template_string="Test unreachable endpoint: {{text}}",
            input_variables=["text"]
        )

        mcp_planned_step = PlannedStep(
            tool=mcp_tool_unreachable,
            prompt=mcp_prompt,  # Use the matching prompt instead of sim_prompt
            relevance_score=0.8
        )

        # Execute with unreachable endpoint - should return error
        mcp_result = mcp_executor.execute_plan_step(mcp_planned_step, test_inputs)

        # Verify it handles unreachable endpoint with proper error
        assert mcp_result["status"].startswith("error_")
        assert mcp_result["execution_mode"] == "live_mcp_failed"
        assert "error_information" in mcp_result

        print(f"✅ Unreachable endpoint handling test passed (status: {mcp_result['status']})")

        # Test 3: Error handling for invalid inputs
        with pytest.raises(ValueError, match="Plan must be a PlannedStep instance"):
            mcp_executor.execute_plan_step("invalid_plan", test_inputs)

        with pytest.raises(ValueError, match="Inputs must be a dictionary"):
            mcp_executor.execute_plan_step(planned_step, "invalid_inputs")

        print("✅ Input validation test passed")

        # Test 4: Different tool types
        summarizer_tool = MCPTool(
            tool_id="test_summarizer",
            name="Test Text Summarizer",
            description="Test text summarization tool",
            tags=["summarization", "test"],
            invocation_command_stub="test_summarize",
            execution_type="simulated"
        )

        summarizer_prompt = MCPPrompt(
            prompt_id="test_summarizer_prompt",
            name="Test Summarization Prompt",
            description="Test summarization prompt",
            target_tool_id="test_summarizer",
            template_string="Summarize: {{text}} with max length {{max_length}}",
            input_variables=["text", "max_length"]
        )

        summarizer_step = PlannedStep(
            tool=summarizer_tool,
            prompt=summarizer_prompt,
            relevance_score=0.9
        )

        summarizer_inputs = {
            "text": "This is a long document that needs to be summarized for better understanding.",
            "max_length": "50"
        }

        summarizer_result = mcp_executor.execute_plan_step(summarizer_step, summarizer_inputs)

        # Verify summarization results (handle random error simulation)
        assert summarizer_result["status"] in ["simulated_success", "simulated_error_timeout", "simulated_error_rate_limit",
                                              "simulated_error_invalid_input", "simulated_error_service_unavailable",
                                              "simulated_error_authentication_failed"]

        # If successful, check output content
        if summarizer_result["status"] == "simulated_success":
            assert "summary" in summarizer_result["tool_specific_output"].lower()

        print(f"✅ Multi-tool type test passed (status: {summarizer_result['status']})")

    def _is_mcp_server_available(self, endpoint_url: str) -> bool:
        """Check if MCP server is available at the given endpoint."""
        try:
            response = requests.get(endpoint_url.replace("/gradio_api/mcp/sse", "/"), timeout=5)
            return response.status_code == 200
        except:
            return False

    def _create_planned_step_from_api_response(self, step_data: dict[str, Any]) -> PlannedStep:
        """Create PlannedStep object from API response data."""
        tool_info = step_data["tool"]
        prompt_info = step_data["prompt"]

        tool = MCPTool(
            tool_id=tool_info["tool_id"],
            name=tool_info["name"],
            description=tool_info["description"],
            tags=tool_info.get("tags", []),
            invocation_command_stub=tool_info.get("invocation_command_stub", ""),
            execution_type=tool_info.get("execution_type", "simulated"),
            mcp_endpoint_url=tool_info.get("mcp_endpoint_url"),
            input_parameter_order=tool_info.get("input_parameter_order", []),
            timeout_seconds=tool_info.get("timeout_seconds", 30)
        )

        prompt = MCPPrompt(
            prompt_id=prompt_info["prompt_id"],
            name=prompt_info["name"],
            description=prompt_info["description"],
            target_tool_id=prompt_info.get("target_tool_id", tool_info["tool_id"]),
            template_string=prompt_info["template_string"],
            input_variables=prompt_info["input_variables"],
            difficulty_level=prompt_info.get("difficulty_level", "beginner")
        )

        return PlannedStep(
            tool=tool,
            prompt=prompt,
            relevance_score=step_data["relevance_score"]
        )


class TestE2EMcpIntegrationScenarios:
    """Test integration scenarios across the complete system."""

    @pytest.fixture
    def client(self):
        """Provide test client with Gradio UI mounted."""
        return TestClient(app_with_ui)

    def test_health_check_before_execution(self, client):
        """Test that system health check works before attempting execution."""
        response = client.get("/health")
        assert response.status_code == 200

        health_data = response.json()
        assert health_data["status"] == "healthy"
        assert "timestamp" in health_data

    def test_api_documentation_accessibility(self, client):
        """Test that API documentation is accessible."""
        response = client.get("/docs")
        assert response.status_code == 200

    def test_gradio_ui_integration(self, client):
        """Test that Gradio UI is accessible."""
        response = client.get("/ui/")
        assert response.status_code == 200

    def test_error_propagation_through_system(self, client):
        """Test that errors propagate correctly through the system."""
        # Test with malformed request
        response = client.post("/api/plan/generate", json={})
        assert response.status_code == 422  # Validation error

        # Test with invalid data types
        response = client.post(
            "/api/plan/generate",
            json={"query": 123, "top_k": "invalid"}
        )
        assert response.status_code == 422

    def test_system_resilience_under_load(self, client):
        """Test system resilience under concurrent load."""
        import concurrent.futures

        def make_request():
            return client.post(
                "/api/plan/generate",
                json={"query": "test sentiment analysis", "top_k": 1}
            )

        # Submit multiple concurrent requests
        with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
            futures = [executor.submit(make_request) for _ in range(20)]
            responses = [future.result() for future in concurrent.futures.as_completed(futures)]

        # Verify all requests completed
        assert len(responses) == 20
        for response in responses:
            assert response.status_code in [200, 503]  # Success or service unavailable

    def test_data_consistency_across_components(self, client):
        """Test that data remains consistent across system components."""
        # Get a plan
        response = client.post(
            "/api/plan/generate",
            json={"query": "sentiment analysis", "top_k": 1}
        )
        assert response.status_code == 200

        plan_data = response.json()
        if len(plan_data["planned_steps"]) > 0:
            step = plan_data["planned_steps"][0]

            # Verify data consistency
            assert "tool" in step
            assert "prompt" in step
            assert "relevance_score" in step

            # Verify tool-prompt relationship
            tool_id = step["tool"]["tool_id"]
            target_tool_id = step["prompt"].get("target_tool_id")
            if target_tool_id:
                assert tool_id == target_tool_id


class TestE2EMcpEdgeCases:
    """Test edge cases in end-to-end MCP execution."""

    @pytest.fixture
    def mcp_executor(self):
        """Provide McpExecutorAgent for testing."""
        return McpExecutorAgent()

    def test_empty_input_handling(self, mcp_executor):
        """Test handling of empty inputs."""
        tool = MCPTool(
            tool_id="empty_test",
            name="Empty Input Test Tool",
            description="Tool for testing empty inputs",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="simulated"
        )

        prompt = MCPPrompt(
            prompt_id="empty_prompt",
            name="Empty Test Prompt",
            description="Test empty prompt",
            target_tool_id="empty_test",
            template_string="Test: {{input}}",
            input_variables=["input"]
        )

        planned_step = PlannedStep(tool=tool, prompt=prompt, relevance_score=0.9)

        # Test with empty inputs
        empty_inputs = {}
        result = mcp_executor.execute_plan_step(planned_step, empty_inputs)

        # Should handle gracefully (handle random error simulation)
        assert result["status"] in ["simulated_success", "simulated_error_missing_input",
                                   "simulated_error_timeout", "simulated_error_rate_limit",
                                   "simulated_error_invalid_input", "simulated_error_service_unavailable",
                                   "simulated_error_authentication_failed"]

    def test_large_input_handling(self, mcp_executor):
        """Test handling of very large inputs."""
        tool = MCPTool(
            tool_id="large_test",
            name="Large Input Test Tool",
            description="Tool for testing large inputs",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="simulated"
        )

        prompt = MCPPrompt(
            prompt_id="large_prompt",
            name="Large Test Prompt",
            description="Test large prompt",
            target_tool_id="large_test",
            template_string="Test: {{input}}",
            input_variables=["input"]
        )

        planned_step = PlannedStep(tool=tool, prompt=prompt, relevance_score=0.9)

        # Test with very large input
        large_input = "x" * 100000  # 100KB of text
        large_inputs = {"input": large_input}

        result = mcp_executor.execute_plan_step(planned_step, large_inputs)

        # Should handle gracefully (handle random error simulation)
        assert result["status"] in ["simulated_success", "simulated_error_input_too_large",
                                   "simulated_error_timeout", "simulated_error_rate_limit",
                                   "simulated_error_invalid_input", "simulated_error_service_unavailable",
                                   "simulated_error_authentication_failed"]

    def test_special_characters_in_input(self, mcp_executor):
        """Test handling of special characters and Unicode in inputs."""
        tool = MCPTool(
            tool_id="unicode_test",
            name="Unicode Test Tool",
            description="Tool for testing Unicode inputs",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="simulated"
        )

        prompt = MCPPrompt(
            prompt_id="unicode_prompt",
            name="Unicode Test Prompt",
            description="Test Unicode prompt",
            target_tool_id="unicode_test",
            template_string="Test: {{input}}",
            input_variables=["input"]
        )

        planned_step = PlannedStep(tool=tool, prompt=prompt, relevance_score=0.9)

        # Test with special characters and Unicode
        special_inputs = {
            "input": "Test with émojis 🎯, special chars @#$%, and Unicode: 你好世界"
        }

        result = mcp_executor.execute_plan_step(planned_step, special_inputs)

        # Should handle gracefully (handle random error simulation - 10% chance)
        assert result["status"] in ["simulated_success", "simulated_error_timeout", "simulated_error_rate_limit",
                                   "simulated_error_invalid_input", "simulated_error_service_unavailable",
                                   "simulated_error_authentication_failed"]
        assert special_inputs["input"] in str(result["inputs_received"])

    def test_malformed_tool_configuration(self, mcp_executor):
        """Test handling of malformed tool configurations."""
        # Test 1: Empty name should be rejected at construction
        with pytest.raises(ValueError, match="name cannot be empty"):
            MCPTool(
                tool_id="malformed_test",
                name="",  # Empty name
                description="Tool with malformed config",
                tags=["test"],
                invocation_command_stub="test",
                execution_type="simulated"
            )

        # Test 2: Empty tool_id should be rejected at construction
        with pytest.raises(ValueError, match="tool_id cannot be empty"):
            MCPTool(
                tool_id="",  # Empty tool_id
                name="Valid Name",
                description="Tool with malformed config",
                tags=["test"],
                invocation_command_stub="test",
                execution_type="simulated"
            )

        # Test 3: Invalid execution type should be rejected at construction
        with pytest.raises(ValueError, match="execution_type must be"):
            MCPTool(
                tool_id="malformed_test",
                name="Valid Name",
                description="Tool with malformed config",
                tags=["test"],
                invocation_command_stub="test",
                execution_type="invalid_type"  # Invalid execution type
            )

        # Test 4: Missing endpoint URL for remote MCP should be rejected
        with pytest.raises(ValueError, match="mcp_endpoint_url is required"):
            MCPTool(
                tool_id="malformed_test",
                name="Valid Name",
                description="Tool with malformed config",
                tags=["test"],
                invocation_command_stub="test",
                execution_type="remote_mcp_gradio",  # Requires endpoint URL
                mcp_endpoint_url=None  # Missing required URL
            )

        # Test 5: Test a configuration that passes validation but might cause execution issues
        # Create a tool with a problematic endpoint that will fail during execution
        problematic_tool = MCPTool(
            tool_id="problematic_test",
            name="Problematic Test Tool",
            description="Tool that will fail during execution",
            tags=["test"],
            invocation_command_stub="test",
            execution_type="remote_mcp_gradio",
            mcp_endpoint_url="http://nonexistent.invalid/endpoint",  # Invalid URL
            timeout_seconds=1  # Very short timeout
        )

        prompt = MCPPrompt(
            prompt_id="problematic_prompt",
            name="Problematic Test Prompt",
            description="Test prompt for problematic tool",
            target_tool_id="problematic_test",
            template_string="Test: {{input}}",
            input_variables=["input"]
        )

        planned_step = PlannedStep(tool=problematic_tool, prompt=prompt, relevance_score=0.9)
        test_inputs = {"input": "test"}

        # This should execute but fail gracefully due to invalid endpoint
        result = mcp_executor.execute_plan_step(planned_step, test_inputs)

        # Should handle execution errors gracefully
        assert result["status"].startswith("error_")
        assert "error_information" in result
        assert result["execution_mode"] == "live_mcp_failed"