fix(core): keep unsupported MCP media out of tool attachments (#57640)

This commit is contained in:
Aiden Cline
2026-10-21 00:21:01 -05:00
committed by GitHub
Unverified
parent 15adc3d91e
commit 94bc5662c4
7 changed files with 109 additions and 23 deletions
+2 -2
View File
@@ -81,7 +81,7 @@ export class SessionExpiredError extends Schema.TaggedError<SessionExpiredError>
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 []
+10
View File
@@ -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)
}
+9
View File
@@ -30,6 +30,8 @@ export type Editor = {
export interface Interface extends State.Transformable<Editor> {
readonly truncate: (result: Result) => Effect.Effect<Result>
/** Writes bytes the model cannot receive inline to a retained file and returns its path. */
readonly save: (bytes: Uint8Array, extension: string) => Effect.Effect<string>
readonly cleanup: () => Effect.Effect<void>
}
@@ -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),
})
}),
+25 -10
View File
@@ -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],
})
@@ -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 }]
: [],
),
+2 -10
View File
@@ -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(
+57
View File
@@ -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<Permission.AssertInput>()
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