Skip to content

Commit cb3a7f4

Browse files
authored
Merge pull request #1 from pietz/feat/brave-retry
retries, v bump
2 parents 2130c4d + 6e49412 commit cb3a7f4

File tree

3 files changed

+38
-26
lines changed

3 files changed

+38
-26
lines changed

mcp_web_tools/search.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,42 @@ async def brave_search(query: str, limit: int = 10) -> dict | None:
2222
headers = {"X-Subscription-Token": api_key}
2323
params = {"q": query, "count": limit}
2424

25-
try:
26-
async with httpx.AsyncClient() as client:
27-
r = await client.get(url, headers=headers, params=params, timeout=10)
28-
r.raise_for_status()
29-
results = r.json()["web"]["results"]
30-
return {
31-
"provider": "brave",
32-
"results": [
33-
{
34-
"title": x["title"],
35-
"url": x["url"],
36-
"description": x.get("description", ""),
37-
}
38-
for x in results
39-
],
40-
}
41-
except httpx.HTTPStatusError as e:
42-
logger.warning(
43-
f"Brave Search API returned status code {e.response.status_code}"
44-
)
45-
except httpx.TimeoutException:
46-
logger.warning("Brave Search API request timed out")
47-
except Exception as e:
48-
logger.warning(f"Error using Brave Search: {str(e)}")
25+
import asyncio
26+
27+
attempts = 3
28+
for attempt in range(1, attempts + 1):
29+
try:
30+
async with httpx.AsyncClient() as client:
31+
r = await client.get(url, headers=headers, params=params, timeout=10)
32+
r.raise_for_status()
33+
results = r.json()["web"]["results"]
34+
return {
35+
"provider": "brave",
36+
"results": [
37+
{
38+
"title": x["title"],
39+
"url": x["url"],
40+
"description": x.get("description", ""),
41+
}
42+
for x in results
43+
],
44+
}
45+
except httpx.HTTPStatusError as e:
46+
status = e.response.status_code if e.response is not None else None
47+
if status == 429 and attempt < attempts:
48+
logger.info("Brave rate limited (429); retrying in 1s (%d/%d)", attempt, attempts)
49+
await asyncio.sleep(1)
50+
continue
51+
logger.warning(
52+
f"Brave Search API returned status code {status}"
53+
)
54+
break
55+
except httpx.TimeoutException:
56+
logger.warning("Brave Search API request timed out")
57+
break
58+
except Exception as e:
59+
logger.warning(f"Error using Brave Search: {str(e)}")
60+
break
4961

5062
return None
5163

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "mcp-web-tools"
3-
version = "0.7.7"
3+
version = "0.7.8"
44
description = "A powerful MCP server to equip LLMs with web access, search, and content extraction capabilities"
55
readme = "README.md"
66
authors = [

uv.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)