Search with Bocha Web Search and get enhanced search details from billions of web documents, including page titles, urls, summaries, site names, site icons, publication dates, image links, and more. Args: query: Search query (required) freshness: The time range for the searc
(
query: str, freshness: str = "noLimit", count: int = 10
)
| 41 | |
| 42 | @server.tool() |
| 43 | async def bocha_web_search( |
| 44 | query: str, freshness: str = "noLimit", count: int = 10 |
| 45 | ) -> str: |
| 46 | """Search with Bocha Web Search and get enhanced search details from billions of web documents, |
| 47 | including page titles, urls, summaries, site names, site icons, publication dates, image links, and more. |
| 48 | |
| 49 | Args: |
| 50 | query: Search query (required) |
| 51 | freshness: The time range for the search results. (Available options YYYY-MM-DD, YYYY-MM-DD..YYYY-MM-DD, noLimit, oneYear, oneMonth, oneWeek, oneDay. Default is noLimit) |
| 52 | count: Number of results (1-50, default 10) |
| 53 | """ |
| 54 | # Get API key from environment |
| 55 | boch_api_key = os.environ.get("BOCHA_API_KEY", "") |
| 56 | |
| 57 | if not boch_api_key: |
| 58 | return ( |
| 59 | "Error: Bocha API key is not configured. Please set the " |
| 60 | "BOCHA_API_KEY environment variable." |
| 61 | ) |
| 62 | |
| 63 | # Endpoint |
| 64 | endpoint = "https://api.bochaai.com/v1/web-search?utm_source=bocha-mcp-local" |
| 65 | |
| 66 | try: |
| 67 | payload = { |
| 68 | "query": query, |
| 69 | "summary": True, |
| 70 | "freshness": freshness, |
| 71 | "count": count, |
| 72 | } |
| 73 | |
| 74 | headers = { |
| 75 | "Authorization": f"Bearer {boch_api_key}", |
| 76 | "Content-Type": "application/json", |
| 77 | } |
| 78 | |
| 79 | async with httpx.AsyncClient() as client: |
| 80 | response = await client.post( |
| 81 | endpoint, headers=headers, json=payload, timeout=10.0 |
| 82 | ) |
| 83 | |
| 84 | response.raise_for_status() |
| 85 | resp = response.json() |
| 86 | if "data" not in resp: |
| 87 | return "Search error." |
| 88 | |
| 89 | data = resp["data"] |
| 90 | |
| 91 | if "webPages" not in data: |
| 92 | return "No results found." |
| 93 | |
| 94 | results = [] |
| 95 | for result in data["webPages"]["value"]: |
| 96 | results.append( |
| 97 | f"Title: {result['name']}\n" |
| 98 | f"URL: {result['url']}\n" |
| 99 | f"Description: {result['summary']}\n" |
| 100 | f"Published date: {result['datePublished']}\n" |