arcade-mcp/toolkits/search/arcade_search/tools/google_finance.py
Eric Gustin 99ff11d30e
Various Search Toolkits (#285)
1. Add the following tools:
* Google Finance
    - get_stock_summary
    - get_stock_historical_data
* Google Flights
    - search_roundtrip_flights
    - search_one_way_flights
* Google Hotels
    - search_hotels
    
2. Add some common helper functions for serpAPI tools.
2025-03-14 14:23:14 -07:00

86 lines
2.7 KiB
Python

from typing import Annotated, Any
from arcade.sdk import ToolContext, tool
from arcade_search.enums import GoogleFinanceWindow
from arcade_search.utils import call_serpapi, prepare_params
@tool(requires_secrets=["SERP_API_KEY"])
async def get_stock_summary(
context: ToolContext,
ticker_symbol: Annotated[
str,
"The stock ticker to get summary for. For example, 'GOOG' is the ticker symbol for Google",
],
exchange_identifier: Annotated[
str,
"The exchange identifier. This part indicates the market where the "
"stock is traded. For example, 'NASDAQ', 'NYSE', 'TSE', 'LSE', etc.",
],
) -> Annotated[dict[str, Any], "Summary of the stock's recent performance"]:
"""Retrieve the summary information for a given stock ticker using the Google Finance API.
Gets the stock's current price as well as price movement from the most recent trading day.
"""
# Prepare the request
query = (
f"{ticker_symbol.upper()}:{exchange_identifier.upper()}"
if exchange_identifier
else ticker_symbol.upper()
)
params = prepare_params("google_finance", q=query)
# Execute the request
results = call_serpapi(context, params)
# Parse the results
summary: dict = results.get("summary", {})
return summary
@tool(requires_secrets=["SERP_API_KEY"])
async def get_stock_historical_data(
context: ToolContext,
ticker_symbol: Annotated[
str,
"The stock ticker to get summary for. For example, 'GOOG' is the ticker symbol for Google",
],
exchange_identifier: Annotated[
str,
"The exchange identifier. This part indicates the market where the "
"stock is traded. For example, 'NASDAQ', 'NYSE', 'TSE', 'LSE', etc.",
],
window: Annotated[
GoogleFinanceWindow, "Time window for the graph data. Defaults to 1 month"
] = GoogleFinanceWindow.ONE_MONTH,
) -> Annotated[
dict[str, Any],
"A stock's price and volume data at a specific time interval over a specified time window",
]:
"""Fetch historical stock price data over a specified time window
Returns a stock's price and volume data over a specified time window
"""
# Prepare the request
query = (
f"{ticker_symbol.upper()}:{exchange_identifier.upper()}"
if exchange_identifier
else ticker_symbol.upper()
)
params = prepare_params("google_finance", q=query, window=window.value)
# Execute the request
results = call_serpapi(context, params)
# Parse the results
data = {
"summary": results.get("summary", {}),
"graph": results.get("graph", []),
}
key_events = results.get("key_events")
if key_events:
data["key_events"] = key_events
return data