Repository navigation
Expand file tree
/
Copy patherrors.py
More file actions
61 lines (46 loc) · 2.36 KB
/
Copy patherrors.py
File metadata and controls
61 lines (46 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Safe error reporting for MCP tool handlers.
See GHSA-xv3j-j949-g8wx (issue #7). Tool handlers must not return raw
exception strings to the caller. Exception messages can carry filesystem
paths, internal class names, SQL fragments, upstream HTTP response bodies,
and PII -- all of which leak to the LLM and any MCP client able to read the
tool response, and the upstream-body case also opens an indirect
prompt-injection channel.
Two pieces:
* ``UserFacingError`` -- marker base class for messages that the handler
has deliberately constructed to be safe (e.g. argument validation,
"tenant not set"). These propagate to the caller verbatim.
* ``tool_error_response`` / ``log_tool_error`` -- for any other exception.
Generate a short correlation id, log the full traceback under it, and
return only ``{"error": "internal error", "correlation_id": "<id>"}``
(or the bare id, for tools whose response is plain text). The caller
quotes the id to an operator, who looks up the full detail in the log.
"""
from __future__ import annotations
import json
import uuid
from mcp_logging import logger
class UserFacingError(Exception):
"""An exception whose ``str()`` is safe to surface to the MCP caller.
Raise this for argument validation and other handler-constructed
messages. Anything else should propagate to ``tool_error_response``
so the original exception text stays on the server side.
"""
def log_tool_error(exc: BaseException, *, context: str) -> str:
"""Log ``exc`` with a fresh correlation id and return the id.
Use this when the caller needs to embed the id in a non-JSON response
shape. The full traceback is recorded at ERROR level against the
``agentmcp`` logger; the returned id is the only thing safe to send back.
"""
cid = uuid.uuid4().hex[:8]
logger.error("tool error in %s (cid=%s)", context, cid, exc_info=exc)
return cid
def tool_error_response(exc: BaseException, *, context: str) -> str:
"""Log ``exc`` and return a sanitised JSON error payload.
Standard use from a tool handler::
except UserFacingError as exc:
return json.dumps({"error": str(exc)})
except Exception as exc:
return tool_error_response(exc, context="get_rows")
"""
cid = log_tool_error(exc, context=context)
return json.dumps({"error": "internal error", "correlation_id": cid})