create-or-update-file
Create or update files in GitHub repositories using the GitHub Enterprise MCP Server to manage repository content through direct file operations.
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
- src/tools/files.ts:13-52 (handler)The core handler function for the 'create-or-update-file' tool. Validates input with Zod schema, ensures the branch exists, and uses the GitHub API to create or update the file content.export async function createOrUpdateFile(args: unknown): Promise<any> { const { owner, repo, path, content, message, branch, sha } = CreateOrUpdateFileSchema.parse(args); const github = getGitHubApi(); return tryCatchAsync(async () => { // Check if branch exists, create it if it doesn't const branchExists = await github.branchExists(owner, repo, branch); if (!branchExists) { await github.createBranch(owner, repo, branch); } // Create or update the file const { data } = await github.getOctokit().repos.createOrUpdateFileContents({ owner, repo, path, message, content: utf8ToBase64(content), branch, sha, }); return { content: { name: data.content?.name, path: data.content?.path, sha: data.content?.sha, size: data.content?.size, url: data.content?.html_url, }, commit: { sha: data.commit.sha, url: data.commit.html_url, message: data.commit.message, author: data.commit.author, committer: data.commit.committer, }, }; }, 'Failed to create or update file'); }
- src/utils/validation.ts:15-21 (schema)Zod schema used for input validation in the createOrUpdateFile handler, extending OwnerRepoSchema.export const CreateOrUpdateFileSchema = OwnerRepoSchema.extend({ path: z.string().min(1, 'File path is required'), content: z.string().min(1, 'File content is required'), message: z.string().min(1, 'Commit message is required'), branch: z.string().min(1, 'Branch name is required'), sha: z.string().optional(), });
- src/server.ts:388-425 (registration)Registration of the 'create-or-update-file' tool in the server's ListToolsRequestHandler, including the MCP input schema definition.{ name: 'create-or-update-file', description: 'Create or update a single file in a GitHub repository', inputSchema: { type: 'object', properties: { owner: { type: 'string', description: 'Repository owner (username or organization)', }, repo: { type: 'string', description: 'Repository name', }, path: { type: 'string', description: 'Path where to create/update the file', }, content: { type: 'string', description: 'Content of the file', }, message: { type: 'string', description: 'Commit message', }, branch: { type: 'string', description: 'Branch to create/update the file in', }, sha: { type: 'string', description: 'SHA of the file being replaced (required when updating existing files)', }, }, required: ['owner', 'repo', 'path', 'content', 'message', 'branch'], additionalProperties: false, },
- src/server.ts:1192-1194 (registration)Dispatch to the createOrUpdateFile handler in the CallToolRequestHandler switch statement.case 'create-or-update-file': result = await createOrUpdateFile(parsedArgs); break;