get_url_content
Extract content from any URL using the Tavily API to retrieve real-time web page data for analysis or integration into AI workflows.
Instructions
Get the content from a specific URL using Tavily API
Args:
url (str): The URL to extract content from
Returns:
str: The extracted content from the URL
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| url | Yes |
Implementation Reference
- mcp2tavily.py:111-122 (handler)The handler function for the 'get_url_content' tool, decorated with @mcp.tool() which registers it in the MCP server. It delegates execution to the internal _get_url_content helper.@mcp.tool() def get_url_content(url: str) -> str: """Get the content from a specific URL using Tavily API Args: url (str): The URL to extract content from Returns: str: The extracted content from the URL """ return _get_url_content(url)
- mcp2tavily.py:62-100 (helper)The core helper function that implements the tool logic: extracts content from the given URL using TavilyClient.extract(), handles UTF-8 encoding, adds metadata, and includes error handling.def _get_url_content(url: str) -> str: """Internal function to get content from a specific URL using Tavily API""" try: tavily_client = TavilyClient(api_key=API_KEY) logger.info(f"Attempting to extract content from URL: {url}") response = tavily_client.extract(url) # logger.info(f"Raw API response: {response}") # 使用 logger 替代 print # 处理返回的数据结构 results = response.get('results', []) if not results: logger.error(f"No results found in response: {response}") return "No content found for this URL. API response contains no results." # 获取第一个结果的原始内容 first_result = results[0] # logger.info(f"First result structure: {list(first_result.keys())}") # 只记录键名,避免日志过大 content = first_result.get('raw_content', '') if not content: logger.error("No raw_content found in first result") return "No raw content available in the API response" # 确保响应文本是UTF-8编码 content = content.encode('utf-8').decode('utf-8') # 添加一些元数据到输出中 metadata = f"URL: {url}\n" metadata += f"Content length: {len(content)} characters\n" metadata += "---\n\n" logger.info(f"Successfully extracted content with length: {len(content)}") return f"{metadata}{content}" except Exception as e: logger.exception(f"Detailed error while extracting URL content") return f"Error getting content from URL: {str(e)}"
- mcp2tavily.py:113-120 (schema)Input/output schema defined via function type hints (url: str -> str) and detailed docstring describing the argument and return value."""Get the content from a specific URL using Tavily API Args: url (str): The URL to extract content from Returns: str: The extracted content from the URL """
- mcp2tavily.py:111-111 (registration)The @mcp.tool() decorator registers the 'get_url_content' function as an MCP tool.@mcp.tool()