search_web
Search the web for real-time information using the Tavily API to answer queries and gather current data.
Instructions
Search the web for information using Tavily API
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes |
Implementation Reference
- mcp2tavily.py:101-104 (handler)The main handler function for the 'search_web' tool, decorated with @mcp.tool() for registration and execution. It delegates the actual search logic to the internal _do_search helper.@mcp.tool() def search_web(query: str) -> str: """Search the web for information using Tavily API""" return _do_search(query)
- mcp2tavily.py:32-61 (helper)Helper function containing the core logic for web search using TavilyClient, including UTF-8 handling, API call, response parsing, and error management.def _do_search(query: str) -> str: """Internal function to handle the search logic with UTF-8 support""" try: # 确保查询字符串是UTF-8编码 query = query.encode('utf-8').decode('utf-8') tavily_client = TavilyClient(api_key=API_KEY) response = tavily_client.search( query=query, search_depth="basic", include_answer=True, include_raw_content=False ) # 确保响应文本是UTF-8编码 answer = response.get('answer', 'No answer found').encode('utf-8').decode('utf-8') sources = response.get('sources', []) result = f"Answer: {answer}\n\nSources:" for source in sources[:3]: title = source.get('title', 'No title').encode('utf-8').decode('utf-8') url = source.get('url', 'No URL') result += f"\n- {title}: {url}" return result except UnicodeError as e: logger.error(f"Encoding error: {str(e)}") return "Error: Unicode encoding issue occurred" except Exception as e: logger.error(f"Search error: {str(e)}") return f"Error performing search: {str(e)}"