chore: ruff py313, conftest unification, audit log, app factory comune
- pyproject.toml: ruff target-version py311 → py313 (auto-fix 42 lint warnings via UP rules); aggiunto consider_namespace_packages = true che risolve la collisione conftest tra servizi e permette di lanciare pytest sull'intera suite cross-servizio. - mcp_common.audit: nuovo helper audit_write_op() con logger dedicato mcp.audit. Wirato su tutti i write endpoint di deribit, bybit, alpaca e hyperliquid (place_order, place_combo_order, cancel_*, set_*, close_*, transfer_*, switch_*, amend_*) con principal + target + payload non-sensibile + result summarizzato. - mcp_common.app_factory: ExchangeAppSpec + run_exchange_main() centralizza il boilerplate dei __main__.py (configure_root_logging, fail_fast_if_missing, summarize, load creds, resolve_environment, load token store, uvicorn). I 4 __main__.py exchange ridotti da ~60 LOC ognuno a ~25 LOC dichiarativi. mcp_common.env_validation promosso da mcp_deribit (mantenuto re-export shim per back-compat test_env_validation). - 8 test nuovi (4 audit + 4 app_factory). Suite full: 450/450 verdi. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,63 +1,29 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
|
||||
import uvicorn
|
||||
from mcp_common.auth import load_token_store_from_files
|
||||
from mcp_common.environment import resolve_environment
|
||||
from mcp_common.logging import configure_root_logging
|
||||
from mcp_common.app_factory import ExchangeAppSpec, run_exchange_main
|
||||
|
||||
from mcp_deribit.client import DeribitClient
|
||||
from mcp_deribit.env_validation import (
|
||||
fail_fast_if_missing,
|
||||
require_env,
|
||||
summarize,
|
||||
)
|
||||
from mcp_deribit.server import create_app
|
||||
|
||||
configure_root_logging() # CER-P5-009: JSON default, env LOG_FORMAT=text per dev
|
||||
|
||||
|
||||
def main():
|
||||
# CER-P5-010: fail-fast boot su env mandatory
|
||||
fail_fast_if_missing(["CREDENTIALS_FILE"])
|
||||
summarize(["CREDENTIALS_FILE", "CORE_TOKEN_FILE", "OBSERVER_TOKEN_FILE", "PORT", "HOST"])
|
||||
creds_file = require_env("CREDENTIALS_FILE", "deribit credentials JSON path")
|
||||
with open(creds_file) as f:
|
||||
creds = json.load(f)
|
||||
|
||||
env_info = resolve_environment(
|
||||
creds,
|
||||
env_var="DERIBIT_TESTNET",
|
||||
flag_key="testnet",
|
||||
exchange="deribit",
|
||||
default_base_url_live="https://www.deribit.com/api/v2",
|
||||
default_base_url_testnet="https://test.deribit.com/api/v2",
|
||||
)
|
||||
|
||||
client = DeribitClient(
|
||||
SPEC = ExchangeAppSpec(
|
||||
exchange="deribit",
|
||||
creds_env_var="CREDENTIALS_FILE",
|
||||
env_var="DERIBIT_TESTNET",
|
||||
flag_key="testnet",
|
||||
default_base_url_live="https://www.deribit.com/api/v2",
|
||||
default_base_url_testnet="https://test.deribit.com/api/v2",
|
||||
default_port=9011,
|
||||
build_client=lambda creds, env_info: DeribitClient(
|
||||
client_id=creds["client_id"],
|
||||
client_secret=creds["client_secret"],
|
||||
testnet=(env_info.environment == "testnet"),
|
||||
)
|
||||
),
|
||||
build_app=create_app,
|
||||
)
|
||||
|
||||
token_store = load_token_store_from_files(
|
||||
core_token_file=os.environ.get("CORE_TOKEN_FILE"),
|
||||
observer_token_file=os.environ.get("OBSERVER_TOKEN_FILE"),
|
||||
)
|
||||
app = create_app(
|
||||
client=client,
|
||||
token_store=token_store,
|
||||
creds=creds,
|
||||
env_info=env_info,
|
||||
)
|
||||
uvicorn.run(
|
||||
app,
|
||||
log_config=None, # CER-P5-009: delega al root JSON logger
|
||||
host=os.environ.get("HOST", "0.0.0.0"),
|
||||
port=int(os.environ.get("PORT", "9011")),
|
||||
)
|
||||
|
||||
def main():
|
||||
run_exchange_main(SPEC)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -1,80 +1,18 @@
|
||||
"""CER-P5-010: env validation policy — fail-fast per mandatory, soft per optional.
|
||||
|
||||
Usage al boot di ogni mcp `__main__.py`:
|
||||
|
||||
from option_mcp_common.env_validation import require_env, optional_env, summarize
|
||||
|
||||
creds_file = require_env("CREDENTIALS_FILE", "deribit credentials JSON path")
|
||||
host = optional_env("HOST", default="0.0.0.0")
|
||||
summarize(["CREDENTIALS_FILE", "HOST", "PORT"])
|
||||
"""Re-export shim per backward-compat: la logica vive ora in
|
||||
mcp_common.env_validation. Non aggiungere nuovo codice qui.
|
||||
"""
|
||||
from mcp_common.env_validation import (
|
||||
MissingEnvError,
|
||||
fail_fast_if_missing,
|
||||
optional_env,
|
||||
require_env,
|
||||
summarize,
|
||||
)
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class MissingEnvError(RuntimeError):
|
||||
"""Mandatory env var absent or empty."""
|
||||
|
||||
|
||||
def require_env(name: str, description: str = "") -> str:
|
||||
"""Fail-fast: raise MissingEnvError se name non presente o vuoto.
|
||||
|
||||
Uscita dal processo con codice 2 se chiamato dal main(). Comporta
|
||||
logging chiaro del missing var prima dell'exit.
|
||||
"""
|
||||
val = (os.environ.get(name) or "").strip()
|
||||
if not val:
|
||||
msg = f"missing mandatory env var: {name}"
|
||||
if description:
|
||||
msg += f" ({description})"
|
||||
logger.error(msg)
|
||||
raise MissingEnvError(msg)
|
||||
return val
|
||||
|
||||
|
||||
def optional_env(name: str, *, default: str = "") -> str:
|
||||
"""Soft: ritorna env o default. Log INFO se default usato."""
|
||||
val = (os.environ.get(name) or "").strip()
|
||||
if not val:
|
||||
if default:
|
||||
logger.info("env %s not set, using default=%r", name, default)
|
||||
return default
|
||||
return val
|
||||
|
||||
|
||||
def summarize(names: list[str]) -> None:
|
||||
"""Log INFO di tutti gli env rilevanti con presenza (mask se SECRET/KEY/TOKEN)."""
|
||||
sensitive_tokens = ("SECRET", "KEY", "TOKEN", "PASSWORD", "CREDENTIAL", "WALLET")
|
||||
for n in names:
|
||||
val = os.environ.get(n)
|
||||
if val is None:
|
||||
logger.info("env[%s]: <unset>", n)
|
||||
continue
|
||||
if any(t in n.upper() for t in sensitive_tokens):
|
||||
logger.info("env[%s]: <set, %d chars>", n, len(val))
|
||||
else:
|
||||
logger.info("env[%s]: %s", n, val)
|
||||
|
||||
|
||||
def fail_fast_if_missing(names: list[str]) -> None:
|
||||
"""Verifica lista di nomi mandatory al boot. Exit 2 se uno solo manca.
|
||||
|
||||
Uso preferito: early call in main() per bloccare boot se config incompleta.
|
||||
"""
|
||||
missing: list[str] = []
|
||||
for n in names:
|
||||
if not (os.environ.get(n) or "").strip():
|
||||
missing.append(n)
|
||||
if missing:
|
||||
logger.error("boot aborted: missing mandatory env vars: %s", missing)
|
||||
print(
|
||||
f"FATAL: missing mandatory env vars: {missing}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
sys.exit(2)
|
||||
__all__ = [
|
||||
"MissingEnvError",
|
||||
"fail_fast_if_missing",
|
||||
"optional_env",
|
||||
"require_env",
|
||||
"summarize",
|
||||
]
|
||||
|
||||
@@ -3,15 +3,16 @@ from __future__ import annotations
|
||||
import os
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException
|
||||
from mcp_common.audit import audit_write_op
|
||||
from mcp_common.auth import Principal, TokenStore, require_principal
|
||||
from mcp_common.environment import EnvironmentInfo
|
||||
from mcp_common.mcp_bridge import mount_mcp_endpoint
|
||||
from mcp_deribit.leverage_cap import enforce_leverage as _enforce_leverage
|
||||
from mcp_deribit.leverage_cap import get_max_leverage
|
||||
from mcp_common.server import build_app
|
||||
from pydantic import BaseModel, field_validator, model_validator
|
||||
|
||||
from mcp_deribit.client import DeribitClient
|
||||
from mcp_deribit.leverage_cap import enforce_leverage as _enforce_leverage
|
||||
from mcp_deribit.leverage_cap import get_max_leverage
|
||||
|
||||
# --- Body models ---
|
||||
|
||||
@@ -554,7 +555,7 @@ def create_app(
|
||||
await client.set_leverage(body.instrument_name, lev)
|
||||
except Exception:
|
||||
pass
|
||||
return await client.place_order(
|
||||
result = await client.place_order(
|
||||
instrument_name=body.instrument_name,
|
||||
side=body.side,
|
||||
amount=body.amount,
|
||||
@@ -564,6 +565,14 @@ def create_app(
|
||||
post_only=body.post_only,
|
||||
label=body.label,
|
||||
)
|
||||
audit_write_op(
|
||||
principal=principal, action="place_order", exchange="deribit",
|
||||
target=body.instrument_name,
|
||||
payload={"side": body.side, "amount": body.amount, "type": body.type,
|
||||
"price": body.price, "leverage": lev, "label": body.label},
|
||||
result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/place_combo_order", tags=["writes"])
|
||||
async def t_place_combo_order(
|
||||
@@ -577,7 +586,7 @@ def create_app(
|
||||
await client.set_leverage(leg.instrument_name, lev)
|
||||
except Exception:
|
||||
pass
|
||||
return await client.place_combo_order(
|
||||
result = await client.place_combo_order(
|
||||
legs=[leg.model_dump() for leg in body.legs],
|
||||
side=body.side,
|
||||
amount=body.amount,
|
||||
@@ -585,34 +594,62 @@ def create_app(
|
||||
price=body.price,
|
||||
label=body.label,
|
||||
)
|
||||
audit_write_op(
|
||||
principal=principal, action="place_combo_order", exchange="deribit",
|
||||
target=result.get("combo_instrument") if isinstance(result, dict) else None,
|
||||
payload={"legs": [leg.model_dump() for leg in body.legs],
|
||||
"side": body.side, "amount": body.amount, "leverage": lev},
|
||||
result=result if isinstance(result, dict) else None,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/cancel_order", tags=["writes"])
|
||||
async def t_cancel_order(
|
||||
body: CancelOrderReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.cancel_order(body.order_id)
|
||||
result = await client.cancel_order(body.order_id)
|
||||
audit_write_op(
|
||||
principal=principal, action="cancel_order", exchange="deribit",
|
||||
target=body.order_id, payload={}, result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/set_stop_loss", tags=["writes"])
|
||||
async def t_set_sl(
|
||||
body: SetStopLossReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.set_stop_loss(body.order_id, body.stop_price)
|
||||
result = await client.set_stop_loss(body.order_id, body.stop_price)
|
||||
audit_write_op(
|
||||
principal=principal, action="set_stop_loss", exchange="deribit",
|
||||
target=body.order_id, payload={"stop_price": body.stop_price}, result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/set_take_profit", tags=["writes"])
|
||||
async def t_set_tp(
|
||||
body: SetTakeProfitReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.set_take_profit(body.order_id, body.tp_price)
|
||||
result = await client.set_take_profit(body.order_id, body.tp_price)
|
||||
audit_write_op(
|
||||
principal=principal, action="set_take_profit", exchange="deribit",
|
||||
target=body.order_id, payload={"tp_price": body.tp_price}, result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
@app.post("/tools/close_position", tags=["writes"])
|
||||
async def t_close_position(
|
||||
body: ClosePositionReq, principal: Principal = Depends(require_principal)
|
||||
):
|
||||
_check(principal, core=True)
|
||||
return await client.close_position(body.instrument_name)
|
||||
result = await client.close_position(body.instrument_name)
|
||||
audit_write_op(
|
||||
principal=principal, action="close_position", exchange="deribit",
|
||||
target=body.instrument_name, payload={}, result=result,
|
||||
)
|
||||
return result
|
||||
|
||||
# ───── MCP endpoint (/mcp) — bridge verso /tools/* ─────
|
||||
port = int(os.environ.get("PORT", "9011"))
|
||||
|
||||
@@ -3,7 +3,6 @@ from __future__ import annotations
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from mcp_common.auth import Principal, TokenStore
|
||||
from mcp_common.environment import EnvironmentInfo
|
||||
from mcp_deribit.server import create_app
|
||||
|
||||
@@ -2,7 +2,6 @@ from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
|
||||
from mcp_deribit.leverage_cap import enforce_leverage, get_max_leverage
|
||||
|
||||
|
||||
|
||||
@@ -4,8 +4,8 @@ from unittest.mock import AsyncMock, MagicMock
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from mcp_deribit.server import create_app
|
||||
from mcp_common.auth import Principal, TokenStore
|
||||
from mcp_deribit.server import create_app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
Reference in New Issue
Block a user