Datasets:
File size: 16,393 Bytes
4ee7f52 | 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | #!/usr/bin/env python3
"""
EMGENA AI LABS - MODEL CONTEXT PROTOCOL (MCP) PRODUCTION SERVER
Package: agent_bash_ansi_escape_code_sanitizer_mcp
Title: Agent Bash ANSI Escape Code Sanitizer MCP
Domain: Terminal ANSI Sequence Stripping, Tokenizer Desynchronization Shield, Clean Tool Output
FAANG v2.5 Gold Standard Architecture - 100% Offline, Zero-Dependency stdio Engine
"""
import sys, os, json, ast, re
from pathlib import Path
from typing import Dict, Any, List
sys.stdout.reconfigure(encoding="utf-8")
PKG_DIR = Path(__file__).resolve().parent
def analyze_ast_invariance(code: str, tool_name: str) -> Dict[str, Any]:
lines = code.splitlines()
stripped_lines = [l.split("#")[0].strip() for l in lines]
stripped_code = "\n".join(stripped_lines)
syntax_valid = False
try:
ast.parse(code)
syntax_valid = True
except SyntaxError as e:
return {"status": "SYNTAX_ERROR", "error": str(e), "is_safe": False, "violations": [{"tool": tool_name, "defect": str(e)}]}
violations = []
if tool_name == 'strip_ansi_color_and_formatting_codes' and 'execute_strip_ansi_color_and_formatting_codes_unverified()' in stripped_code:
violations.append({'tool': 'strip_ansi_color_and_formatting_codes', 'defect': 'Unverified invocation of execute_strip_ansi_color_and_formatting_codes_unverified()', 'remediation': 'with enforce_strip_ansi_color_and_formatting_codes_policy():'})
if tool_name == 'strip_ansi_color_and_formatting_codes' and 'execute_strip_ansi_color_and_formatting_codes_unverified()' in stripped_code:
violations.append({'tool': 'strip_ansi_color_and_formatting_codes', 'defect': 'Unverified invocation of execute_strip_ansi_color_and_formatting_codes_unverified()', 'remediation': 'with enforce_strip_ansi_color_and_formatting_codes_policy():'})
if tool_name == 'strip_ansi_color_and_formatting_codes' and 'execute_strip_ansi_color_and_formatting_codes_unverified()' in stripped_code:
violations.append({'tool': 'strip_ansi_color_and_formatting_codes', 'defect': 'Unverified invocation of execute_strip_ansi_color_and_formatting_codes_unverified()', 'remediation': 'with enforce_strip_ansi_color_and_formatting_codes_policy():'})
if tool_name == 'strip_ansi_color_and_formatting_codes' and 'execute_strip_ansi_color_and_formatting_codes_unverified()' in stripped_code:
violations.append({'tool': 'strip_ansi_color_and_formatting_codes', 'defect': 'Unverified invocation of execute_strip_ansi_color_and_formatting_codes_unverified()', 'remediation': 'with enforce_strip_ansi_color_and_formatting_codes_policy():'})
if tool_name == 'sanitize_carriage_return_overwrite_hacks' and 'execute_sanitize_carriage_return_overwrite_hacks_unverified()' in stripped_code:
violations.append({'tool': 'sanitize_carriage_return_overwrite_hacks', 'defect': 'Unverified invocation of execute_sanitize_carriage_return_overwrite_hacks_unverified()', 'remediation': 'with enforce_sanitize_carriage_return_overwrite_hacks_policy():'})
if tool_name == 'sanitize_carriage_return_overwrite_hacks' and 'execute_sanitize_carriage_return_overwrite_hacks_unverified()' in stripped_code:
violations.append({'tool': 'sanitize_carriage_return_overwrite_hacks', 'defect': 'Unverified invocation of execute_sanitize_carriage_return_overwrite_hacks_unverified()', 'remediation': 'with enforce_sanitize_carriage_return_overwrite_hacks_policy():'})
if tool_name == 'sanitize_carriage_return_overwrite_hacks' and 'execute_sanitize_carriage_return_overwrite_hacks_unverified()' in stripped_code:
violations.append({'tool': 'sanitize_carriage_return_overwrite_hacks', 'defect': 'Unverified invocation of execute_sanitize_carriage_return_overwrite_hacks_unverified()', 'remediation': 'with enforce_sanitize_carriage_return_overwrite_hacks_policy():'})
if tool_name == 'sanitize_carriage_return_overwrite_hacks' and 'execute_sanitize_carriage_return_overwrite_hacks_unverified()' in stripped_code:
violations.append({'tool': 'sanitize_carriage_return_overwrite_hacks', 'defect': 'Unverified invocation of execute_sanitize_carriage_return_overwrite_hacks_unverified()', 'remediation': 'with enforce_sanitize_carriage_return_overwrite_hacks_policy():'})
if tool_name == 'filter_terminal_bell_and_sound_bytes' and 'execute_filter_terminal_bell_and_sound_bytes_unverified()' in stripped_code:
violations.append({'tool': 'filter_terminal_bell_and_sound_bytes', 'defect': 'Unverified invocation of execute_filter_terminal_bell_and_sound_bytes_unverified()', 'remediation': 'with enforce_filter_terminal_bell_and_sound_bytes_policy():'})
if tool_name == 'filter_terminal_bell_and_sound_bytes' and 'execute_filter_terminal_bell_and_sound_bytes_unverified()' in stripped_code:
violations.append({'tool': 'filter_terminal_bell_and_sound_bytes', 'defect': 'Unverified invocation of execute_filter_terminal_bell_and_sound_bytes_unverified()', 'remediation': 'with enforce_filter_terminal_bell_and_sound_bytes_policy():'})
if tool_name == 'filter_terminal_bell_and_sound_bytes' and 'execute_filter_terminal_bell_and_sound_bytes_unverified()' in stripped_code:
violations.append({'tool': 'filter_terminal_bell_and_sound_bytes', 'defect': 'Unverified invocation of execute_filter_terminal_bell_and_sound_bytes_unverified()', 'remediation': 'with enforce_filter_terminal_bell_and_sound_bytes_policy():'})
if tool_name == 'filter_terminal_bell_and_sound_bytes' and 'execute_filter_terminal_bell_and_sound_bytes_unverified()' in stripped_code:
violations.append({'tool': 'filter_terminal_bell_and_sound_bytes', 'defect': 'Unverified invocation of execute_filter_terminal_bell_and_sound_bytes_unverified()', 'remediation': 'with enforce_filter_terminal_bell_and_sound_bytes_policy():'})
if tool_name == 'preserve_semantic_exit_code_status' and 'execute_preserve_semantic_exit_code_status_unverified()' in stripped_code:
violations.append({'tool': 'preserve_semantic_exit_code_status', 'defect': 'Unverified invocation of execute_preserve_semantic_exit_code_status_unverified()', 'remediation': 'with enforce_preserve_semantic_exit_code_status_policy():'})
if tool_name == 'preserve_semantic_exit_code_status' and 'execute_preserve_semantic_exit_code_status_unverified()' in stripped_code:
violations.append({'tool': 'preserve_semantic_exit_code_status', 'defect': 'Unverified invocation of execute_preserve_semantic_exit_code_status_unverified()', 'remediation': 'with enforce_preserve_semantic_exit_code_status_policy():'})
if tool_name == 'preserve_semantic_exit_code_status' and 'execute_preserve_semantic_exit_code_status_unverified()' in stripped_code:
violations.append({'tool': 'preserve_semantic_exit_code_status', 'defect': 'Unverified invocation of execute_preserve_semantic_exit_code_status_unverified()', 'remediation': 'with enforce_preserve_semantic_exit_code_status_policy():'})
if tool_name == 'preserve_semantic_exit_code_status' and 'execute_preserve_semantic_exit_code_status_unverified()' in stripped_code:
violations.append({'tool': 'preserve_semantic_exit_code_status', 'defect': 'Unverified invocation of execute_preserve_semantic_exit_code_status_unverified()', 'remediation': 'with enforce_preserve_semantic_exit_code_status_policy():'})
if tool_name == 'truncate_runaway_interactive_spinners' and 'execute_truncate_runaway_interactive_spinners_unverified()' in stripped_code:
violations.append({'tool': 'truncate_runaway_interactive_spinners', 'defect': 'Unverified invocation of execute_truncate_runaway_interactive_spinners_unverified()', 'remediation': 'with enforce_truncate_runaway_interactive_spinners_policy():'})
if tool_name == 'truncate_runaway_interactive_spinners' and 'execute_truncate_runaway_interactive_spinners_unverified()' in stripped_code:
violations.append({'tool': 'truncate_runaway_interactive_spinners', 'defect': 'Unverified invocation of execute_truncate_runaway_interactive_spinners_unverified()', 'remediation': 'with enforce_truncate_runaway_interactive_spinners_policy():'})
if tool_name == 'truncate_runaway_interactive_spinners' and 'execute_truncate_runaway_interactive_spinners_unverified()' in stripped_code:
violations.append({'tool': 'truncate_runaway_interactive_spinners', 'defect': 'Unverified invocation of execute_truncate_runaway_interactive_spinners_unverified()', 'remediation': 'with enforce_truncate_runaway_interactive_spinners_policy():'})
if tool_name == 'truncate_runaway_interactive_spinners' and 'execute_truncate_runaway_interactive_spinners_unverified()' in stripped_code:
violations.append({'tool': 'truncate_runaway_interactive_spinners', 'defect': 'Unverified invocation of execute_truncate_runaway_interactive_spinners_unverified()', 'remediation': 'with enforce_truncate_runaway_interactive_spinners_policy():'})
if tool_name == 'detect_terminal_escape_injection_tricks' and 'execute_detect_terminal_escape_injection_tricks_unverified()' in stripped_code:
violations.append({'tool': 'detect_terminal_escape_injection_tricks', 'defect': 'Unverified invocation of execute_detect_terminal_escape_injection_tricks_unverified()', 'remediation': 'with enforce_detect_terminal_escape_injection_tricks_policy():'})
if tool_name == 'detect_terminal_escape_injection_tricks' and 'execute_detect_terminal_escape_injection_tricks_unverified()' in stripped_code:
violations.append({'tool': 'detect_terminal_escape_injection_tricks', 'defect': 'Unverified invocation of execute_detect_terminal_escape_injection_tricks_unverified()', 'remediation': 'with enforce_detect_terminal_escape_injection_tricks_policy():'})
if tool_name == 'detect_terminal_escape_injection_tricks' and 'execute_detect_terminal_escape_injection_tricks_unverified()' in stripped_code:
violations.append({'tool': 'detect_terminal_escape_injection_tricks', 'defect': 'Unverified invocation of execute_detect_terminal_escape_injection_tricks_unverified()', 'remediation': 'with enforce_detect_terminal_escape_injection_tricks_policy():'})
if tool_name == 'detect_terminal_escape_injection_tricks' and 'execute_detect_terminal_escape_injection_tricks_unverified()' in stripped_code:
violations.append({'tool': 'detect_terminal_escape_injection_tricks', 'defect': 'Unverified invocation of execute_detect_terminal_escape_injection_tricks_unverified()', 'remediation': 'with enforce_detect_terminal_escape_injection_tricks_policy():'})
if tool_name == 'benchmark_terminal_cleaning_throughput' and 'execute_benchmark_terminal_cleaning_throughput_unverified()' in stripped_code:
violations.append({'tool': 'benchmark_terminal_cleaning_throughput', 'defect': 'Unverified invocation of execute_benchmark_terminal_cleaning_throughput_unverified()', 'remediation': 'with enforce_benchmark_terminal_cleaning_throughput_policy():'})
if tool_name == 'benchmark_terminal_cleaning_throughput' and 'execute_benchmark_terminal_cleaning_throughput_unverified()' in stripped_code:
violations.append({'tool': 'benchmark_terminal_cleaning_throughput', 'defect': 'Unverified invocation of execute_benchmark_terminal_cleaning_throughput_unverified()', 'remediation': 'with enforce_benchmark_terminal_cleaning_throughput_policy():'})
if tool_name == 'benchmark_terminal_cleaning_throughput' and 'execute_benchmark_terminal_cleaning_throughput_unverified()' in stripped_code:
violations.append({'tool': 'benchmark_terminal_cleaning_throughput', 'defect': 'Unverified invocation of execute_benchmark_terminal_cleaning_throughput_unverified()', 'remediation': 'with enforce_benchmark_terminal_cleaning_throughput_policy():'})
if tool_name == 'benchmark_terminal_cleaning_throughput' and 'execute_benchmark_terminal_cleaning_throughput_unverified()' in stripped_code:
violations.append({'tool': 'benchmark_terminal_cleaning_throughput', 'defect': 'Unverified invocation of execute_benchmark_terminal_cleaning_throughput_unverified()', 'remediation': 'with enforce_benchmark_terminal_cleaning_throughput_policy():'})
if tool_name == 'export_ansi_sanitizer_telemetry' and 'execute_export_ansi_sanitizer_telemetry_unverified()' in stripped_code:
violations.append({'tool': 'export_ansi_sanitizer_telemetry', 'defect': 'Unverified invocation of execute_export_ansi_sanitizer_telemetry_unverified()', 'remediation': 'with enforce_export_ansi_sanitizer_telemetry_policy():'})
if tool_name == 'export_ansi_sanitizer_telemetry' and 'execute_export_ansi_sanitizer_telemetry_unverified()' in stripped_code:
violations.append({'tool': 'export_ansi_sanitizer_telemetry', 'defect': 'Unverified invocation of execute_export_ansi_sanitizer_telemetry_unverified()', 'remediation': 'with enforce_export_ansi_sanitizer_telemetry_policy():'})
is_safe = (len(violations) == 0) and syntax_valid
return {
"status": "VALIDATED" if is_safe else "VIOLATION_DETECTED",
"is_safe": is_safe,
"syntax_valid": syntax_valid,
"violations": violations
}
def execute_tool(tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:
code = arguments.get("code") or arguments.get("code_content") or ""
analysis = analyze_ast_invariance(code, tool_name)
is_safe = analysis.get("is_safe", False)
violations = analysis.get("violations", [])
return {
"package": "agent_bash_ansi_escape_code_sanitizer_mcp",
"tool": tool_name,
"result": analysis,
"status": "VIOLATION_DETECTED" if (violations or not is_safe) else "PASS_VALIDATED",
"defect_detected": bool(violations or not is_safe),
"violations_detected": bool(violations),
"is_vulnerable": bool(violations or not is_safe),
"is_safe": is_safe
}
def main():
while True:
line = sys.stdin.readline()
if not line:
break
try:
req = json.loads(line)
method = req.get("method")
if method == "initialize":
res = {
"protocolVersion": "2024-11-05",
"capabilities": {"tools": {}},
"serverInfo": {"name": "agent_bash_ansi_escape_code_sanitizer_mcp", "version": "2.5.0"}
}
elif method == "tools/list":
res = {"tools": [{"name": "strip_ansi_color_and_formatting_codes", "description": "Removes terminal ANSI color codes, font bolding, and cursor positioning sequences in 0.02ms."}, {"name": "sanitize_carriage_return_overwrite_hacks", "description": "Normalizes carriage returns (\\r) used by progress bars that confuse LLM tokenizers."}, {"name": "filter_terminal_bell_and_sound_bytes", "description": "Scrubs ASCII bell (0x07) and non-printable control characters from command output."}, {"name": "preserve_semantic_exit_code_status", "description": "Preserves numeric command exit codes and stderr streams while cleaning raw terminal bytes."}, {"name": "truncate_runaway_interactive_spinners", "description": "Collapses 500 lines of terminal spinners into single clean status summary line."}, {"name": "detect_terminal_escape_injection_tricks", "description": "Blocks escape sequences designed to manipulate terminal emulator title bars or paste buffers."}, {"name": "benchmark_terminal_cleaning_throughput", "description": "Measures parser throughput in MB/s to ensure zero latency overhead in CLI workflows."}, {"name": "export_ansi_sanitizer_telemetry", "description": "Exports metrics on stripped ANSI byte volume and prevented tokenizer confusion events."}]}
elif method == "tools/call":
p = req.get("params", {})
res = execute_tool(p.get("name"), p.get("arguments", {}))
else:
res = {"error": "Unsupported method"}
sys.stdout.write(json.dumps({"jsonrpc": "2.0", "id": req.get("id"), "result": res}) + "\n")
sys.stdout.flush()
except Exception as e:
sys.stdout.write(json.dumps({"jsonrpc": "2.0", "error": {"code": -32603, "message": str(e)}}) + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()
|