From 94bc5662c4ad3b983276021ce9df7f0bc1b11d5c Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Wed, 21 Oct 2026 00:21:01 -0500 Subject: [PATCH] fix(core): keep unsupported MCP media out of tool attachments (#57640) --- packages/core/src/mcp/client.ts | 4 +- packages/core/src/mime.ts | 10 ++++ packages/core/src/tool-output.ts | 9 +++ packages/core/src/tool/mcp.ts | 35 ++++++++---- packages/core/src/tool/plugin/mcp-resource.ts | 5 +- packages/core/src/tool/plugin/webfetch.ts | 12 +--- packages/core/test/mcp.test.ts | 57 +++++++++++++++++++ 7 files changed, 109 insertions(+), 23 deletions(-) diff --git a/packages/core/src/mcp/client.ts b/packages/core/src/mcp/client.ts index 0848eb4cad5..f652e741cbb 100644 --- a/packages/core/src/mcp/client.ts +++ b/packages/core/src/mcp/client.ts @@ -81,7 +81,7 @@ export class SessionExpiredError extends Schema.TaggedError export type CallToolContent = | { readonly type: "text"; readonly text: string } - | { readonly type: "media"; readonly data: string; readonly mimeType: string } + | { readonly type: "media"; readonly data: string; readonly mimeType: string; readonly uri?: string } export interface CallToolResult { readonly isError: boolean @@ -437,7 +437,7 @@ function toCallToolResult(result: SdkCallToolResult): CallToolResult { const resource = part.resource if ("text" in resource && typeof resource.text === "string") return [{ type: "text", text: resource.text }] if ("blob" in resource && typeof resource.blob === "string" && typeof resource.mimeType === "string") - return [{ type: "media", data: resource.blob, mimeType: resource.mimeType }] + return [{ type: "media", data: resource.blob, mimeType: resource.mimeType, uri: resource.uri }] return [{ type: "text", text: resource.uri }] } return [] diff --git a/packages/core/src/mime.ts b/packages/core/src/mime.ts index 20b469271af..35f449ca86f 100644 --- a/packages/core/src/mime.ts +++ b/packages/core/src/mime.ts @@ -16,6 +16,16 @@ export function detect(bytes: Uint8Array) { return isText(bytes) ? "text/plain" : "application/octet-stream" } +export const isTextual = (mime: string) => + !mime || + mime.startsWith("text/") || + mime === "application/json" || + mime.endsWith("+json") || + mime === "application/xml" || + mime.endsWith("+xml") || + mime === "application/javascript" || + mime === "application/x-javascript" + function startsWith(bytes: Uint8Array, prefix: number[]) { return prefix.every((value, index) => bytes[index] === value) } diff --git a/packages/core/src/tool-output.ts b/packages/core/src/tool-output.ts index cc30f5976b6..f64e1688094 100644 --- a/packages/core/src/tool-output.ts +++ b/packages/core/src/tool-output.ts @@ -30,6 +30,8 @@ export type Editor = { export interface Interface extends State.Transformable { readonly truncate: (result: Result) => Effect.Effect + /** Writes bytes the model cannot receive inline to a retained file and returns its path. */ + readonly save: (bytes: Uint8Array, extension: string) => Effect.Effect readonly cleanup: () => Effect.Effect } @@ -117,10 +119,17 @@ const layer = Layer.effect( } }) + const save = Effect.fnUntraced(function* (bytes: Uint8Array, extension: string) { + const file = path.join(directory, fileName() + extension) + yield* fs.writeWithDirs(file, bytes).pipe(Effect.orDie) + return file + }) + return Service.of({ transform: state.transform, reload: state.reload, truncate, + save, cleanup: () => cleanup(fs, directory), }) }), diff --git a/packages/core/src/tool/mcp.ts b/packages/core/src/tool/mcp.ts index 4ba21e1bfe7..d259162ce6d 100644 --- a/packages/core/src/tool/mcp.ts +++ b/packages/core/src/tool/mcp.ts @@ -4,11 +4,15 @@ import { ToolFailure } from "@opencode/ai" import { McpEvent } from "@opencode/schema/mcp-event" import { Context, Effect, Fiber, type JsonSchema, Layer, PubSub, Semaphore, Stream } from "effect" import { makeLocationNode } from "@opencode/util/effect/app-node" +import { extension } from "mime-types" import { Bus } from "../bus.js" import { Mcp } from "../mcp/index.js" +import { Mime } from "../mime.js" import { Permission } from "../permission.js" import { Tool } from "../tool.js" +import { ToolOutput } from "../tool-output.js" +import { ReadToolFileSystem } from "./read-filesystem.js" /** * Registry namespace and permission action names for MCP tools. @@ -30,6 +34,7 @@ export const layer = Layer.effect( const tools = yield* Tool.Service const bus = yield* Bus.Service const permission = yield* Permission.Service + const toolOutput = yield* ToolOutput.Service const lock = Semaphore.makeUnsafe(1) let discovered: Mcp.Tool[] = [] @@ -83,15 +88,7 @@ export const layer = Layer.effect( .join("\n") .trim() || "MCP tool returned an error", }) - const content = result.content.map((part) => - part.type === "text" - ? { type: "text" as const, text: part.text } - : { - type: "file" as const, - uri: `data:${part.mimeType};base64,${part.data}`, - mime: part.mimeType, - }, - ) + const content = yield* Effect.forEach(result.content, (part) => toContent(part, toolOutput)) const text = content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n") const output = () => { if (result.structured !== undefined) return result.structured @@ -145,8 +142,26 @@ export const layer = Layer.effect( }), ) +// Only media the read tool also accepts is sent inline; other types would fail request lowering on every later turn. +const toContent = Effect.fnUntraced(function* (part: Mcp.ToolResultContent, toolOutput: ToolOutput.Interface) { + if (part.type === "text") return { type: "text" as const, text: part.text } + const mime = part.mimeType.split(";", 1)[0].trim().toLowerCase() + if (ReadToolFileSystem.MEDIA_MIMES.has(mime)) + return { type: "file" as const, uri: `data:${part.mimeType};base64,${part.data}`, mime: part.mimeType } + const bytes = Buffer.from(part.data, "base64") + const label = part.uri ?? "content" + if (Mime.isTextual(mime)) + return { type: "text" as const, text: `[Resource ${label} (${part.mimeType})]\n${bytes.toString("utf8")}` } + const ext = extension(mime) + const file = yield* toolOutput.save(bytes, ext ? `.${ext}` : "") + return { + type: "text" as const, + text: `[Binary resource ${label} (${part.mimeType}, ${bytes.byteLength} bytes) saved to ${file}]`, + } +}) + export const node = makeLocationNode({ service: Service, layer, - deps: [Tool.node, Mcp.node, Bus.node, Permission.node], + deps: [Tool.node, Mcp.node, Bus.node, Permission.node, ToolOutput.node], }) diff --git a/packages/core/src/tool/plugin/mcp-resource.ts b/packages/core/src/tool/plugin/mcp-resource.ts index f7bf6e3db9d..845ab97ad80 100644 --- a/packages/core/src/tool/plugin/mcp-resource.ts +++ b/packages/core/src/tool/plugin/mcp-resource.ts @@ -5,6 +5,7 @@ import type { Context } from "@opencode/plugin/effect/plugin" import { Effect, Schema } from "effect" import { Mcp } from "../../mcp/index.js" import { Permission } from "../../permission.js" +import { ReadToolFileSystem } from "../read-filesystem.js" export const Plugin = { id: "opencode.tools.mcp-resources", @@ -79,7 +80,9 @@ export const Plugin = { return { output: resource, content: resource.contents.flatMap((part) => - part.type === "blob" && (part.mimeType?.startsWith("image/") || part.mimeType === "application/pdf") + part.type === "blob" && + part.mimeType !== undefined && + ReadToolFileSystem.MEDIA_MIMES.has(part.mimeType) ? [{ type: "file" as const, uri: `data:${part.mimeType};base64,${part.blob}`, mime: part.mimeType }] : [], ), diff --git a/packages/core/src/tool/plugin/webfetch.ts b/packages/core/src/tool/plugin/webfetch.ts index 7c40d5b5106..9e94d587e3b 100644 --- a/packages/core/src/tool/plugin/webfetch.ts +++ b/packages/core/src/tool/plugin/webfetch.ts @@ -5,6 +5,7 @@ import { ToolFailure } from "@opencode/ai" import { Duration, Effect, Schema } from "effect" import { HttpClient, type HttpClientError, HttpClientRequest, HttpClientResponse } from "effect/http" import { Parser } from "htmlparser2" +import { Mime } from "../../mime.js" import { Permission } from "../../permission.js" import { convertHTMLToMarkdown, MAX_MARKDOWN_BYTES } from "../html-markdown.js" import { collectBoundedResponseBody } from "../http-body.js" @@ -84,15 +85,6 @@ const collectBody = (response: HttpClientResponse.HttpClientResponse) => const mimeFrom = (contentType: string) => contentType.split(";", 1)[0]?.trim().toLowerCase() ?? "" const isImageAttachment = (mime: string) => mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet" -const isTextualMime = (mime: string) => - !mime || - mime.startsWith("text/") || - mime === "application/json" || - mime.endsWith("+json") || - mime === "application/xml" || - mime.endsWith("+xml") || - mime === "application/javascript" || - mime === "application/x-javascript" const convert = (content: string, contentType: string, format: Format) => { if (!contentType.includes("text/html")) return content if (format === "markdown") return convertHTMLToMarkdown(content) @@ -139,7 +131,7 @@ export const Plugin = { const mime = mimeFrom(contentType) if (isImageAttachment(mime)) return yield* Effect.fail(new Error(`Unsupported fetched image content type: ${mime}`)) - if (!isTextualMime(mime)) + if (!Mime.isTextual(mime)) return yield* Effect.fail(new Error(`Unsupported fetched file content type: ${mime}`)) return { body: yield* collectBody(response), contentType } }).pipe( diff --git a/packages/core/test/mcp.test.ts b/packages/core/test/mcp.test.ts index 7edd7c4e953..bee57c0a0e0 100644 --- a/packages/core/test/mcp.test.ts +++ b/packages/core/test/mcp.test.ts @@ -1,5 +1,6 @@ import path from "node:path" import fs from "node:fs/promises" +import os from "node:os" import { describe, expect, test } from "bun:test" import { Client, InMemoryTransport, StreamableHTTPClientTransport } from "@modelcontextprotocol/client" import { @@ -56,6 +57,7 @@ import { TestClock } from "effect/testing" import { ChildProcess, ChildProcessSpawner } from "effect/process" import { ExitCode, makeHandle, ProcessId } from "effect/process/ChildProcessSpawner" import { Image } from "@opencode/core/image" +import { Global } from "@opencode/util/global" import { advance, drain } from "./lib/clock" import { testEffect } from "./lib/effect" import { registerIntegrationPolicy } from "./fixture/policy" @@ -446,6 +448,13 @@ const mcp = Layer.mock(Mcp.Service, { description: "Returns text and an image", inputSchema: { type: "object", properties: {} }, } satisfies Mcp.Tool, + { + server: Mcp.ServerName.make("direct"), + name: "attachments", + codemode: false, + description: "Returns resource blobs that cannot be sent inline", + inputSchema: { type: "object", properties: {} }, + } satisfies Mcp.Tool, ]), callTool: (input) => Effect.sync(() => { @@ -468,6 +477,26 @@ const mcp = Layer.mock(Mcp.Service, { { type: "media", data: "aGVsbG8=", mimeType: "image/png" }, ], } satisfies Mcp.ToolResult + if (input.name === "attachments") + return { + server: Mcp.ServerName.make(input.server), + tool: input.name, + isError: false, + content: [ + { + type: "media", + data: Buffer.from("# Notes").toString("base64"), + mimeType: "text/markdown", + uri: "file:///README.md", + }, + { + type: "media", + data: "UEFSMQ==", + mimeType: "application/vnd.apache.parquet", + uri: "file:///data.parquet", + }, + ], + } satisfies Mcp.ToolResult if (input.name === "status") return { server: Mcp.ServerName.make(input.server), @@ -507,12 +536,14 @@ const permissions = Layer.mock(Permission.Service, { }), }) const events = Layer.mock(Bus.Service, { subscribe: () => Stream.never }) +const outputData = await fs.mkdtemp(path.join(os.tmpdir(), "opencode-mcp-output-")) const it = testEffect( AppNodeBuilder.build(LayerNode.group([Tool.node, McpTool.node]), [ Mcp.node.replace(mcp), Permission.node.replace(permissions), Bus.node.replace(events), Image.node.replace(imagePassthrough), + Global.node.replace(Global.layerWith({ data: outputData })), ]), ) @@ -2245,6 +2276,7 @@ it.effect("advertises MCP output schemas to Code Mode", () => const execute = toolSet.definitions.find((tool) => tool.name === "execute") expect(toolSet.definitions.map((tool) => tool.name)).toEqual([ + "direct_attachments", "direct_fail", "direct_issues", "direct_lookup", @@ -2426,6 +2458,31 @@ it.effect("preserves MCP text and media content for the model", () => }), ) +it.effect("decodes text blobs and saves other MCP blobs instead of attaching them", () => + Effect.gen(function* () { + assertion = yield* Deferred.make() + decision = Effect.void + const registry = yield* Tool.Service + const registration = yield* McpTool.Service + yield* registration.flush + + const execution = yield* executeTool(registry, { + sessionID: Session.ID.make("ses_mcp_attachments"), + ...toolIdentity, + call: { type: "tool-call", id: "call_mcp_attachments", name: "direct_attachments", input: {} }, + }) + + const text = execution.content?.map((part) => (part.type === "text" ? part.text : part.mime)) ?? [] + const saved = + /^\[Binary resource file:\/\/\/data\.parquet \(application\/vnd\.apache\.parquet, 4 bytes\) saved to (.+)\]$/.exec( + text[1] ?? "", + )?.[1] + expect(text[0]).toBe("[Resource file:///README.md (text/markdown)]\n# Notes") + expect(saved?.startsWith(path.join(outputData, ToolOutput.DIRECTORY, "tool_"))).toBe(true) + expect(yield* Effect.promise(() => fs.readFile(saved ?? "", "utf8"))).toBe("PAR1") + }), +) + it.effect("waits for permission before calling an MCP tool", () => Effect.gen(function* () { calls = 0