Skip to content

Commit 8bfc09c

Browse files
committed
refactor code and rename
1 parent b919f8d commit 8bfc09c

File tree

7 files changed

+221
-197
lines changed

7 files changed

+221
-197
lines changed

mcp_config.json.example

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"mcpServices": {
33
"context7": {
44
"url": "http://localhost:8123/mcp",
5-
"handler": "rdagent.components.mcp.context7.handler:Context7Handler",
5+
"handler": "rdagent.components.mcp.context7.client:Context7Client",
66
"enabled": true,
77
"timeout": 60,
88
"extra_config": {
@@ -13,11 +13,11 @@
1313
},
1414
"simple_code_search": {
1515
"url": "http://localhost:9001/mcp",
16-
"handler": "rdagent.components.mcp.general_handler:GeneralMCPHandler",
16+
"handler": "rdagent.components.mcp.client:MCPClient",
1717
"timeout": 60,
1818
"enabled": true,
1919
"extra_config": {
20-
"model": "gpt-3.5-turbo",
20+
"model": "gpt-3.5-turbo"
2121
}
2222
}
2323
}

rdagent/components/mcp/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616

1717
# Main user interfaces
1818
from .agent import MCPAgent, MCPServerStreamableHTTP, create_agent
19+
from .client import BaseMCPClient, MCPClient
1920

2021
# Core components (for advanced usage only)
2122
from .connector import StreamableHTTPConfig, StreamableHTTPConnector
22-
from .context7.handler import Context7Handler
23-
from .general_handler import BaseMCPHandler
24-
from .registry import MCPRegistry, MCPRegistryConfig, MCPServiceConfig
23+
from .context7.client import Context7Client
2524

2625
# Core execution functions
27-
from .unified import mcp_execute, mcp_execute_sync
26+
from .registry import (
27+
MCPRegistry,
28+
MCPRegistryConfig,
29+
MCPServiceConfig,
30+
mcp_execute,
31+
mcp_execute_sync,
32+
)
2833

2934
__all__ = [
3035
# Main user interfaces
@@ -37,8 +42,9 @@
3742
# Advanced components (for internal use)
3843
"StreamableHTTPConnector",
3944
"StreamableHTTPConfig",
40-
"BaseMCPHandler",
41-
"Context7Handler",
45+
"BaseMCPClient",
46+
"MCPClient",
47+
"Context7Client",
4248
"MCPRegistry",
4349
"MCPRegistryConfig",
4450
"MCPServiceConfig",

rdagent/components/mcp/agent.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@
1616
from typing import List, Optional, Union
1717
from urllib.parse import urlparse
1818

19-
from rdagent.components.mcp.registry import MCPServiceConfig, get_global_registry
20-
from rdagent.components.mcp.unified import mcp_execute, mcp_execute_sync
19+
from rdagent.components.mcp.registry import (
20+
MCPServiceConfig,
21+
get_global_registry,
22+
mcp_execute,
23+
mcp_execute_sync,
24+
)
2125
from rdagent.log import rdagent_logger as logger
2226

2327

@@ -57,7 +61,7 @@ def __init__(
5761

5862
self.url = url
5963
self.name = name or self._generate_name_from_url(url) if url else name
60-
self.handler = handler or "rdagent.components.mcp.general_handler:GeneralMCPHandler"
64+
self.handler = handler or "rdagent.components.mcp.client:MCPClient"
6165
self.extra_config = extra_config
6266
self.enabled = extra_config.pop("enabled", True)
6367
self.timeout = extra_config.pop("timeout", 120.0)

rdagent/components/mcp/general_handler.py renamed to rdagent/components/mcp/client.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
"""MCP Handler Implementation
1+
"""MCP Client Implementation
22
3-
This module contains both the abstract base class and the general implementation
4-
for MCP service handlers. It provides a unified interface for MCP services using
5-
LiteLLM backend for all LLM calls.
3+
This module provides the core MCP client that handles protocol communication,
4+
tool execution, and LLM interactions for MCP services.
65
"""
76

87
import asyncio
@@ -25,8 +24,8 @@
2524
from rdagent.oai.backend.litellm import LiteLLMAPIBackend
2625

2726

28-
class BaseMCPHandler(ABC):
29-
"""Abstract base class for MCP service handlers."""
27+
class BaseMCPClient(ABC):
28+
"""Abstract base class for MCP clients."""
3029

3130
def __init__(self, service_name: str, **config):
3231
"""Initialize handler with service name and configuration."""
@@ -59,15 +58,15 @@ def get_service_info(self) -> Dict[str, Any]:
5958
}
6059

6160

62-
class GeneralMCPHandler(BaseMCPHandler):
63-
"""General MCP service handler using LiteLLM backend.
61+
class MCPClient(BaseMCPClient):
62+
"""MCP client implementation.
6463
65-
This handler provides common functionality for all MCP services:
66-
- Uses LiteLLM backend for unified model calling and configuration
67-
- Supports multi-round tool calling with detailed logging
68-
- Provides customizable hooks for service-specific logic
69-
- Integrates caching and error handling
70-
- Maintains compatibility with existing MCP interfaces
64+
This client provides complete MCP protocol support:
65+
- Manages connections to MCP services
66+
- Executes tools with multi-round calling
67+
- Integrates with LLM for intelligent processing
68+
- Handles caching, retries, and error recovery
69+
- Supports customization through subclassing
7170
"""
7271

7372
# Rate limit wait times (in seconds)

rdagent/components/mcp/context7/handler.py renamed to rdagent/components/mcp/context7/client.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
"""Context7 Handler Implementation
1+
"""Context7 Client Implementation
22
33
This handler preserves all the experimental results and optimizations
44
for Context7 service, including prompt templates, timm library special
55
handling, caching mechanisms, and error handling logic.
66
7-
Now inherits from GeneralMCPHandler to use unified LiteLLM backend.
7+
Now inherits from MCPClient to use unified LiteLLM backend.
88
"""
99

1010
from typing import Optional
1111

12+
from rdagent.components.mcp.client import MCPClient
1213
from rdagent.components.mcp.connector import MCPConnectionError, StreamableHTTPConfig
13-
from rdagent.components.mcp.general_handler import GeneralMCPHandler
1414
from rdagent.log import rdagent_logger as logger
1515
from rdagent.utils.agent.tpl import T
1616

1717

18-
class Context7Handler(GeneralMCPHandler):
19-
"""Context7 MCP service handler with all experimental optimizations.
18+
class Context7Client(MCPClient):
19+
"""Context7 MCP service client with experimental optimizations.
2020
21-
This handler preserves all the research results including:
21+
This client extends MCPClient with Context7-specific features:
2222
- Enhanced prompt templates for better documentation search
2323
- Special handling for timm library (3+ mentions trigger)
2424
- Intelligent error handling for documentation not found cases
25-
- All common functionality now handled by GeneralMCPHandler + LiteLLM
25+
- Tool response validation for Context7's specific format
2626
"""
2727

2828
# Tool response validation rules for Context7
@@ -40,27 +40,27 @@ class Context7Handler(GeneralMCPHandler):
4040

4141
# TODO: Consider making these configurable based on server response time patterns
4242
# Override parent class retry wait times to fit within Context7's 75-second session window
43-
# These directly override GeneralMCPHandler's class attributes
43+
# These directly override MCPClient's class attributes
4444
RATE_LIMIT_WAIT_TIMES = [15, 20, 25] # Maximum total: 60 seconds
4545
NORMAL_RETRY_WAIT_TIMES = [5, 8, 10] # For non-rate-limit errors
4646

4747
def __init__(self, service_name: str = "context7", service_url: str = "http://localhost:8123/mcp", **config):
48-
# Initialize with GeneralMCPHandler - uses LiteLLM backend
48+
# Initialize with MCPClient - uses LiteLLM backend
4949
super().__init__(service_name, **config)
5050

5151
# Store MCP service URL (from registry config)
5252
self.mcp_url = service_url
5353

5454
# Log that we're using LiteLLM backend
5555
logger.info(
56-
f"Context7Handler initialized with session_timeout={self.SESSION_TIMEOUT}s, "
56+
f"Context7Client initialized with session_timeout={self.SESSION_TIMEOUT}s, "
5757
f"rate_limit_waits={self.RATE_LIMIT_WAIT_TIMES}",
5858
tag="context7_config",
5959
)
6060

6161
# Configuration is now handled by LiteLLM backend - no need for separate resolution
6262

63-
# process_query is now handled by GeneralMCPHandler - we implement the abstract methods
63+
# process_query is now handled by MCPClient - we implement the abstract methods
6464

6565
def preprocess_query(self, query: str, **kwargs) -> str:
6666
"""Preprocess query with Context7 specific enhancements.
@@ -148,7 +148,7 @@ def _build_enhanced_query(self, error_message: str, full_code: Optional[str] = N
148148

149149
return enhanced_query
150150

151-
# All common functionality is now handled by GeneralMCPHandler
151+
# All common functionality is now handled by MCPClient
152152

153153
def customize_connector_config(self, config: "StreamableHTTPConfig") -> "StreamableHTTPConfig":
154154
"""Customize connector configuration for Context7's 75-second session limit.

0 commit comments

Comments
 (0)