Skip to content

Commit acdeee3

Browse files
ochafikclaude
andcommitted
Fix double-stringification of toolOutput in OpenAI transport
Handle different shapes of toolOutput from ChatGPT: - Array of content blocks: use directly - Single content block {type, text}: wrap in array - Object with just {text}: extract and wrap - Other: stringify as fallback This prevents double-stringification when ChatGPT passes content in different formats. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 25e7e43 commit acdeee3

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

src/openai/transport.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -498,18 +498,39 @@ export class OpenAITransport implements Transport {
498498
// Deliver tool output if available (check for both null and undefined)
499499
if (this.openai.toolOutput != null) {
500500
queueMicrotask(() => {
501+
// Normalize toolOutput to MCP content array format
502+
let content: Array<{ type: string; text?: string; [key: string]: unknown }>;
503+
const output = this.openai.toolOutput;
504+
505+
if (Array.isArray(output)) {
506+
// Already an array of content blocks
507+
content = output;
508+
} else if (
509+
typeof output === "object" &&
510+
output !== null &&
511+
"type" in output &&
512+
typeof (output as { type: unknown }).type === "string"
513+
) {
514+
// Single content block object like {type: "text", text: "..."}
515+
content = [output as { type: string; text?: string }];
516+
} else if (
517+
typeof output === "object" &&
518+
output !== null &&
519+
"text" in output &&
520+
typeof (output as { text: unknown }).text === "string"
521+
) {
522+
// Object with just text field - treat as text content
523+
content = [{ type: "text", text: (output as { text: string }).text }];
524+
} else {
525+
// Unknown shape - stringify it
526+
content = [{ type: "text", text: JSON.stringify(output) }];
527+
}
528+
501529
this.onmessage?.({
502530
jsonrpc: "2.0",
503531
method: "ui/notifications/tool-result",
504532
params: {
505-
content: Array.isArray(this.openai.toolOutput)
506-
? this.openai.toolOutput
507-
: [
508-
{
509-
type: "text",
510-
text: JSON.stringify(this.openai.toolOutput),
511-
},
512-
],
533+
content,
513534
// Include _meta from toolResponseMetadata if available (use undefined not null)
514535
_meta: this.openai.toolResponseMetadata ?? undefined,
515536
},

0 commit comments

Comments
 (0)