Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -23,10 +23,32 @@ financial_analyzer = FinancialAnalyzer(
|
|
| 23 |
user_agent="Juntao Peng Financial Report Metrics App (jtyxabc@gmail.com)"
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# Define MCP tools
|
| 27 |
@mcp.tool()
|
| 28 |
def search_company(company_name: str) -> dict:
|
| 29 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
result = edgar_client.search_company_by_name(company_name)
|
| 31 |
return result if result else {"error": f"No company found with name: {company_name}"}
|
| 32 |
|
|
@@ -58,7 +80,32 @@ def get_financial_data(cik: str, period: str) -> dict:
|
|
| 58 |
|
| 59 |
@mcp.tool()
|
| 60 |
def extract_financial_metrics(cik: str, years: int = 3) -> dict:
|
| 61 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
if years < 1 or years > 10:
|
| 63 |
return {"error": "Years parameter must be between 1 and 10"}
|
| 64 |
|
|
@@ -70,13 +117,51 @@ def extract_financial_metrics(cik: str, years: int = 3) -> dict:
|
|
| 70 |
|
| 71 |
@mcp.tool()
|
| 72 |
def get_latest_financial_data(cik: str) -> dict:
|
| 73 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
result = financial_analyzer.get_latest_financial_data(cik)
|
| 75 |
return result if result and "period" in result else {"error": f"No latest financial data found for CIK: {cik}"}
|
| 76 |
|
| 77 |
@mcp.tool()
|
| 78 |
def advanced_search_company(company_input: str) -> dict:
|
| 79 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
result = financial_analyzer.search_company(company_input)
|
| 81 |
return result if not result.get("error") else {"error": result["error"]}
|
| 82 |
|
|
|
|
| 23 |
user_agent="Juntao Peng Financial Report Metrics App (jtyxabc@gmail.com)"
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Define MCP tools with detailed descriptions for LLM
|
| 27 |
@mcp.tool()
|
| 28 |
def search_company(company_name: str) -> dict:
|
| 29 |
+
"""
|
| 30 |
+
**STEP 1: START HERE** - Search for a company by name to get its CIK code.
|
| 31 |
+
|
| 32 |
+
IMPORTANT: You MUST call this tool FIRST when the user mentions a company name.
|
| 33 |
+
The CIK code returned by this tool is REQUIRED for all other financial data tools.
|
| 34 |
+
|
| 35 |
+
When to use:
|
| 36 |
+
- User mentions a company name ("Tesla", "Apple", "Microsoft")
|
| 37 |
+
- Before calling ANY other tool that requires a CIK parameter
|
| 38 |
+
- User asks about a company without providing CIK
|
| 39 |
+
|
| 40 |
+
Workflow:
|
| 41 |
+
1. User asks: "Show me Tesla's financials"
|
| 42 |
+
2. YOU MUST: Call search_company("Tesla") FIRST
|
| 43 |
+
3. Get CIK from result: {"cik": "0001318605", ...}
|
| 44 |
+
4. THEN use that CIK in other tools: extract_financial_metrics("0001318605")
|
| 45 |
+
|
| 46 |
+
Args:
|
| 47 |
+
company_name: Company name (e.g., "Tesla", "Apple Inc", "Microsoft")
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
dict with cik (REQUIRED for other tools), name, ticker, sic, sic_description
|
| 51 |
+
"""
|
| 52 |
result = edgar_client.search_company_by_name(company_name)
|
| 53 |
return result if result else {"error": f"No company found with name: {company_name}"}
|
| 54 |
|
|
|
|
| 80 |
|
| 81 |
@mcp.tool()
|
| 82 |
def extract_financial_metrics(cik: str, years: int = 3) -> dict:
|
| 83 |
+
"""
|
| 84 |
+
**RECOMMENDED TOOL** - Extract multi-year financial data (annual + quarterly).
|
| 85 |
+
|
| 86 |
+
CRITICAL: This tool requires a valid 10-digit CIK code.
|
| 87 |
+
If you don't have the CIK, you MUST call search_company() or advanced_search_company() FIRST!
|
| 88 |
+
|
| 89 |
+
When to use:
|
| 90 |
+
- User asks about financial trends, growth, or multi-period analysis
|
| 91 |
+
- "Show me [company]'s financials" (most common use case)
|
| 92 |
+
- Need comprehensive financial overview
|
| 93 |
+
|
| 94 |
+
CORRECT Workflow:
|
| 95 |
+
1. User: "Show me Tesla's financial metrics"
|
| 96 |
+
2. YOU: Call search_company("Tesla") → get CIK "0001318605"
|
| 97 |
+
3. YOU: Call extract_financial_metrics("0001318605", 3)
|
| 98 |
+
|
| 99 |
+
WRONG Workflow (DO NOT DO THIS):
|
| 100 |
+
❌ extract_financial_metrics("0001198872") ← Wrong CIK! Will fail!
|
| 101 |
+
|
| 102 |
+
Args:
|
| 103 |
+
cik: 10-digit CIK code (e.g., "0001318605") from search_company()
|
| 104 |
+
years: Number of years (1-10, default 3)
|
| 105 |
+
|
| 106 |
+
Returns:
|
| 107 |
+
Multi-year financial data with annual and quarterly metrics
|
| 108 |
+
"""
|
| 109 |
if years < 1 or years > 10:
|
| 110 |
return {"error": "Years parameter must be between 1 and 10"}
|
| 111 |
|
|
|
|
| 117 |
|
| 118 |
@mcp.tool()
|
| 119 |
def get_latest_financial_data(cik: str) -> dict:
|
| 120 |
+
"""
|
| 121 |
+
Get the most recent financial snapshot (latest annual report only).
|
| 122 |
+
|
| 123 |
+
CRITICAL: This tool requires a valid 10-digit CIK code.
|
| 124 |
+
If you don't have the CIK, you MUST call search_company() or advanced_search_company() FIRST!
|
| 125 |
+
|
| 126 |
+
When to use:
|
| 127 |
+
- User asks for "latest" or "most recent" financial data
|
| 128 |
+
- Quick snapshot of current financial status
|
| 129 |
+
|
| 130 |
+
CORRECT Workflow:
|
| 131 |
+
1. User: "What's Tesla's latest financial data?"
|
| 132 |
+
2. YOU: Call search_company("Tesla") → get CIK "0001318605"
|
| 133 |
+
3. YOU: Call get_latest_financial_data("0001318605")
|
| 134 |
+
|
| 135 |
+
WRONG Workflow (DO NOT DO THIS):
|
| 136 |
+
❌ get_latest_financial_data("0001198872") ← Wrong CIK! Will fail!
|
| 137 |
+
|
| 138 |
+
Args:
|
| 139 |
+
cik: 10-digit CIK code (e.g., "0001318605") from search_company()
|
| 140 |
+
|
| 141 |
+
Returns:
|
| 142 |
+
Latest annual financial data
|
| 143 |
+
"""
|
| 144 |
result = financial_analyzer.get_latest_financial_data(cik)
|
| 145 |
return result if result and "period" in result else {"error": f"No latest financial data found for CIK: {cik}"}
|
| 146 |
|
| 147 |
@mcp.tool()
|
| 148 |
def advanced_search_company(company_input: str) -> dict:
|
| 149 |
+
"""
|
| 150 |
+
**ALTERNATIVE TO search_company** - Flexible search supporting company name or CIK.
|
| 151 |
+
|
| 152 |
+
Use this when:
|
| 153 |
+
- You need to find a company's CIK before calling other tools
|
| 154 |
+
- User provides either a company name OR a CIK code
|
| 155 |
+
- You want a single tool that handles both search types
|
| 156 |
+
|
| 157 |
+
This tool returns the SAME format as search_company(), including the CIK needed for other tools.
|
| 158 |
+
|
| 159 |
+
Args:
|
| 160 |
+
company_input: Company name OR CIK code
|
| 161 |
+
|
| 162 |
+
Returns:
|
| 163 |
+
dict with cik (REQUIRED for other tools), name, ticker
|
| 164 |
+
"""
|
| 165 |
result = financial_analyzer.search_company(company_input)
|
| 166 |
return result if not result.get("error") else {"error": result["error"]}
|
| 167 |
|