Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ravenwits authored Jan 15, 2025
2 parents e29b4ff + 53f4a23 commit 2d7c52e
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 96 deletions.
24 changes: 12 additions & 12 deletions src/fetch/src/mcp_server_fetch/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ async def check_may_autonomously_fetch_url(url: str, user_agent: str) -> None:
)
except HTTPError:
raise McpError(ErrorData(
INTERNAL_ERROR,
f"Failed to fetch robots.txt {robot_txt_url} due to a connection issue",
code=INTERNAL_ERROR,
message=f"Failed to fetch robots.txt {robot_txt_url} due to a connection issue",
))
if response.status_code in (401, 403):
raise McpError(ErrorData(
INTERNAL_ERROR,
f"When fetching robots.txt ({robot_txt_url}), received status {response.status_code} so assuming that autonomous fetching is not allowed, the user can try manually fetching by using the fetch prompt",
code=INTERNAL_ERROR,
message=f"When fetching robots.txt ({robot_txt_url}), received status {response.status_code} so assuming that autonomous fetching is not allowed, the user can try manually fetching by using the fetch prompt",
))
elif 400 <= response.status_code < 500:
return
Expand All @@ -98,8 +98,8 @@ async def check_may_autonomously_fetch_url(url: str, user_agent: str) -> None:
robot_parser = Protego.parse(processed_robot_txt)
if not robot_parser.can_fetch(str(url), user_agent):
raise McpError(ErrorData(
INTERNAL_ERROR,
f"The sites robots.txt ({robot_txt_url}), specifies that autonomous fetching of this page is not allowed, "
code=INTERNAL_ERROR,
message=f"The sites robots.txt ({robot_txt_url}), specifies that autonomous fetching of this page is not allowed, "
f"<useragent>{user_agent}</useragent>\n"
f"<url>{url}</url>"
f"<robots>\n{robot_txt}\n</robots>\n"
Expand All @@ -125,11 +125,11 @@ async def fetch_url(
timeout=30,
)
except HTTPError as e:
raise McpError(ErrorData(INTERNAL_ERROR, f"Failed to fetch {url}: {e!r}"))
raise McpError(ErrorData(code=INTERNAL_ERROR, message=f"Failed to fetch {url}: {e!r}"))
if response.status_code >= 400:
raise McpError(ErrorData(
INTERNAL_ERROR,
f"Failed to fetch {url} - status code {response.status_code}",
code=INTERNAL_ERROR,
message=f"Failed to fetch {url} - status code {response.status_code}",
))

page_raw = response.text
Expand Down Expand Up @@ -222,11 +222,11 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
try:
args = Fetch(**arguments)
except ValueError as e:
raise McpError(ErrorData(INVALID_PARAMS, str(e)))
raise McpError(ErrorData(code=INVALID_PARAMS, message=str(e)))

url = str(args.url)
if not url:
raise McpError(ErrorData(INVALID_PARAMS, "URL is required"))
raise McpError(ErrorData(code=INVALID_PARAMS, message="URL is required"))

if not ignore_robots_txt:
await check_may_autonomously_fetch_url(url, user_agent_autonomous)
Expand Down Expand Up @@ -254,7 +254,7 @@ async def call_tool(name, arguments: dict) -> list[TextContent]:
@server.get_prompt()
async def get_prompt(name: str, arguments: dict | None) -> GetPromptResult:
if not arguments or "url" not in arguments:
raise McpError(ErrorData(INVALID_PARAMS, "URL is required"))
raise McpError(ErrorData(code=INVALID_PARAMS, message="URL is required"))

url = arguments["url"]

Expand Down
Loading

0 comments on commit 2d7c52e

Please sign in to comment.