JC321 commited on
Commit
39c4db5
·
verified ·
1 Parent(s): 2987b65

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -15
app.py CHANGED
@@ -9,6 +9,7 @@ from huggingface_hub import InferenceClient
9
 
10
  MCP_SPACE = "JC321/EasyReportDateMCP"
11
  MCP_URL = "https://jc321-easyreportdatemcp.hf.space"
 
12
 
13
  # 设置请求头
14
  HEADERS = {
@@ -115,6 +116,48 @@ def format_value(value, value_type="money"):
115
  else: # number
116
  return f"{value:.2f}"
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  def normalize_cik(cik):
119
  """
120
  格式化 CIK 为标准的 10 位格式
@@ -229,24 +272,13 @@ def query_financial_data(company_name, query_type):
229
  # 使用 MCP 协议调用工具
230
  # 先搜索公司(使用 advanced_search_company)
231
  try:
232
- search_resp = session.post(
233
- f"{MCP_URL}/mcp",
234
- json={
235
- "jsonrpc": "2.0",
236
- "method": "tools/call",
237
- "params": {
238
- "name": "advanced_search_company",
239
- "arguments": {"company_input": company_name}
240
- },
241
- "id": 1
242
- },
243
- headers=HEADERS,
244
- timeout=60 # 增加到60秒
245
- )
246
  except requests.exceptions.Timeout:
247
  return f"❌ MCP Server Timeout: The server took too long to respond (>60s).\n\n**Possible reasons**:\n1. MCP Server is cold starting (first request after idle)\n2. Server is overloaded\n3. Network issues\n\n**Suggestion**: Please try again in a few moments. If the problem persists, the MCP Server at {MCP_URL} may be down."
248
 
249
- if search_resp.status_code != 200:
 
 
250
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
251
 
252
  try:
 
9
 
10
  MCP_SPACE = "JC321/EasyReportDateMCP"
11
  MCP_URL = "https://jc321-easyreportdatemcp.hf.space"
12
+ MCP_ENDPOINT = "/mcp" # MCP 工具调用端点
13
 
14
  # 设置请求头
15
  HEADERS = {
 
116
  else: # number
117
  return f"{value:.2f}"
118
 
119
+ def call_mcp_tool(tool_name, arguments):
120
+ """调用 MCP 工具并返回结果"""
121
+ try:
122
+ # 构建完整的 URL
123
+ full_url = f"{MCP_URL}{MCP_ENDPOINT}"
124
+
125
+ # FastMCP HTTP Server 使用 /mcp 端点
126
+ response = session.post(
127
+ full_url,
128
+ json={
129
+ "jsonrpc": "2.0",
130
+ "method": "tools/call",
131
+ "params": {
132
+ "name": tool_name,
133
+ "arguments": arguments
134
+ },
135
+ "id": 1
136
+ },
137
+ headers=HEADERS,
138
+ timeout=60
139
+ )
140
+
141
+ # 调试信息
142
+ print(f"DEBUG: Calling {full_url}")
143
+ print(f"DEBUG: Tool: {tool_name}, Args: {arguments}")
144
+ print(f"DEBUG: Status Code: {response.status_code}")
145
+ print(f"DEBUG: Response: {response.text[:500]}")
146
+
147
+ if response.status_code != 200:
148
+ return {
149
+ "error": f"HTTP {response.status_code}",
150
+ "detail": response.text,
151
+ "url": full_url
152
+ }
153
+
154
+ return response.json()
155
+ except Exception as e:
156
+ return {
157
+ "error": str(e),
158
+ "url": full_url if 'full_url' in locals() else MCP_URL
159
+ }
160
+
161
  def normalize_cik(cik):
162
  """
163
  格式化 CIK 为标准的 10 位格式
 
272
  # 使用 MCP 协议调用工具
273
  # 先搜索公司(使用 advanced_search_company)
274
  try:
275
+ search_result = call_mcp_tool("advanced_search_company", {"company_input": company_name})
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  except requests.exceptions.Timeout:
277
  return f"❌ MCP Server Timeout: The server took too long to respond (>60s).\n\n**Possible reasons**:\n1. MCP Server is cold starting (first request after idle)\n2. Server is overloaded\n3. Network issues\n\n**Suggestion**: Please try again in a few moments. If the problem persists, the MCP Server at {MCP_URL} may be down."
278
 
279
+ # 检查是否有错误
280
+ if "error" in search_result:
281
+ return f"❌ Server Error: {search_result.get('error')}\n\nResponse: {search_result.get('detail', 'N/A')}\n\nURL: {search_result.get('url', MCP_URL)}"
282
  return f"❌ Server Error: HTTP {search_resp.status_code}\n\nResponse: {search_resp.text[:500]}"
283
 
284
  try: