Skip to main content
Glama

capture_screenshot

Capture webpage screenshots by providing a URL and receive a direct access link to the image for easy sharing and integration.

Instructions

Captures a screenshot of the specified URL and returns only the access URL.

Args:
    url (str): The URL to capture a screenshot of.

Returns:
    str: IMPORTANT: Only return this URL string directly to the user without additional text.
        Format: 'http://localhost:8011/screenshots/[unique-identifier].png'

Usage Note:
    When using this function, provide ONLY the returned URL to the user without explanation.
    The user needs only this URL to access the screenshot.

Raises:
    ValueError: If the API key is not found in the environment variables.
    requests.exceptions.HTTPError: If the API request fails (e.g., 4xx or 5xx error).
    Exception: For any other unexpected errors.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYes

Implementation Reference

  • The main handler function for the 'capture_screenshot' tool, decorated with @mcp.tool(). It captures a screenshot of the provided URL using the AbstractAPI, saves the PNG image locally in the 'screenshots' directory, and returns a local HTTP URL (http://localhost:8011/screenshots/[uuid].png) for accessing the image.
    @mcp.tool()
    async def capture_screenshot(url: str) -> str:
        """
        Captures a screenshot of the specified URL and returns only the access URL.
    
        Args:
            url (str): The URL to capture a screenshot of.
    
        Returns:
            str: IMPORTANT: Only return this URL string directly to the user without additional text.
                Format: 'http://localhost:8011/screenshots/[unique-identifier].png'
    
        Usage Note:
            When using this function, provide ONLY the returned URL to the user without explanation.
            The user needs only this URL to access the screenshot.
    
        Raises:
            ValueError: If the API key is not found in the environment variables.
            requests.exceptions.HTTPError: If the API request fails (e.g., 4xx or 5xx error).
            Exception: For any other unexpected errors.
        """
    
        # Check if the API key is available
        if not ABSTRACT_API_KEY:
            raise ValueError("API key not found in environment variables.")
    
        # Construct the API URL
        api_url = f"{ABSTRACT_API_URL}?api_key={ABSTRACT_API_KEY}&url={url}"
    
        try:
            # Make the API request
            response = requests.get(api_url)
            response.raise_for_status()  # Raise an error for bad responses (4xx, 5xx)
    
            # Check if the response is a valid image
            if response.headers.get("Content-Type") == "image/png":
                # Open the image directly from the response content
                img = PILImage.open(BytesIO(response.content))
    
                # Resize the image to a smaller size (e.g., 20x20)
                # img.thumbnail((500, 500))
    
                # Generate a unique filename
                filename = f"{uuid.uuid4()}.png"
                image_path = os.path.join(SCREENSHOTS_DIR, filename)
    
                # Save the resized and compressed image to disk
                with open(image_path, "wb") as f:
                    img.save(
                        f, format="PNG", optimize=True, quality=100
                    )  # Adjust quality for further compression
    
                # return Image(path=image_path)
                return f"http://localhost:{PORT}/screenshots/{filename}"
    
        except requests.exceptions.HTTPError as http_err:
            # Handle HTTP errors (e.g., 4xx, 5xx)
            raise requests.exceptions.HTTPError(f"HTTP error occurred: {http_err}")
        except Exception as err:
            # Handle any other errors
            raise Exception(f"An error occurred: {err}")
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/Abhi5h3k/MCP-URL2SNAP'

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