Skip to main content
Glama
example.py5.5 kB
#!/usr/bin/env python3 """ Usage example for MCP Fetch as Markdown tool This script demonstrates how to use the fetch_page functionality with different configurations and URLs. """ import asyncio import sys from main import handle_fetch_page async def example_basic_fetch(): """Basic example of fetching a page as markdown.""" print("=== Basic Fetch Example ===") # Simple fetch with default settings args = { "url": "https://httpbin.org/html", "include_links": True, "include_images": False, "timeout": 10 } try: results = await handle_fetch_page(args) print(f"✓ Successfully fetched {len(results)} content blocks") # Display metadata if results: metadata = results[0].text print("\nMetadata:") print(metadata[:200] + "..." if len(metadata) > 200 else metadata) # Display content preview if len(results) > 1: content = results[1].text print(f"\nContent Preview ({len(content)} characters):") print(content[:300] + "..." if len(content) > 300 else content) except Exception as e: print(f"✗ Error: {e}") async def example_with_images(): """Example including images in the markdown output.""" print("\n=== Fetch with Images Example ===") args = { "url": "https://httpbin.org/html", "include_links": True, "include_images": True, # Include image references "timeout": 15 } try: results = await handle_fetch_page(args) print(f"✓ Fetched with images: {len(results)} blocks") if len(results) > 1: content = results[1].text # Check if any image references were found if "![" in content or "](http" in content: print("✓ Image references found in markdown") else: print("ℹ No image references found in this page") except Exception as e: print(f"✗ Error: {e}") async def example_error_handling(): """Example demonstrating error handling.""" print("\n=== Error Handling Examples ===") # Test cases that should fail gracefully test_cases = [ { "name": "Invalid URL", "args": {"url": "not-a-valid-url"} }, { "name": "Non-existent domain", "args": {"url": "https://this-domain-does-not-exist-12345.com"} }, { "name": "Timeout test", "args": {"url": "https://httpbin.org/delay/20", "timeout": 2} } ] for test_case in test_cases: print(f"\nTesting: {test_case['name']}") try: results = await handle_fetch_page(test_case['args']) # Check if error was handled gracefully if results and "Error" in results[0].text: print("✓ Error handled gracefully") print(f" {results[0].text.split('**Error:**')[1].strip()[:100]}...") else: print("? Unexpected success") except Exception as e: print(f"✓ Exception caught: {type(e).__name__}: {e}") async def example_real_website(): """Example with a real website (be respectful!).""" print("\n=== Real Website Example ===") print("Note: This example uses a real website. Please be respectful!") # Using a simple, fast website args = { "url": "https://example.com", "include_links": True, "include_images": False, "timeout": 10 } try: results = await handle_fetch_page(args) if results: print("✓ Successfully fetched real website") # Show the clean markdown conversion if len(results) > 1: content = results[1].text print(f"\nMarkdown content ({len(content)} characters):") print(content) # Analyze the content lines = content.split('\n') non_empty_lines = [line for line in lines if line.strip()] print(f"\nContent analysis:") print(f" - Total lines: {len(lines)}") print(f" - Non-empty lines: {len(non_empty_lines)}") print(f" - Headers: {len([line for line in lines if line.startswith('#')])}") print(f" - Links: {content.count('[')}") except Exception as e: print(f"✗ Error fetching real website: {e}") async def main(): """Run all examples.""" print("MCP Fetch as Markdown - Usage Examples") print("=" * 50) # Run examples await example_basic_fetch() await example_with_images() await example_error_handling() # Ask before hitting real website response = input("\nRun real website example? (y/N): ").lower().strip() if response == 'y': await example_real_website() print("\n" + "=" * 50) print("Examples completed!") print("\nUsage in MCP client:") print("Add this to your MCP client configuration:") print(""" { "mcpServers": { "fetch-as-markdown": { "command": "uv", "args": ["run", "mcp-fetch-as-markdown"], "cwd": "/path/to/mcp-fetch-as-markdown" } } } """) if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: print("\nExamples interrupted by user") except Exception as e: print(f"\nExample failed: {e}") sys.exit(1)

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/bhubbb/mcp-fetch-as-markdown'

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