Skip to content

adding pdf support #28

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
22 changes: 20 additions & 2 deletions mcpdoc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from markdownify import markdownify
from mcp.server.fastmcp import FastMCP
from typing_extensions import NotRequired, TypedDict

from pypdf import PdfReader

class DocSource(TypedDict):
"""A source of documentation for a library or a package."""
Expand All @@ -21,6 +21,17 @@ class DocSource(TypedDict):
description: NotRequired[str]
"""Description of the documentation source (optional)."""

def extract_text_from_pdf(pdf_file) -> str:
"""Extract text content from a PDF file."""
try:
reader = PdfReader(pdf_file)
text = ""
for page in reader.pages:
text += page.extract_text() + "\n"
return text.strip()
except Exception as e:
raise ValueError(f"Failed to extract text from PDF: {str(e)}")


def extract_domain(url: str) -> str:
"""Extract domain from URL.
Expand Down Expand Up @@ -213,7 +224,14 @@ async def fetch_docs(url: str) -> str:
try:
response = await httpx_client.get(url, timeout=timeout)
response.raise_for_status()
return markdownify(response.text)
if url.endswith(".txt"):
return response.text
elif url.endswith(".md"):
return markdownify(response.text)
elif url.endswith(".pdf"):
return extract_text_from_pdf(io.BytesIO(response.content))
else:
return markdownify(response.text)
except (httpx.HTTPStatusError, httpx.RequestError) as e:
return f"Encountered an HTTP error: {str(e)}"

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies = [
"httpx>=0.28.1",
"markdownify>=1.1.0",
"mcp[cli]>=1.4.1",
"pypdf>=5.4.0",
"pyyaml>=6.0.1",
]

Expand Down
14 changes: 14 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.