File size: 31,425 Bytes
8c6097b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
#!/usr/bin/env python3
"""
MCP Client for Agent-to-Server Communication
Provides a proper MCP client that uses the official MCP package
to connect to and communicate with MCP servers through the Model Context Protocol.
"""

import json
import logging
import time
from typing import Dict, Any, List, Optional
from dataclasses import dataclass, field
from pathlib import Path
import sys
sys.path.append(str(Path(__file__).parent.parent.parent))
from ..utils.status_codes import JsonRpcErr
from http import HTTPStatus

try:
    import httpx
    MCP_AVAILABLE = True
except ImportError:
    MCP_AVAILABLE = False
    logging.warning("HTTP client dependencies not available. Falling back to direct tools.")

logger = logging.getLogger(__name__)


@dataclass
class MCPClientResult:
    """Standard result format for MCP client operations"""
    success: bool
    data: Any = None
    error: str = None
    metadata: Dict[str, Any] = field(default_factory=dict)
    
    def to_dict(self) -> Dict[str, Any]:
        return {
            "success": self.success,
            "data": self.data,
            "error": self.error,
            "metadata": self.metadata
        }


@dataclass
class MCPTool:
    """Simple representation of an MCP tool"""
    name: str
    description: str = ""
    input_schema: Dict[str, Any] = field(default_factory=dict)


@dataclass 
class RetryConfig:
    """Configuration for retry behavior on rate limiting"""
    max_retries: int = 20              # Maximum number of retry attempts
    base_delay: float = 2.0            # Base delay between retries (seconds)
    max_delay: float = 60.0            # Maximum delay between retries (seconds)
    exponential_backoff: bool = True   # Use exponential backoff
    respect_retry_after: bool = True   # Respect server's Retry-After header
    retry_on_rate_limit: bool = True   # Enable automatic retry on rate limits


class MCPClient:
    """
    Simple HTTP-based MCP Client for dynamic tool discovery and execution.
    
    This client makes direct HTTP JSON-RPC calls to the MCP server,
    avoiding the complexity of streaming connections.
    
    Session management is handled entirely by the server:
    - Server assigns session IDs on connection
    - Server manages workspace creation and isolation
    - All tool operations use server-managed workspaces
    """
    
    def __init__(self, server_url: str = "http://localhost:6274/mcp", retry_config: Optional[RetryConfig] = None):
        self.server_url = server_url.rstrip('/')
        self.retry_config = retry_config or RetryConfig()
        self._tools: Dict[str, MCPTool] = {}
        self._connected = False
        self._request_id = 0
        self._session_id = None
        
        if not MCP_AVAILABLE:
            logger.warning("HTTP client not available. Some functionality may be limited.")
            return
        
        # Initialize connection and discover tools
        self._initialize_connection()
    
    def _get_next_id(self) -> int:
        """Get next request ID"""
        self._request_id += 1
        return self._request_id

    @staticmethod
    def _parse_sse_response(sse_text: str) -> Dict[str, Any]:
        """Parse Server-Sent Events response and extract JSON data"""
        try:
            # SSE format: "event: message\ndata: {json}\n\n"
            lines = sse_text.strip().split('\n')
            
            for line in lines:
                if line.startswith('data: '):
                    json_data = line[6:]  # Remove "data: " prefix
                    return json.loads(json_data)
            
            # If no data line found, try parsing entire response as JSON
            return json.loads(sse_text)
            
        except json.JSONDecodeError as e:
            logger.error(f"Failed to parse SSE response: {e}")
            logger.error(f"SSE text: {sse_text[:200]}...")
            return {"error": {"code": JsonRpcErr.PARSE_ERROR, "message": f"Parse error: {e}"}}
    
    def _make_request(self, method: str, params: Dict[str, Any] = None) -> MCPClientResult:
        """Make a JSON-RPC request to the MCP server with automatic retry on rate limits"""
        if not MCP_AVAILABLE:
            return MCPClientResult(success=False, error="HTTP client not available")
        
        # Prepare JSON-RPC request
        request_data = {
            "jsonrpc": "2.0",
            "id": self._get_next_id(),
            "method": method,
            "params": params or {}
        }
        
        # Make HTTP request with proper MCP headers
        headers = {
            "Content-Type": "application/json",
            "Accept": "application/json, text/event-stream"
        }
        
        # Add session ID if available
        if self._session_id:
            headers["X-Session-ID"] = self._session_id
        
        last_error = None
        retry_count = 0
        
        while retry_count <= self.retry_config.max_retries:
            try:
                # Disable proxy for localhost/127.0.0.1 connections to avoid proxy interference
                import os
                from urllib.parse import urlparse
                parsed_url = urlparse(self.server_url)
                is_localhost = parsed_url.hostname in ['localhost', '127.0.0.1', '::1']
                
                # Add localhost to NO_PROXY for localhost connections
                original_no_proxy = None
                if is_localhost:
                    original_no_proxy = os.environ.get('NO_PROXY', os.environ.get('no_proxy', ''))
                    # Add localhost and 127.0.0.1 to NO_PROXY
                    no_proxy_hosts = ['localhost', '127.0.0.1', '::1']
                    if original_no_proxy:
                        existing_hosts = [h.strip() for h in original_no_proxy.split(',')]
                        no_proxy_hosts.extend(existing_hosts)
                    os.environ['NO_PROXY'] = ','.join(no_proxy_hosts)
                    os.environ['no_proxy'] = ','.join(no_proxy_hosts)
                
                try:
                    # Create client with connection pooling for high-concurrency
                    limits = httpx.Limits(
                        max_keepalive_connections=3000,  # Keep more connections alive
                        max_connections=3000,           # Allow more concurrent connections
                        keepalive_expiry=1000.0         # Keep connections alive longer
                    )
                    timeout = httpx.Timeout(
                        connect=100.0,
                        read=None,
                        write=60.0,
                        pool=30.0
                    )
                    with httpx.Client(
                        timeout=timeout,  # Higher timeout for high-concurrency scenarios
                        limits=limits,   # Connection pooling for better performance
                        trust_env=False,
                        http2=True      # Enable HTTP/2 for better multiplexing
                    ) as client:
                        response = client.post(
                            self.server_url,
                            json=request_data,
                            headers=headers
                        )

                    # Check for rate limiting (HTTP 429)
                    if response.status_code == 429:
                        if not self.retry_config.retry_on_rate_limit:
                            return MCPClientResult(
                                success=False,
                                error=f"Rate limit exceeded (HTTP 429) - retries disabled",
                                metadata={"status_code": 429, "retry_count": retry_count}
                            )
                        
                        if retry_count >= self.retry_config.max_retries:
                            return MCPClientResult(
                                success=False,
                                error=f"Rate limit exceeded (HTTP 429) - max retries ({self.retry_config.max_retries}) reached",
                                metadata={"status_code": 429, "retry_count": retry_count}
                            )
                        
                        # Calculate retry delay
                        delay = self._calculate_retry_delay(response, retry_count)
                        
                        logger.warning(f"Rate limit exceeded for {method} (attempt {retry_count + 1}/{self.retry_config.max_retries + 1}). Retrying in {delay:.1f}s...")
                        
                        # Wait before retry
                        time.sleep(delay)
                        retry_count += 1
                        continue
                    
                    # Handle other HTTP errors
                    if response.status_code != HTTPStatus.OK:
                        return MCPClientResult(
                            success=False,
                            error=f"HTTP {response.status_code}: {response.text}",
                            metadata={"status_code": response.status_code, "retry_count": retry_count}
                        )
                    
                    # Parse successful response (could be JSON or SSE format)
                    if response.headers.get("content-type", "").startswith("text/event-stream"):
                        # Parse SSE format
                        response_data = self._parse_sse_response(response.text)
                    else:
                        # Parse regular JSON
                        response_data = response.json()
                    
                    if "error" in response_data:
                        return MCPClientResult(
                            success=False,
                            error=f"MCP Error: {response_data['error']}",
                            metadata={"retry_count": retry_count}
                        )
                    
                    # Capture session ID from response data (for all methods, not just initialize)
                    if "session_id" in response_data:
                        self._session_id = response_data["session_id"]
                        logger.info(f"Captured session ID from response: {self._session_id}")
                    
                    # Success! Log retry info if this wasn't the first attempt
                    if retry_count > 0:
                        logger.info(f"Request {method} succeeded after {retry_count} retries")
                    
                    return MCPClientResult(
                        success=True,
                        data=response_data.get("result"),
                        metadata={
                            "method": method, 
                            "server_url": self.server_url,
                            "session_id": self._session_id,
                            "retry_count": retry_count
                        }
                    )
                finally:
                    # Restore original NO_PROXY environment variable
                    if is_localhost:
                        if original_no_proxy is not None:
                            if original_no_proxy:
                                os.environ['NO_PROXY'] = original_no_proxy
                                os.environ['no_proxy'] = original_no_proxy
                            else:
                                # Remove NO_PROXY if it wasn't set originally
                                os.environ.pop('NO_PROXY', None)
                                os.environ.pop('no_proxy', None)
                                   
            except Exception as e:
                last_error = str(e)
                logger.error(f"MCP request failed for {method} (attempt {retry_count + 1}): {e}")
                
                # Only retry on certain exceptions (network issues, timeouts)
                if not self._should_retry_exception(e) or retry_count >= self.retry_config.max_retries:
                    break
                
                # Calculate retry delay for exceptions
                delay = self._calculate_exception_retry_delay(retry_count)
                logger.warning(f"Request {method} failed, retrying in {delay:.1f}s... (attempt {retry_count + 1}/{self.retry_config.max_retries + 1})")
                
                time.sleep(delay)
                retry_count += 1
        
        # All retries exhausted
        return MCPClientResult(
            success=False,
            error=f"Request failed after {retry_count} retries. Last error: {last_error}",
            metadata={"retry_count": retry_count}
        )
    
    def _calculate_retry_delay(self, response, retry_count: int) -> float:
        """Calculate delay before retry based on server response and retry count"""
        delay = self.retry_config.base_delay
        
        # Respect server's Retry-After header if available
        if self.retry_config.respect_retry_after and "Retry-After" in response.headers:
            try:
                retry_after = float(response.headers["Retry-After"])
                delay = min(retry_after, self.retry_config.max_delay)
                logger.debug("Using server Retry-After: {%s}s", delay)
            except (ValueError, TypeError):
                logger.warning(f"Invalid Retry-After header: {response.headers.get('Retry-After')}")
        
        # Apply exponential backoff if enabled
        elif self.retry_config.exponential_backoff:
            delay = min(
                self.retry_config.base_delay * (2 ** retry_count),
                self.retry_config.max_delay
            )
        
        return delay
    
    def _calculate_exception_retry_delay(self, retry_count: int) -> float:
        """Calculate delay for exception-based retries"""
        if self.retry_config.exponential_backoff:
            return min(
                self.retry_config.base_delay * (2 ** retry_count),
                self.retry_config.max_delay
            )
        return self.retry_config.base_delay

    @staticmethod
    def _should_retry_exception(exception: Exception) -> bool:
        """Determine if an exception warrants a retry"""
        # Retry on network-related exceptions
        if isinstance(exception, (httpx.RequestError, httpx.TimeoutException, httpx.ConnectError)):
            return True
        
        # Don't retry on other exceptions (parsing errors, etc.)
        return False
    
    def _initialize_connection(self):
        """Initialize MCP client connection and fetch available tools"""
        if not MCP_AVAILABLE:
            return
        
        try:
            # Initialize session
            init_result = self._make_request("initialize", {
                "protocolVersion": "2025-06-18",
                "capabilities": {},
                "clientInfo": {
                    "name": "DeepDiver-MCP-Client",
                    "version": "1.0.0"
                }
            })
            print(init_result)
            if not init_result.success:
                logger.error(f"MCP initialization failed: {init_result.error}")
                return
            
            logger.info("MCP client initialized successfully")
            
            # Fetch available tools
            tools_result = self._make_request("tools/list")
            
            if tools_result.success and tools_result.data:
                tools_data = tools_result.data.get("tools", [])
                self._tools = {}
                
                for tool_data in tools_data:
                    tool = MCPTool(
                        name=tool_data.get("name", ""),
                        description=tool_data.get("description", ""),
                        input_schema=tool_data.get("inputSchema", {})
                    )
                    self._tools[tool.name] = tool
                
                logger.info(f"Discovered {len(self._tools)} tools from MCP server: {list(self._tools.keys())}")
            
            self._connected = True
            
        except Exception as e:
            logger.error(f"Failed to initialize MCP client: {e}")
            self._connected = False
    
    def _ensure_connection(self):
        """Ensure MCP client is connected"""
        if not MCP_AVAILABLE:
            raise RuntimeError("HTTP client not available")
        
        if not self._connected:
            self._initialize_connection()
        
        if not self._connected:
            raise RuntimeError("MCP client not connected to server")
    
    def call_tool(self, tool_name: str, arguments: Dict[str, Any]) -> MCPClientResult:
        """
        Generic method to call any tool available on the MCP server.
        
        Args:
            tool_name: Name of the tool to call
            arguments: Dictionary of arguments to pass to the tool
            
        Returns:
            MCPClientResult with the tool execution result
        """
        try:
            self._ensure_connection()
            
            if tool_name not in self._tools:
                return MCPClientResult(
                    success=False,
                    error=f"Tool '{tool_name}' not available on server. Available tools: {list(self._tools.keys())}"
                )
            
            # Call the tool via JSON-RPC
            result = self._make_request("tools/call", {
                "name": tool_name,
                "arguments": arguments
            })
            
            return result
            
        except Exception as e:
            logger.error(f"Error calling tool '{tool_name}': {e}")
            return MCPClientResult(
                success=False,
                error=str(e)
            )
    
    def get_available_tools(self) -> Dict[str, MCPTool]:
        """Get dictionary of available tools from the server"""
        return self._tools.copy()
    
    def list_tools(self) -> List[str]:
        """Get list of available tool names"""
        return list(self._tools.keys())
    
    def get_tool_info(self, tool_name: str) -> Optional[MCPTool]:
        """Get detailed information about a specific tool"""
        return self._tools.get(tool_name)
    
    def is_connected(self) -> bool:
        """Check if client is connected to MCP server"""
        return self._connected and MCP_AVAILABLE
    
    def refresh_tools(self):
        """Refresh the list of available tools from the server"""
        try:
            # Fetch available tools
            tools_result = self._make_request("tools/list")
            
            if tools_result.success and tools_result.data:
                tools_data = tools_result.data.get("tools", [])
                self._tools = {}
                print(self._tools)

                for tool_data in tools_data:
                    tool = MCPTool(
                        name=tool_data.get("name", ""),
                        description=tool_data.get("description", ""),
                        input_schema=tool_data.get("inputSchema", {})
                    )
                    self._tools[tool.name] = tool
                
                logger.info(f"Refreshed {len(self._tools)} tools from MCP server")
            else:
                logger.error(f"Failed to refresh tools: {tools_result.error}")
                
        except Exception as e:
            logger.error(f"Error refreshing tools: {e}")
    
    def close(self):
        """Close MCP client connection"""
        # Since we create connections per request, just mark as disconnected
        self._connected = False


class MCPToolsAdapter:
    """
    Adapter class that provides the MCPTools interface while using the generic MCP client.
    
    This adapter provides backward compatibility with existing agents by mapping
    MCPTools method calls to generic MCP client tool calls.
    """
    
    def __init__(self, server_url: str = "http://localhost:6274/mcp", retry_config: Optional[RetryConfig] = None):
        self.client = MCPClient(server_url, retry_config)
    
    def _call_tool(self, tool_name: str, **kwargs) -> MCPClientResult:
        """Internal method to call tools through the MCP client"""
        return self.client.call_tool(tool_name, kwargs)
    
    def __getattr__(self, name: str):
        """
        Dynamic method creation for any tool available on the server.
        This allows calling tools like adapter.batch_web_search(...) or adapter.file_read(...)
        """
        if name.startswith('_'):
            raise AttributeError(f"'{self.__class__.__name__}' object has no attribute '{name}'")
        
        # Create a dynamic method that calls the tool
        def tool_method(**kwargs):
            result = self._call_tool(name, **kwargs)
            # For backward compatibility, return the data portion
            return result.data if result.success else {"error": result.error}
        
        return tool_method
    

    
    def is_connected(self) -> bool:
        """Check if the MCP client is connected to the server."""
        return self.client.is_connected()
    
    def get_available_tools(self) -> Dict[str, MCPTool]:
        """Get available tools from the MCP server."""
        return self.client.get_available_tools()
    
    def list_tools(self) -> List[str]:
        """Get list of available tool names."""
        return self.client.list_tools()
    
    def get_tool_schemas(self) -> List[Dict[str, Any]]:
        """
        Get tool schemas for all available tools.
        This is the proper MCP way - schemas come from server, not direct imports.
        """
        schemas = []
        available_tools = self.get_available_tools()
        
        for tool_name, tool_info in available_tools.items():
            schema = {
                "type": "function",
                "function": {
                    "name": tool_name,
                    "description": tool_info.description,
                    "parameters": tool_info.input_schema
                }
            }
            schemas.append(schema)
        
        return schemas
    
    def refresh_tools(self):
        """Refresh the list of available tools from the server."""
        self.client.refresh_tools()
    
    def get_session_info(self) -> Optional[Dict[str, Any]]:
        """Get session information from the underlying MCP client."""
        try:
            if hasattr(self.client, '_session_id'):
                return {
                    "session_id": self.client._session_id,
                    "connected": self.client.is_connected(),
                    "server_url": getattr(self.client, 'server_url', 'unknown')
                }
            return None
        except Exception:
            return None
    
    def close(self):
        """Close the MCP client connection."""
        self.client.close()


class FilteredMCPToolsAdapter:
    """
    Filtered adapter that shares MCP client connection but restricts tool access per agent type.
    
    This allows agents to:
    - Share the same session/workspace (via shared client)
    - Have different tool sets appropriate for their role
    - Maintain proper separation of concerns
    """
    
    def __init__(self, shared_client: MCPClient, allowed_tools: List[str]):
        """
        Initialize with shared client and allowed tools list
        
        Args:
            shared_client: Shared MCPClient instance (same session)
            allowed_tools: List of tools this agent can access
        """
        self.client = shared_client
        self.allowed_tools = set(allowed_tools)
        
        # Validate that allowed tools exist on server
        available_tools = set(self.client.list_tools())
        invalid_tools = self.allowed_tools - available_tools
        if invalid_tools:
            logger.warning(f"Requested tools not available on server: {invalid_tools}")
            self.allowed_tools = self.allowed_tools & available_tools
    
    def _call_tool(self, tool_name: str, **kwargs) -> MCPClientResult:
        """Call tool if allowed, otherwise return error"""
        if tool_name not in self.allowed_tools:
            return MCPClientResult(
                success=False,
                error=f"Tool '{tool_name}' not allowed for this agent. Allowed tools: {list(self.allowed_tools)}"
            )
        
        # Remove any workspace_path if accidentally passed - server handles workspace
        kwargs.pop('workspace_path', None)
        return self.client.call_tool(tool_name, kwargs)
    
    def __getattr__(self, name: str):
        """
        Dynamic method resolution with tool filtering.
        
        Only allows access to tools in the allowed_tools list.
        """
        if name in self.allowed_tools:
            def tool_method(**kwargs):
                return self._call_tool(name, **kwargs)
            return tool_method
        
        if name in self.client.list_tools():
            # Tool exists but not allowed for this agent
            raise AttributeError(f"Tool '{name}' not allowed for this agent. Allowed tools: {list(self.allowed_tools)}")
        else:
            # Tool doesn't exist on server
            raise AttributeError(f"Tool '{name}' not available on server. Available tools: {self.client.list_tools()}")
    

    
    # ================ CLIENT MANAGEMENT ================


    def is_connected(self) -> bool:
        """Check if client is connected to MCP server"""
        return self.client.is_connected()
    
    def get_available_tools(self) -> Dict[str, MCPTool]:
        """Get filtered list of available tools for this agent"""
        all_tools = self.client.get_available_tools()
        return {name: tool for name, tool in all_tools.items() if name in self.allowed_tools}
    
    def list_tools(self) -> List[str]:
        """Get list of allowed tool names for this agent"""
        return list(self.allowed_tools)
    
    def get_tool_schemas(self) -> List[Dict[str, Any]]:
        """
        Get tool schemas for tools allowed for this agent.
        This is the proper MCP way - schemas come from server, not direct imports.
        """
        schemas = []
        available_tools = self.get_available_tools()
        
        for tool_name, tool_info in available_tools.items():
            schema = {
                "type": "function",
                "function": {
                    "name": tool_name,
                    "description": tool_info.description,
                    "parameters": tool_info.input_schema
                }
            }
            schemas.append(schema)
        
        return schemas
    
    def refresh_tools(self):
        """Refresh the underlying client's tools"""
        self.client.refresh_tools()
        
        # Re-validate allowed tools after refresh
        available_tools = set(self.client.list_tools())
        invalid_tools = self.allowed_tools - available_tools
        if invalid_tools:
            logger.warning(f"Some allowed tools no longer available after refresh: {invalid_tools}")
            self.allowed_tools = self.allowed_tools & available_tools
    
    def close(self):
        """Close MCP client connection"""
        self.client.close()


# ================ AGENT TOOL SETS ================
# Define what tools each agent type should have access to

PLANNER_AGENT_TOOLS = [
    "download_files",
    "document_qa",

    "file_read",
    "file_write",
    "str_replace_based_edit_tool",

    "list_workspace",
    "file_find_by_name",
]


INFORMATION_SEEKER_TOOLS = [
    "batch_web_search",
    "url_crawler",
    "document_extract",
    "document_qa",
    "download_files",
    "file_read",
    "file_write",
    "str_replace_based_edit_tool",
    "list_workspace",
    "file_find_by_name",
]

WRITER_AGENT_TOOLS = [
    "file_read",
    "list_workspace",
    "file_find_by_name",

    "search_result_classifier",
    "section_writer",
    "concat_section_files",
]


def create_filtered_mcp_tools_adapter(
    shared_client: MCPClient, 
    agent_type: str
) -> FilteredMCPToolsAdapter:
    """
    Create a filtered MCP tools adapter for specific agent type
    
    Args:
        shared_client: Shared MCPClient instance 
        agent_type: Type of agent ("planner", "information_seeker", "writer")
        
    Returns:
        FilteredMCPToolsAdapter with appropriate tools for agent type
    """
    tool_sets = {
        "planner": PLANNER_AGENT_TOOLS,
        "information_seeker": INFORMATION_SEEKER_TOOLS, 
        "writer": WRITER_AGENT_TOOLS
    }
    
    allowed_tools = tool_sets.get(agent_type, PLANNER_AGENT_TOOLS)
    
    return FilteredMCPToolsAdapter(
        shared_client=shared_client,
        allowed_tools=allowed_tools
    )


def create_agent_mcp_tools(
    agent_type: str,
    server_url: str = "http://localhost:6274/mcp",
    retry_config: Optional[RetryConfig] = None
) -> FilteredMCPToolsAdapter:
    """
    Convenience factory to create a filtered MCP tools adapter with retry support.
    This is the RECOMMENDED way to create MCP tools for agents.
    
    Args:
        agent_type: Type of agent ("planner", "information_seeker", "writer")
        server_url: URL of the MCP server (default: http://localhost:6274/mcp)
        retry_config: Optional retry configuration for handling rate limits
        
    Returns:
        FilteredMCPToolsAdapter with appropriate tools and retry support for the agent type
    """
    # Create client with retry support
    client = create_mcp_client(server_url=server_url, retry_config=retry_config)
    
    # Create filtered adapter for the agent type
    return create_filtered_mcp_tools_adapter(client, agent_type)


def create_mcp_client(
    server_url: str = "http://localhost:6274/mcp",
    retry_config: Optional[RetryConfig] = None
) -> MCPClient:
    """
    Factory function to create a generic MCP Client with optional retry configuration
    
    Args:
        server_url: URL of the MCP server (default: http://localhost:6274/mcp)
        retry_config: Optional retry configuration for handling rate limits
    
    Returns:
        MCPClient instance for direct tool calling with automatic retry on rate limits
    """
    return MCPClient(server_url=server_url, retry_config=retry_config)


def create_mcp_tools_adapter(
    server_url: str = "http://localhost:6274/mcp",
    retry_config: Optional[RetryConfig] = None
) -> MCPToolsAdapter:
    """
    Factory function to create an MCP Tools Adapter for backward compatibility with retry support.
    
    Args:
        server_url: URL of the MCP server (default: http://localhost:6274/mcp)
        retry_config: Optional retry configuration for handling rate limits
    
    Returns:
        MCPToolsAdapter instance that behaves like MCPTools but uses MCP client with automatic retries
    """
    return MCPToolsAdapter(server_url=server_url, retry_config=retry_config)


# Export for compatibility
__all__ = [
    'MCPClientResult',
    'MCPClient',
    'MCPTool',
    'RetryConfig',
    'MCPToolsAdapter',
    'FilteredMCPToolsAdapter',
    'create_mcp_client',
    'create_mcp_tools_adapter',
    'create_filtered_mcp_tools_adapter',
    'create_agent_mcp_tools',  # RECOMMENDED for agents
    'PLANNER_AGENT_TOOLS',
    'INFORMATION_SEEKER_TOOLS', 
    'WRITER_AGENT_TOOLS'
]