Skip to main content
Glama
LangGPT

Context MCP Server

by LangGPT

fetch_and_save

Fetches web content from a URL and saves it as a file, using Jina Reader for markdown conversion with fallback to standard fetch. Automatically generates filenames when not specified.

Instructions

Fetches a URL from the internet using Jina Reader API (with fallback to standard fetch) and saves the content to a file.

This tool first tries to fetch content using Jina Reader API for better markdown conversion, and falls back to the standard fetch method if Jina fails. Files are saved in the configured working directory. If no file path is specified, an automatic filename will be generated based on the URL.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYesURL to fetch
file_pathNoFile path to save the content (optional, will auto-generate if not provided)
rawNoGet the actual HTML content of the requested page, without simplification.

Implementation Reference

  • Pydantic schema defining input parameters for the fetch_and_save tool: url (required), optional file_path, and raw flag.
    class FetchAndSave(BaseModel):
        """Parameters for fetching a URL and saving to file."""
    
        url: Annotated[AnyUrl, Field(description="URL to fetch")]
        file_path: Annotated[Optional[str], Field(default=None, description="File path to save the content (optional, will auto-generate if not provided)")]
        raw: Annotated[
            bool,
            Field(
                default=False,
                description="Get the actual HTML content of the requested page, without simplification.",
            ),
        ]
  • Registration of the 'fetch_and_save' tool in the server's list_tools() method, including name, description, and input schema reference.
                Tool(
                    name="fetch_and_save",
                    description="""Fetches a URL from the internet using Jina Reader API (with fallback to standard fetch) and saves the content to a file.
    
    This tool first tries to fetch content using Jina Reader API for better markdown conversion, and falls back to the standard fetch method if Jina fails. Files are saved in the configured working directory. If no file path is specified, an automatic filename will be generated based on the URL.""",
                    inputSchema=FetchAndSave.model_json_schema(),
                )
  • Core handler logic within call_tool: validates input, determines output file path (auto-generates if needed), fetches content via Jina fallback, saves to file, returns success message with preview.
    elif name == "fetch_and_save":
        try:
            args = FetchAndSave(**arguments)
        except ValueError as e:
            raise McpError(ErrorData(code=INVALID_PARAMS, message=str(e)))
    
        url = str(args.url)
        if not url:
            raise McpError(ErrorData(code=INVALID_PARAMS, message="URL is required"))
        
        # Debug: Log received arguments
        debug_info = f"Received arguments: url={url}, file_path={args.file_path}, raw={args.raw}"
        
        # Generate file path if not provided
        if args.file_path and args.file_path.strip():
            # Use provided file path, but ensure it's within work_dir
            provided_path = args.file_path.strip()
            if os.path.isabs(provided_path):
                # If absolute path, use as-is (user responsibility)
                file_path = provided_path
                debug_info += f"\nUsing absolute path: {file_path}"
            else:
                # If relative path, make it relative to work_dir
                file_path = os.path.join(work_dir, provided_path)
                debug_info += f"\nUsing relative path in work_dir: {file_path}"
        else:
            # Auto-generate filename
            filename = generate_filename_from_url(url)
            file_path = os.path.join(work_dir, filename)
            debug_info += f"\nAuto-generated filename: {file_path}"
    
        try:
            content, prefix = await fetch_with_jina_fallback(
                url, user_agent_autonomous, force_raw=args.raw, proxy_url=proxy_url
            )
            
            # Create directory if it doesn't exist
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
            
            # Save content to file
            with open(file_path, 'w', encoding='utf-8') as f:
                f.write(content)
            
            return [TextContent(type="text", text=f"Successfully fetched content from {url} and saved to {file_path}\n\nDebug info: {debug_info}\n\n{prefix}Content preview (first 500 chars):\n{content[:500]}{'...' if len(content) > 500 else ''}")]
            
        except Exception as e:
            raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Failed to fetch and save: {str(e)}"))
  • Helper function to generate a safe, unique filename from the URL when file_path is not provided.
    def generate_filename_from_url(url: str) -> str:
        """Generate a safe filename from URL."""
        # Extract domain and path
        parsed = urlparse(url)
        domain = parsed.netloc.replace('www.', '')
        path = parsed.path.strip('/')
        
        # Create base filename
        if path:
            # Use last part of path
            filename_base = path.split('/')[-1]
            # Remove file extension if present
            if '.' in filename_base:
                filename_base = filename_base.rsplit('.', 1)[0]
        else:
            filename_base = domain
        
        # Clean filename - remove invalid characters
        filename_base = re.sub(r'[^\w\-_.]', '_', filename_base)
        
        # Add timestamp to ensure uniqueness
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        
        return f"{filename_base}_{timestamp}.md"
  • Helper function for fetching content, preferring Jina Reader API for better markdown extraction with fallback to standard fetch_url.
    async def fetch_with_jina_fallback(
        url: str, user_agent: str, force_raw: bool = False, proxy_url: str | None = None
    ) -> Tuple[str, str]:
        """
        Fetch URL using Jina Reader API first, fallback to original fetch logic if failed.
        """
        from httpx import AsyncClient, HTTPError
        
        # Try Jina Reader API first
        jina_url = f"https://r.jina.ai/{url}"
        
        async with AsyncClient(proxies=proxy_url) as client:
            try:
                response = await client.get(
                    jina_url,
                    follow_redirects=True,
                    headers={"User-Agent": user_agent},
                    timeout=30,
                )
                
                if response.status_code == 200:
                    content = response.text
                    # Check if it's a Jina error response
                    try:
                        error_data = json.loads(content)
                        if "code" in error_data and error_data.get("data") is None:
                            # This is an error response, fallback to original logic
                            raise Exception(f"Jina API error: {error_data.get('message', 'Unknown error')}")
                    except json.JSONDecodeError:
                        # Not JSON, assume it's valid content
                        pass
                    
                    # Jina Reader already returns markdown content
                    return content, "Content fetched via Jina Reader API:\n"
                    
            except Exception:
                # Jina failed, fallback to original logic
                pass
        
        # Fallback to original fetch logic
        return await fetch_url(url, user_agent, force_raw, proxy_url)
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It effectively describes key behaviors: the two-step fetch process (Jina Reader API with fallback to standard fetch), file saving location (configured working directory), and automatic filename generation. However, it does not mention error handling, rate limits, authentication needs, or file format details, leaving some behavioral aspects uncovered.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is appropriately sized and front-loaded: the first sentence clearly states the core functionality, followed by details on the fetch process and file handling. Every sentence adds value without redundancy, making it efficient and well-structured for quick understanding.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (fetching and saving with fallback logic), no annotations, and no output schema, the description does a good job covering the main operations and parameters. However, it lacks details on error responses, output format, or potential side effects, which would enhance completeness for a tool with mutation (file creation) and external dependencies.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all parameters. The description adds marginal value by explaining the fallback mechanism and auto-generation of filenames, but does not provide additional semantic details beyond what the schema specifies for 'url', 'file_path', or 'raw'. The baseline score of 3 is appropriate as the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Fetches a URL from the internet... and saves the content to a file.' It specifies the verb (fetch and save), resource (URL content), and distinguishes from the sibling 'fetch' tool by explicitly mentioning the saving functionality. The description is specific and avoids tautology.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides clear context for when to use this tool: for fetching URLs and saving content to files. It implicitly contrasts with the sibling 'fetch' tool by emphasizing the saving aspect, but does not explicitly state when to choose one over the other or mention any exclusions. The guidance is useful but lacks explicit alternatives or when-not-to-use details.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/LangGPT/context-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server