fetch_json
Retrieve and parse JSON data from any web URL, then return it in a readable, formatted structure. Use this tool to access and interpret JSON content directly from online sources.
Instructions
Fetch JSON from a URL, parse it, and return it formatted.
This tool allows Claude to retrieve and parse JSON data from any accessible web URL.
The JSON is prettified for better readability.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| headers | No | Additional headers to send with the request | |
| timeout | No | Request timeout in seconds | |
| url | Yes | The URL to fetch JSON from |
Implementation Reference
- src/url_fetch_mcp/main.py:114-171 (handler)The fetch_json tool handler: decorated with @app.tool() for automatic registration and schema generation. Fetches JSON from URL using httpx, validates content-type loosely, parses with response.json(), formats with json.dumps(indent=2), handles errors, and integrates with MCP Context for logging.@app.tool() async def fetch_json( url: Annotated[AnyUrl, Field(description="The URL to fetch JSON from")], headers: Annotated[ Optional[Dict[str, str]], Field(description="Additional headers to send with the request") ] = None, timeout: Annotated[int, Field(description="Request timeout in seconds")] = 10, ctx: Context = None, ) -> str: """Fetch JSON from a URL, parse it, and return it formatted. This tool allows Claude to retrieve and parse JSON data from any accessible web URL. The JSON is prettified for better readability. """ if ctx: await ctx.info(f"Fetching JSON from URL: {url}") request_headers = { "User-Agent": "URL-Fetch-MCP/0.1.0", "Accept": "application/json", } if headers: request_headers.update(headers) async with httpx.AsyncClient(follow_redirects=True, timeout=timeout) as client: try: response = await client.get(str(url), headers=request_headers) response.raise_for_status() content_type = response.headers.get("content-type", "") if "json" not in content_type and not content_type.startswith("application/json"): # Try to parse anyway, but warn if ctx: await ctx.warning(f"URL did not return JSON content-type (got: {content_type})") # Parse and format JSON try: json_data = response.json() formatted_json = json.dumps(json_data, indent=2) if ctx: await ctx.info(f"Successfully fetched and parsed JSON ({len(formatted_json)} bytes)") return formatted_json except json.JSONDecodeError as e: error_message = f"Failed to parse JSON from response: {str(e)}" if ctx: await ctx.error(error_message) return error_message except Exception as e: error_message = f"Error fetching JSON from URL {url}: {str(e)}" if ctx: await ctx.error(error_message) return error_message