generate_favicon_from_url
Download an image from a URL and generate a complete favicon set, including multiple sizes, ICO files, Apple touch icons, and a manifest.json for web applications.
Instructions
Download an image from a URL and generate a complete favicon set.
Args:
image_url: URL of the image to download.
output_path: Directory where favicon files will be generated.
Returns:
A message describing the generated files and output directory.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| image_url | Yes | ||
| output_path | Yes |
Implementation Reference
- src/auto_favicon/server.py:152-187 (handler)The main handler function implementing the 'generate_favicon_from_url' MCP tool. It validates the output path, downloads the image using aiohttp, calls create_favicon_set to generate favicons, and returns a success message with generated files list.@mcp.tool() async def generate_favicon_from_url(image_url: str, output_path: str) -> str: """ Download an image from a URL and generate a complete favicon set. Args: image_url: URL of the image to download. output_path: Absolute path to the directory where favicon files will be generated. Returns: A message describing the generated files and output directory. """ try: # Validate absolute path for output directory output_path_obj = validate_absolute_path(output_path, "output_path") # Check if output directory can be created try: output_path_obj.mkdir(parents=True, exist_ok=True) except Exception as e: return f"Error: Cannot create output directory {output_path}: {str(e)}" async with aiohttp.ClientSession() as session: async with session.get(image_url) as response: response.raise_for_status() image_data = await response.read() result = create_favicon_set(image_data, str(output_path_obj)) return ( f"Successfully downloaded image from {image_url} and generated favicon set!\n\n" f"Output directory: {result['output_directory']}\n" f"Generated files:\n" + "\n".join(f"- {f}" for f in result['generated_files']) ) except ValueError as e: return f"Error: {str(e)}" except Exception as e: return f"Error generating favicon from URL: {str(e)}"
- src/auto_favicon/server.py:38-107 (helper)Supporting helper function that generates the complete favicon set (PNG sizes, ICO, Apple touch icons, manifest.json) from raw image bytes using Pillow.def create_favicon_set(image_data: bytes, output_dir: str) -> Dict[str, Any]: """Create a complete favicon set from image data.""" # Write image data to a temp file with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file: temp_file.write(image_data) temp_file.flush() temp_path = temp_file.name try: with Image.open(temp_path) as img: # Convert to RGBA if needed if img.mode != 'RGBA': img = img.convert('RGBA') output_path = Path(output_dir) output_path.mkdir(parents=True, exist_ok=True) generated_files = [] # Generate PNG favicons for size in FAVICON_SIZES: resized = img.resize((size, size), Image.Resampling.LANCZOS) filename = f"favicon-{size}x{size}.png" filepath = output_path / filename resized.save(filepath, "PNG") generated_files.append(str(filepath)) # Generate ICO file ico_images = [img.resize((size, size), Image.Resampling.LANCZOS) for size in ICO_SIZES] ico_path = output_path / "favicon.ico" ico_images[0].save(ico_path, format='ICO', sizes=[(size, size) for size in ICO_SIZES]) generated_files.append(str(ico_path)) # Generate Apple touch icons for size in APPLE_SIZES: resized = img.resize((size, size), Image.Resampling.LANCZOS) filename = f"apple-touch-icon-{size}x{size}.png" filepath = output_path / filename resized.save(filepath, "PNG") generated_files.append(str(filepath)) # Generate manifest.json manifest = { "name": "Favicon App", "short_name": "Favicon", "description": "Generated favicon application", "start_url": "/", "display": "standalone", "background_color": "#ffffff", "theme_color": "#000000", "icons": [ { "src": f"favicon-{size}x{size}.png", "sizes": f"{size}x{size}", "type": "image/png" } for size in FAVICON_SIZES ] } manifest_path = output_path / "manifest.json" with open(manifest_path, 'w') as f: json.dump(manifest, f, indent=2) generated_files.append(str(manifest_path)) return { "generated_files": generated_files, "manifest": manifest, "output_directory": str(output_path) } finally: os.unlink(temp_path)
- src/auto_favicon/server.py:28-36 (helper)Utility helper to validate that the output_path is an absolute path.def validate_absolute_path(path: str, path_type: str) -> Path: """Validate that a path is absolute and exists (for files) or can be created (for directories).""" path_obj = Path(path) if not path_obj.is_absolute(): raise ValueError(f"{path_type} must be an absolute path: {path}") return path_obj
- src/auto_favicon/server.py:152-152 (registration)The @mcp.tool() decorator registers the generate_favicon_from_url function as an MCP tool.@mcp.tool()