diff --git a/src/mcp/github-file-ops-server.ts b/src/mcp/github-file-ops-server.ts index 9a769af..ef03178 100644 --- a/src/mcp/github-file-ops-server.ts +++ b/src/mcp/github-file-ops-server.ts @@ -125,13 +125,58 @@ server.tool( ? filePath : join(REPO_DIR, filePath); - const content = await readFile(fullPath, "utf-8"); - return { - path: filePath, - mode: "100644", - type: "blob", - content: content, - }; + // Check if file is binary (images, etc.) + const isBinaryFile = + /\.(png|jpg|jpeg|gif|webp|ico|pdf|zip|tar|gz|exe|bin|woff|woff2|ttf|eot)$/i.test( + filePath, + ); + + if (isBinaryFile) { + // For binary files, create a blob first using the Blobs API + const binaryContent = await readFile(fullPath); + + // Create blob using Blobs API (supports encoding parameter) + const blobUrl = `${GITHUB_API_URL}/repos/${owner}/${repo}/git/blobs`; + const blobResponse = await fetch(blobUrl, { + method: "POST", + headers: { + Accept: "application/vnd.github+json", + Authorization: `Bearer ${githubToken}`, + "X-GitHub-Api-Version": "2022-11-28", + "Content-Type": "application/json", + }, + body: JSON.stringify({ + content: binaryContent.toString("base64"), + encoding: "base64", + }), + }); + + if (!blobResponse.ok) { + const errorText = await blobResponse.text(); + throw new Error( + `Failed to create blob for ${filePath}: ${blobResponse.status} - ${errorText}`, + ); + } + + const blobData = (await blobResponse.json()) as { sha: string }; + + // Return tree entry with blob SHA + return { + path: filePath, + mode: "100644", + type: "blob", + sha: blobData.sha, + }; + } else { + // For text files, include content directly in tree + const content = await readFile(fullPath, "utf-8"); + return { + path: filePath, + mode: "100644", + type: "blob", + content: content, + }; + } }), );