File size: 6,632 Bytes
fa4c2b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# API Enhancements Summary

## Overview

The MCP Server has been enhanced to include all important methods from `FinancialAnalyzer`, providing both basic and advanced financial data analysis capabilities.

## Total API Endpoints: 9

### Basic Endpoints (6) - Using `EdgarDataClient`

1. **POST /api/search_company**

   - Search company by name

   - Returns: CIK, name, ticker



2. **POST /api/get_company_info**

   - Get detailed company information

   - Returns: Full company details with SIC



3. **POST /api/get_company_filings**

   - Get company SEC filings

   - Supports: 10-K, 10-Q, 20-F, etc.



4. **POST /api/get_company_facts**

   - Get complete XBRL financial facts

   - Returns: Raw financial data



5. **POST /api/get_financial_data**

   - Get financial data for specific period

   - Supports: Annual (YYYY) and Quarterly (YYYYQX)



6. **GET /health**

   - Health check endpoint

   - Returns: Server status



### Advanced Endpoints (3) - Using `FinancialAnalyzer` ⭐



#### 7. POST /api/advanced_search

**Purpose**: Intelligent company search supporting both name and CIK



**From**: `FinancialAnalyzer.search_company()`



**Request**:

```json

{

  "company_input": "Apple"  // or "0000320193"

}

```



**Features**:

- Auto-detects input type (name vs CIK)

- Returns complete company information

- More intelligent than basic search



**Use Case**: When you don't know if user input is name or CIK



---



#### 8. POST /api/extract_financial_metrics

**Purpose**: Extract multi-year financial metrics (annual + quarterly)



**From**: `FinancialAnalyzer.extract_financial_metrics()`



**Request**:

```json

{

  "cik": "0001045810",

  "years": 3

}

```



**Response**:

```json

{

  "metrics": [

    {

      "period": "2024",

      "total_revenue": 60922000000,

      "net_income": 29760000000,

      "earnings_per_share": 12.04,

      ...

    },

    {

      "period": "2024Q4",

      ...

    },

    {

      "period": "2024Q3",

      ...

    }

  ],

  "count": 15

}

```



**Features**:

- Returns both annual and quarterly data

- Automatically formats data

- Sorted by period (newest first)

- Configurable years (1-10)



**Use Cases**:

- Historical trend analysis

- Multi-period comparison

- Financial modeling

- Data visualization



---



#### 9. POST /api/get_latest_financial_data

**Purpose**: Get the most recent annual financial data



**From**: `FinancialAnalyzer.get_latest_financial_data()`



**Request**:

```json

{

  "cik": "0001045810"

}

```



**Response**:

```json

{

  "period": "2024",

  "total_revenue": 60922000000,

  "net_income": 29760000000,

  "earnings_per_share": 12.04,

  "operating_expenses": 11822000000,

  "operating_cash_flow": 28091000000,

  "source_url": "https://www.sec.gov/...",

  "source_form": "10-K"

}

```



**Features**:

- Automatically finds latest fiscal year

- No need to know the year

- Returns most recent 10-K or 20-F data



**Use Cases**:

- Quick company snapshot

- Current financial position

- Latest performance metrics



---



## Key Benefits



### 1. **Flexibility**

- Basic endpoints for precise queries

- Advanced endpoints for intelligent analysis



### 2. **Efficiency**

- `extract_financial_metrics` - Get multiple years in one call

- `get_latest_financial_data` - No need to determine fiscal year

- `advanced_search` - Flexible input handling



### 3. **Complete Coverage**

All important `FinancialAnalyzer` methods are now exposed:

- βœ… `search_company()` β†’ `/api/advanced_search`

- βœ… `extract_financial_metrics()` β†’ `/api/extract_financial_metrics`

- βœ… `get_latest_financial_data()` β†’ `/api/get_latest_financial_data`



### 4. **Data Quality**

- Automatic data formatting

- Consistent response structure

- Comprehensive error handling



## Comparison: Basic vs Advanced



| Feature | Basic Endpoint | Advanced Endpoint |

|---------|---------------|-------------------|

| Company Search | `/api/search_company` | `/api/advanced_search` |

| Input Type | Name only | Name or CIK |

| Return Data | Basic (CIK, name, ticker) | Complete (includes SIC) |

| | | |

| Financial Data | `/api/get_financial_data` | `/api/extract_financial_metrics` |

| Period | Single period | Multiple years |

| Data Type | Annual OR quarterly | Annual AND quarterly |

| Request Count | 1 per period | 1 for all periods |

| | | |

| Latest Data | `/api/get_financial_data` | `/api/get_latest_financial_data` |

| Year Required | Yes (must specify) | No (auto-detects) |

| Convenience | Manual | Automatic |



## Usage Examples



### Example 1: Quick Company Analysis



```python

import requests



BASE_URL = "https://YOUR_SPACE.hf.space"



# Step 1: Advanced search (works with name or CIK)

response = requests.post(

    f"{BASE_URL}/api/advanced_search",

    json={"company_input": "NVIDIA"}

)

company = response.json()

cik = company['cik']



# Step 2: Get latest data (no year needed!)

response = requests.post(

    f"{BASE_URL}/api/get_latest_financial_data",

    json={"cik": cik}

)

latest = response.json()

print(f"Latest FY{latest['period']} Revenue: ${latest['total_revenue']:,.0f}")

```



### Example 2: Multi-Year Trend Analysis



```python

# Get 5 years of data in one call

response = requests.post(

    f"{BASE_URL}/api/extract_financial_metrics",

    json={"cik": cik, "years": 5}

)

data = response.json()



# Analyze trends

import pandas as pd

df = pd.DataFrame(data['metrics'])

annual_data = df[~df['period'].str.contains('Q')]  # Filter annual only

print(annual_data[['period', 'total_revenue', 'net_income']])

```



### Example 3: Flexible Search



```python

# Works with either input type

def search_company(input_str):

    response = requests.post(

        f"{BASE_URL}/api/advanced_search",

        json={"company_input": input_str}

    )

    return response.json()



# Both work!

result1 = search_company("Apple")         # By name

result2 = search_company("0000320193")    # By CIK

```



## Documentation



- **Complete API Examples**: See `mcp_client_examples.json`

- **Interactive Testing**: Visit `/docs` (Swagger UI)

- **Detailed Usage**: See `API_USAGE.md`



## Summary



The MCP Server now provides **9 comprehensive API endpoints** covering:
- βœ… Basic data retrieval (EdgarDataClient)
- βœ… Advanced analysis (FinancialAnalyzer)
- βœ… Flexible search options
- βœ… Multi-year data extraction
- βœ… Automatic latest data retrieval

All important functionality from both `edgar_client.py` and `financial_analyzer.py` is now accessible via RESTful APIs! πŸŽ‰