Skip to content

Commit 5552af1

Browse files
committed
format with black
1 parent b6710da commit 5552af1

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/fetch/src/mcp_server_fetch/server.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ async def fetch_url(url: str, user_agent: str, force_raw: bool = False) -> (str,
9898
async with AsyncClient() as client:
9999
try:
100100
response = await client.get(
101-
url, follow_redirects=True, headers={"User-Agent": user_agent}, timeout=30,
101+
url,
102+
follow_redirects=True,
103+
headers={"User-Agent": user_agent},
104+
timeout=30,
102105
)
103106
except HTTPError as e:
104107
raise McpError(INTERNAL_ERROR, f"Failed to fetch {url}: {e!r}")
@@ -111,19 +114,30 @@ async def fetch_url(url: str, user_agent: str, force_raw: bool = False) -> (str,
111114
page_raw = response.text
112115

113116
content_type = response.headers.get("content-type", "")
114-
is_page_html = "<html" in page_raw[:100] or "text/html" in content_type or not content_type
117+
is_page_html = (
118+
"<html" in page_raw[:100] or "text/html" in content_type or not content_type
119+
)
115120

116121
if is_page_html and not force_raw:
117122
return extract_content_from_html(page_raw), ""
118123

119-
return page_raw, f"Content type {content_type} cannot be simplified to markdown, but here is the raw content:\n"
124+
return (
125+
page_raw,
126+
f"Content type {content_type} cannot be simplified to markdown, but here is the raw content:\n",
127+
)
120128

121129

122130
class Fetch(BaseModel):
123131
url: str = Field(..., description="URL to fetch")
124132
max_length: int = Field(5000, description="Maximum number of characters to return.")
125-
start_index: int = Field(0, description="On return output starting at this character index, useful if a previous fetch was truncated and more context is required.")
126-
raw: bool = Field(False, description="Get the actual HTML content if the requested page, without simplification.")
133+
start_index: int = Field(
134+
0,
135+
description="On return output starting at this character index, useful if a previous fetch was truncated and more context is required.",
136+
)
137+
raw: bool = Field(
138+
False,
139+
description="Get the actual HTML content if the requested page, without simplification.",
140+
)
127141

128142

129143
async def serve(
@@ -173,7 +187,9 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
173187
if not ignore_robots_txt:
174188
await check_may_autonomously_fetch_url(url, user_agent_autonomous)
175189

176-
content, prefix = await fetch_url(url, user_agent_autonomous, force_raw=args.raw)
190+
content, prefix = await fetch_url(
191+
url, user_agent_autonomous, force_raw=args.raw
192+
)
177193
if len(content) > args.max_length:
178194
content = content[args.start_index : args.start_index + args.max_length]
179195
content += f"\n\n<error>Content truncated. Call the fetch tool with a start_index of {args.start_index + args.max_length} to get more content.</error>"

0 commit comments

Comments
 (0)