Spaces:
Runtime error
Runtime error
Upload mcp_server.py
Browse files- mcp_server.py +311 -2
mcp_server.py
CHANGED
|
@@ -5,6 +5,8 @@ Based on FastAPI framework
|
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
|
|
| 8 |
from pydantic import BaseModel, Field
|
| 9 |
from typing import Optional, List, Dict, Any
|
| 10 |
from edgar_client import EdgarDataClient
|
|
@@ -141,9 +143,316 @@ class MetricsListResponse(BaseModel):
|
|
| 141 |
|
| 142 |
|
| 143 |
# API Endpoints
|
| 144 |
-
@app.get("/")
|
| 145 |
async def root():
|
| 146 |
-
"""Root endpoint with
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
return {
|
| 148 |
"service": "SEC Financial Report MCP Server",
|
| 149 |
"version": "1.0.0",
|
|
|
|
| 5 |
|
| 6 |
from fastapi import FastAPI, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
+
from fastapi.responses import HTMLResponse
|
| 9 |
+
from fastapi.staticfiles import StaticFiles
|
| 10 |
from pydantic import BaseModel, Field
|
| 11 |
from typing import Optional, List, Dict, Any
|
| 12 |
from edgar_client import EdgarDataClient
|
|
|
|
| 143 |
|
| 144 |
|
| 145 |
# API Endpoints
|
| 146 |
+
@app.get("/", response_class=HTMLResponse)
|
| 147 |
async def root():
|
| 148 |
+
"""Root endpoint with beautiful HTML interface"""
|
| 149 |
+
html_content = """
|
| 150 |
+
<!DOCTYPE html>
|
| 151 |
+
<html lang="en">
|
| 152 |
+
<head>
|
| 153 |
+
<meta charset="UTF-8">
|
| 154 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 155 |
+
<title>SEC Financial Report MCP Server</title>
|
| 156 |
+
<style>
|
| 157 |
+
body {
|
| 158 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 159 |
+
line-height: 1.6;
|
| 160 |
+
color: #333;
|
| 161 |
+
max-width: 1200px;
|
| 162 |
+
margin: 0 auto;
|
| 163 |
+
padding: 20px;
|
| 164 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 165 |
+
min-height: 100vh;
|
| 166 |
+
}
|
| 167 |
+
.container {
|
| 168 |
+
background: white;
|
| 169 |
+
border-radius: 12px;
|
| 170 |
+
padding: 40px;
|
| 171 |
+
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
|
| 172 |
+
}
|
| 173 |
+
.header {
|
| 174 |
+
text-align: center;
|
| 175 |
+
margin-bottom: 40px;
|
| 176 |
+
padding-bottom: 20px;
|
| 177 |
+
border-bottom: 3px solid #667eea;
|
| 178 |
+
}
|
| 179 |
+
.header h1 {
|
| 180 |
+
color: #667eea;
|
| 181 |
+
margin: 0;
|
| 182 |
+
font-size: 2.5em;
|
| 183 |
+
font-weight: 700;
|
| 184 |
+
}
|
| 185 |
+
.status {
|
| 186 |
+
display: inline-block;
|
| 187 |
+
background: #10b981;
|
| 188 |
+
color: white;
|
| 189 |
+
padding: 8px 16px;
|
| 190 |
+
border-radius: 20px;
|
| 191 |
+
font-size: 0.9em;
|
| 192 |
+
margin-top: 10px;
|
| 193 |
+
}
|
| 194 |
+
.quick-start {
|
| 195 |
+
background: linear-gradient(135deg, #667eea, #764ba2);
|
| 196 |
+
color: white;
|
| 197 |
+
padding: 30px;
|
| 198 |
+
border-radius: 10px;
|
| 199 |
+
margin-bottom: 30px;
|
| 200 |
+
}
|
| 201 |
+
.quick-start h2 {
|
| 202 |
+
margin-top: 0;
|
| 203 |
+
font-size: 1.8em;
|
| 204 |
+
}
|
| 205 |
+
.steps {
|
| 206 |
+
display: grid;
|
| 207 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 208 |
+
gap: 20px;
|
| 209 |
+
margin-top: 20px;
|
| 210 |
+
}
|
| 211 |
+
.step {
|
| 212 |
+
background: rgba(255,255,255,0.1);
|
| 213 |
+
padding: 20px;
|
| 214 |
+
border-radius: 8px;
|
| 215 |
+
border: 1px solid rgba(255,255,255,0.2);
|
| 216 |
+
}
|
| 217 |
+
.step h3 {
|
| 218 |
+
margin: 0 0 10px 0;
|
| 219 |
+
font-size: 1.1em;
|
| 220 |
+
}
|
| 221 |
+
.code {
|
| 222 |
+
background: rgba(0,0,0,0.1);
|
| 223 |
+
padding: 10px;
|
| 224 |
+
border-radius: 6px;
|
| 225 |
+
font-family: 'Monaco', 'Consolas', monospace;
|
| 226 |
+
font-size: 0.9em;
|
| 227 |
+
overflow-x: auto;
|
| 228 |
+
}
|
| 229 |
+
.endpoints-grid {
|
| 230 |
+
display: grid;
|
| 231 |
+
grid-template-columns: 1fr 1fr;
|
| 232 |
+
gap: 30px;
|
| 233 |
+
margin-bottom: 30px;
|
| 234 |
+
}
|
| 235 |
+
.endpoint-section {
|
| 236 |
+
border: 2px solid #e5e7eb;
|
| 237 |
+
border-radius: 10px;
|
| 238 |
+
padding: 25px;
|
| 239 |
+
background: #f9fafb;
|
| 240 |
+
}
|
| 241 |
+
.endpoint-section.advanced {
|
| 242 |
+
border-color: #667eea;
|
| 243 |
+
background: #f0f4ff;
|
| 244 |
+
}
|
| 245 |
+
.endpoint-section h3 {
|
| 246 |
+
margin-top: 0;
|
| 247 |
+
color: #667eea;
|
| 248 |
+
font-size: 1.4em;
|
| 249 |
+
display: flex;
|
| 250 |
+
align-items: center;
|
| 251 |
+
gap: 10px;
|
| 252 |
+
}
|
| 253 |
+
.recommended {
|
| 254 |
+
background: #fef3c7;
|
| 255 |
+
color: #92400e;
|
| 256 |
+
padding: 4px 8px;
|
| 257 |
+
border-radius: 12px;
|
| 258 |
+
font-size: 0.75em;
|
| 259 |
+
font-weight: 600;
|
| 260 |
+
}
|
| 261 |
+
.endpoint {
|
| 262 |
+
margin-bottom: 15px;
|
| 263 |
+
padding: 15px;
|
| 264 |
+
background: white;
|
| 265 |
+
border-radius: 6px;
|
| 266 |
+
border-left: 4px solid #10b981;
|
| 267 |
+
}
|
| 268 |
+
.endpoint-path {
|
| 269 |
+
font-family: 'Monaco', 'Consolas', monospace;
|
| 270 |
+
color: #059669;
|
| 271 |
+
font-weight: 600;
|
| 272 |
+
font-size: 0.9em;
|
| 273 |
+
}
|
| 274 |
+
.endpoint-desc {
|
| 275 |
+
color: #6b7280;
|
| 276 |
+
margin-top: 5px;
|
| 277 |
+
font-size: 0.9em;
|
| 278 |
+
}
|
| 279 |
+
.workflows {
|
| 280 |
+
background: #f8fafc;
|
| 281 |
+
padding: 30px;
|
| 282 |
+
border-radius: 10px;
|
| 283 |
+
margin-top: 30px;
|
| 284 |
+
}
|
| 285 |
+
.workflow {
|
| 286 |
+
margin-bottom: 25px;
|
| 287 |
+
padding: 20px;
|
| 288 |
+
background: white;
|
| 289 |
+
border-radius: 8px;
|
| 290 |
+
border-left: 4px solid #8b5cf6;
|
| 291 |
+
}
|
| 292 |
+
.workflow h4 {
|
| 293 |
+
margin: 0 0 10px 0;
|
| 294 |
+
color: #8b5cf6;
|
| 295 |
+
font-size: 1.1em;
|
| 296 |
+
}
|
| 297 |
+
.workflow-step {
|
| 298 |
+
background: #f3f4f6;
|
| 299 |
+
padding: 10px;
|
| 300 |
+
margin: 5px 0;
|
| 301 |
+
border-radius: 4px;
|
| 302 |
+
font-family: 'Monaco', 'Consolas', monospace;
|
| 303 |
+
font-size: 0.85em;
|
| 304 |
+
}
|
| 305 |
+
.links {
|
| 306 |
+
text-align: center;
|
| 307 |
+
margin-top: 40px;
|
| 308 |
+
padding-top: 20px;
|
| 309 |
+
border-top: 2px solid #e5e7eb;
|
| 310 |
+
}
|
| 311 |
+
.link-btn {
|
| 312 |
+
display: inline-block;
|
| 313 |
+
background: #667eea;
|
| 314 |
+
color: white;
|
| 315 |
+
padding: 12px 24px;
|
| 316 |
+
margin: 0 10px;
|
| 317 |
+
text-decoration: none;
|
| 318 |
+
border-radius: 6px;
|
| 319 |
+
font-weight: 600;
|
| 320 |
+
transition: background-color 0.3s;
|
| 321 |
+
}
|
| 322 |
+
.link-btn:hover {
|
| 323 |
+
background: #5a67d8;
|
| 324 |
+
}
|
| 325 |
+
.link-btn.secondary {
|
| 326 |
+
background: #6b7280;
|
| 327 |
+
}
|
| 328 |
+
.link-btn.secondary:hover {
|
| 329 |
+
background: #4b5563;
|
| 330 |
+
}
|
| 331 |
+
@media (max-width: 768px) {
|
| 332 |
+
.endpoints-grid {
|
| 333 |
+
grid-template-columns: 1fr;
|
| 334 |
+
}
|
| 335 |
+
.steps {
|
| 336 |
+
grid-template-columns: 1fr;
|
| 337 |
+
}
|
| 338 |
+
.container {
|
| 339 |
+
padding: 20px;
|
| 340 |
+
}
|
| 341 |
+
}
|
| 342 |
+
</style>
|
| 343 |
+
</head>
|
| 344 |
+
<body>
|
| 345 |
+
<div class="container">
|
| 346 |
+
<div class="header">
|
| 347 |
+
<h1>π SEC Financial Report MCP Server</h1>
|
| 348 |
+
<div class="status">β
Operational</div>
|
| 349 |
+
<p>MCP Server for querying SEC EDGAR financial data - A tool-ready API for AI agents and applications</p>
|
| 350 |
+
</div>
|
| 351 |
+
|
| 352 |
+
<div class="quick-start">
|
| 353 |
+
<h2>π Quick Start</h2>
|
| 354 |
+
<div class="steps">
|
| 355 |
+
<div class="step">
|
| 356 |
+
<h3>Step 1: Search Company</h3>
|
| 357 |
+
<div class="code">POST /api/advanced_search<br>{"company_input": "NVIDIA"}</div>
|
| 358 |
+
</div>
|
| 359 |
+
<div class="step">
|
| 360 |
+
<h3>Step 2: Get Latest Data</h3>
|
| 361 |
+
<div class="code">POST /api/get_latest_financial_data<br>{"cik": "0001045810"}</div>
|
| 362 |
+
</div>
|
| 363 |
+
<div class="step">
|
| 364 |
+
<h3>Step 3: Analyze Trends</h3>
|
| 365 |
+
<div class="code">POST /api/extract_financial_metrics<br>{"cik": "0001045810", "years": 3}</div>
|
| 366 |
+
</div>
|
| 367 |
+
</div>
|
| 368 |
+
</div>
|
| 369 |
+
|
| 370 |
+
<div class="endpoints-grid">
|
| 371 |
+
<div class="endpoint-section">
|
| 372 |
+
<h3>π§ Basic Tools</h3>
|
| 373 |
+
<div class="endpoint">
|
| 374 |
+
<div class="endpoint-path">POST /api/search_company</div>
|
| 375 |
+
<div class="endpoint-desc">Search company by name only</div>
|
| 376 |
+
</div>
|
| 377 |
+
<div class="endpoint">
|
| 378 |
+
<div class="endpoint-path">POST /api/get_company_info</div>
|
| 379 |
+
<div class="endpoint-desc">Get company information by CIK</div>
|
| 380 |
+
</div>
|
| 381 |
+
<div class="endpoint">
|
| 382 |
+
<div class="endpoint-path">POST /api/get_company_filings</div>
|
| 383 |
+
<div class="endpoint-desc">Get company SEC filings (10-K, 10-Q, 20-F)</div>
|
| 384 |
+
</div>
|
| 385 |
+
<div class="endpoint">
|
| 386 |
+
<div class="endpoint-path">POST /api/get_company_facts</div>
|
| 387 |
+
<div class="endpoint-desc">Get complete XBRL financial facts</div>
|
| 388 |
+
</div>
|
| 389 |
+
<div class="endpoint">
|
| 390 |
+
<div class="endpoint-path">POST /api/get_financial_data</div>
|
| 391 |
+
<div class="endpoint-desc">Get financial data for specific period</div>
|
| 392 |
+
</div>
|
| 393 |
+
<div class="endpoint">
|
| 394 |
+
<div class="endpoint-path">GET /health</div>
|
| 395 |
+
<div class="endpoint-desc">Server health check</div>
|
| 396 |
+
</div>
|
| 397 |
+
</div>
|
| 398 |
+
|
| 399 |
+
<div class="endpoint-section advanced">
|
| 400 |
+
<h3>β Advanced Tools <span class="recommended">RECOMMENDED</span></h3>
|
| 401 |
+
<div class="endpoint">
|
| 402 |
+
<div class="endpoint-path">POST /api/advanced_search</div>
|
| 403 |
+
<div class="endpoint-desc">Smart search - accepts company name OR CIK</div>
|
| 404 |
+
</div>
|
| 405 |
+
<div class="endpoint">
|
| 406 |
+
<div class="endpoint-path">POST /api/extract_financial_metrics</div>
|
| 407 |
+
<div class="endpoint-desc">Extract multi-year metrics (annual + quarterly)</div>
|
| 408 |
+
</div>
|
| 409 |
+
<div class="endpoint">
|
| 410 |
+
<div class="endpoint-path">POST /api/get_latest_financial_data</div>
|
| 411 |
+
<div class="endpoint-desc">Get most recent annual financial data automatically</div>
|
| 412 |
+
</div>
|
| 413 |
+
</div>
|
| 414 |
+
</div>
|
| 415 |
+
|
| 416 |
+
<div class="workflows">
|
| 417 |
+
<h3>π Example Workflows</h3>
|
| 418 |
+
<div class="workflow">
|
| 419 |
+
<h4>Get Latest Company Financials</h4>
|
| 420 |
+
<div class="workflow-step">1. POST /api/advanced_search {"company_input": "Apple"}</div>
|
| 421 |
+
<div class="workflow-step">2. POST /api/get_latest_financial_data {"cik": "<from_step_1>"}</div>
|
| 422 |
+
</div>
|
| 423 |
+
<div class="workflow">
|
| 424 |
+
<h4>Analyze 5-Year Trends</h4>
|
| 425 |
+
<div class="workflow-step">1. POST /api/advanced_search {"company_input": "TSMC"}</div>
|
| 426 |
+
<div class="workflow-step">2. POST /api/extract_financial_metrics {"cik": "<from_step_1>", "years": 5}</div>
|
| 427 |
+
</div>
|
| 428 |
+
<div class="workflow">
|
| 429 |
+
<h4>Get Specific Quarter Data</h4>
|
| 430 |
+
<div class="workflow-step">1. POST /api/advanced_search {"company_input": "NVIDIA"}</div>
|
| 431 |
+
<div class="workflow-step">2. POST /api/get_financial_data {"cik": "<from_step_1>", "period": "2024Q3"}</div>
|
| 432 |
+
</div>
|
| 433 |
+
</div>
|
| 434 |
+
|
| 435 |
+
<div class="links">
|
| 436 |
+
<a href="/docs" class="link-btn">π Interactive API Docs</a>
|
| 437 |
+
<a href="/redoc" class="link-btn">π ReDoc Documentation</a>
|
| 438 |
+
<a href="/health" class="link-btn secondary">π Health Check</a>
|
| 439 |
+
<a href="/api" class="link-btn secondary">π JSON API Info</a>
|
| 440 |
+
</div>
|
| 441 |
+
|
| 442 |
+
<div style="text-align: center; margin-top: 30px; color: #6b7280; font-size: 0.9em;">
|
| 443 |
+
<p><strong>Data Source:</strong> US SEC EDGAR system (real-time) | <strong>Rate Limit:</strong> 10 requests/second</p>
|
| 444 |
+
<p><strong>User Agent:</strong> Juntao Peng (jtyxabc@gmail.com) | <strong>Always-On:</strong> HF CPU Upgrade</p>
|
| 445 |
+
</div>
|
| 446 |
+
</div>
|
| 447 |
+
</body>
|
| 448 |
+
</html>
|
| 449 |
+
"""
|
| 450 |
+
return html_content
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
@app.get("/api")
|
| 454 |
+
async def api_info():
|
| 455 |
+
"""JSON API information for programmatic access"""
|
| 456 |
return {
|
| 457 |
"service": "SEC Financial Report MCP Server",
|
| 458 |
"version": "1.0.0",
|