get_income_statement
Retrieve income statements for a specific stock symbol, with options for yearly, quarterly, or trailing frequency, to analyze company financial performance via Yahoo Finance data.
Instructions
Get income statement for a given stock symbol.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| freq | No | At what frequency to get cashflow statements. Defaults to "yearly". Valid freqencies: "yearly", "quarterly", "trainling" | |
| symbol | Yes | Stock symbol in Yahoo Finance format. |
Input Schema (JSON Schema)
{
"properties": {
"freq": {
"description": "At what frequency to get cashflow statements. Defaults to \"yearly\". Valid freqencies: \"yearly\", \"quarterly\", \"trainling\"",
"type": "string"
},
"symbol": {
"description": "Stock symbol in Yahoo Finance format.",
"type": "string"
}
},
"required": [
"symbol"
],
"type": "object"
}
Implementation Reference
- src/mcp_yahoo_finance/server.py:135-154 (handler)The core handler function that executes the tool logic: fetches income statement data using yfinance Ticker.get_income_stmt(), formats columns as dates, and returns JSON string.def get_income_statement( self, symbol: str, freq: Literal["yearly", "quarterly", "trainling"] = "yearly" ) -> str: """Get income statement for a given stock symbol. Args: symbol (str): Stock symbol in Yahoo Finance format. freq (str): At what frequency to get cashflow statements. Defaults to "yearly". Valid freqencies: "yearly", "quarterly", "trainling" """ stock = Ticker(ticker=symbol, session=self.session) income_statement = stock.get_income_stmt(freq=freq, pretty=True) if isinstance(income_statement, pd.DataFrame): income_statement.columns = [ str(col.date()) for col in income_statement.columns ] return f"{income_statement.to_json()}" return f"{income_statement}"
- src/mcp_yahoo_finance/server.py:215-215 (registration)Registration of the tool in the list_tools() handler using generate_tool to create the Tool object with inferred schema.generate_tool(yf.get_income_statement),
- src/mcp_yahoo_finance/server.py:238-240 (handler)Dispatch handler in call_tool() that invokes the specific tool handler with arguments and formats response as TextContent.case "get_income_statement": price = yf.get_income_statement(**args) return [TextContent(type="text", text=price)]
- src/mcp_yahoo_finance/utils.py:31-65 (helper)Helper utility that generates the tool schema (inputSchema) by inspecting function signature, type annotations, and parsing docstring for descriptions.def generate_tool(func: Any) -> Tool: """Generates a tool schema from a Python function.""" signature = inspect.signature(func) docstring = inspect.getdoc(func) or "" param_descriptions = parse_docstring(docstring) schema = { "name": func.__name__, "description": docstring.split("Args:")[0].strip(), "inputSchema": { "type": "object", "properties": {}, }, } for param_name, param in signature.parameters.items(): param_type = ( "number" if param.annotation is float else "string" if param.annotation is str else "string" ) schema["inputSchema"]["properties"][param_name] = { "type": param_type, "description": param_descriptions.get(param_name, ""), } if "required" not in schema["inputSchema"]: schema["inputSchema"]["required"] = [param_name] else: if "=" not in str(param): schema["inputSchema"]["required"].append(param_name) return Tool(**schema)
- Function signature and type annotations that define the input schema, along with docstring used for parameter descriptions.def get_income_statement( self, symbol: str, freq: Literal["yearly", "quarterly", "trainling"] = "yearly" ) -> str: """Get income statement for a given stock symbol. Args: symbol (str): Stock symbol in Yahoo Finance format. freq (str): At what frequency to get cashflow statements. Defaults to "yearly". Valid freqencies: "yearly", "quarterly", "trainling" """ stock = Ticker(ticker=symbol, session=self.session) income_statement = stock.get_income_stmt(freq=freq, pretty=True) if isinstance(income_statement, pd.DataFrame): income_statement.columns = [ str(col.date()) for col in income_statement.columns ] return f"{income_statement.to_json()}" return f"{income_statement}"