nothingworry's picture
feat(web-search): use Google Custom Search for live web results
29116ed
raw
history blame
1.03 kB
from fastapi import APIRouter, Header, HTTPException, Query
from api.mcp_clients.web_client import WebClient
router = APIRouter()
web_client = WebClient()
@router.post("/web/search")
async def web_search(
query: str,
max_results: int = Query(5, ge=1, le=10),
region: str = Query("us"),
x_tenant_id: str = Header(None),
):
"""
Perform a live Google Custom Search query for the tenant.
"""
if not x_tenant_id:
raise HTTPException(status_code=400, detail="Missing tenant ID")
try:
results = await web_client.search(query, max_results=max_results, region=region)
return {
"tenant_id": x_tenant_id,
"query": query,
"results": results,
"metadata": {"max_results": max_results, "region": region},
}
except RuntimeError as exc:
raise HTTPException(status_code=500, detail=str(exc)) from exc
except Exception as exc:
raise HTTPException(status_code=500, detail="Web search failed") from exc