Skip to content

Commit 7f9ebca

Browse files
Refactor multi-agent portfolio collaboration notebook by removing execution counts and outputs from code cells for cleaner presentation. Update investment analysis questions to focus on current market dynamics. Enhance Yahoo Finance server functionality by adding JSON file saving capabilities for stock info, news, and option expiration dates, including schema and preview in responses.
1 parent 0d294f5 commit 7f9ebca

File tree

2 files changed

+52
-191
lines changed

2 files changed

+52
-191
lines changed

examples/agents_sdk/multi-agent-portfolio-collaboration/mcp/yahoo_finance_server.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,25 @@ def save_df_to_csv(df, base_name):
4848
df_clean.to_csv(file_path, index=False)
4949
return str(file_path), list(df_clean.columns)
5050

51+
def save_json_to_file(data, base_name):
52+
file_path = OUTPUTS_DIR / f"{base_name}.json"
53+
if file_path.exists():
54+
unique_id = uuid.uuid4().hex[:8]
55+
file_path = OUTPUTS_DIR / f"{base_name}_{unique_id}.json"
56+
with open(file_path, "w") as f:
57+
json.dump(data, f, indent=2)
58+
# Schema: for dict, top-level keys; for list, type of first element or 'list'; else type
59+
if isinstance(data, dict):
60+
schema = list(data.keys())
61+
preview = {k: data[k] for k in list(data)[:PREVIEW_ROWS]}
62+
elif isinstance(data, list):
63+
schema = [type(data[0]).__name__] if data else ["list"]
64+
preview = data[:PREVIEW_ROWS]
65+
else:
66+
schema = [type(data).__name__]
67+
preview = data
68+
return str(file_path), schema, preview
69+
5170
class FinancialType(str, Enum):
5271
income_stmt = "income_stmt"
5372
quarterly_income_stmt = "quarterly_income_stmt"
@@ -134,8 +153,13 @@ def get_stock_info_sync(ticker):
134153
logger.error(f"Company ticker {ticker} not found.")
135154
return json.dumps({"error": f"Company ticker {ticker} not found."})
136155
info = company.info
156+
file_path, schema, preview = save_json_to_file(info, f"{ticker}_stock_info")
137157
logger.info(f"Returning stock info for {ticker}")
138-
return json.dumps(info)
158+
return json.dumps({
159+
"file_path": file_path,
160+
"schema": schema,
161+
"preview": preview
162+
})
139163

140164
@yfinance_server.tool(
141165
name="get_stock_info",
@@ -173,13 +197,18 @@ def get_yahoo_finance_news_sync(ticker):
173197
description = news_item.get("content", {}).get("description", "")
174198
url = news_item.get("content", {}).get("canonicalUrl", {}).get("url", "")
175199
news_list.append(
176-
f"Title: {title}\nSummary: {summary}\nDescription: {description}\nURL: {url}"
200+
{"title": title, "summary": summary, "description": description, "url": url}
177201
)
178202
if not news_list:
179203
logger.warning(f"No news found for company with ticker {ticker}.")
180204
return json.dumps({"error": f"No news found for company that searched with {ticker} ticker."})
205+
file_path, schema, preview = save_json_to_file(news_list, f"{ticker}_news")
181206
logger.info(f"Returning news for {ticker}")
182-
return json.dumps({"news": news_list})
207+
return json.dumps({
208+
"file_path": file_path,
209+
"schema": schema,
210+
"preview": preview
211+
})
183212

184213
@yfinance_server.tool(
185214
name="get_yahoo_finance_news",
@@ -335,8 +364,14 @@ def get_option_expiration_dates_sync(ticker):
335364
if company.isin is None:
336365
logger.error(f"Company ticker {ticker} not found.")
337366
return json.dumps({"error": f"Company ticker {ticker} not found."})
367+
dates = list(company.options)
368+
file_path, schema, preview = save_json_to_file(dates, f"{ticker}_option_expiration_dates")
338369
logger.info(f"Returning option expiration dates for {ticker}")
339-
return json.dumps(list(company.options))
370+
return json.dumps({
371+
"file_path": file_path,
372+
"schema": schema,
373+
"preview": preview
374+
})
340375

341376
@yfinance_server.tool(
342377
name="get_option_expiration_dates",

0 commit comments

Comments
 (0)