1+ // Test script for MCP code escaping
2+ // This script tests the code escaping functionality for MCP tools
3+ // It simulates the behavior of useMcpToolTool.ts with different types of input
4+
5+ function testMcpEscaping ( input , description ) {
6+ console . log ( `\n=== Testing ${ description } ===` ) ;
7+ console . log ( "Input:" , input ) ;
8+
9+ try {
10+ // First try to parse as JSON directly (normal behavior)
11+ const parsed = JSON . parse ( input ) ;
12+ console . log ( "✅ Successfully parsed as JSON:" , parsed ) ;
13+ return parsed ;
14+ } catch ( error ) {
15+ console . log ( "❌ Failed to parse as JSON, trying fallback handling..." ) ;
16+
17+ try {
18+ // Check if it looks like a JSON object already (starts with { or [)
19+ const trimmed = input . trim ( ) ;
20+ if (
21+ ( trimmed . startsWith ( "{" ) && trimmed . endsWith ( "}" ) ) ||
22+ ( trimmed . startsWith ( "[" ) && trimmed . endsWith ( "]" ) )
23+ ) {
24+ // If it looks like JSON but couldn't be parsed, then it's truly invalid
25+ console . log ( "❌ Input appears to be invalid JSON" ) ;
26+ return null ;
27+ }
28+
29+ // Otherwise, handle it as a raw string input - automatically place it in a properly escaped JSON object
30+ const parsedArguments = {
31+ input_data : input ,
32+ } ;
33+ console . log ( "✅ Handled as raw string input:" , parsedArguments ) ;
34+ return parsedArguments ;
35+ } catch ( nestedError ) {
36+ console . log ( "❌ Failed to process arguments:" , nestedError ) ;
37+ return null ;
38+ }
39+ }
40+ }
41+
42+ // Test cases
43+ console . log ( "TESTING MCP CODE ESCAPING" ) ;
44+
45+ // Test 1: Valid JSON
46+ testMcpEscaping ( '{"name": "test", "value": 123}' , "Valid JSON" ) ;
47+
48+ // Test 2: Invalid JSON
49+ testMcpEscaping ( '{name: "test", value: 123}' , "Invalid JSON" ) ;
50+
51+ // Test 3: LaTeX code with backslashes and special characters
52+ testMcpEscaping ( `\\stepcounter{fragenummer}
53+ \\arabic{fragenummer}. & \\multicolumn{1}{|p{12cm}|}{\\raggedright #1 } & \\ifthenelse{#2=1}{{
54+ \\color{blue}{X}
55+ }}{} & \\ifthenelse{#2=1}{}{
56+ \\color{blue}{X}
57+ }` , "LaTeX code with special characters" ) ;
58+
59+ // Test 4: Python code with indentation, quotes and backslashes
60+ testMcpEscaping ( `def process_string(s):
61+ # Handle escape sequences
62+ s = s.replace('\\n', '\\\\n')
63+ s = s.replace('\\t', '\\\\t')
64+
65+ # Handle quotes
66+ s = s.replace('"', '\\"')
67+ s = s.replace("'", "\\'")
68+
69+ return f"Processed: {s}"
70+
71+ print(process_string("Hello\\nWorld"))` , "Python code with quotes and backslashes" ) ;
72+
73+ // Test 5: Plain text
74+ testMcpEscaping ( "This is just plain text without any special formatting" , "Plain text" ) ;
75+
76+ console . log ( "\nAll tests completed!" ) ;
0 commit comments