push_files
Push multiple files to a GitHub repository in a single commit by providing owner, repo, branch, an array of file objects with path and content, and a commit message.
Instructions
Push multiple files to a GitHub repository in a single commit
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| owner | Yes | Repository owner | |
| repo | Yes | Repository name | |
| branch | Yes | Branch to push to | |
| files | Yes | Array of file objects to push, each object with path (string) and content (string) | |
| message | Yes | Commit message |
Implementation Reference
- src/tools/repositories.ts:838-913 (registration)Registration of the 'push_files' tool on the MCP server, defining its name, description, input schema, and async handler.
// Tool: Push Files server.tool( "push_files", "Push multiple files to a GitHub repository in a single commit", { owner: z.string().describe("Repository owner"), repo: z.string().describe("Repository name"), branch: z.string().describe("Branch to push to"), files: z .array(z.object({ path: z.string(), content: z.string() })) .describe( "Array of file objects to push, each object with path (string) and content (string)", ), message: z.string().describe("Commit message"), }, async ({ owner, repo, branch, files, message }) => { try { // Get the reference for the branch const refResp = await octokit.rest.git.getRef({ owner, repo, ref: `heads/${branch}`, }) const baseSha = refResp.data.object.sha // Get the commit object that the branch points to const baseCommit = await octokit.rest.git.getCommit({ owner, repo, commit_sha: baseSha, }) // Create tree entries for all files const treeItems = files.map(file => ({ path: file.path, mode: "100644" as const, // Regular file mode type: "blob" as const, content: file.content, })) // Create a new tree with the file entries const newTree = await octokit.rest.git.createTree({ owner, repo, base_tree: baseCommit.data.tree.sha, tree: treeItems, }) // Create a new commit const newCommit = await octokit.rest.git.createCommit({ owner, repo, message, tree: newTree.data.sha, parents: [baseSha], }) // Update the reference to point to the new commit const updatedRef = await octokit.rest.git.updateRef({ owner, repo, ref: `heads/${branch}`, sha: newCommit.data.sha, force: false, }) return { content: [{ type: "text", text: `Files pushed successfully to **${owner}/${repo}** branch \`${branch}\`\nCommit SHA: ${newCommit.data.sha}\nMessage: ${message}\nFiles: ${files.map((f: any) => f.path).join(", ")}` }], } } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.message}` }], } } }, ) - src/tools/repositories.ts:853-913 (handler)Handler function for 'push_files' that pushes multiple files to a GitHub repository: gets branch ref, creates tree entries, creates a commit, and updates the branch ref.
async ({ owner, repo, branch, files, message }) => { try { // Get the reference for the branch const refResp = await octokit.rest.git.getRef({ owner, repo, ref: `heads/${branch}`, }) const baseSha = refResp.data.object.sha // Get the commit object that the branch points to const baseCommit = await octokit.rest.git.getCommit({ owner, repo, commit_sha: baseSha, }) // Create tree entries for all files const treeItems = files.map(file => ({ path: file.path, mode: "100644" as const, // Regular file mode type: "blob" as const, content: file.content, })) // Create a new tree with the file entries const newTree = await octokit.rest.git.createTree({ owner, repo, base_tree: baseCommit.data.tree.sha, tree: treeItems, }) // Create a new commit const newCommit = await octokit.rest.git.createCommit({ owner, repo, message, tree: newTree.data.sha, parents: [baseSha], }) // Update the reference to point to the new commit const updatedRef = await octokit.rest.git.updateRef({ owner, repo, ref: `heads/${branch}`, sha: newCommit.data.sha, force: false, }) return { content: [{ type: "text", text: `Files pushed successfully to **${owner}/${repo}** branch \`${branch}\`\nCommit SHA: ${newCommit.data.sha}\nMessage: ${message}\nFiles: ${files.map((f: any) => f.path).join(", ")}` }], } } catch (e: any) { return { content: [{ type: "text", text: `Error: ${e.message}` }], } } }, ) - src/tools/repositories.ts:842-852 (schema)Input schema for 'push_files': owner (string), repo (string), branch (string), files (array of {path, content} objects), and message (string).
{ owner: z.string().describe("Repository owner"), repo: z.string().describe("Repository name"), branch: z.string().describe("Branch to push to"), files: z .array(z.object({ path: z.string(), content: z.string() })) .describe( "Array of file objects to push, each object with path (string) and content (string)", ), message: z.string().describe("Commit message"), },