Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,394 +1,357 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
"
|
| 36 |
-
|
| 37 |
-
"
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"""
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
result =
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
return
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
if
|
| 147 |
-
return {"error":
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
"""
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
yield f"❌ Error: {error_detail}\n\n{traceback.format_exc()[:500]}"
|
| 359 |
-
|
| 360 |
-
# ========== Gradio 界面(极简版)==========
|
| 361 |
-
with gr.Blocks(title="Financial AI Assistant") as demo:
|
| 362 |
-
gr.Markdown("# 💬 Financial AI Assistant")
|
| 363 |
-
|
| 364 |
-
chat = gr.ChatInterface(
|
| 365 |
-
fn=chatbot_response,
|
| 366 |
-
examples=[
|
| 367 |
-
"What's Apple's latest revenue and profit?",
|
| 368 |
-
"Show me NVIDIA's 3-year financial trends",
|
| 369 |
-
"How is Tesla's stock performing today?",
|
| 370 |
-
"Get the latest market news about crypto",
|
| 371 |
-
"Compare Microsoft's latest earnings with its current stock price",
|
| 372 |
-
],
|
| 373 |
-
chatbot=gr.Chatbot(height=600),
|
| 374 |
-
)
|
| 375 |
-
|
| 376 |
-
# 启动应用
|
| 377 |
-
if __name__ == "__main__":
|
| 378 |
-
import sys
|
| 379 |
-
|
| 380 |
-
# 修复 asyncio 事件循环问题
|
| 381 |
-
if sys.platform == 'linux':
|
| 382 |
-
try:
|
| 383 |
-
import asyncio
|
| 384 |
-
asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())
|
| 385 |
-
except:
|
| 386 |
-
pass
|
| 387 |
-
|
| 388 |
-
demo.launch(
|
| 389 |
-
server_name="0.0.0.0",
|
| 390 |
-
server_port=7860,
|
| 391 |
-
show_error=True,
|
| 392 |
-
ssr_mode=False,
|
| 393 |
-
quiet=False
|
| 394 |
-
)
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SEC Financial Data MCP Server with Gradio UI
|
| 3 |
+
Provides standard MCP service + Web interface for testing
|
| 4 |
+
"""
|
| 5 |
+
import os
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import json
|
| 8 |
+
from fastapi import FastAPI, Request
|
| 9 |
+
from fastapi.responses import JSONResponse
|
| 10 |
+
from mcp.server.fastmcp import FastMCP
|
| 11 |
+
from edgar_client import EdgarDataClient
|
| 12 |
+
from financial_analyzer import FinancialAnalyzer
|
| 13 |
+
|
| 14 |
+
# Initialize FastMCP server
|
| 15 |
+
mcp = FastMCP("sec-financial-data")
|
| 16 |
+
|
| 17 |
+
# Initialize EDGAR clients
|
| 18 |
+
edgar_client = EdgarDataClient(
|
| 19 |
+
user_agent="Juntao Peng Financial Report Metrics App (jtyxabc@gmail.com)"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
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 |
+
"""Search for a company by name in SEC EDGAR database."""
|
| 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 |
+
|
| 33 |
+
@mcp.tool()
|
| 34 |
+
def get_company_info(cik: str) -> dict:
|
| 35 |
+
"""Get detailed company information."""
|
| 36 |
+
result = edgar_client.get_company_info(cik)
|
| 37 |
+
return result if result else {"error": f"No company found with CIK: {cik}"}
|
| 38 |
+
|
| 39 |
+
@mcp.tool()
|
| 40 |
+
def get_company_filings(cik: str, form_types: list[str] | None = None) -> dict:
|
| 41 |
+
"""Get list of company SEC filings."""
|
| 42 |
+
form_types_tuple = tuple(form_types) if form_types else None
|
| 43 |
+
result = edgar_client.get_company_filings(cik, form_types_tuple)
|
| 44 |
+
if result:
|
| 45 |
+
limited_result = result[:20]
|
| 46 |
+
return {
|
| 47 |
+
"total": len(result),
|
| 48 |
+
"returned": len(limited_result),
|
| 49 |
+
"filings": limited_result
|
| 50 |
+
}
|
| 51 |
+
return {"error": f"No filings found for CIK: {cik}"}
|
| 52 |
+
|
| 53 |
+
@mcp.tool()
|
| 54 |
+
def get_financial_data(cik: str, period: str) -> dict:
|
| 55 |
+
"""Get financial data for a specific period."""
|
| 56 |
+
result = edgar_client.get_financial_data_for_period(cik, period)
|
| 57 |
+
return result if result and "period" in result else {"error": f"No financial data found for CIK: {cik}, Period: {period}"}
|
| 58 |
+
|
| 59 |
+
@mcp.tool()
|
| 60 |
+
def extract_financial_metrics(cik: str, years: int = 3) -> dict:
|
| 61 |
+
"""Extract comprehensive financial metrics for multiple years."""
|
| 62 |
+
if years < 1 or years > 10:
|
| 63 |
+
return {"error": "Years parameter must be between 1 and 10"}
|
| 64 |
+
|
| 65 |
+
metrics = financial_analyzer.extract_financial_metrics(cik, years)
|
| 66 |
+
if metrics:
|
| 67 |
+
formatted = financial_analyzer.format_financial_data(metrics)
|
| 68 |
+
return {"periods": len(formatted), "data": formatted}
|
| 69 |
+
return {"error": f"No financial metrics extracted for CIK: {cik}"}
|
| 70 |
+
|
| 71 |
+
@mcp.tool()
|
| 72 |
+
def get_latest_financial_data(cik: str) -> dict:
|
| 73 |
+
"""Get the most recent financial data available."""
|
| 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 |
+
"""Advanced search supporting both company name and CIK code."""
|
| 80 |
+
result = financial_analyzer.search_company(company_input)
|
| 81 |
+
return result if not result.get("error") else {"error": result["error"]}
|
| 82 |
+
|
| 83 |
+
# Gradio wrapper functions (添加调试和超时处理)
|
| 84 |
+
def gradio_search_company(company_name: str):
|
| 85 |
+
"""Gradio wrapper for search_company"""
|
| 86 |
+
if not company_name or not company_name.strip():
|
| 87 |
+
return json.dumps({"error": "Company name cannot be empty"}, indent=2)
|
| 88 |
+
try:
|
| 89 |
+
import sys
|
| 90 |
+
print(f"[DEBUG] Searching company: {company_name.strip()}", file=sys.stderr)
|
| 91 |
+
result = search_company(company_name.strip())
|
| 92 |
+
print(f"[DEBUG] Search result type: {type(result)}", file=sys.stderr)
|
| 93 |
+
print(f"[DEBUG] Search result: {result}", file=sys.stderr)
|
| 94 |
+
# Ensure result is a dict
|
| 95 |
+
if not isinstance(result, dict):
|
| 96 |
+
result = {"error": f"Unexpected result type: {type(result)}"}
|
| 97 |
+
return json.dumps(result, indent=2)
|
| 98 |
+
except TimeoutError as e:
|
| 99 |
+
return json.dumps({"error": f"Request timeout: {str(e)}"}, indent=2)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
import traceback
|
| 102 |
+
traceback.print_exc()
|
| 103 |
+
return json.dumps({"error": f"Exception: {str(e)}", "type": str(type(e))}, indent=2)
|
| 104 |
+
|
| 105 |
+
def gradio_get_company_info(cik: str):
|
| 106 |
+
"""Gradio wrapper for get_company_info"""
|
| 107 |
+
if not cik or not cik.strip():
|
| 108 |
+
return json.dumps({"error": "CIK cannot be empty"}, indent=2)
|
| 109 |
+
try:
|
| 110 |
+
import sys
|
| 111 |
+
print(f"[DEBUG] Getting company info for CIK: {cik.strip()}", file=sys.stderr)
|
| 112 |
+
result = get_company_info(cik.strip())
|
| 113 |
+
print(f"[DEBUG] Company info result: {result}", file=sys.stderr)
|
| 114 |
+
if not isinstance(result, dict):
|
| 115 |
+
result = {"error": f"Unexpected result type: {type(result)}"}
|
| 116 |
+
return json.dumps(result, indent=2)
|
| 117 |
+
except TimeoutError as e:
|
| 118 |
+
return json.dumps({"error": f"Request timeout: {str(e)}"}, indent=2)
|
| 119 |
+
except Exception as e:
|
| 120 |
+
import traceback
|
| 121 |
+
traceback.print_exc()
|
| 122 |
+
return json.dumps({"error": f"Exception: {str(e)}", "type": str(type(e))}, indent=2)
|
| 123 |
+
|
| 124 |
+
def gradio_extract_metrics(cik: str, years: float):
|
| 125 |
+
"""Gradio wrapper for extract_financial_metrics"""
|
| 126 |
+
if not cik or not cik.strip():
|
| 127 |
+
return json.dumps({"error": "CIK cannot be empty"}, indent=2)
|
| 128 |
+
try:
|
| 129 |
+
import sys
|
| 130 |
+
years_int = int(years)
|
| 131 |
+
print(f"[DEBUG] Extracting metrics for CIK: {cik.strip()}, Years: {years_int}", file=sys.stderr)
|
| 132 |
+
result = extract_financial_metrics(cik.strip(), years_int)
|
| 133 |
+
print(f"[DEBUG] Extract metrics result: {result}", file=sys.stderr)
|
| 134 |
+
if not isinstance(result, dict):
|
| 135 |
+
result = {"error": f"Unexpected result type: {type(result)}"}
|
| 136 |
+
return json.dumps(result, indent=2)
|
| 137 |
+
except TimeoutError as e:
|
| 138 |
+
return json.dumps({"error": f"Request timeout: {str(e)}"}, indent=2)
|
| 139 |
+
except Exception as e:
|
| 140 |
+
import traceback
|
| 141 |
+
traceback.print_exc()
|
| 142 |
+
return json.dumps({"error": f"Exception: {str(e)}", "type": str(type(e))}, indent=2)
|
| 143 |
+
|
| 144 |
+
def gradio_get_latest(cik: str):
|
| 145 |
+
"""Gradio wrapper for get_latest_financial_data"""
|
| 146 |
+
if not cik or not cik.strip():
|
| 147 |
+
return json.dumps({"error": "CIK cannot be empty"}, indent=2)
|
| 148 |
+
try:
|
| 149 |
+
import sys
|
| 150 |
+
print(f"[DEBUG] Getting latest data for CIK: {cik.strip()}", file=sys.stderr)
|
| 151 |
+
result = get_latest_financial_data(cik.strip())
|
| 152 |
+
print(f"[DEBUG] Latest data result: {result}", file=sys.stderr)
|
| 153 |
+
if not isinstance(result, dict):
|
| 154 |
+
result = {"error": f"Unexpected result type: {type(result)}"}
|
| 155 |
+
return json.dumps(result, indent=2)
|
| 156 |
+
except TimeoutError as e:
|
| 157 |
+
return json.dumps({"error": f"Request timeout: {str(e)}"}, indent=2)
|
| 158 |
+
except Exception as e:
|
| 159 |
+
import traceback
|
| 160 |
+
traceback.print_exc()
|
| 161 |
+
return json.dumps({"error": f"Exception: {str(e)}", "type": str(type(e))}, indent=2)
|
| 162 |
+
|
| 163 |
+
# Mount MCP HTTP endpoints for client integration
|
| 164 |
+
app = FastAPI()
|
| 165 |
+
|
| 166 |
+
@app.post("/mcp")
|
| 167 |
+
@app.get("/mcp")
|
| 168 |
+
@app.post("/sse")
|
| 169 |
+
@app.get("/sse")
|
| 170 |
+
async def mcp_http_endpoint(request: Request):
|
| 171 |
+
"""MCP HTTP endpoint - handle both GET and POST (accessible via /mcp or /sse)"""
|
| 172 |
+
try:
|
| 173 |
+
# For POST requests, get JSON body
|
| 174 |
+
if request.method == "POST":
|
| 175 |
+
body = await request.json()
|
| 176 |
+
|
| 177 |
+
# Handle JSON-RPC request
|
| 178 |
+
if body.get("method") == "tools/list":
|
| 179 |
+
# List all tools
|
| 180 |
+
tools = []
|
| 181 |
+
for tool_name, tool_func in mcp._tool_manager._tools.items():
|
| 182 |
+
tools.append({
|
| 183 |
+
"name": tool_name,
|
| 184 |
+
"description": tool_func.__doc__ or "",
|
| 185 |
+
"inputSchema": {
|
| 186 |
+
"type": "object",
|
| 187 |
+
"properties": {},
|
| 188 |
+
"required": []
|
| 189 |
+
}
|
| 190 |
+
})
|
| 191 |
+
|
| 192 |
+
return JSONResponse({
|
| 193 |
+
"jsonrpc": "2.0",
|
| 194 |
+
"id": body.get("id"),
|
| 195 |
+
"result": {"tools": tools}
|
| 196 |
+
})
|
| 197 |
+
|
| 198 |
+
elif body.get("method") == "tools/call":
|
| 199 |
+
# Call a specific tool
|
| 200 |
+
tool_name = body.get("params", {}).get("name")
|
| 201 |
+
arguments = body.get("params", {}).get("arguments", {})
|
| 202 |
+
|
| 203 |
+
# Directly call the tool functions we defined
|
| 204 |
+
tool_map = {
|
| 205 |
+
"search_company": search_company,
|
| 206 |
+
"get_company_info": get_company_info,
|
| 207 |
+
"get_company_filings": get_company_filings,
|
| 208 |
+
"get_financial_data": get_financial_data,
|
| 209 |
+
"extract_financial_metrics": extract_financial_metrics,
|
| 210 |
+
"get_latest_financial_data": get_latest_financial_data,
|
| 211 |
+
"advanced_search_company": advanced_search_company
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
if tool_name in tool_map:
|
| 215 |
+
tool_func = tool_map[tool_name]
|
| 216 |
+
result = tool_func(**arguments)
|
| 217 |
+
|
| 218 |
+
return JSONResponse({
|
| 219 |
+
"jsonrpc": "2.0",
|
| 220 |
+
"id": body.get("id"),
|
| 221 |
+
"result": {
|
| 222 |
+
"content": [{
|
| 223 |
+
"type": "text",
|
| 224 |
+
"text": json.dumps(result)
|
| 225 |
+
}]
|
| 226 |
+
}
|
| 227 |
+
})
|
| 228 |
+
else:
|
| 229 |
+
return JSONResponse({
|
| 230 |
+
"jsonrpc": "2.0",
|
| 231 |
+
"id": body.get("id"),
|
| 232 |
+
"error": {
|
| 233 |
+
"code": -32601,
|
| 234 |
+
"message": f"Tool not found: {tool_name}"
|
| 235 |
+
}
|
| 236 |
+
})
|
| 237 |
+
|
| 238 |
+
else:
|
| 239 |
+
return JSONResponse({
|
| 240 |
+
"jsonrpc": "2.0",
|
| 241 |
+
"id": body.get("id"),
|
| 242 |
+
"error": {
|
| 243 |
+
"code": -32601,
|
| 244 |
+
"message": f"Method not found: {body.get('method')}"
|
| 245 |
+
}
|
| 246 |
+
})
|
| 247 |
+
|
| 248 |
+
else:
|
| 249 |
+
# GET request - return info
|
| 250 |
+
return JSONResponse({
|
| 251 |
+
"service": "SEC Financial Data MCP Server",
|
| 252 |
+
"protocol": "MCP 2024-11-05",
|
| 253 |
+
"transport": "HTTP",
|
| 254 |
+
"status": "online"
|
| 255 |
+
})
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
return JSONResponse(
|
| 259 |
+
content={"error": str(e), "type": "server_error"},
|
| 260 |
+
status_code=500
|
| 261 |
+
)
|
| 262 |
+
|
| 263 |
+
# Create Gradio interface
|
| 264 |
+
with gr.Blocks(title="SEC Financial Data MCP Server", theme=gr.themes.Soft()) as demo:
|
| 265 |
+
gr.Markdown("""
|
| 266 |
+
# 📊 SEC Financial Data MCP Server
|
| 267 |
+
|
| 268 |
+
Access real-time SEC EDGAR financial data via Model Context Protocol
|
| 269 |
+
|
| 270 |
+
**MCP Endpoint:** Use `mcp_server_fastmcp.py` for standard MCP client connections
|
| 271 |
+
""")
|
| 272 |
+
|
| 273 |
+
with gr.Tab("🔍 Search Company"):
|
| 274 |
+
gr.Markdown("### Search for a company by name")
|
| 275 |
+
with gr.Row():
|
| 276 |
+
with gr.Column():
|
| 277 |
+
company_input = gr.Textbox(label="Company Name", placeholder="Tesla", value="Tesla")
|
| 278 |
+
search_btn = gr.Button("Search", variant="primary")
|
| 279 |
+
with gr.Column():
|
| 280 |
+
search_output = gr.Code(label="Result", language="json", lines=15)
|
| 281 |
+
search_btn.click(gradio_search_company, inputs=company_input, outputs=search_output)
|
| 282 |
+
|
| 283 |
+
with gr.Tab("ℹ️ Company Info"):
|
| 284 |
+
gr.Markdown("### Get detailed company information")
|
| 285 |
+
with gr.Row():
|
| 286 |
+
with gr.Column():
|
| 287 |
+
cik_input = gr.Textbox(label="Company CIK", placeholder="0001318605", value="0001318605")
|
| 288 |
+
info_btn = gr.Button("Get Info", variant="primary")
|
| 289 |
+
with gr.Column():
|
| 290 |
+
info_output = gr.Code(label="Result", language="json", lines=15)
|
| 291 |
+
info_btn.click(gradio_get_company_info, inputs=cik_input, outputs=info_output)
|
| 292 |
+
|
| 293 |
+
with gr.Tab("📈 Financial Metrics"):
|
| 294 |
+
gr.Markdown("### Extract multi-year financial metrics ⭐")
|
| 295 |
+
with gr.Row():
|
| 296 |
+
with gr.Column():
|
| 297 |
+
metrics_cik = gr.Textbox(label="Company CIK", placeholder="0001318605", value="0001318605")
|
| 298 |
+
metrics_years = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Years")
|
| 299 |
+
metrics_btn = gr.Button("Extract Metrics", variant="primary")
|
| 300 |
+
with gr.Column():
|
| 301 |
+
metrics_output = gr.Code(label="Result", language="json", lines=20)
|
| 302 |
+
metrics_btn.click(gradio_extract_metrics, inputs=[metrics_cik, metrics_years], outputs=metrics_output)
|
| 303 |
+
|
| 304 |
+
with gr.Tab("🆕 Latest Data"):
|
| 305 |
+
gr.Markdown("### Get latest financial data")
|
| 306 |
+
with gr.Row():
|
| 307 |
+
with gr.Column():
|
| 308 |
+
latest_cik = gr.Textbox(label="Company CIK", placeholder="0001318605", value="0001318605")
|
| 309 |
+
latest_btn = gr.Button("Get Latest", variant="primary")
|
| 310 |
+
with gr.Column():
|
| 311 |
+
latest_output = gr.Code(label="Result", language="json", lines=15)
|
| 312 |
+
latest_btn.click(gradio_get_latest, inputs=latest_cik, outputs=latest_output)
|
| 313 |
+
|
| 314 |
+
with gr.Tab("📖 Documentation"):
|
| 315 |
+
gr.Markdown("""
|
| 316 |
+
## 🛠️ Available Tools (7)
|
| 317 |
+
|
| 318 |
+
1. **search_company** - Search by company name
|
| 319 |
+
2. **get_company_info** - Get company details by CIK
|
| 320 |
+
3. **get_company_filings** - List SEC filings
|
| 321 |
+
4. **get_financial_data** - Get specific period data
|
| 322 |
+
5. **extract_financial_metrics** ⭐ - Multi-year trends
|
| 323 |
+
6. **get_latest_financial_data** - Latest snapshot
|
| 324 |
+
7. **advanced_search_company** - Flexible search
|
| 325 |
+
|
| 326 |
+
## 🔗 MCP Integration
|
| 327 |
+
|
| 328 |
+
For MCP client integration, run:
|
| 329 |
+
```bash
|
| 330 |
+
python mcp_server_fastmcp.py
|
| 331 |
+
```
|
| 332 |
+
|
| 333 |
+
Then configure your MCP client (e.g., Claude Desktop):
|
| 334 |
+
```json
|
| 335 |
+
{
|
| 336 |
+
"mcpServers": {
|
| 337 |
+
"sec-financial-data": {
|
| 338 |
+
"command": "python",
|
| 339 |
+
"args": ["path/to/mcp_server_fastmcp.py"]
|
| 340 |
+
}
|
| 341 |
+
}
|
| 342 |
+
}
|
| 343 |
+
```
|
| 344 |
+
|
| 345 |
+
## 📊 Data Source
|
| 346 |
+
|
| 347 |
+
- **SEC EDGAR API** - Official SEC data
|
| 348 |
+
- **Financial Statements** - 10-K, 10-Q, 20-F forms
|
| 349 |
+
- **XBRL Data** - Structured metrics
|
| 350 |
+
""")
|
| 351 |
+
|
| 352 |
+
# Mount Gradio app to FastAPI
|
| 353 |
+
app = gr.mount_gradio_app(app, demo, path="/")
|
| 354 |
+
|
| 355 |
+
if __name__ == "__main__":
|
| 356 |
+
import uvicorn
|
| 357 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|