Skip to content

Commit 3488bdb

Browse files
committed
feat: Add utility function to escape Unicode characters in tool results
1 parent 043f604 commit 3488bdb

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

client/src/components/ToolsTab.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,31 @@ import { Textarea } from "@/components/ui/textarea";
88
import DynamicJsonForm, { JsonSchemaType, JsonValue } from "./DynamicJsonForm";
99
import { generateDefaultValue } from "@/utils/schemaUtils";
1010
import {
11+
CallToolResultSchema,
12+
CompatibilityCallToolResult,
1113
ListToolsResult,
1214
Tool,
13-
CallToolResultSchema,
1415
} from "@modelcontextprotocol/sdk/types.js";
1516
import { AlertCircle, Send } from "lucide-react";
1617
import { useEffect, useState } from "react";
1718
import ListPane from "./ListPane";
1819

19-
import { CompatibilityCallToolResult } from "@modelcontextprotocol/sdk/types.js";
20+
// Utility function to escape Unicode characters
21+
function escapeUnicode(obj: any): string {
22+
return JSON.stringify(
23+
obj,
24+
(_key: string, value) => {
25+
if (typeof value === "string") {
26+
// Replace non-ASCII characters with their Unicode escape sequences
27+
return value.replace(/[^\0-\x7F]/g, (char) => {
28+
return "\\u" + ("0000" + char.charCodeAt(0).toString(16)).slice(-4);
29+
});
30+
}
31+
return value;
32+
},
33+
2,
34+
);
35+
}
2036

2137
const ToolsTab = ({
2238
tools,
@@ -54,15 +70,15 @@ const ToolsTab = ({
5470
<>
5571
<h4 className="font-semibold mb-2">Invalid Tool Result:</h4>
5672
<pre className="bg-gray-50 dark:bg-gray-800 dark:text-gray-100 p-4 rounded text-sm overflow-auto max-h-64">
57-
{JSON.stringify(toolResult, null, 2)}
73+
{escapeUnicode(toolResult)}
5874
</pre>
5975
<h4 className="font-semibold mb-2">Errors:</h4>
6076
{parsedResult.error.errors.map((error, idx) => (
6177
<pre
6278
key={idx}
6379
className="bg-gray-50 dark:bg-gray-800 dark:text-gray-100 p-4 rounded text-sm overflow-auto max-h-64"
6480
>
65-
{JSON.stringify(error, null, 2)}
81+
{escapeUnicode(error)}
6682
</pre>
6783
))}
6884
</>
@@ -101,7 +117,7 @@ const ToolsTab = ({
101117
</audio>
102118
) : (
103119
<pre className="bg-gray-50 dark:bg-gray-800 dark:text-gray-100 whitespace-pre-wrap break-words p-4 rounded text-sm overflow-auto max-h-64">
104-
{JSON.stringify(item.resource, null, 2)}
120+
{escapeUnicode(item.resource)}
105121
</pre>
106122
))}
107123
</div>
@@ -113,7 +129,7 @@ const ToolsTab = ({
113129
<>
114130
<h4 className="font-semibold mb-2">Tool Result (Legacy):</h4>
115131
<pre className="bg-gray-50 dark:bg-gray-800 dark:text-gray-100 p-4 rounded text-sm overflow-auto max-h-64">
116-
{JSON.stringify(toolResult.toolResult, null, 2)}
132+
{escapeUnicode(toolResult.toolResult)}
117133
</pre>
118134
</>
119135
);

0 commit comments

Comments
 (0)