From 26dbc64b4aee24c48e798368a29ed50cd07bbc34 Mon Sep 17 00:00:00 2001 From: CREDO23 Date: Mon, 6 Jul 2026 02:29:19 +0200 Subject: [PATCH] feat(mcp): add response rendering helpers --- .../src/surfsense_mcp/core/rendering.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 surfsense_mcp/src/surfsense_mcp/core/rendering.py diff --git a/surfsense_mcp/src/surfsense_mcp/core/rendering.py b/surfsense_mcp/src/surfsense_mcp/core/rendering.py new file mode 100644 index 000000000..9fcb6b9af --- /dev/null +++ b/surfsense_mcp/src/surfsense_mcp/core/rendering.py @@ -0,0 +1,28 @@ +"""Shape tool results into the format the caller asked for. + +Tools default to Markdown (readable for the model and the human watching), and +can return raw JSON when a caller wants to post-process the data. Large payloads +are clipped so a single call can't blow the context window. +""" + +from __future__ import annotations + +import json +from typing import Any, Literal + +ResponseFormat = Literal["markdown", "json"] + +DEFAULT_CLIP_CHARS = 20_000 + + +def to_json(payload: Any) -> str: + """Pretty-print a payload as JSON, tolerating non-serializable values.""" + return json.dumps(payload, indent=2, ensure_ascii=False, default=str) + + +def clip(text: str, limit: int = DEFAULT_CLIP_CHARS) -> str: + """Trim overlong text, leaving a visible marker of how much was dropped.""" + if len(text) <= limit: + return text + dropped = len(text) - limit + return f"{text[:limit]}\n\n… [{dropped} more characters truncated]"