Skip to content

Commit 29ae979

Browse files
committed
Switch to newer serpapi client
1 parent 86987d7 commit 29ae979

File tree

3 files changed

+564
-562
lines changed

3 files changed

+564
-562
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ classifiers = [
2424
requires-python = ">=3.12"
2525
dependencies = [
2626
"fastmcp>=2.13.0.2",
27-
"google-search-results>=2.4.2",
2827
"python-dotenv>=1.0.0",
2928
"httpx>=0.25.0",
3029
"uvicorn>=0.38.0",
31-
"starlette>=0.50.0"
30+
"starlette>=0.50.0",
31+
"serpapi>=0.1.5",
3232
]
3333

3434
[project.optional-dependencies]

src/server.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import os
1111
import json
1212
from typing import Any
13-
from serpapi import SerpApiClient as SerpApiSearch
13+
import serpapi
1414
import httpx
1515
import logging
1616
from datetime import datetime
@@ -186,8 +186,7 @@ async def search(params: dict[str, Any] = {}, raw: bool = False) -> str:
186186
}
187187

188188
try:
189-
search_client = SerpApiSearch(search_params)
190-
data = search_client.get_dict()
189+
data = serpapi.search(search_params).as_dict()
191190

192191
# Return raw JSON if requested
193192
if raw:
@@ -240,15 +239,15 @@ async def search(params: dict[str, Any] = {}, raw: bool = False) -> str:
240239
else:
241240
return "No results found for the given query. Try adjusting your search parameters or engine."
242241

243-
except httpx.HTTPStatusError as e:
244-
if e.response.status_code == 429:
242+
except serpapi.exceptions.HTTPError as e:
243+
if "429" in str(e):
245244
return "Error: Rate limit exceeded. Please try again later."
246-
elif e.response.status_code == 401:
245+
elif "401" in str(e):
247246
return "Error: Invalid SerpApi API key. Check your API key in the path or Authorization header."
248-
elif e.response.status_code == 403:
247+
elif "403" in str(e):
249248
return "Error: SerpApi API key forbidden. Verify your subscription and key validity."
250249
else:
251-
return f"Error: {e.response.status_code} - {e.response.text}"
250+
return f"Error: {str(e)}"
252251
except Exception as e:
253252
return f"Error: {str(e)}"
254253

0 commit comments

Comments
 (0)