Skip to content
Merged
Changes from 1 commit
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
22 changes: 20 additions & 2 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 @@ -229,6 +230,7 @@ def list_doc_sources() -> str:
@server.tool(description=fetch_docs_description)
async def fetch_docs(url: str) -> str:
nonlocal domains
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,23 @@ 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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to incorporate allowed_domains and follow redirects into this code branch

content,
re.IGNORECASE,
)

if match:
redirect_url = match.group(1)
new_url = urljoin(str(response.url), redirect_url)
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