Dabococo commited on
Commit
f408df8
·
verified ·
1 Parent(s): 7c9ca56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -14
app.py CHANGED
@@ -2,7 +2,7 @@ import os
2
  import sys
3
  import zipfile
4
  import uvicorn
5
- from fastapi import FastAPI, HTTPException
6
  from fastapi.responses import JSONResponse
7
 
8
  # --- 1️⃣ Décompression du zip MCP ---
@@ -17,15 +17,14 @@ if os.path.exists(ZIP_PATH) and not os.path.exists(EXTRACT_DIR):
17
  # --- 2️⃣ Ajouter le dossier MCP au PYTHONPATH ---
18
  sys.path.append(EXTRACT_DIR)
19
 
20
- # Patch rapide pour corriger les f-strings avec strftime
21
- main_path = "app/MistralHackathonMCP/main.py"
22
  if os.path.exists(main_path):
23
  with open(main_path, "r", encoding="utf-8") as f:
24
  lines = f.readlines()
25
 
26
  fixed_lines = []
27
  for line in lines:
28
- # Remplace f"[{current_time.strftime("%Y-%m-%d %H:%M:%S")}] ..." par f"[{current_time.strftime('%Y-%m-%d %H:%M:%S')}] ..."
29
  fixed_lines.append(line.replace('strftime("%Y-%m-%d %H:%M:%S")', "strftime('%Y-%m-%d %H:%M:%S')"))
30
 
31
  with open(main_path, "w", encoding="utf-8") as f:
@@ -45,6 +44,7 @@ except Exception as e:
45
  # --- 4️⃣ Créer l'application FastAPI ---
46
  app = FastAPI(title="Atlas-MCP Server")
47
 
 
48
  @app.get("/")
49
  def root():
50
  return {
@@ -52,19 +52,18 @@ def root():
52
  "endpoints": ["/mcp/list_tools", "/mcp/call_tool", "/health"]
53
  }
54
 
55
- from fastapi import Request
56
-
57
  @app.post("/")
58
  async def root_post(request: Request):
59
  return {"status": "MCP Atlas actif 🚀 (POST OK)"}
60
 
61
  # --- 5️⃣ Route pour lister les outils MCP ---
62
  @app.get("/mcp/list_tools")
63
- def list_tools():
64
  if not mcp:
65
  raise HTTPException(status_code=500, detail="MCP non initialisé")
66
  try:
67
- tools = mcp.list_tools() # FastMCP gère les outils enregistrés
68
  return JSONResponse(tools)
69
  except Exception as e:
70
  raise HTTPException(status_code=500, detail=f"Erreur list_tools(): {e}")
@@ -75,18 +74,22 @@ def call_tool(name: str):
75
  if not mcp:
76
  raise HTTPException(status_code=500, detail="MCP non initialisé")
77
  try:
78
- # FastMCP call_tool() peut prendre args/kwargs si nécessaire
79
- result = mcp.call_tool(name)
80
  return JSONResponse(result)
81
  except Exception as e:
82
  raise HTTPException(status_code=500, detail=f"Erreur call_tool(): {e}")
83
 
84
  # --- 7️⃣ Route healthcheck ---
85
  @app.get("/health")
86
- def health():
87
- if mcp:
88
- return {"status": "ok", "mcp_tools_count": len(mcp.list_tools())}
89
- return {"status": "error", "detail": "MCP non initialisé"}
 
 
 
 
90
 
91
  # --- 8️⃣ Point d'entrée pour lancer l'API ---
92
  if __name__ == "__main__":
 
2
  import sys
3
  import zipfile
4
  import uvicorn
5
+ from fastapi import FastAPI, HTTPException, Request
6
  from fastapi.responses import JSONResponse
7
 
8
  # --- 1️⃣ Décompression du zip MCP ---
 
17
  # --- 2️⃣ Ajouter le dossier MCP au PYTHONPATH ---
18
  sys.path.append(EXTRACT_DIR)
19
 
20
+ # --- Patch rapide pour corriger les f-strings avec strftime ---
21
+ main_path = os.path.join(EXTRACT_DIR, "main.py")
22
  if os.path.exists(main_path):
23
  with open(main_path, "r", encoding="utf-8") as f:
24
  lines = f.readlines()
25
 
26
  fixed_lines = []
27
  for line in lines:
 
28
  fixed_lines.append(line.replace('strftime("%Y-%m-%d %H:%M:%S")', "strftime('%Y-%m-%d %H:%M:%S')"))
29
 
30
  with open(main_path, "w", encoding="utf-8") as f:
 
44
  # --- 4️⃣ Créer l'application FastAPI ---
45
  app = FastAPI(title="Atlas-MCP Server")
46
 
47
+ # --- Route GET racine ---
48
  @app.get("/")
49
  def root():
50
  return {
 
52
  "endpoints": ["/mcp/list_tools", "/mcp/call_tool", "/health"]
53
  }
54
 
55
+ # --- Route POST racine (pour front HF) ---
 
56
  @app.post("/")
57
  async def root_post(request: Request):
58
  return {"status": "MCP Atlas actif 🚀 (POST OK)"}
59
 
60
  # --- 5️⃣ Route pour lister les outils MCP ---
61
  @app.get("/mcp/list_tools")
62
+ async def list_tools():
63
  if not mcp:
64
  raise HTTPException(status_code=500, detail="MCP non initialisé")
65
  try:
66
+ tools = await mcp.list_tools() # await la coroutine
67
  return JSONResponse(tools)
68
  except Exception as e:
69
  raise HTTPException(status_code=500, detail=f"Erreur list_tools(): {e}")
 
74
  if not mcp:
75
  raise HTTPException(status_code=500, detail="MCP non initialisé")
76
  try:
77
+ # passer arguments vides si outil n'en requiert pas
78
+ result = mcp.call_tool(name, arguments=[])
79
  return JSONResponse(result)
80
  except Exception as e:
81
  raise HTTPException(status_code=500, detail=f"Erreur call_tool(): {e}")
82
 
83
  # --- 7️⃣ Route healthcheck ---
84
  @app.get("/health")
85
+ async def health():
86
+ if not mcp:
87
+ return {"status": "error", "detail": "MCP non initialisé"}
88
+ try:
89
+ tools = await mcp.list_tools()
90
+ return {"status": "ok", "mcp_tools_count": len(tools)}
91
+ except Exception as e:
92
+ return {"status": "error", "detail": f"Healthcheck failed: {e}"}
93
 
94
  # --- 8️⃣ Point d'entrée pour lancer l'API ---
95
  if __name__ == "__main__":