mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-07-08 22:22:17 +02:00
feat(mcp): add response rendering helpers
This commit is contained in:
parent
439796ea15
commit
26dbc64b4a
1 changed files with 28 additions and 0 deletions
28
surfsense_mcp/src/surfsense_mcp/core/rendering.py
Normal file
28
surfsense_mcp/src/surfsense_mcp/core/rendering.py
Normal file
|
|
@ -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]"
|
||||
Loading…
Add table
Add a link
Reference in a new issue