Spaces:
Sleeping
Sleeping
Commit
·
b86e5c9
1
Parent(s):
6d531e9
feat: switch web client to Google Custom Search
Browse files
backend/api/mcp_clients/web_client.py
CHANGED
|
@@ -6,24 +6,32 @@ load_dotenv()
|
|
| 6 |
|
| 7 |
class WebClient:
|
| 8 |
"""
|
| 9 |
-
Communicates with the
|
| 10 |
"""
|
| 11 |
|
| 12 |
def __init__(self):
|
| 13 |
-
self.
|
| 14 |
-
self.
|
|
|
|
| 15 |
|
| 16 |
async def search(self, query: str):
|
| 17 |
"""
|
| 18 |
-
Sends the query to
|
| 19 |
"""
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
try:
|
| 22 |
async with httpx.AsyncClient() as client:
|
| 23 |
-
response = await client.
|
| 24 |
self.search_endpoint,
|
| 25 |
-
|
| 26 |
-
"
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
)
|
| 29 |
|
|
@@ -31,7 +39,15 @@ class WebClient:
|
|
| 31 |
return []
|
| 32 |
|
| 33 |
data = response.json()
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
except Exception as e:
|
| 37 |
print("Web Client Error:", e)
|
|
|
|
| 6 |
|
| 7 |
class WebClient:
|
| 8 |
"""
|
| 9 |
+
Communicates with the Google Custom Search API.
|
| 10 |
"""
|
| 11 |
|
| 12 |
def __init__(self):
|
| 13 |
+
self.api_key = os.getenv("GOOGLE_SEARCH_API_KEY")
|
| 14 |
+
self.cx_id = os.getenv("GOOGLE_SEARCH_CX_ID")
|
| 15 |
+
self.search_endpoint = "https://www.googleapis.com/customsearch/v1"
|
| 16 |
|
| 17 |
async def search(self, query: str):
|
| 18 |
"""
|
| 19 |
+
Sends the query to Google Custom Search and returns search results.
|
| 20 |
"""
|
| 21 |
|
| 22 |
+
if not self.api_key or not self.cx_id:
|
| 23 |
+
print("Web Client Error: Google Custom Search credentials not configured.")
|
| 24 |
+
return []
|
| 25 |
+
|
| 26 |
try:
|
| 27 |
async with httpx.AsyncClient() as client:
|
| 28 |
+
response = await client.get(
|
| 29 |
self.search_endpoint,
|
| 30 |
+
params={
|
| 31 |
+
"key": self.api_key,
|
| 32 |
+
"cx": self.cx_id,
|
| 33 |
+
"q": query,
|
| 34 |
+
"num": 5,
|
| 35 |
}
|
| 36 |
)
|
| 37 |
|
|
|
|
| 39 |
return []
|
| 40 |
|
| 41 |
data = response.json()
|
| 42 |
+
items = data.get("items", [])
|
| 43 |
+
return [
|
| 44 |
+
{
|
| 45 |
+
"title": item.get("title"),
|
| 46 |
+
"link": item.get("link"),
|
| 47 |
+
"snippet": item.get("snippet"),
|
| 48 |
+
}
|
| 49 |
+
for item in items
|
| 50 |
+
]
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
print("Web Client Error:", e)
|
backend/mcp_server/server.py
CHANGED
|
@@ -25,8 +25,10 @@ if not logger.handlers:
|
|
| 25 |
"[%(asctime)s] %(levelname)s %(name)s - %(message)s",
|
| 26 |
datefmt="%Y-%m-%d %H:%M:%S",
|
| 27 |
)
|
|
|
|
| 28 |
handler.setFormatter(formatter)
|
| 29 |
logger.addHandler(handler)
|
|
|
|
| 30 |
logger.setLevel(logging.INFO)
|
| 31 |
|
| 32 |
|
|
|
|
| 25 |
"[%(asctime)s] %(levelname)s %(name)s - %(message)s",
|
| 26 |
datefmt="%Y-%m-%d %H:%M:%S",
|
| 27 |
)
|
| 28 |
+
print("formatter", formatter)
|
| 29 |
handler.setFormatter(formatter)
|
| 30 |
logger.addHandler(handler)
|
| 31 |
+
print("logger", logger)
|
| 32 |
logger.setLevel(logging.INFO)
|
| 33 |
|
| 34 |
|