JC321 commited on
Commit
83f20c9
·
verified ·
1 Parent(s): 08a6820

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -35
app.py CHANGED
@@ -88,13 +88,16 @@ def query_financial_data(company_name, query_type):
88
  internal_query_type = query_type_mapping.get(query_type, query_type)
89
 
90
  try:
91
- # 调用 MCP Server 工具(使用 MCP 协议格式)
92
- # 先搜索公司 - 尝试使用 tools/call 端点
93
  search_resp = requests.post(
94
- f"{MCP_URL}/tools/call",
95
  json={
96
- "name": "advanced_search",
97
- "arguments": {"company_input": company_name}
 
 
 
98
  },
99
  headers=HEADERS,
100
  timeout=30
@@ -104,22 +107,17 @@ def query_financial_data(company_name, query_type):
104
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
105
 
106
  try:
107
- company_result = search_resp.json()
108
- # MCP 协议返回格式可能是 {"content": [{"type": "text", "text": "..."}]} 或直接返回数据
109
- if isinstance(company_result, dict):
110
- if "content" in company_result:
111
- # MCP 标准返回格式
112
- content = company_result.get("content", [])
113
- if content and len(content) > 0:
114
- text_content = content[0].get("text", "{}")
115
- company = json.loads(text_content) if isinstance(text_content, str) else text_content
116
- else:
117
- company = {}
118
  else:
119
- # 直接返回数据
120
- company = company_result
121
  else:
122
- company = company_result
123
  except (ValueError, KeyError) as e:
124
  return f"❌ JSON Parse Error: {str(e)}\n\nResponse: {search_resp.text[:500]}"
125
 
@@ -135,10 +133,13 @@ def query_financial_data(company_name, query_type):
135
  # 根据查询类型获取数据
136
  if internal_query_type == "最新财务数据":
137
  data_resp = requests.post(
138
- f"{MCP_URL}/tools/call",
139
  json={
140
- "name": "get_latest_financial_data",
141
- "arguments": {"cik": cik}
 
 
 
142
  },
143
  headers=HEADERS,
144
  timeout=30
@@ -149,7 +150,7 @@ def query_financial_data(company_name, query_type):
149
 
150
  try:
151
  data_result = data_resp.json()
152
- # 处理 MCP 协议返回格式
153
  if isinstance(data_result, dict) and "content" in data_result:
154
  content = data_result.get("content", [])
155
  if content and len(content) > 0:
@@ -186,10 +187,13 @@ def query_financial_data(company_name, query_type):
186
 
187
  elif internal_query_type == "3年趋势":
188
  metrics_resp = requests.post(
189
- f"{MCP_URL}/tools/call",
190
  json={
191
- "name": "extract_financial_metrics",
192
- "arguments": {"cik": cik, "years": 3}
 
 
 
193
  },
194
  headers=HEADERS,
195
  timeout=60
@@ -200,7 +204,7 @@ def query_financial_data(company_name, query_type):
200
 
201
  try:
202
  metrics_result = metrics_resp.json()
203
- # 处理 MCP 协议返回格式
204
  if isinstance(metrics_result, dict) and "content" in metrics_result:
205
  content = metrics_result.get("content", [])
206
  if content and len(content) > 0:
@@ -273,10 +277,13 @@ def query_financial_data(company_name, query_type):
273
 
274
  elif internal_query_type == "5年趋势":
275
  metrics_resp = requests.post(
276
- f"{MCP_URL}/tools/call",
277
  json={
278
- "name": "extract_financial_metrics",
279
- "arguments": {"cik": cik, "years": 5}
 
 
 
280
  },
281
  headers=HEADERS,
282
  timeout=60
@@ -287,7 +294,7 @@ def query_financial_data(company_name, query_type):
287
 
288
  try:
289
  metrics_result = metrics_resp.json()
290
- # 处理 MCP 协议返回格式
291
  if isinstance(metrics_result, dict) and "content" in metrics_result:
292
  content = metrics_result.get("content", [])
293
  if content and len(content) > 0:
@@ -360,10 +367,13 @@ def query_financial_data(company_name, query_type):
360
  elif internal_query_type == "公司报表列表":
361
  # 查询公司所有报表
362
  filings_resp = requests.post(
363
- f"{MCP_URL}/tools/call",
364
  json={
365
- "name": "get_company_filings",
366
- "arguments": {"cik": cik, "limit": 50}
 
 
 
367
  },
368
  headers=HEADERS,
369
  timeout=60
@@ -374,7 +384,7 @@ def query_financial_data(company_name, query_type):
374
 
375
  try:
376
  filings_result = filings_resp.json()
377
- # 处理 MCP 协议返回格式
378
  if isinstance(filings_result, dict) and "content" in filings_result:
379
  content = filings_result.get("content", [])
380
  if content and len(content) > 0:
 
88
  internal_query_type = query_type_mapping.get(query_type, query_type)
89
 
90
  try:
91
+ # 使用 MCP 协议调用工具
92
+ # 先搜索公司
93
  search_resp = requests.post(
94
+ f"{MCP_URL}/message",
95
  json={
96
+ "method": "tools/call",
97
+ "params": {
98
+ "name": "advanced_search",
99
+ "arguments": {"company_input": company_name}
100
+ }
101
  },
102
  headers=HEADERS,
103
  timeout=30
 
107
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
108
 
109
  try:
110
+ result = search_resp.json()
111
+ # MCP 协议返回格式: {"content": [{"type": "text", "text": "..."}]}
112
+ if isinstance(result, dict) and "content" in result:
113
+ content = result.get("content", [])
114
+ if content and len(content) > 0:
115
+ text_content = content[0].get("text", "{}")
116
+ company = json.loads(text_content) if isinstance(text_content, str) else text_content
 
 
 
 
117
  else:
118
+ company = {}
 
119
  else:
120
+ company = result
121
  except (ValueError, KeyError) as e:
122
  return f"❌ JSON Parse Error: {str(e)}\n\nResponse: {search_resp.text[:500]}"
123
 
 
133
  # 根据查询类型获取数据
134
  if internal_query_type == "最新财务数据":
135
  data_resp = requests.post(
136
+ f"{MCP_URL}/message",
137
  json={
138
+ "method": "tools/call",
139
+ "params": {
140
+ "name": "get_latest_financial_data",
141
+ "arguments": {"cik": cik}
142
+ }
143
  },
144
  headers=HEADERS,
145
  timeout=30
 
150
 
151
  try:
152
  data_result = data_resp.json()
153
+ # MCP 协议返回格式
154
  if isinstance(data_result, dict) and "content" in data_result:
155
  content = data_result.get("content", [])
156
  if content and len(content) > 0:
 
187
 
188
  elif internal_query_type == "3年趋势":
189
  metrics_resp = requests.post(
190
+ f"{MCP_URL}/message",
191
  json={
192
+ "method": "tools/call",
193
+ "params": {
194
+ "name": "extract_financial_metrics",
195
+ "arguments": {"cik": cik, "years": 3}
196
+ }
197
  },
198
  headers=HEADERS,
199
  timeout=60
 
204
 
205
  try:
206
  metrics_result = metrics_resp.json()
207
+ # MCP 协议返回格式
208
  if isinstance(metrics_result, dict) and "content" in metrics_result:
209
  content = metrics_result.get("content", [])
210
  if content and len(content) > 0:
 
277
 
278
  elif internal_query_type == "5年趋势":
279
  metrics_resp = requests.post(
280
+ f"{MCP_URL}/message",
281
  json={
282
+ "method": "tools/call",
283
+ "params": {
284
+ "name": "extract_financial_metrics",
285
+ "arguments": {"cik": cik, "years": 5}
286
+ }
287
  },
288
  headers=HEADERS,
289
  timeout=60
 
294
 
295
  try:
296
  metrics_result = metrics_resp.json()
297
+ # MCP 协议返回格式
298
  if isinstance(metrics_result, dict) and "content" in metrics_result:
299
  content = metrics_result.get("content", [])
300
  if content and len(content) > 0:
 
367
  elif internal_query_type == "公司报表列表":
368
  # 查询公司所有报表
369
  filings_resp = requests.post(
370
+ f"{MCP_URL}/message",
371
  json={
372
+ "method": "tools/call",
373
+ "params": {
374
+ "name": "get_company_filings",
375
+ "arguments": {"cik": cik, "limit": 50}
376
+ }
377
  },
378
  headers=HEADERS,
379
  timeout=60
 
384
 
385
  try:
386
  filings_result = filings_resp.json()
387
+ # MCP 协议返回格式
388
  if isinstance(filings_result, dict) and "content" in filings_result:
389
  content = filings_result.get("content", [])
390
  if content and len(content) > 0: