arcade-mcp/toolkits/search/arcade_search/tools/google.py
Eric Gustin 53fa083efd
Add initial X toolkit, remove Github toolkit, rename math toolkit (#52)
* Renamed `arcade_arithmetic` to `arcade_math`
* Deleted `arcade_github` toolkit for the next release. This will be
reintroduced later.
* Added 5 tools to `arcade_x` toolkit
- post_tweet
- delete_tweet_by_id
- search_recent_tweets_by_username
- search_recent_tweets_by_keywords
- lookup_single_user_by_username
2024-09-23 13:42:22 -07:00

35 lines
969 B
Python

import json
import os
import serpapi
from typing import Annotated, Any, Optional
from arcade.sdk import tool
@tool
async def search_google(
query: Annotated[str, "Search query"],
n_results: Annotated[int, "Number of results to retrieve"] = 5,
) -> str:
"""Search Google using SerpAPI and return organic search results."""
api_key = get_secret("SERP_API_KEY")
if not api_key:
raise ValueError("SERP_API_KEY is not set")
client = serpapi.Client(api_key=api_key)
params = {"engine": "google", "q": query}
search = client.search(params)
results = search.as_dict()
organic_results = results.get("organic_results", [])
return json.dumps(organic_results[:n_results])
def get_secret(name: str, default: Optional[Any] = None) -> Any:
secret = os.getenv(name)
if secret is None:
if default is not None:
return default
raise ValueError(f"Secret {name} is not set.")
return secret