Skip to content

Commit 227083d

Browse files
fix: 修复截图 MIME 类型硬编码导致 API 拒绝的问题
macOS screencapture 输出 PNG,但代码硬编码 mimeType 为 image/jpeg, 导致 API 报错 "specified using image/jpeg but appears to be image/png"。 改为通过 magic bytes 检测实际图片格式。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 14c46df commit 227083d

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

packages/@ant/computer-use-mcp/src/toolCalls.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
3838
import { randomUUID } from "node:crypto";
3939

40+
/** Detect actual image MIME type from base64 data using magic bytes. */
41+
function detectMimeFromBase64(b64: string): string {
42+
// First byte is enough to distinguish PNG (0x89) from JPEG (0xFF)
43+
const c = b64.charCodeAt(0);
44+
if (c === 0x89) return "image/png";
45+
if (c === 0xFF) return "image/jpeg";
46+
// RIFF = WebP
47+
if (c === 0x52) return "image/webp";
48+
// GIF
49+
if (c === 0x47) return "image/gif";
50+
return "image/png";
51+
}
52+
4053
import { getDefaultTierForApp, getDeniedCategoryForApp, isPolicyDenied } from "./deniedApps.js";
4154
import type {
4255
ComputerExecutor,
@@ -2162,7 +2175,7 @@ async function handleScreenshot(
21622175
{
21632176
type: "image",
21642177
data: shot.base64,
2165-
mimeType: "image/jpeg",
2178+
mimeType: detectMimeFromBase64(shot.base64),
21662179
},
21672180
],
21682181
screenshot: shot,
@@ -2231,7 +2244,7 @@ async function handleScreenshot(
22312244
{
22322245
type: "image",
22332246
data: shot.base64,
2234-
mimeType: "image/jpeg",
2247+
mimeType: detectMimeFromBase64(shot.base64),
22352248
},
22362249
],
22372250
// Piggybacked for serverDef.ts to stash on InternalServerContext.
@@ -2310,7 +2323,7 @@ async function handleZoom(
23102323

23112324
// Return the image. NO `.screenshot` piggyback — this is the invariant.
23122325
return {
2313-
content: [{ type: "image", data: zoomed.base64, mimeType: "image/jpeg" }],
2326+
content: [{ type: "image", data: zoomed.base64, mimeType: detectMimeFromBase64(zoomed.base64) }],
23142327
};
23152328
}
23162329

src/utils/computerUse/wrapper.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { getSessionId } from '../../bootstrap/state.js'
3030
import { ComputerUseApproval } from '../../components/permissions/ComputerUseApproval/ComputerUseApproval.js'
3131
import type { Tool, ToolUseContext } from '../../Tool.js'
3232
import { logForDebugging } from '../debug.js'
33+
import { detectImageFormatFromBase64 } from '../imageResizer.js'
3334
import {
3435
checkComputerUseLock,
3536
tryAcquireComputerUseLock,
@@ -330,7 +331,7 @@ export function getComputerUseMCPToolOverrides(
330331
type: 'image' as const,
331332
source: {
332333
type: 'base64' as const,
333-
media_type: item.mimeType ?? 'image/jpeg',
334+
media_type: item.mimeType ?? detectImageFormatFromBase64(item.data),
334335
data: item.data,
335336
},
336337
}

0 commit comments

Comments
 (0)