Enable logging mcp usage

#2
by tompoulenard - opened
Ekimetrics org
โ€ข
edited 9 days ago

Summary

  • Adds mcp_server/logging.py, a self-contained module for persisting MCP tool calls to a private Hugging Face dataset (mirrors the Gradio chat logging pattern, but scoped to the MCP server).
  • Validates the dataset repo at startup via async httpx; if env vars are missing, disabled, or the repo is unreachable, logging is turned off for the process with a single log line (no repeated failures on every tool call).
  • Wires retrieve_data_mcp and retrieve_graphs to log input, structured output, and FastMCP session metadata (session_id, request_id, client_id, client_info).
  • Documents new env vars in the README.

Log format

Each tool call is uploaded as mcp/{tool}_{timestamp}.json:

{
  "tool": "retrieve_data_mcp",
  "input": { "query": "..." },
  "output": { "documents": [...], "images": [...] },
  "session_id": "...",
  "request_id": "...",
  "client_id": "...",
  "client_info": { "name": "...", "version": "..." },
  "time": "...",
  "timestamp": "..."
}

The envelope is tool-agnostic โ€” future tools (e.g. TTD) only need to call log_tool_call(...).

Configuration (Space secrets)
Variable Purpose
HF_LOGS_MCP_TOKEN
HF write token (fine-grained token scoped to the log dataset recommended)
HF_DATASET_REPO_MCP
e.g. Ekimetrics/climateqa-mcp-logs
MCP_LOG_TO_HF
false to disable (default: true)
The MCP Space can remain public; the token lives in Space secrets and the dataset stays private.

Design notes
Startup short-circuit: init_logging() runs in the lifespan before serving requests; _ready gates all uploads.
Non-blocking: uploads run in a background thread (asyncio.to_thread); tool responses are not awaited on HF I/O.
Decoupled from climateqa: no dependency on the sync Gradio logging path in climateqa/logging.py.

tompoulenard changed pull request status to open
Ekimetrics org

Ok for me
Make sure you add the secrets in the space parameters before merging

tompoulenard changed pull request status to merged

Sign up or log in to comment