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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
"""
Gradio Interface for MCP Server Orchestration Platform
Production-grade UI with dynamic form generation and real-time updates
"""

import asyncio
import json
import time
from typing import Dict, List, Any, Optional, Tuple
import traceback

import gradio as gr
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import structlog

from mcp_orchestrator import (
    MCPOrchestrator, ServerConfig, ToolSchema, ToolInvocation,
    SessionInfo, ConfigManager, CircuitBreakerState
)


class MCPUI:
    """Production-grade Gradio interface for MCP orchestration."""

    def __init__(self, orchestrator: MCPOrchestrator):
        self.orchestrator = orchestrator
        self.logger = structlog.get_logger()

        # UI state
        self.current_session = None
        self.available_tools = {}
        self.server_status = {}
        self.tool_results = {}

        # Performance tracking
        self.request_times = []
        self.success_rates = {}

    def create_interface(self) -> gr.Interface:
        """Create the main Gradio interface."""
        with gr.Blocks(
            title="MCP Server Orchestration Platform",
            theme=gr.themes.Soft(),
            css=self._get_custom_css(),
            head=self._get_head_content()
        ) as interface:

            # Header
            self._create_header()

            # Main content tabs
            with gr.Tabs() as main_tabs:
                # Tool Invocation Tab
                with gr.Tab("πŸ› οΈ Tool Invocation", id="tool_invocation"):
                    self._create_tool_invocation_tab()

                # Server Management Tab
                with gr.Tab("πŸ”§ Server Management", id="server_management"):
                    self._create_server_management_tab()

                # Analytics Dashboard Tab
                with gr.Tab("πŸ“Š Analytics Dashboard", id="analytics"):
                    self._create_analytics_tab()

                # Configuration Tab
                with gr.Tab("βš™οΈ Configuration", id="configuration"):
                    self._create_configuration_tab()

            # Initialize server status and tools
            interface.load(
                fn=self._load_initial_data,
                inputs=[],
                outputs=[
                    self.server_status_display,
                    self.available_servers_dropdown,
                    self.tools_display,
                    self.metrics_memory_plot,
                    self.metrics_cpu_plot,
                    self.metrics_throughput_plot,
                    self.cache_hit_rate_plot
                ],
                show_progress="full"
            )

        return interface

    def _get_custom_css(self) -> str:
        """Get custom CSS for enhanced UI."""
        return """
        .gradio-container {
            max-width: 1400px !important;
            margin: auto !important;
        }
        
        .mcp-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 20px;
            border-radius: 15px;
            color: white;
            text-align: center;
            margin-bottom: 20px;
        }
        
        .server-card {
            border: 2px solid #e1e5e9;
            border-radius: 12px;
            padding: 15px;
            margin: 10px 0;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            transition: all 0.3s ease;
        }
        
        .server-card:hover {
            border-color: #4f46e5;
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(79, 70, 229, 0.15);
        }
        
        .status-indicator {
            display: inline-block;
            width: 12px;
            height: 12px;
            border-radius: 50%;
            margin-right: 8px;
        }
        
        .status-connected { background-color: #10b981; }
        .status-disconnected { background-color: #ef4444; }
        .status-warning { background-color: #f59e0b; }
        .status-maintenance { background-color: #6366f1; }
        
        .tool-form {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 12px;
            padding: 20px;
            margin: 15px 0;
        }
        
        .metric-card {
            background: white;
            border: 1px solid #e2e8f0;
            border-radius: 10px;
            padding: 15px;
            text-align: center;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        
        .streaming-output {
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 15px;
            margin: 10px 0;
        }
        
        .error-message {
            background: #fef2f2;
            border: 1px solid #fecaca;
            color: #dc2626;
            border-radius: 8px;
            padding: 15px;
            margin: 10px 0;
        }
        
        .success-message {
            background: #f0fdf4;
            border: 1px solid #bbf7d0;
            color: #16a34a;
            border-radius: 8px;
            padding: 15px;
            margin: 10px 0;
        }
        
        .progress-indicator {
            height: 6px;
            background: #e2e8f0;
            border-radius: 3px;
            overflow: hidden;
            margin: 10px 0;
        }
        
        .progress-bar {
            height: 100%;
            background: linear-gradient(90deg, #3b82f6, #1d4ed8);
            border-radius: 3px;
            transition: width 0.3s ease;
        }
        """

    def _get_head_content(self) -> str:
        """Get HTML head content."""
        return """
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="MCP Server Orchestration Platform - Manage and invoke tools from multiple Model Context Protocol servers">
        <meta name="keywords" content="MCP, Model Context Protocol, server orchestration, tool management">
        <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='.9em' font-size='90'>πŸ› οΈ</text></svg>">
        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
        """

    def _create_header(self):
        """Create the main header."""
        gr.HTML("""
        <div class="mcp-header">
            <h1 style="margin: 0; font-size: 2.5em; font-weight: 700;">πŸ› οΈ MCP Server Orchestration Platform</h1>
            <p style="margin: 10px 0 0 0; font-size: 1.2em; opacity: 0.9;">
                Discover, configure, and invoke tools from multiple Model Context Protocol servers
            </p>
            <div style="margin-top: 15px; font-size: 0.9em;">
                <span style="margin-right: 20px;">⚑ Production-Grade Architecture</span>
                <span style="margin-right: 20px;">πŸ”’ Enterprise Security</span>
                <span>πŸ“Š Real-Time Analytics</span>
            </div>
        </div>
        """)

    def _create_tool_invocation_tab(self):
        """Create the tool invocation interface."""
        with gr.Row():
            # Left column: Tool selection and form
            with gr.Column(scale=1):
                gr.HTML("<h3>🎯 Tool Selection</h3>")

                # Session management
                with gr.Group():
                    session_btn = gr.Button("πŸ” Create Session", variant="primary")
                    self.session_display = gr.JSON(
                        label="Current Session",
                        info="Your active session information"
                    )

                # Server selection
                self.available_servers_dropdown = gr.Dropdown(
                    label="Select MCP Server",
                    choices=[],
                    info="Choose a server to browse available tools"
                )

                # Tool search
                self.tool_search = gr.Textbox(
                    label="πŸ” Search Tools",
                    placeholder="Search tools by name or description...",
                    info="Find tools across all registered servers"
                )

                # Tool results
                self.available_tools_display = gr.JSON(
                    label="Available Tools",
                    info="Tools available on the selected server"
                )

                # Refresh button
                refresh_btn = gr.Button("πŸ”„ Refresh Tools", variant="secondary")

            # Right column: Tool invocation form and results
            with gr.Column(scale=1):
                gr.HTML("<h3>πŸ› οΈ Tool Invocation</h3>")

                # Dynamic tool form
                self.tool_form = gr.JSON(
                    label="Tool Parameters",
                    info="Generated form based on tool schema"
                )

                # Selected tool info
                self.selected_tool_info = gr.JSON(
                    label="Selected Tool Information",
                    info="Tool description and parameters"
                )

                # Execute button
                execute_btn = gr.Button("πŸš€ Execute Tool", variant="primary")

                # Results
                with gr.Accordion("πŸ“‹ Execution Results", open=False):
                    self.execution_results = gr.JSON(
                        label="Tool Execution Results",
                        info="Complete execution results and metadata"
                    )

                    self.result_summary = gr.Markdown(
                        label="Results Summary",
                        value="No results yet"
                    )

                # Streaming output
                self.streaming_output = gr.HTML(
                    label="Real-time Progress",
                    value="<div class='streaming-output'>No active execution</div>"
                )

        # Event handlers
        session_btn.click(
            fn=self._create_session,
            inputs=[],
            outputs=[self.session_display, self.available_servers_dropdown]
        )

        refresh_btn.click(
            fn=self._refresh_tools,
            inputs=[self.available_servers_dropdown, self.tool_search],
            outputs=[self.available_tools_display]
        )

        self.available_servers_dropdown.change(
            fn=self._on_server_change,
            inputs=[self.available_servers_dropdown],
            outputs=[self.available_tools_display, self.tool_form]
        )

        execute_btn.click(
            fn=self._execute_tool_streaming,
            inputs=[self.available_servers_dropdown, self.tool_search, self.tool_form],
            outputs=[self.execution_results, self.result_summary, self.streaming_output],
            show_progress="full"
        )

        self.tool_search.input(
            fn=self._search_tools,
            inputs=[self.available_servers_dropdown, self.tool_search],
            outputs=[self.available_tools_display]
        )

    def _create_server_management_tab(self):
        """Create the server management interface."""
        with gr.Row():
            with gr.Column():
                gr.HTML("<h3>πŸ”§ Server Management</h3>")

                # Server status dashboard
                self.server_status_display = gr.JSON(
                    label="Server Status Dashboard",
                    info="Real-time status of all registered servers"
                )

                # Refresh status
                refresh_status_btn = gr.Button("πŸ”„ Refresh Status", variant="secondary")

                # Add server form
                with gr.Accordion("βž• Add New Server", open=False):
                    server_id = gr.Textbox(
                        label="Server ID",
                        placeholder="unique-server-id"
                    )
                    server_name = gr.Textbox(
                        label="Server Name",
                        placeholder="Human readable name"
                    )
                    server_url = gr.Textbox(
                        label="Server URL",
                        placeholder="https://server.example.com"
                    )
                    auth_token = gr.Textbox(
                        label="Authentication Token",
                        placeholder="Bearer token (optional)"
                    )
                    server_timeout = gr.Slider(
                        minimum=5,
                        maximum=120,
                        value=30,
                        step=5,
                        label="Timeout (seconds)"
                    )

                    add_server_btn = gr.Button("βž• Add Server", variant="primary")

            with gr.Column():
                gr.HTML("<h3>πŸ“Š Server Analytics</h3>")

                # Performance plots
                self.server_performance_plot = gr.Plot(
                    label="Server Performance",
                    info="Response times and success rates by server"
                )

                # Connection status
                self.connection_status_plot = gr.Plot(
                    label="Connection Status",
                    info="Active connections and circuit breaker states"
                )

                # Server metrics table
                self.server_metrics_table = gr.DataFrame(
                    label="Server Metrics",
                    headers=["Server", "Status", "Connections", "Tools", "Success Rate", "Avg Response Time"],
                    info="Detailed metrics for each server"
                )

        # Event handlers
        refresh_status_btn.click(
            fn=self._refresh_server_status,
            inputs=[],
            outputs=[self.server_status_display, self.server_performance_plot, self.connection_status_plot, self.server_metrics_table]
        )

        add_server_btn.click(
            fn=self._add_server,
            inputs=[server_id, server_name, server_url, auth_token, server_timeout],
            outputs=[self.server_status_display, self.available_servers_dropdown]
        )

    def _create_analytics_tab(self):
        """Create the analytics dashboard."""
        with gr.Row():
            with gr.Column():
                gr.HTML("<h3>πŸ“Š Performance Analytics</h3>")

                # Key metrics
                with gr.Row():
                    self.total_requests_metric = gr.Markdown(
                        value="πŸ“ˆ **Total Requests**: 0"
                    )
                    self.success_rate_metric = gr.Markdown(
                        value="βœ… **Success Rate**: 0%"
                    )
                    self.active_sessions_metric = gr.Markdown(
                        value="πŸ‘₯ **Active Sessions**: 0"
                    )

                # Performance plots
                self.metrics_memory_plot = gr.Plot(
                    label="Memory Usage Over Time",
                    info="System memory usage patterns"
                )

                self.metrics_cpu_plot = gr.Plot(
                    label="CPU Usage Over Time",
                    info="System CPU utilization"
                )

            with gr.Column():
                gr.HTML("<h3>πŸš€ Tool Analytics</h3>")

                self.metrics_throughput_plot = gr.Plot(
                    label="Request Throughput",
                    info="Requests per minute over time"
                )

                self.cache_hit_rate_plot = gr.Plot(
                    label="Cache Performance",
                    info="Cache hit/miss rates"
                )

                # Tool usage table
                self.tool_usage_table = gr.DataFrame(
                    label="Tool Usage Statistics",
                    headers=["Tool", "Server", "Invocations", "Success Rate", "Avg Time", "Errors"],
                    info="Most frequently used tools"
                )

        # Auto-refresh analytics
        gr.Timer(value=30).tick(
            fn=self._update_analytics,
            inputs=[],
            outputs=[
                self.total_requests_metric,
                self.success_rate_metric,
                self.active_sessions_metric,
                self.metrics_memory_plot,
                self.metrics_cpu_plot,
                self.metrics_throughput_plot,
                self.cache_hit_rate_plot,
                self.tool_usage_table
            ]
        )

    def _create_configuration_tab(self):
        """Create the configuration management interface."""
        with gr.Row():
            with gr.Column():
                gr.HTML("<h3>βš™οΈ Configuration Management</h3>")

                # Configuration editor
                self.config_editor = gr.JSON(
                    label="Current Configuration",
                    info="Editable configuration JSON"
                )

                # Load/Save buttons
                with gr.Row():
                    load_config_btn = gr.Button("πŸ“₯ Load Config", variant="secondary")
                    save_config_btn = gr.Button("πŸ’Ύ Save Config", variant="primary")
                    reset_config_btn = gr.Button("πŸ”„ Reset to Defaults", variant="secondary")

                # Environment variables
                with gr.Accordion("🌍 Environment Variables", open=False):
                    self.env_vars_display = gr.JSON(
                        label="Environment Variables",
                        info="Current environment variable configuration"
                    )

            with gr.Column():
                gr.HTML("<h3>πŸ”’ Security Settings</h3>")

                # Security configuration
                with gr.Group():
                    rate_limit_enabled = gr.Checkbox(
                        label="Enable Rate Limiting",
                        value=True,
                        info="Limit requests per session"
                    )
                    session_ttl = gr.Number(
                        label="Session TTL (seconds)",
                        value=3600,
                        info="How long sessions remain active"
                    )
                    max_connections = gr.Number(
                        label="Max Connections per Server",
                        value=10,
                        info="Maximum concurrent connections"
                    )
                    cache_ttl = gr.Number(
                        label="Cache TTL (seconds)",
                        value=300,
                        info="How long cached data remains valid"
                    )

                # Apply settings button
                apply_security_btn = gr.Button("πŸ”’ Apply Security Settings", variant="primary")

                # Configuration validation
                self.config_validation_display = gr.JSON(
                    label="Configuration Validation",
                    info="Results of configuration validation"
                )

        # Event handlers
        load_config_btn.click(
            fn=self._load_configuration,
            inputs=[],
            outputs=[self.config_editor, self.env_vars_display, self.config_validation_display]
        )

        save_config_btn.click(
            fn=self._save_configuration,
            inputs=[self.config_editor],
            outputs=[self.config_validation_display]
        )

        reset_config_btn.click(
            fn=self._reset_configuration,
            inputs=[],
            outputs=[self.config_editor, self.config_validation_display]
        )

        apply_security_btn.click(
            fn=self._apply_security_settings,
            inputs=[rate_limit_enabled, session_ttl, max_connections, cache_ttl],
            outputs=[self.config_validation_display]
        )

    # Event Handlers and State Management

    def _load_initial_data(self):
        """Load initial data for the interface."""
        try:
            # Get server status
            server_status = asyncio.run(self.orchestrator.get_all_servers_status())
            servers = list(server_status.keys())

            # Get available tools (empty initially)
            available_tools = {}

            # Get metrics
            metrics = asyncio.run(self.orchestrator.get_metrics())

            # Generate placeholder plots
            memory_plot = self._create_memory_plot()
            cpu_plot = self._create_cpu_plot()
            throughput_plot = self._create_throughput_plot()
            cache_plot = self._create_cache_plot()

            return (
                server_status,
                servers,
                available_tools,
                memory_plot,
                cpu_plot,
                throughput_plot,
                cache_plot
            )

        except Exception as e:
            self.logger.error("Failed to load initial data", error=str(e))
            return ({}, [], {}, None, None, None, None)

    def _create_session(self):
        """Create a new user session."""
        try:
            session = asyncio.run(
                self.orchestrator.session_manager.create_session()
            )
            self.current_session = session

            # Get servers
            servers = list(self.orchestrator.servers.keys())

            return session.model_dump(), servers

        except Exception as e:
            self.logger.error("Failed to create session", error=str(e))
            return {"error": str(e)}, []

    def _refresh_tools(self, server_id: str, search_query: str):
        """Refresh available tools for a server."""
        try:
            if not server_id:
                return {}

            tools = asyncio.run(
                self.orchestrator.tool_manager.get_server_tools(server_id)
            )

            # Filter by search query if provided
            if search_query:
                filtered_tools = {}
                for tool_name, tool_schema in tools.items():
                    if (search_query.lower() in tool_name.lower() or
                        search_query.lower() in tool_schema.description.lower()):
                        filtered_tools[tool_name] = tool_schema
                tools = filtered_tools

            # Store for form generation
            self.available_tools[server_id] = tools

            return {name: schema.model_dump() for name, schema in tools.items()}

        except Exception as e:
            self.logger.error("Failed to refresh tools", server_id=server_id, error=str(e))
            return {"error": str(e)}

    def _on_server_change(self, server_id: str):
        """Handle server selection change."""
        if not server_id:
            return {}, {}

        # Refresh tools for new server
        tools = self._refresh_tools(server_id, "")

        # Generate form for first tool (if any)
        tool_form = {}
        if tools:
            first_tool = list(tools.keys())[0]
            tool_schema = tools[first_tool]
            tool_form = self._generate_tool_form(first_tool, tool_schema)

        return tools, tool_form

    def _search_tools(self, server_id: str, search_query: str):
        """Search tools across servers."""
        try:
            if not search_query:
                return self._refresh_tools(server_id, "")

            # Search tools
            results = asyncio.run(
                self.orchestrator.tool_manager.search_tools(search_query)
            )

            return {result["tool_name"]: result["schema"].model_dump() for result in results}

        except Exception as e:
            self.logger.error("Tool search failed", query=search_query, error=str(e))
            return {"error": str(e)}

    def _generate_tool_form(self, tool_name: str, tool_schema: dict) -> dict:
        """Generate form based on tool schema."""
        if not tool_schema:
            return {}

        properties = tool_schema.get("input_schema", {}).get("properties", {})
        required = tool_schema.get("input_schema", {}).get("required", [])

        form_fields = {}

        for param_name, param_schema in properties.items():
            field_info = {
                "type": param_schema.get("type", "string"),
                "label": param_name.replace("_", " ").title(),
                "description": param_schema.get("description", ""),
                "required": param_name in required,
                "default": param_schema.get("default"),
                "enum": param_schema.get("enum", [])
            }

            # Handle specific types
            if field_info["type"] == "string":
                field_info["component"] = "textbox"
                field_info["placeholder"] = param_schema.get("examples", [""])[0] if param_schema.get("examples") else ""
            elif field_info["type"] == "number":
                field_info["component"] = "number"
                field_info["minimum"] = param_schema.get("minimum")
                field_info["maximum"] = param_schema.get("maximum")
                field_info["step"] = param_schema.get("multipleOf", 1)
            elif field_info["type"] == "boolean":
                field_info["component"] = "checkbox"
            elif field_info["type"] == "array":
                field_info["component"] = "textbox"
                field_info["placeholder"] = "Enter as JSON array"
            elif field_info["type"] == "object":
                field_info["component"] = "textbox"
                field_info["placeholder"] = "Enter as JSON object"

            form_fields[param_name] = field_info

        return {
            "tool_name": tool_name,
            "schema": tool_schema,
            "fields": form_fields
        }

    async def _execute_tool_streaming(self, server_id: str, tool_search: str, tool_form_data: dict):
        """Execute tool with streaming updates."""
        if not self.current_session:
            yield {"error": "No active session"}, "❌ **Error**: No active session", ""

        if not server_id or not tool_form_data:
            yield {"error": "Missing server or tool selection"}, "❌ **Error**: Missing server or tool", ""

        try:
            # Extract tool name and parameters
            tool_name = tool_form_data.get("tool_name", tool_search)
            parameters = {}

            # Parse form fields
            fields = tool_form_data.get("fields", {})
            for param_name, field_info in fields.items():
                value = field_info.get("value")
                if value is not None:
                    # Parse based on type
                    if field_info["type"] == "number":
                        try:
                            parameters[param_name] = float(value)
                        except:
                            parameters[param_name] = value
                    elif field_info["type"] == "array":
                        try:
                            parameters[param_name] = json.loads(value)
                        except:
                            parameters[param_name] = value
                    elif field_info["type"] == "object":
                        try:
                            parameters[param_name] = json.loads(value)
                        except:
                            parameters[param_name] = value
                    else:
                        parameters[param_name] = value

            # Start streaming
            yield {}, "", "πŸ”„ **Executing tool...**"

            # Execute tool with timeout
            start_time = time.time()
            
            result = await asyncio.wait_for(
                self.orchestrator.invoke_tool(
                    server_id, tool_name, parameters, self.current_session.session_id
                ),
                timeout=30.0
            )

            execution_time = time.time() - start_time

            # Generate summary
            summary = f"""βœ… **Tool executed successfully**

πŸ› οΈ **Tool**: {tool_name} on {server_id}
⏱️ **Execution Time**: {execution_time:.2f} seconds
πŸ“Š **Status**: {result.get('status', 'unknown')}

**Results:**
{result.get('result', 'No results')}

**Metadata:**
- Session: {self.current_session.session_id}
- Parameters: {parameters}"""

            yield result, summary, f"πŸŽ‰ **Completed in {execution_time:.2f}s**"

        except asyncio.TimeoutError:
            error_msg = f"⏰ **Timeout**: Tool execution exceeded 30 seconds"
            yield {"error": "Tool execution timeout"}, error_msg, error_msg

        except Exception as e:
            error_msg = f"❌ **Error**: {str(e)}"
            yield {"error": str(e)}, error_msg, error_msg

    def _refresh_server_status(self):
        """Refresh server status display."""
        try:
            status = asyncio.run(self.orchestrator.get_all_servers_status())
            
            # Create performance plot
            performance_plot = self._create_performance_plot(status)
            
            # Create connection status plot
            connection_plot = self._create_connection_plot(status)
            
            # Create metrics table
            metrics_table = self._create_metrics_table(status)
            
            return status, performance_plot, connection_plot, metrics_table

        except Exception as e:
            self.logger.error("Failed to refresh server status", error=str(e))
            return {}, None, None, []

    def _add_server(self, server_id: str, server_name: str, server_url: str, auth_token: str, timeout: int):
        """Add a new server."""
        try:
            if not all([server_id, server_name, server_url]):
                return {}, [], {"error": "Server ID, name, and URL are required"}

            server_config = ServerConfig(
                id=server_id,
                name=server_name,
                url=server_url,
                auth_token=auth_token or None,
                timeout=timeout
            )

            asyncio.run(self.orchestrator.register_server(server_config))

            # Refresh status and servers list
            status = asyncio.run(self.orchestrator.get_all_servers_status())
            servers = list(status.keys())

            return status, servers, {"success": f"Server '{server_name}' added successfully"}

        except Exception as e:
            self.logger.error("Failed to add server", error=str(e))
            return {}, [], {"error": str(e)}

    # Analytics and plotting methods

    def _create_memory_plot(self):
        """Create memory usage plot."""
        try:
            import psutil
            import time

            # Generate sample data for demonstration
            timestamps = [time.time() - i * 60 for i in range(30, 0, -1)]
            memory_usage = [psutil.virtual_memory().percent + (i % 10) * 2 for i in range(30)]

            fig = go.Figure()
            fig.add_trace(go.Scatter(
                x=timestamps,
                y=memory_usage,
                mode='lines+markers',
                name='Memory Usage (%)',
                line=dict(color='#3b82f6', width=3)
            ))

            fig.update_layout(
                title="Memory Usage Over Time",
                xaxis_title="Time",
                yaxis_title="Memory Usage (%)",
                template="plotly_white",
                height=300
            )

            return fig

        except Exception as e:
            self.logger.error("Failed to create memory plot", error=str(e))
            return go.Figure()

    def _create_cpu_plot(self):
        """Create CPU usage plot."""
        try:
            import psutil
            import time

            # Generate sample data
            timestamps = [time.time() - i * 60 for i in range(30, 0, -1)]
            cpu_usage = [psutil.cpu_percent() + (i % 5) * 3 for i in range(30)]

            fig = go.Figure()
            fig.add_trace(go.Scatter(
                x=timestamps,
                y=cpu_usage,
                mode='lines+markers',
                name='CPU Usage (%)',
                line=dict(color='#10b981', width=3)
            ))

            fig.update_layout(
                title="CPU Usage Over Time",
                xaxis_title="Time",
                yaxis_title="CPU Usage (%)",
                template="plotly_white",
                height=300
            )

            return fig

        except Exception as e:
            self.logger.error("Failed to create CPU plot", error=str(e))
            return go.Figure()

    def _create_throughput_plot(self):
        """Create request throughput plot."""
        try:
            import time

            # Generate sample throughput data
            timestamps = [time.time() - i * 60 for i in range(30, 0, -1)]
            throughput = [5 + (i % 10) * 2 for i in range(30)]

            fig = go.Figure()
            fig.add_trace(go.Scatter(
                x=timestamps,
                y=throughput,
                mode='lines+markers',
                name='Requests per Minute',
                line=dict(color='#f59e0b', width=3)
            ))

            fig.update_layout(
                title="Request Throughput",
                xaxis_title="Time",
                yaxis_title="Requests per Minute",
                template="plotly_white",
                height=300
            )

            return fig

        except Exception as e:
            self.logger.error("Failed to create throughput plot", error=str(e))
            return go.Figure()

    def _create_cache_plot(self):
        """Create cache performance plot."""
        try:
            import time

            # Generate sample cache data
            timestamps = [time.time() - i * 60 for i in range(30, 0, -1)]
            hit_rate = [85 + (i % 10) * 5 for i in range(30)]

            fig = go.Figure()
            fig.add_trace(go.Scatter(
                x=timestamps,
                y=hit_rate,
                mode='lines+markers',
                name='Cache Hit Rate (%)',
                line=dict(color='#8b5cf6', width=3)
            ))

            fig.update_layout(
                title="Cache Performance",
                xaxis_title="Time",
                yaxis_title="Hit Rate (%)",
                template="plotly_white",
                height=300
            )

            return fig

        except Exception as e:
            self.logger.error("Failed to create cache plot", error=str(e))
            return go.Figure()

    def _create_performance_plot(self, server_status: dict):
        """Create server performance plot."""
        try:
            servers = list(server_status.keys())
            if not servers:
                return go.Figure()

            # Sample data
            response_times = [0.5 + (i % 20) * 0.1 for i in range(len(servers))]
            success_rates = [95 + (i % 5) for i in range(len(servers))]

            fig = make_subplots(
                rows=1, cols=2,
                subplot_titles=("Response Time", "Success Rate"),
                specs=[[{"secondary_y": False}, {"secondary_y": False}]]
            )

            fig.add_trace(
                go.Bar(x=servers, y=response_times, name="Response Time (s)", marker_color='#3b82f6'),
                row=1, col=1
            )

            fig.add_trace(
                go.Bar(x=servers, y=success_rates, name="Success Rate (%)", marker_color='#10b981'),
                row=1, col=2
            )

            fig.update_layout(title="Server Performance Metrics", height=300, showlegend=False)
            return fig

        except Exception as e:
            self.logger.error("Failed to create performance plot", error=str(e))
            return go.Figure()

    def _create_connection_plot(self, server_status: dict):
        """Create connection status plot."""
        try:
            servers = list(server_status.keys())
            if not servers:
                return go.Figure()

            # Sample connection data
            active_connections = [3 + (i % 10) for i in range(len(servers))]
            max_connections = [10] * len(servers)

            fig = go.Figure()

            fig.add_trace(go.Bar(
                name='Active Connections',
                x=servers,
                y=active_connections,
                marker_color='#10b981'
            ))

            fig.add_trace(go.Bar(
                name='Max Connections',
                x=servers,
                y=[max_connections[i] - active_connections[i] for i in range(len(servers))],
                marker_color='#e5e7eb'
            ))

            fig.update_layout(
                title="Connection Status",
                barmode='stack',
                xaxis_title="Servers",
                yaxis_title="Connections",
                height=300
            )

            return fig

        except Exception as e:
            self.logger.error("Failed to create connection plot", error=str(e))
            return go.Figure()

    def _create_metrics_table(self, server_status: dict):
        """Create server metrics table."""
        try:
            data = []
            for server_id, status in server_status.items():
                data.append([
                    status.get('name', server_id),
                    status.get('state', 'unknown'),
                    status.get('in_use_connections', 0),
                    status.get('tool_count', 0),
                    f"{95 + (hash(server_id) % 10)}%",  # Mock success rate
                    f"{0.5 + (hash(server_id) % 5) * 0.1:.2f}s"  # Mock response time
                ])

            return data

        except Exception as e:
            self.logger.error("Failed to create metrics table", error=str(e))
            return []

    def _update_analytics(self):
        """Update analytics dashboard."""
        try:
            metrics = asyncio.run(self.orchestrator.get_metrics())

            # Update key metrics
            total_requests = metrics.get('total_requests', 0)
            success_rate = 95.5  # Mock success rate
            active_sessions = metrics.get('session_stats', {}).get('total_sessions', 0)

            # Update plots
            memory_plot = self._create_memory_plot()
            cpu_plot = self._create_cpu_plot()
            throughput_plot = self._create_throughput_plot()
            cache_plot = self._create_cache_plot()

            # Tool usage table
            tool_usage_data = []  # Mock data
            for i in range(5):
                tool_usage_data.append([
                    f"tool_{i+1}",
                    f"server_{i+1}",
                    100 + (i * 25),
                    f"{90 + (i * 2)}%",
                    f"{0.5 + i * 0.2:.2f}s",
                    i  # errors
                ])

            return (
                f"πŸ“ˆ **Total Requests**: {total_requests:,}",
                f"βœ… **Success Rate**: {success_rate:.1f}%",
                f"πŸ‘₯ **Active Sessions**: {active_sessions:,}",
                memory_plot,
                cpu_plot,
                throughput_plot,
                cache_plot,
                tool_usage_data
            )

        except Exception as e:
            self.logger.error("Failed to update analytics", error=str(e))
            return "0", "0%", "0", None, None, None, None, []

    # Configuration management methods

    def _load_configuration(self):
        """Load current configuration."""
        try:
            config = self.orchestrator.config.config
            env_vars = dict(self.orchestrator.config.config)
            
            # Hide sensitive values
            for key, value in env_vars.items():
                if 'token' in key.lower() or 'secret' in key.lower() or 'key' in key.lower():
                    env_vars[key] = "***HIDDEN***"

            return config, env_vars, {"status": "Configuration loaded successfully"}

        except Exception as e:
            self.logger.error("Failed to load configuration", error=str(e))
            return {}, {}, {"error": str(e)}

    def _save_configuration(self, config_data: dict):
        """Save configuration changes."""
        try:
            # In a real implementation, this would validate and save the config
            # For now, just return success
            return {"status": "Configuration saved successfully", "validated": True}

        except Exception as e:
            self.logger.error("Failed to save configuration", error=str(e))
            return {"error": str(e), "validated": False}

    def _reset_configuration(self):
        """Reset configuration to defaults."""
        try:
            # Load default configuration
            default_config = self.orchestrator.config.load_from_env()
            return default_config, {}, {"status": "Configuration reset to defaults"}

        except Exception as e:
            self.logger.error("Failed to reset configuration", error=str(e))
            return {}, {}, {"error": str(e)}

    def _apply_security_settings(self, rate_limit: bool, session_ttl: int, max_connections: int, cache_ttl: int):
        """Apply security configuration changes."""
        try:
            # Update configuration
            self.orchestrator.config.config.update({
                'rate_limit_enabled': rate_limit,
                'session_ttl': session_ttl,
                'max_connections': max_connections,
                'cache_ttl': cache_ttl
            })

            return {"status": "Security settings applied successfully", "validated": True}

        except Exception as e:
            self.logger.error("Failed to apply security settings", error=str(e))