diff --git a/openrouter/server/tools/llm-binding.ts b/openrouter/server/tools/llm-binding.ts index 5fb36b55..0432e7ce 100644 --- a/openrouter/server/tools/llm-binding.ts +++ b/openrouter/server/tools/llm-binding.ts @@ -481,7 +481,7 @@ export const createLLMStreamTool = (env: Env) => execute: async ({ context }) => { const { modelId, - callOptions: { abortSignal: _abortSignal, ...callOptions }, + callOptions: { abortSignal: _abortSignal, ...callOptions } = {}, } = context; env.MESH_REQUEST_CONTEXT.ensureAuthenticated(); @@ -516,7 +516,7 @@ export const createLLMGenerateTool = (env: Env) => execute: async ({ context }) => { const { modelId, - callOptions: { abortSignal: _abortSignal, ...callOptions }, + callOptions: { abortSignal: _abortSignal, ...callOptions } = {}, } = context; env.MESH_REQUEST_CONTEXT.ensureAuthenticated(); @@ -531,6 +531,12 @@ export const createLLMGenerateTool = (env: Env) => callOptions as LanguageModelV2CallOptions, ); + // Clean up response.body if it's a Uint8Array (raw HTTP response) + // This ensures JSON serialization works correctly over MCP + if (result?.response?.body instanceof Uint8Array) { + result.response.body = new TextDecoder().decode(result.response.body); + } + return result as unknown as z.infer; }, });