Skip to content

Fix: handle client-side meta refresh redirects #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 22, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions mcpdoc/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""MCP Llms-txt server for docs."""

import os
from urllib.parse import urlparse
import re
from urllib.parse import urlparse, urljoin

import httpx
from markdownify import markdownify
Expand Down Expand Up @@ -228,7 +229,8 @@ def list_doc_sources() -> str:

@server.tool(description=fetch_docs_description)
async def fetch_docs(url: str) -> str:
nonlocal domains
nonlocal domains, follow_redirects
url = url.strip()
# Handle local file paths (either as file:// URLs or direct filesystem paths)
if not _is_http_or_https(url):
abs_path = _normalize_path(url)
Expand All @@ -255,7 +257,33 @@ async def fetch_docs(url: str) -> str:
try:
response = await httpx_client.get(url, timeout=timeout)
response.raise_for_status()
return markdownify(response.text)
content = response.text

if follow_redirects:
# Check for meta refresh tag which indicates a client-side redirect
match = re.search(
r'<meta http-equiv="refresh" content="[^;]+;\s*url=([^"]+)"',
content,
re.IGNORECASE,
)

if match:
redirect_url = match.group(1)
new_url = urljoin(str(response.url), redirect_url)

if "*" not in domains and not any(
new_url.startswith(domain) for domain in domains
):
return (
"Error: Redirect URL not allowed. Must start with one of the following domains: "
+ ", ".join(domains)
)

response = await httpx_client.get(new_url, timeout=timeout)
response.raise_for_status()
content = response.text

return markdownify(content)
except (httpx.HTTPStatusError, httpx.RequestError) as e:
return f"Encountered an HTTP error: {str(e)}"

Expand Down
Loading