create_or_update_file
Create or update a file in a GitHub repository by specifying the owner, repo, path, content, commit message, and branch. Includes optional SHA for updating existing files.
Instructions
Create or update a single file in a GitHub repository
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| branch | Yes | Branch to create/update the file in | |
| content | Yes | Content of the file | |
| message | Yes | Commit message | |
| owner | Yes | Repository owner (username or organization) | |
| path | Yes | Path where to create/update the file | |
| repo | Yes | Repository name | |
| sha | No | SHA of the file being replaced (required when updating existing files) |
Implementation Reference
- operations/files.ts:94-131 (handler)Core implementation of the create_or_update_file tool. Encodes content, fetches existing SHA if not provided, and makes a PUT request to GitHub API to create or update the file.export async function createOrUpdateFile( owner: string, repo: string, path: string, content: string, message: string, branch: string, sha?: string ) { const encodedContent = Buffer.from(content).toString("base64"); let currentSha = sha; if (!currentSha) { try { const existingFile = await getFileContents(owner, repo, path, branch); if (!Array.isArray(existingFile)) { currentSha = existingFile.sha; } } catch (error) { console.error("Note: File does not exist in branch, will create new file"); } } const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`; const body = { message, content: encodedContent, branch, ...(currentSha ? { sha: currentSha } : {}), }; const response = await githubRequest(url, { method: "PUT", body, }); return GitHubCreateUpdateFileResponseSchema.parse(response); }
- operations/files.ts:18-26 (schema)Zod schema defining the input parameters for the create_or_update_file tool.export const CreateOrUpdateFileSchema = z.object({ owner: z.string().describe("Repository owner (username or organization)"), repo: z.string().describe("Repository name"), path: z.string().describe("Path where to create/update the file"), content: z.string().describe("Content of the file"), message: z.string().describe("Commit message"), branch: z.string().describe("Branch to create/update the file in"), sha: z.string().optional().describe("SHA of the file being replaced (required when updating existing files)"), });
- index.ts:70-74 (registration)Tool registration in the MCP server's ListTools response, including name, description, and input schema reference.{ name: "create_or_update_file", description: "Create or update a single file in a GitHub repository", inputSchema: zodToJsonSchema(files.CreateOrUpdateFileSchema), },
- index.ts:370-384 (handler)Dispatcher handler in the main CallToolRequestSchema switch statement that parses arguments and delegates to the core createOrUpdateFile function.case "create_or_update_file": { const args = files.CreateOrUpdateFileSchema.parse(request.params.arguments); const result = await files.createOrUpdateFile( args.owner, args.repo, args.path, args.content, args.message, args.branch, args.sha ); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; }