Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,51 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import zipfile
|
| 3 |
-
from fastapi import FastAPI
|
| 4 |
-
from fastapi.responses import JSONResponse
|
| 5 |
import uvicorn
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
ZIP_PATH = "MistralHackathonMCP.zip"
|
| 9 |
-
EXTRACT_DIR = "app"
|
| 10 |
-
|
| 11 |
if os.path.exists(ZIP_PATH) and not os.path.exists(EXTRACT_DIR):
|
| 12 |
with zipfile.ZipFile(ZIP_PATH, 'r') as zip_ref:
|
| 13 |
-
zip_ref.extractall(
|
| 14 |
-
print(f"✅ Décompressé {ZIP_PATH} dans
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 17 |
app = FastAPI()
|
| 18 |
|
| 19 |
-
# ✅ Route d’accueil (évite le 404 sur "/")
|
| 20 |
@app.get("/")
|
| 21 |
def root():
|
| 22 |
return {
|
| 23 |
-
"status": "MCP
|
| 24 |
"endpoints": ["/mcp/list_tools", "/mcp/call_tool"]
|
| 25 |
}
|
| 26 |
|
| 27 |
-
# Exemple d'endpoint MCP "list_tools"
|
| 28 |
@app.get("/mcp/list_tools")
|
| 29 |
def list_tools():
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
# Exemple d'endpoint MCP "call_tool"
|
| 37 |
@app.get("/mcp/call_tool")
|
| 38 |
def call_tool(name: str):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
#
|
| 44 |
if __name__ == "__main__":
|
| 45 |
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
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 |
+
# --- Décompression du zip ---
|
| 9 |
+
ZIP_PATH = "MistralHackathonMCP.zip"
|
| 10 |
+
EXTRACT_DIR = "app/MistralHackathonMCP"
|
|
|
|
| 11 |
if os.path.exists(ZIP_PATH) and not os.path.exists(EXTRACT_DIR):
|
| 12 |
with zipfile.ZipFile(ZIP_PATH, 'r') as zip_ref:
|
| 13 |
+
zip_ref.extractall("app")
|
| 14 |
+
print(f"✅ Décompressé {ZIP_PATH} dans app/")
|
| 15 |
+
|
| 16 |
+
# --- Ajouter MCP au PYTHONPATH ---
|
| 17 |
+
sys.path.append("app/MistralHackathonMCP")
|
| 18 |
+
|
| 19 |
+
# --- Importer FastMCP instance ---
|
| 20 |
+
from main import get_mcp_instance
|
| 21 |
+
mcp = get_mcp_instance()
|
| 22 |
|
| 23 |
+
# --- FastAPI ---
|
| 24 |
app = FastAPI()
|
| 25 |
|
|
|
|
| 26 |
@app.get("/")
|
| 27 |
def root():
|
| 28 |
return {
|
| 29 |
+
"status": "MCP Atlas actif 🚀",
|
| 30 |
"endpoints": ["/mcp/list_tools", "/mcp/call_tool"]
|
| 31 |
}
|
| 32 |
|
|
|
|
| 33 |
@app.get("/mcp/list_tools")
|
| 34 |
def list_tools():
|
| 35 |
+
try:
|
| 36 |
+
tools = mcp.list_tools() # FastMCP gère les outils
|
| 37 |
+
return JSONResponse(tools)
|
| 38 |
+
except Exception as e:
|
| 39 |
+
raise HTTPException(status_code=500, detail=f"Erreur list_tools(): {e}")
|
| 40 |
|
|
|
|
| 41 |
@app.get("/mcp/call_tool")
|
| 42 |
def call_tool(name: str):
|
| 43 |
+
try:
|
| 44 |
+
result = mcp.call_tool(name) # Appel FastMCP
|
| 45 |
+
return JSONResponse(result)
|
| 46 |
+
except Exception as e:
|
| 47 |
+
raise HTTPException(status_code=500, detail=f"Erreur call_tool(): {e}")
|
| 48 |
|
| 49 |
+
# --- Point d'entrée ---
|
| 50 |
if __name__ == "__main__":
|
| 51 |
uvicorn.run("app:app", host="0.0.0.0", port=7860)
|