Spaces:
Sleeping
Sleeping
File size: 21,785 Bytes
0bedf4a 505a787 0bedf4a 83b398d 0bedf4a c583058 ce94df5 83b398d ce94df5 0bedf4a 875213d 0bedf4a ce1f3b5 0bedf4a c583058 0bedf4a ce94df5 c583058 ce94df5 c583058 6d91fb6 ce94df5 6d91fb6 ce94df5 c583058 ce94df5 6d91fb6 ce94df5 c583058 ce94df5 c583058 0bedf4a 74fcd29 0bedf4a ce94df5 c583058 ce94df5 c583058 ce94df5 0bedf4a ce94df5 6d91fb6 ce94df5 0bedf4a ce94df5 0bedf4a 505a787 0bedf4a ce94df5 0bedf4a 505a787 ce94df5 505a787 0bedf4a 505a787 0bedf4a ce1f3b5 0bedf4a ce94df5 0bedf4a ce94df5 0bedf4a c583058 0bedf4a c583058 0bedf4a c583058 0bedf4a 70b4bcc 83b398d 0e0e09a 0bedf4a c583058 0bedf4a ce1f3b5 0bedf4a c583058 ce94df5 c583058 0bedf4a c583058 309ddb4 0bedf4a |
1 2 3 4 5 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 |
"""
MCP Server for Hugging Face Space Deployment
Uses Server-Sent Events (SSE) transport for remote MCP access
Fully compatible with MCP clients like Claude Desktop
Returns clean JSON without emoji formatting
"""
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import StreamingResponse, JSONResponse, HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import Dict, Any, List, Optional
import json
import asyncio
from datetime import datetime
from edgar_client import EdgarDataClient
from financial_analyzer import FinancialAnalyzer
import time
import sys
import signal
import os
from pathlib import Path
from contextlib import contextmanager
# Initialize FastAPI app
app = FastAPI(
title="SEC Financial Report MCP Server API",
description="Model Context Protocol Server for SEC EDGAR Financial Data",
version="2.3.6"
)
# Server startup time for monitoring
server_start_time = time.time()
request_count = 0
error_count = 0
# Configure CORS for remote access
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Request tracking middleware with timeout protection
@app.middleware("http")
async def track_requests(request: Request, call_next):
global request_count, error_count
request_count += 1
start_time = time.time()
try:
# Set a timeout for the entire request - increased for better reliability
response = await asyncio.wait_for(
call_next(request),
timeout=180.0 # Increased from 120 to 180 seconds (3 minutes)
)
if response.status_code >= 400:
error_count += 1
return response
except asyncio.TimeoutError:
error_count += 1
print(f"Request timeout after {time.time() - start_time:.2f}s: {request.url.path}")
return JSONResponse(
status_code=504,
content={"error": "Request timeout", "message": "The request took too long to process. Please try again."}
)
except Exception as e:
error_count += 1
print(f"Request error: {e}")
raise
# Initialize EDGAR clients
edgar_client = EdgarDataClient(
user_agent="Juntao Peng Financial Report Metrics App (jtyxabc@gmail.com)"
)
financial_analyzer = FinancialAnalyzer(
user_agent="Juntao Peng Financial Report Metrics App (jtyxabc@gmail.com)"
)
# Preload company tickers data on startup for better performance
print("[Startup] Preloading company tickers data...")
try:
edgar_client._load_company_tickers()
print("[Startup] Company tickers preloaded successfully")
except Exception as e:
print(f"[Startup] Warning: Failed to preload company tickers: {e}")
print("[Startup] Will load on first request")
# ==================== MCP Protocol Implementation ====================
class MCPRequest(BaseModel):
jsonrpc: str = "2.0"
id: Optional[Any] = None
method: str
params: Optional[Dict[str, Any]] = None
class MCPResponse(BaseModel):
jsonrpc: str = "2.0"
id: Optional[Any] = None
result: Optional[Any] = None
error: Optional[Dict[str, Any]] = None
# MCP Tools Definition
MCP_TOOLS = [
{
"name": "search_company",
"description": "Search for a company by name in SEC EDGAR database. Returns company CIK, name, and ticker symbol.",
"inputSchema": {
"type": "object",
"properties": {
"company_name": {
"type": "string",
"description": "Company name to search (e.g., 'Microsoft', 'Apple', 'Tesla')"
}
},
"required": ["company_name"]
}
},
{
"name": "get_company_info",
"description": "Get detailed company information including name, tickers, SIC code, and industry description.",
"inputSchema": {
"type": "object",
"properties": {
"cik": {
"type": "string",
"description": "Company CIK code (10-digit format, e.g., '0000789019')"
}
},
"required": ["cik"]
}
},
{
"name": "get_company_filings",
"description": "Get list of company SEC filings (10-K, 10-Q, 20-F, etc.) with filing dates and document links.",
"inputSchema": {
"type": "object",
"properties": {
"cik": {
"type": "string",
"description": "Company CIK code"
},
"form_types": {
"type": "array",
"items": {"type": "string"},
"description": "Optional: Filter by form types (e.g., ['10-K', '10-Q'])"
}
},
"required": ["cik"]
}
},
{
"name": "get_financial_data",
"description": "Get financial data for a specific period including revenue, net income, EPS, operating expenses, and cash flow.",
"inputSchema": {
"type": "object",
"properties": {
"cik": {
"type": "string",
"description": "Company CIK code"
},
"period": {
"type": "string",
"description": "Period in format 'YYYY' for annual or 'YYYYQX' for quarterly (e.g., '2024', '2024Q3')"
}
},
"required": ["cik", "period"]
}
},
{
"name": "extract_financial_metrics",
"description": "Extract comprehensive financial metrics for multiple years including both annual and quarterly data. Returns data in chronological order (newest first).",
"inputSchema": {
"type": "object",
"properties": {
"cik": {
"type": "string",
"description": "Company CIK code"
},
"years": {
"type": "integer",
"description": "Number of recent years to extract (1-10, default: 3)",
"minimum": 1,
"maximum": 10,
"default": 3
}
},
"required": ["cik"]
}
},
{
"name": "get_latest_financial_data",
"description": "Get the most recent financial data available for a company.",
"inputSchema": {
"type": "object",
"properties": {
"cik": {
"type": "string",
"description": "Company CIK code"
}
},
"required": ["cik"]
}
},
{
"name": "advanced_search_company",
"description": "Advanced search supporting both company name and CIK code. Automatically detects input type.",
"inputSchema": {
"type": "object",
"properties": {
"company_input": {
"type": "string",
"description": "Company name, ticker, or CIK code"
}
},
"required": ["company_input"]
}
}
]
@contextmanager
def timeout_context(seconds):
"""Context manager for timeout on Unix-like systems"""
def timeout_handler(signum, frame):
raise TimeoutError(f"Operation timeout after {seconds} seconds")
# Only works on Unix-like systems
try:
old_handler = signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(seconds)
try:
yield
finally:
signal.alarm(0)
signal.signal(signal.SIGALRM, old_handler)
except (AttributeError, ValueError):
# Windows or signal not available
yield
def execute_tool(tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:
"""Execute MCP tool and return clean JSON result with enhanced timeout protection"""
try:
# Use context manager for timeout (90 seconds per tool - increased from 60)
with timeout_context(90):
if tool_name == "search_company":
result = edgar_client.search_company_by_name(arguments["company_name"])
if result:
return {
"type": "text",
"text": json.dumps(result, ensure_ascii=False)
}
else:
return {
"type": "text",
"text": json.dumps({
"error": f"No company found with name: {arguments['company_name']}"
}, ensure_ascii=False)
}
elif tool_name == "get_company_info":
result = edgar_client.get_company_info(arguments["cik"])
if result:
return {
"type": "text",
"text": json.dumps(result, ensure_ascii=False)
}
else:
return {
"type": "text",
"text": json.dumps({
"error": f"No company found with CIK: {arguments['cik']}"
}, ensure_ascii=False)
}
elif tool_name == "get_company_filings":
form_types = arguments.get("form_types")
result = edgar_client.get_company_filings(arguments["cik"], form_types)
if result:
# Limit to 20 results
limited_result = result[:20]
return {
"type": "text",
"text": json.dumps({
"total": len(result),
"returned": len(limited_result),
"filings": limited_result
}, ensure_ascii=False)
}
else:
return {
"type": "text",
"text": json.dumps({
"error": f"No filings found for CIK: {arguments['cik']}"
}, ensure_ascii=False)
}
elif tool_name == "get_financial_data":
result = edgar_client.get_financial_data_for_period(arguments["cik"], arguments["period"])
if result and "period" in result:
return {
"type": "text",
"text": json.dumps(result, ensure_ascii=False)
}
else:
return {
"type": "text",
"text": json.dumps({
"error": f"No financial data found for CIK: {arguments['cik']}, Period: {arguments['period']}"
}, ensure_ascii=False)
}
elif tool_name == "extract_financial_metrics":
years = arguments.get("years", 3)
if years < 1 or years > 10:
return {
"type": "text",
"text": json.dumps({
"error": "Years parameter must be between 1 and 10"
}, ensure_ascii=False)
}
metrics = financial_analyzer.extract_financial_metrics(arguments["cik"], years)
if metrics:
formatted = financial_analyzer.format_financial_data(metrics)
return {
"type": "text",
"text": json.dumps({
"periods": len(formatted),
"data": formatted
}, ensure_ascii=False)
}
else:
return {
"type": "text",
"text": json.dumps({
"error": f"No financial metrics found for CIK: {arguments['cik']}"
}, ensure_ascii=False)
}
elif tool_name == "get_latest_financial_data":
result = financial_analyzer.get_latest_financial_data(arguments["cik"])
if result and "period" in result:
return {
"type": "text",
"text": json.dumps(result, ensure_ascii=False)
}
else:
return {
"type": "text",
"text": json.dumps({
"error": f"No latest financial data found for CIK: {arguments['cik']}"
}, ensure_ascii=False)
}
elif tool_name == "advanced_search_company" or tool_name == "advanced_search":
# Support both names for backward compatibility
result = financial_analyzer.search_company(arguments["company_input"])
if result.get("error"):
return {
"type": "text",
"text": json.dumps({
"error": result["error"]
}, ensure_ascii=False)
}
return {
"type": "text",
"text": json.dumps(result, ensure_ascii=False)
}
else:
return {
"type": "text",
"text": json.dumps({
"error": f"Unknown tool: {tool_name}"
}, ensure_ascii=False)
}
except Exception as e:
return {
"type": "text",
"text": json.dumps({
"error": f"Error executing {tool_name}: {str(e)}"
}, ensure_ascii=False)
}
# ==================== MCP Endpoints ====================
@app.post("/message")
async def handle_mcp_message(request: MCPRequest):
"""
Main MCP message handler
Supports: initialize, tools/list, tools/call
"""
method = request.method
params = request.params or {}
# Handle initialize
if method == "initialize":
return MCPResponse(
id=request.id,
result={
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {}
},
"serverInfo": {
"name": "sec-financial-data",
"version": "2.3.6"
}
}
).dict()
# Handle tools/list
elif method == "tools/list":
return MCPResponse(
id=request.id,
result={
"tools": MCP_TOOLS
}
).dict()
# Handle tools/call
elif method == "tools/call":
tool_name = params.get("name")
arguments = params.get("arguments", {})
if not tool_name:
return MCPResponse(
id=request.id,
error={
"code": -32602,
"message": "Missing tool name"
}
).dict()
result = execute_tool(tool_name, arguments)
return MCPResponse(
id=request.id,
result={
"content": [result]
}
).dict()
else:
return MCPResponse(
id=request.id,
error={
"code": -32601,
"message": f"Method not found: {method}"
}
).dict()
@app.get("/sse")
async def sse_endpoint(request: Request):
"""
Server-Sent Events endpoint for MCP transport
Keeps connection alive and handles MCP messages
"""
async def event_stream():
# Send initial endpoint message
init_message = {
"jsonrpc": "2.0",
"method": "endpoint",
"params": {
"endpoint": "/message"
}
}
yield f"data: {json.dumps(init_message)}\n\n"
# Keep connection alive with optimized ping interval
try:
while True:
await asyncio.sleep(20) # 20 seconds ping interval for stability
# Send ping to keep connection alive
ping_message = {
"jsonrpc": "2.0",
"method": "ping",
"timestamp": datetime.now().isoformat()
}
yield f"data: {json.dumps(ping_message)}\n\n"
except asyncio.CancelledError:
pass
return StreamingResponse(
event_stream(),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache, no-transform",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
"Content-Type": "text/event-stream"
}
)
@app.get("/", response_class=HTMLResponse)
async def root():
"""Interactive landing page with tool testing functionality"""
# Get the path to the templates directory
current_dir = Path(__file__).parent
template_path = current_dir / "templates" / "index.html"
# Read and return the HTML file
try:
with open(template_path, "r", encoding="utf-8") as f:
html_content = f.read()
return HTMLResponse(content=html_content)
except FileNotFoundError:
# Enhanced error message with debugging information
import os
debug_info = f"""
<h1>Error: Template not found</h1>
<h2>Debugging Information:</h2>
<p><strong>Looking for:</strong> {template_path.absolute()}</p>
<p><strong>Exists:</strong> {template_path.exists()}</p>
<p><strong>Current dir:</strong> {current_dir.absolute()}</p>
<h3>Directory contents:</h3>
<pre>{chr(10).join(os.listdir(current_dir))}</pre>
<h3>Fix:</h3>
<p>Ensure Dockerfile contains: <code>COPY templates/ templates/</code></p>
"""
return HTMLResponse(content=debug_info, status_code=500)
@app.get("/tools")
async def list_tools():
"""List all available MCP tools (for documentation)"""
return {"tools": MCP_TOOLS}
@app.get("/health")
async def health_check():
"""Enhanced health check endpoint with diagnostics"""
uptime_seconds = time.time() - server_start_time
return {
"status": "healthy",
"server": "sec-financial-data",
"version": "2.3.6",
"protocol": "MCP",
"transport": "SSE",
"tools_count": len(MCP_TOOLS),
"uptime_seconds": round(uptime_seconds, 2),
"python_version": sys.version,
"request_count": request_count,
"error_count": error_count,
"error_rate": round(error_count / max(request_count, 1) * 100, 2),
"timestamp": datetime.now().isoformat()
}
@app.get("/ready")
async def readiness_check():
"""Readiness check for load balancers"""
try:
# Quick validation that clients are initialized
if edgar_client and financial_analyzer:
return {"status": "ready", "timestamp": datetime.now().isoformat()}
else:
raise HTTPException(status_code=503, detail="Services not initialized")
except Exception as e:
raise HTTPException(status_code=503, detail=f"Not ready: {str(e)}")
@app.api_route("/api/{path:path}", methods=["GET", "POST", "PUT", "DELETE"])
async def redirect_old_api(path: str):
"""Handle old REST API endpoints with helpful message"""
return JSONResponse(
status_code=404,
content={
"error": "API endpoint not found",
"message": "This server now uses the MCP (Model Context Protocol).",
"migration_guide": {
"old_endpoint": f"/api/{path}",
"new_method": "Use MCP protocol via POST /message",
"documentation": "Visit / for setup instructions",
"example": {
"method": "POST",
"url": "/message",
"body": {
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "advanced_search_company",
"arguments": {"company_input": "Microsoft"}
}
}
}
},
"available_tools": "/tools",
"health_check": "/health"
}
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"mcp_server_sse:app",
host="0.0.0.0",
port=7860,
reload=True
)
|