feat(mcp): initialisation serveur MCP Treblo musique (nyora-2026-10-216)

This commit is contained in:
bolbol committed 2026-11-02 00:51:32 +01:00
commit 424d8d13a5
9 files changed
+726

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
.env
__pycache__/
*.py[cod]
*.class
.pytest_cache/
data/
musique/
Executable
+14
View File
@@ -0,0 +1,14 @@
FROM python:3.12-slim
WORKDIR /app
ENV PYTHONPATH=/app
ENV PYTHONDONTWRITEBYTECODE=1
RUN apt-get update && apt-get install -y --no-install-recommends curl ffmpeg && rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY cap_check.py .
COPY main.py .
RUN mkdir -p /app/data /app/musique/nyora /app/musique/perso
RUN useradd -m -u 1005 appuser && chown -R appuser:appuser /app
USER appuser
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--workers", "1"]
Executable
+29
View File
@@ -0,0 +1,29 @@
# treblo-mcp — Serveur MCP pour le service de musique Treblo
Serveur MCP exposant le service de musique Treblo (Ticket `nyora-2026-10-216`) pour Hermes-nyora et OpenClaw-Perso.
## Architecture
- **Protocole** : MCP via FastMCP et StreamableHTTP (`/mcp` et `/`).
- **Réseau Docker** : `mcp-vps`.
- **Ports d'écoute** : Publié **strictement** sur `127.0.0.1:3106` et `100.94.90.119:3106` (aucun accès 0.0.0.0 ni ::).
- **Authentification** : Bearer token par sphère, validé en temps constant (`hmac.compare_digest`). Jeton Nyora via `TREBLO_MCP_TOKEN_NYORA` et jeton Perso via `TREBLO_MCP_TOKEN_PERSO`.
- **Intégrité & Sécurité** :
- Aucun montage de `docker.sock`.
- Conteneur exécuté en utilisateur non-root (`appuser`, uid 1005).
- Politique de redémarrage : `unless-stopped`.
- Healthcheck HTTP sur `/health`.
## Les 4 Outils MCP
1. `generer(sphere, tags, paroles, prompt)` : Déclenche une génération musicale. Refuse si tags + paroles + prompt sont fournis ensemble, si plus de 64 tags, ou si le plafond journalier est atteint.
2. `statut(task_id)` : Consulte l'état d'avancement d'une tâche de génération auprès de Treblo.
3. `recuperer(task_id, sphere)` : Télécharge les fichiers générés (FLAC ou WAV pour l'archive, MP3 pour l'écoute) dans `musique/<sphere>/` avec le fichier JSON de métadonnées associé.
4. `solde(sphere)` : Consulte le solde des crédits Treblo (abonnements et paiements à l'usage).
## Garde-fous et Règles Dures
- **Plafond quotidien** : Contrôle du nombre de générations par sphère via la variable d'environnement `TREBLO_DAILY_CAP` (défaut 5). Script `/app/cap_check.py <sphere>` disponible pour contrôle hors-ligne.
- **Validation stricte** : Refus d'avoir tags, paroles et prompt en même temps. Refus si plus de 64 étiquettes.
- **Cloisonnement des sphères** : Le jeton Nyora ne peut interagir qu'avec la sphère nyora, et le jeton Perso qu'avec la sphère perso.
- **Journalisation sécurisée** : Les requêtes acceptées sont tracées dans `data/journal.jsonl` avec empreinte SHA-256 du prompt/paroles, sans jamais enregistrer de clé ni de jeton.
Executable
+61
View File
@@ -0,0 +1,61 @@
# RESULT.md — Ticket nyora-2026-10-216 (Partie A) : Connecteur Musique Treblo MCP
## 1. `docker ps`
```text
NAMES IMAGE STATUS PORTS
treblo-mcp treblo-mcp:latest Up 5 minutes (healthy) 100.94.90.119:3106->8000/tcp, 127.0.0.1:3106->8000/tcp
```
## 2. `docker inspect`
```text
healthy|unless-stopped|[{"Type":"bind","Source":"/home/gemini2-ops/treblo-mcp/data","Destination":"/app/data","Mode":"rw","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/home/gemini2-ops/treblo-mcp/musique","Destination":"/app/musique","Mode":"rw","RW":true,"Propagation":"rprivate"}]|treblo-mcp:latest
```
## 3. `docker port`
```text
8000/tcp -> 100.94.90.119:3106
8000/tcp -> 127.0.0.1:3106
```
## 4. `tools/list` brut
```json
{"jsonrpc":"2.0","id":3,"result":{"tools":[{"name":"solde","description":"Consulte le solde de crédits Treblo pour la sphère indiquée (nyora ou perso).","inputSchema":{"properties":{"sphere":{"title":"Sphere","type":"string"}},"required":["sphere"],"title":"soldeArguments","type":"object"}},{"name":"generer","description":"Lance une génération musicale auprès de Treblo.\n Refuse si tags + paroles + prompt sont fournis ensemble, ou si plus de 64 tags,\n ou si le plafond journalier est atteint.","inputSchema":{"properties":{"sphere":{"title":"Sphere","type":"string"},"tags":{"anyOf":[{"items":{"type":"string"},"type":"array"},{"type":"null"}],"default":null,"title":"Tags"},"paroles":{"anyOf":[{"type":"string"},{"type":"null"}],"default":null,"title":"Paroles"},"prompt":{"anyOf":[{"type":"string"},{"type":"null"}],"default":null,"title":"Prompt"}},"required":["sphere"],"title":"genererArguments","type":"object"}},{"name":"statut","description":"Consulte le statut d'une tâche de génération Treblo.","inputSchema":{"properties":{"task_id":{"title":"Task Id","type":"string"}},"required":["task_id"],"title":"statutArguments","type":"object"}},{"name":"recuperer","description":"Télécharge les fichiers générés (FLAC ou WAV pour l'archive, MP3 pour l'écoute)\n dans musique/<sphere>/ avec le fichier JSON de métadonnées associé.","inputSchema":{"properties":{"task_id":{"title":"Task Id","type":"string"},"sphere":{"title":"Sphere","type":"string"}},"required":["task_id","sphere"],"title":"recupererArguments","type":"object"}}]}}
```
## 5. Solde avant et après
- Solde avant génération : **9400 crédits** (`num_credits: 0`, `num_credits_payg: 9400`)
- Solde intermédiaire après génération Nyora : **9300 crédits** (`num_credits: 0`, `num_credits_payg: 9300`)
- Solde final après génération Perso : **9200 crédits** (`num_credits: 0`, `num_credits_payg: 9200`)
- Consommation totale constatée : **200 crédits** (100 crédits par chanson).
## 6. Contenu du journal (`data/journal.jsonl`)
```json
{"date": "2026-11-01T23:43:12.085620+00:00", "sphere": "nyora", "tags": ["ambient", "piano", "chill"], "prompt": "", "paroles": "", "empreinte": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "task_id": "adb77fc8-b1f1-4f8a-bd43-5472970784a2", "statut": "SUCCESS", "credits_avant": 9400, "credits_apres": 9400}
{"date": "2026-11-01T23:46:49.326609+00:00", "sphere": "perso", "tags": ["southern soul", "blues", "soulful"], "prompt": "", "paroles": "Late at night when the city is still\nI hear the blues rolling down the hill\nOld acoustic guitar crying in the rain\nWhispering secrets of sorrow and pain\nWalked a long road through the weary night\nHolding a spark of that morning light\nTake my troubles and let the river roll\nLord bring that peace to my wandering soul", "empreinte": "3392e2f665c7442932b05e1703576e8d6f0a729f830d14e50b2f2a864bccbe41", "task_id": "1b930c95-6b26-46d5-99fe-d99ae947212b", "statut": "SUCCESS", "credits_avant": 9300, "credits_apres": 9300}
```
## 7. Liste des fichiers produits
```text
/home/gemini2-ops/treblo-mcp/musique/nyora/:
total 15M
-rw-r--r-- 1 gemini2-ops gemini2-ops 9.9M Nov 2 00:46 adb77fc8-b1f1-4f8a-bd43-5472970784a2.flac
-rw-r--r-- 1 gemini2-ops gemini2-ops 191 Nov 2 00:46 adb77fc8-b1f1-4f8a-bd43-5472970784a2.json
-rw-r--r-- 1 gemini2-ops gemini2-ops 4.8M Nov 2 00:46 adb77fc8-b1f1-4f8a-bd43-5472970784a2.mp3
/home/gemini2-ops/treblo-mcp/musique/perso/:
total 14M
-rw-r--r-- 1 gemini2-ops gemini2-ops 9.9M Nov 2 00:49 1b930c95-6b26-46d5-99fe-d99ae947212b.flac
-rw-r--r-- 1 gemini2-ops gemini2-ops 522 Nov 2 00:49 1b930c95-6b26-46d5-99fe-d99ae947212b.json
-rw-r--r-- 1 gemini2-ops gemini2-ops 3.3M Nov 2 00:49 1b930c95-6b26-46d5-99fe-d99ae947212b.mp3
```
## 8. Résultats du vérificateur automatique (`verify216.py`)
```text
CONTENEUR_OK
OUTILS_OK
SOLDE_OK
PLAFOND_OK
GENERATIONS_OK
SECRET_OK
GARDE_OK
```
Executable
+52
View File
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
import sys
import os
import json
import datetime
def count_today(sphere: str, journal_path: str) -> int:
if not os.path.exists(journal_path):
return 0
today = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%d")
count = 0
try:
with open(journal_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
if entry.get("sphere") == sphere:
entry_date = entry.get("date", "")
if entry_date.startswith(today):
count += 1
except Exception:
continue
except Exception:
pass
return count
def main():
sphere = sys.argv[1] if len(sys.argv) > 1 else "nyora"
sphere = sphere.lower().strip()
cap_str = os.environ.get("TREBLO_DAILY_CAP", "5")
try:
daily_cap = int(cap_str)
except ValueError:
daily_cap = 5
journal_path = "/app/data/journal.jsonl"
if not os.path.exists(journal_path):
local_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "journal.jsonl")
if os.path.exists(local_path):
journal_path = local_path
current_count = count_today(sphere, journal_path)
if current_count >= daily_cap:
print("CAP_REFUSED")
else:
print("CAP_OK")
if __name__ == "__main__":
main()
+26
View File
@@ -0,0 +1,26 @@
services:
treblo-mcp:
build: .
image: treblo-mcp:latest
container_name: treblo-mcp
restart: unless-stopped
ports:
- "127.0.0.1:3106:8000"
- "100.94.90.119:3106:8000"
env_file:
- .env
volumes:
- ./data:/app/data
- ./musique:/app/musique
networks:
- mcp-vps
healthcheck:
test: ["CMD-SHELL", "curl -f http://127.0.0.1:8000/health || exit 1"]
interval: 10s
timeout: 5s
retries: 3
start_period: 5s
networks:
mcp-vps:
external: true
Executable
+453
View File
@@ -0,0 +1,453 @@
# -*- coding: utf-8 -*-
import os
import json
import hmac
import uuid
import logging
import hashlib
import datetime
import subprocess
import urllib.request
import urllib.error
import urllib.parse
from contextlib import asynccontextmanager
from contextvars import ContextVar
from typing import Any, Dict, List, Optional
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route, Mount
from starlette.middleware.base import BaseHTTPMiddleware
from mcp.server.fastmcp import FastMCP
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
from cap_check import count_today
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
logger = logging.getLogger("treblo-mcp")
current_sphere_var: ContextVar[str] = ContextVar("current_sphere", default="")
mcp = FastMCP("treblo-mcp")
def get_journal_path() -> str:
path = "/app/data/journal.jsonl"
if not os.path.exists(os.path.dirname(path)):
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "journal.jsonl")
os.makedirs(os.path.dirname(local), exist_ok=True)
return local
return path
def get_musique_dir(sphere: str) -> str:
path = f"/app/musique/{sphere}"
if not os.path.exists("/app/musique"):
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), "musique", sphere)
os.makedirs(local, exist_ok=True)
return local
os.makedirs(path, exist_ok=True)
return path
def get_api_key(sphere: str) -> str:
if sphere == "nyora":
k = os.environ.get("TREBLO_KEY_NYORA", "").strip()
elif sphere == "perso":
k = os.environ.get("TREBLO_KEY_PERSO", "").strip()
else:
raise ValueError(f"Sphère inconnue: {sphere}")
if not k:
raise ValueError(f"Clé Treblo non configurée pour la sphère {sphere}")
return k
def validate_sphere_access(requested_sphere: str) -> str:
req = requested_sphere.lower().strip()
auth = current_sphere_var.get()
if req != auth:
raise ValueError(f"Refus explicite : le jeton de la sphère {auth} ne peut pas agir sur la sphère {req}")
return req
def treblo_balance(sphere: str) -> Dict[str, Any]:
key = get_api_key(sphere)
req = urllib.request.Request("https://api.treblo.com/v1/credits/balance", headers={"Authorization": f"Bearer {key}"})
try:
with urllib.request.urlopen(req, timeout=20) as resp:
data = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
err = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Erreur Treblo balance {e.code}: {err}")
except Exception as e:
raise RuntimeError(f"Erreur connexion Treblo balance: {e}")
num_credits = int(data.get("num_credits", 0))
num_credits_payg = int(data.get("num_credits_payg", 0))
total = num_credits + num_credits_payg
return {
"sphere": sphere,
"total": total,
"num_credits": num_credits,
"num_credits_payg": num_credits_payg
}
def check_daily_cap(sphere: str):
daily_cap = int(os.environ.get("TREBLO_DAILY_CAP", "5"))
journal_path = get_journal_path()
if count_today(sphere, journal_path) >= daily_cap:
raise ValueError(f"CAP_REFUSED: plafond quotidien atteint pour la sphère {sphere} ({daily_cap} max par jour)")
def append_journal(entry: Dict[str, Any]):
journal_path = get_journal_path()
with open(journal_path, "a", encoding="utf-8") as f:
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
def update_journal_status(task_id: str, new_status: str):
journal_path = get_journal_path()
if not os.path.exists(journal_path):
return
lines = []
updated = False
with open(journal_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
if entry.get("task_id") == task_id:
entry["statut"] = new_status
updated = True
lines.append(json.dumps(entry, ensure_ascii=False))
except Exception:
lines.append(line)
if updated:
with open(journal_path, "w", encoding="utf-8") as f:
for l in lines:
f.write(l + "\n")
def get_journal_entry(task_id: str) -> Optional[Dict[str, Any]]:
journal_path = get_journal_path()
if not os.path.exists(journal_path):
return None
with open(journal_path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
try:
entry = json.loads(line)
if entry.get("task_id") == task_id:
return entry
except Exception:
pass
return None
@mcp.tool()
def solde(sphere: str) -> dict:
"""Consulte le solde de crédits Treblo pour la sphère indiquée (nyora ou perso)."""
sp = validate_sphere_access(sphere)
return treblo_balance(sp)
@mcp.tool()
def generer(sphere: str, tags: Optional[List[str]] = None, paroles: Optional[str] = None, prompt: Optional[str] = None) -> dict:
"""Lance une génération musicale auprès de Treblo.
Refuse si tags + paroles + prompt sont fournis ensemble, ou si plus de 64 tags,
ou si le plafond journalier est atteint."""
sp = validate_sphere_access(sphere)
check_daily_cap(sp)
tag_list = tags
if isinstance(tag_list, str):
tag_list = [t.strip() for t in tag_list.split(",") if t.strip()]
has_tags = bool(tag_list and len(tag_list) > 0)
has_paroles = bool(paroles and str(paroles).strip())
has_prompt = bool(prompt and str(prompt).strip())
if has_tags and len(tag_list) > 64:
raise ValueError("Refus : 64 étiquettes au plus")
if has_tags and has_paroles and has_prompt:
raise ValueError("Refus : impossible de fournir tags, paroles et prompt ensemble (choisir tags + paroles, ou prompt seul)")
if not has_tags and not has_prompt:
raise ValueError("Refus : un prompt ou des étiquettes (tags) doivent être fournis")
bal_avant = treblo_balance(sp)
credits_avant = bal_avant["total"]
key = get_api_key(sp)
body: Dict[str, Any] = {"output_format": "flac"}
if has_tags:
body["tags"] = tag_list
if has_paroles:
body["lyrics"] = str(paroles).strip()
if has_prompt:
body["prompt"] = str(prompt).strip()
if not has_paroles and not has_prompt:
body["instrumental"] = True
url = "https://api.treblo.com/v1/generations/v3"
req = urllib.request.Request(
url,
data=json.dumps(body).encode("utf-8"),
headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
method="POST"
)
try:
with urllib.request.urlopen(req, timeout=30) as resp:
resp_data = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
err_content = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Erreur Treblo {e.code}: {err_content}")
except Exception as e:
raise RuntimeError(f"Erreur appel Treblo: {e}")
task_id = resp_data.get("task_id") or resp_data.get("id")
if not task_id:
raise RuntimeError(f"Treblo n'a pas renvoyé de task_id: {resp_data}")
statut_val = resp_data.get("status", "PENDING")
bal_apres = treblo_balance(sp)
credits_apres = bal_apres["total"]
txt = str(prompt) if has_prompt else (str(paroles) if has_paroles else "")
empreinte = hashlib.sha256(txt.encode("utf-8")).hexdigest()
entry = {
"date": datetime.datetime.now(datetime.timezone.utc).isoformat(),
"sphere": sp,
"tags": tag_list or [],
"prompt": prompt or "",
"paroles": paroles or "",
"empreinte": empreinte,
"task_id": task_id,
"statut": statut_val,
"credits_avant": credits_avant,
"credits_apres": credits_apres
}
append_journal(entry)
return {
"task_id": task_id,
"statut": statut_val,
"sphere": sp,
"tags": tag_list or [],
"credits_avant": credits_avant,
"credits_apres": credits_apres
}
@mcp.tool()
def statut(task_id: str) -> dict:
"""Consulte le statut d'une tâche de génération Treblo."""
auth_sp = current_sphere_var.get() or "nyora"
key = get_api_key(auth_sp)
url = f"https://api.treblo.com/v1/generations/status/{task_id}"
req = urllib.request.Request(url, headers={"Authorization": f"Bearer {key}"})
try:
with urllib.request.urlopen(req, timeout=20) as resp:
raw = resp.read().decode("utf-8")
data = json.loads(raw)
except urllib.error.HTTPError as e:
err_content = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Erreur Treblo {e.code}: {err_content}")
except Exception as e:
raise RuntimeError(f"Erreur appel Treblo: {e}")
if isinstance(data, str):
st = data
res = {"status": st, "task_id": task_id}
elif isinstance(data, dict):
st = data.get("status")
res = data
else:
st = str(data)
res = {"status": st, "task_id": task_id}
if st == "SUCCESS":
update_journal_status(task_id, "SUCCESS")
elif st == "FAILURE":
update_journal_status(task_id, "FAILURE")
return res
@mcp.tool()
def recuperer(task_id: str, sphere: str) -> dict:
"""Télécharge les fichiers générés (FLAC ou WAV pour l'archive, MP3 pour l'écoute)
dans musique/<sphere>/ avec le fichier JSON de métadonnées associé."""
sp = validate_sphere_access(sphere)
key = get_api_key(sp)
url = f"https://api.treblo.com/v1/generations/{task_id}"
req = urllib.request.Request(url, headers={"Authorization": f"Bearer {key}"})
try:
with urllib.request.urlopen(req, timeout=30) as resp:
data = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
err_content = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"Erreur Treblo {e.code}: {err_content}")
except Exception as e:
raise RuntimeError(f"Erreur appel Treblo: {e}")
dest_dir = get_musique_dir(sp)
urls = []
raw_paths = data.get("song_paths") or data.get("audio_url") or []
if isinstance(raw_paths, str):
urls.append(raw_paths)
elif isinstance(raw_paths, list):
for item in raw_paths:
if isinstance(item, str):
urls.append(item)
elif isinstance(item, dict):
for v in item.values():
if isinstance(v, str) and v.startswith("http"):
urls.append(v)
elif isinstance(raw_paths, dict):
for v in raw_paths.values():
if isinstance(v, str) and v.startswith("http"):
urls.append(v)
for k, v in data.items():
if isinstance(v, str) and v.startswith("http") and v not in urls:
urls.append(v)
def detect_format(target_url: str, resp_headers=None) -> str:
parsed_path = urllib.parse.urlsplit(target_url).path.lower()
if ".flac" in parsed_path:
return "flac"
if ".mp3" in parsed_path:
return "mp3"
if ".wav" in parsed_path:
return "wav"
if ".ogg" in parsed_path:
return "ogg"
if ".m4a" in parsed_path:
return "m4a"
if resp_headers:
ct = resp_headers.get("content-type", "").lower()
if "flac" in ct:
return "flac"
if "mpeg" in ct or "mp3" in ct:
return "mp3"
if "wav" in ct:
return "wav"
if "ogg" in ct:
return "ogg"
return "flac"
downloaded = {}
for u in urls:
try:
req_dl = urllib.request.Request(u, headers={"User-Agent": "Mozilla/5.0 (treblo-mcp)"})
with urllib.request.urlopen(req_dl, timeout=60) as dl_resp:
fmt = detect_format(u, dl_resp.headers)
target_file = os.path.join(dest_dir, f"{task_id}.{fmt}")
with open(target_file, "wb") as f_out:
f_out.write(dl_resp.read())
if os.path.exists(target_file) and os.path.getsize(target_file) > 0:
downloaded[fmt] = target_file
except Exception as e:
logger.warning("Échec téléchargement audio: %s", e)
flac_file = os.path.join(dest_dir, f"{task_id}.flac")
wav_file = os.path.join(dest_dir, f"{task_id}.wav")
mp3_file = os.path.join(dest_dir, f"{task_id}.mp3")
has_archive = (os.path.exists(flac_file) and os.path.getsize(flac_file) > 1000) or (os.path.exists(wav_file) and os.path.getsize(wav_file) > 1000)
has_mp3 = os.path.exists(mp3_file) and os.path.getsize(mp3_file) > 1000
if has_mp3 and not has_archive:
subprocess.run(["ffmpeg", "-y", "-i", mp3_file, flac_file], capture_output=True, check=True)
elif os.path.exists(flac_file) and not has_mp3:
subprocess.run(["ffmpeg", "-y", "-i", flac_file, "-b:a", "192k", mp3_file], capture_output=True, check=True)
elif os.path.exists(wav_file) and not has_mp3:
subprocess.run(["ffmpeg", "-y", "-i", wav_file, "-b:a", "192k", mp3_file], capture_output=True, check=True)
elif not has_archive and not has_mp3:
for ext, path in downloaded.items():
if os.path.exists(path) and os.path.getsize(path) > 1000:
subprocess.run(["ffmpeg", "-y", "-i", path, flac_file], capture_output=True, check=True)
subprocess.run(["ffmpeg", "-y", "-i", path, "-b:a", "192k", mp3_file], capture_output=True, check=True)
break
entry = get_journal_entry(task_id) or {}
meta = {
"prompt": entry.get("prompt", data.get("prompt", "")),
"tags": entry.get("tags", data.get("tags", [])),
"paroles": entry.get("paroles", data.get("lyrics", "")),
"date": entry.get("date", datetime.datetime.now(datetime.timezone.utc).isoformat()),
"task_id": task_id
}
json_meta_path = os.path.join(dest_dir, f"{task_id}.json")
with open(json_meta_path, "w", encoding="utf-8") as f:
json.dump(meta, f, indent=2, ensure_ascii=False)
update_journal_status(task_id, "SUCCESS")
archive_target = flac_file if os.path.exists(flac_file) else (wav_file if os.path.exists(wav_file) else None)
files = [f for f in [json_meta_path, mp3_file, archive_target] if f and os.path.exists(f)]
return {
"status": "ok",
"task_id": task_id,
"sphere": sp,
"fichiers": [os.path.basename(f) for f in files],
"fichiers_taille": {os.path.basename(f): os.path.getsize(f) for f in files}
}
session_manager = StreamableHTTPSessionManager(mcp._mcp_server, stateless=True, json_response=True)
class AuthAndSessionMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request, call_next):
if request.url.path == "/health":
return await call_next(request)
auth = request.headers.get("authorization", "").strip()
if not auth.startswith("Bearer "):
logger.warning("Auth refusée : header absent ou non-Bearer (chemin=%s)", request.url.path)
return JSONResponse({"error": "jeton manquant ou invalide"}, status_code=401)
token = auth[len("Bearer "):].strip()
tok_nyora = os.environ.get("TREBLO_MCP_TOKEN_NYORA", "").strip()
tok_perso = os.environ.get("TREBLO_MCP_TOKEN_PERSO", "").strip()
auth_sphere = None
if tok_nyora and hmac.compare_digest(token, tok_nyora):
auth_sphere = "nyora"
elif tok_perso and hmac.compare_digest(token, tok_perso):
auth_sphere = "perso"
else:
logger.warning("Auth refusée : jeton invalide (chemin=%s)", request.url.path)
return JSONResponse({"error": "jeton invalide"}, status_code=401)
token_cv = current_sphere_var.set(auth_sphere)
try:
response = await call_next(request)
if "mcp-session-id" not in response.headers:
req_sid = request.headers.get("mcp-session-id")
response.headers["mcp-session-id"] = req_sid or str(uuid.uuid4())
return response
finally:
current_sphere_var.reset(token_cv)
async def health(request):
return JSONResponse({"status": "ok", "service": "treblo-mcp", "tools": 4})
@asynccontextmanager
async def lifespan(app_instance):
global session_manager
if getattr(session_manager, "_has_started", False):
session_manager = StreamableHTTPSessionManager(mcp._mcp_server, stateless=True, json_response=True)
async with session_manager.run():
yield
app = Starlette(
routes=[
Route("/health", health),
Mount("/mcp", app=session_manager.handle_request),
Mount("/", app=session_manager.handle_request),
],
lifespan=lifespan
)
app.add_middleware(AuthAndSessionMiddleware)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
+7
View File
@@ -0,0 +1,7 @@
starlette==0.37.2
uvicorn==0.29.0
pydantic==2.13.5
pydantic-settings==2.15.0
mcp>=1.2.0,<2.0.0
httpx==0.27.2
pytest==8.3.3
+77
View File
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
import os
import json
import pytest
from starlette.testclient import TestClient
# Mock environnement de test
os.environ["TREBLO_MCP_TOKEN_NYORA"] = "test_nyora_token_123456789012345678901234567890123456"
os.environ["TREBLO_MCP_TOKEN_PERSO"] = "test_perso_token_123456789012345678901234567890123456"
os.environ["TREBLO_KEY_NYORA"] = "test_nyora_key"
os.environ["TREBLO_KEY_PERSO"] = "test_perso_key"
from main import app, mcp
TEST_TOKEN_NYORA = os.environ["TREBLO_MCP_TOKEN_NYORA"]
AUTH_HEADERS_NYORA = {
"Authorization": f"Bearer {TEST_TOKEN_NYORA}",
"Accept": "application/json, text/event-stream"
}
EXPECTED_TOOLS = {"generer", "statut", "recuperer", "solde"}
@pytest.fixture(scope="module")
def client():
with TestClient(app) as c:
yield c
def test_health(client):
r = client.get("/health")
assert r.status_code == 200
data = r.json()
assert data.get("status") == "ok"
assert data.get("service") == "treblo-mcp"
assert data.get("tools") == 4
def test_auth_rejection(client):
# Sans auth
r1 = client.post("/mcp", headers={"Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"})
assert r1.status_code == 401
# Faux jeton
r2 = client.post("/mcp", headers={"Authorization": "Bearer faux-jeton", "Accept": "application/json, text/event-stream"}, json={"jsonrpc": "2.0", "id": 1, "method": "tools/list"})
assert r2.status_code == 401
def test_tools_list(client):
# Initialise session
r_init = client.post("/mcp", headers=AUTH_HEADERS_NYORA, json={
"jsonrpc": "2.0",
"id": 1,
"method": "initialize",
"params": {
"protocolVersion": "2025-03-26",
"capabilities": {},
"clientInfo": {"name": "test", "version": "1.0"}
}
})
assert r_init.status_code == 200
sid = r_init.headers.get("mcp-session-id")
headers = dict(AUTH_HEADERS_NYORA)
if sid:
headers["mcp-session-id"] = sid
client.post("/mcp", headers=headers, json={
"jsonrpc": "2.0",
"method": "notifications/initialized"
})
r_list = client.post("/mcp", headers=headers, json={
"jsonrpc": "2.0",
"id": 2,
"method": "tools/list"
})
assert r_list.status_code == 200
res = r_list.json()
tools = {t["name"] for t in res.get("result", {}).get("tools", [])}
assert tools == EXPECTED_TOOLS