Spaces:
Sleeping
Sleeping
File size: 10,796 Bytes
e0aa230 |
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 |
"""
MCP Tavily Integration Module
This module demonstrates how to integrate Tavily API via MCP (Model Context Protocol)
for live web search functionality in the RAG system.
Technology: MCP + Tavily API
"""
import logging
import time
from typing import Dict, List, Any, Optional
from datetime import datetime
class MCPTavilyIntegration:
"""
Handles MCP integration with Tavily API for live web search.
This class provides the bridge between the RAG system and Tavily's
search capabilities through the Model Context Protocol.
"""
def __init__(self, config: Optional[Dict[str, Any]] = None):
"""
Initialize MCP Tavily integration.
Args:
config: Configuration dictionary
"""
self.config = config or {}
self.logger = logging.getLogger(__name__)
# π§ MCP Configuration
self.server_name = self.config.get("mcp_server_name", "tavily-mcp")
self.tool_name = self.config.get("mcp_tool_name", "tavily-search")
self.timeout = self.config.get("timeout", 30)
self.logger.info(" MCP Tavily Integration initialized")
def search_web(
self,
query: str,
max_results: int = 5,
search_depth: str = "basic",
time_range: str = "month",
topic: str = "general",
) -> Dict[str, Any]:
"""
Perform web search using Tavily API via MCP.
Args:
query: Search query
max_results: Maximum number of results
search_depth: Search depth (basic/advanced)
time_range: Time range for results
topic: Search topic category
Returns:
Dictionary with search results
"""
try:
self.logger.info(f" MCP Tavily search: '{query}' (depth: {search_depth})")
# π Prepare MCP arguments
mcp_arguments = {
"query": query,
"max_results": min(max_results, 20), # Tavily limit
"search_depth": search_depth,
"topic": topic,
"include_raw_content": True,
"time_range": time_range,
}
# π This is where the actual MCP call would be made
# In a real implementation, this would use the MCP client:
"""
Example MCP call structure:
result = use_mcp_tool(
server_name=self.server_name,
tool_name=self.tool_name,
arguments=mcp_arguments
)
"""
# π§ For demonstration, we'll simulate the MCP response structure
simulated_result = self._simulate_tavily_response(query, max_results)
# π Process and validate MCP response
processed_result = self._process_mcp_response(simulated_result, query)
self.logger.info(
f" MCP search completed: {processed_result.get('total_results', 0)} results"
)
return processed_result
except Exception as e:
self.logger.error(f" MCP Tavily search failed: {str(e)}")
return {
"query": query,
"results": [],
"total_results": 0,
"error": str(e),
"status": "mcp_error",
}
def _simulate_tavily_response(self, query: str, max_results: int) -> Dict[str, Any]:
"""
Simulate Tavily API response for demonstration.
In production, this would be replaced by actual MCP call results.
"""
# π§ Simulated response structure matching Tavily API
return {
"query": query,
"follow_up_questions": None,
"answer": f"Based on web search for '{query}'...",
"images": [],
"results": [
{
"title": f"Example Result 1 for {query}",
"url": "https://example.com/result1",
"content": f"This is example content related to {query}. It provides comprehensive information about the topic.",
"raw_content": f"Raw content for {query} with additional details...",
"published_date": "2024-01-15",
"score": 0.95,
},
{
"title": f"Example Result 2 for {query}",
"url": "https://example.com/result2",
"content": f"Another relevant result for {query} with different perspective and insights.",
"raw_content": f"Extended raw content for {query}...",
"published_date": "2024-01-14",
"score": 0.87,
},
][:max_results],
"response_time": 1.2,
}
def _process_mcp_response(
self, mcp_result: Dict[str, Any], original_query: str
) -> Dict[str, Any]:
"""
Process and validate MCP response from Tavily.
Args:
mcp_result: Raw MCP response
original_query: Original search query
Returns:
Processed search results
"""
try:
# π Extract results from MCP response
raw_results = mcp_result.get("results", [])
# π Process each result
processed_results = []
for i, result in enumerate(raw_results):
processed_result = {
"title": result.get("title", f"Web Result {i+1}"),
"url": result.get("url", ""),
"content": result.get("content", ""),
"raw_content": result.get("raw_content", ""),
"score": result.get("score", 0.0),
"published_date": result.get("published_date", ""),
"rank": i + 1,
"source": "tavily_web_search",
"search_engine": "tavily",
"metadata": {
"title": result.get("title", ""),
"url": result.get("url", ""),
"content_length": len(result.get("content", "")),
"has_raw_content": bool(result.get("raw_content")),
"search_rank": i + 1,
"published_date": result.get("published_date", ""),
},
}
processed_results.append(processed_result)
# π Prepare final response
return {
"query": original_query,
"results": processed_results,
"total_results": len(processed_results),
"answer": mcp_result.get("answer", ""),
"follow_up_questions": mcp_result.get("follow_up_questions", []),
"response_time": mcp_result.get("response_time", 0),
"timestamp": datetime.now(),
"status": "success",
"source": "mcp_tavily",
}
except Exception as e:
self.logger.error(f" Error processing MCP response: {str(e)}")
return {
"query": original_query,
"results": [],
"total_results": 0,
"error": f"Response processing failed: {str(e)}",
"status": "processing_error",
}
def test_connection(self) -> Dict[str, Any]:
"""
Test MCP connection to Tavily.
Returns:
Connection test results
"""
try:
self.logger.info(" Testing MCP Tavily connection...")
# π Simple test query
test_result = self.search_web(
query="test connection", max_results=1, search_depth="basic"
)
if test_result.get("status") == "success":
return {
"status": "success",
"message": " MCP Tavily connection successful",
"server_name": self.server_name,
"tool_name": self.tool_name,
"response_time": test_result.get("response_time", 0),
}
else:
return {
"status": "error",
"message": " MCP Tavily connection failed",
"error": test_result.get("error", "Unknown error"),
}
except Exception as e:
self.logger.error(f" MCP connection test failed: {str(e)}")
return {
"status": "error",
"message": " MCP connection test failed",
"error": str(e),
}
def get_server_info(self) -> Dict[str, Any]:
"""
Get MCP server information.
Returns:
Server information dictionary
"""
return {
"server_name": self.server_name,
"tool_name": self.tool_name,
"timeout": self.timeout,
"status": "configured",
"description": "MCP integration for Tavily web search API",
}
# π§ Helper function for easy integration
def create_mcp_tavily_client(
config: Optional[Dict[str, Any]] = None,
) -> MCPTavilyIntegration:
"""
Create and configure MCP Tavily client.
Args:
config: Optional configuration dictionary
Returns:
Configured MCPTavilyIntegration instance
"""
return MCPTavilyIntegration(config)
# π Example usage and integration guide
if __name__ == "__main__":
"""
Example usage of MCP Tavily Integration
This demonstrates how to use the MCP integration in your RAG system.
"""
# π§ Configure MCP client
config = {
"mcp_server_name": "tavily-mcp",
"mcp_tool_name": "tavily-search",
"timeout": 30,
}
# π Create client
mcp_client = create_mcp_tavily_client(config)
# π§ͺ Test connection
connection_test = mcp_client.test_connection()
print(f"Connection test: {connection_test}")
# π Example search
search_result = mcp_client.search_web(
query="latest AI developments 2024",
max_results=5,
search_depth="basic",
time_range="month",
)
print(f"Search results: {search_result.get('total_results', 0)} found")
for result in search_result.get("results", []):
print(f"- {result['title']}: {result['url']}")
|