Skip to main content

escape_display

Function escape_display 

Source
pub fn escape_display(s: &str) -> String
Expand description

Escapes a string for safe interpolation into hand-crafted Text/Pretty output lines.

Commands that render whole structs through format_output get escaping for free, since every string value is serialized through serde_json before printing. Commands that instead build freeform lines (e.g. "Server: {name} ({id})") must escape server-supplied strings themselves, or a malicious MCP server could inject raw ANSI/control escape sequences into the user’s terminal via handshake or tool metadata fields. pretty’s internal value formatter delegates to this same function for its String values, so control characters (including ESC) are backslash-escaped instead of passed through verbatim, and both call sites share one implementation. The returned string is always JSON-quoted, even for input with no control characters, since callers need one consistent (and unambiguous) rendering rather than conditionally-quoted output.

§Examples

use mcp_execution_cli::formatters::escape_display;

assert_eq!(escape_display("hello"), "\"hello\"");
assert_eq!(escape_display("esc\u{1b}[2J"), "\"esc\\u001b[2J\"");