11"""Stocks tools for the quantflow MCP server."""
22
33from datetime import timedelta
4+ from typing import cast
45
56import pandas as pd
7+ from ccy import period as to_period
8+ from ccy .tradingcentres import prevbizday
9+ from fluid .utils .data import compact_dict
610from mcp .server .fastmcp import FastMCP
711
812from quantflow .utils .dates import utcnow
@@ -17,7 +21,7 @@ async def stock_indices() -> str:
1721 """List available stock market indices."""
1822 async with tool .fmp () as client :
1923 data = await client .indices ()
20- return tool . rich ( pd .DataFrame (data ))
24+ return pd .DataFrame (data ). to_csv ( index = False )
2125
2226 @mcp .tool ()
2327 async def stock_search (query : str ) -> str :
@@ -30,7 +34,7 @@ async def stock_search(query: str) -> str:
3034 data = await client .search (query )
3135
3236 df = pd .DataFrame (data , columns = ["symbol" , "name" , "currency" , "stockExchange" ])
33- return f"Search results for ' { query } ': \n { df .to_string (index = False )} "
37+ return df .to_csv (index = False )
3438
3539 @mcp .tool ()
3640 async def stock_profile (symbol : str ) -> str :
@@ -62,7 +66,7 @@ async def stock_prices(symbol: str, frequency: str = "") -> str:
6266 if df .empty :
6367 return f"No price data for { symbol } "
6468 df = df [["date" , "open" , "high" , "low" , "close" , "volume" ]].sort_values ("date" )
65- return f"Prices for { symbol } : \n { df .tail ( 50 ). to_string ( index = False )} "
69+ return df .to_csv ( index = False )
6670
6771 @mcp .tool ()
6872 async def sector_performance (period : str = "1d" ) -> str :
@@ -71,10 +75,6 @@ async def sector_performance(period: str = "1d") -> str:
7175 Args:
7276 period: Time period - 1d, 1w, 1m, 3m, 6m, 1y (default: 1d)
7377 """
74- from ccy import period as to_period
75- from ccy .tradingcentres import prevbizday
76- from fluid .utils .data import compact_dict
77-
7878 async with tool .fmp () as client :
7979 to_date = utcnow ().date ()
8080 if period != "1d" :
@@ -89,16 +89,11 @@ async def sector_performance(period: str = "1d") -> str:
8989 pe = await client .sector_pe (
9090 params = compact_dict (date = prevbizday (to_date , 0 ).isoformat ())
9191 )
92-
93- from typing import cast
94-
95- import pandas as pd
96-
9792 spd = cast (dict , sp )
9893 pes = {k ["sector" ]: round (float (k ["pe" ]), 3 ) for k in pe if k ["sector" ] in spd }
9994 rows = [
10095 {"sector" : k , "performance" : float (v ), "pe" : pes .get (k , float ("nan" ))}
10196 for k , v in spd .items ()
10297 ]
10398 df = pd .DataFrame (rows ).sort_values ("performance" , ascending = False )
104- return f"Sector performance ( { period } ): \n { df .to_string (index = False )} "
99+ return df .to_csv (index = False )
0 commit comments