Skip to main content
Glama

create_or_update_file

Create or update a single file in a GitHub repository by specifying the owner, repo, path, content, branch, and commit message. Automate file management with ease.

Instructions

Create or update a single file in a GitHub repository

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
branchYesBranch to create/update the file in
contentYesContent of the file
messageYesCommit message
ownerYesRepository owner (username or organization)
pathYesPath where to create/update the file
repoYesRepository name
shaNoSHA of the file being replaced (required when updating existing files)

Implementation Reference

  • Main handler function that implements the create_or_update_file tool logic: optionally fetches existing file SHA, base64-encodes the content, and sends a PUT request to the GitHub Contents API to create or update the file.
    export async function createOrUpdateFile( github_pat: string, owner: string, repo: string, path: string, content: string, message: string, branch: string, sha?: string ) { let currentSha = sha; if (!currentSha) { try { const existingFile = await getFileContents({github_pat, 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 encodedContent = Buffer.from(content).toString("base64"); const url = `https://api.github.com/repos/${owner}/${repo}/contents/${path}`; const body = { message, content: encodedContent, branch, ...(currentSha ? { sha: currentSha } : {}), }; const response = await githubRequest(github_pat, url, { method: "PUT", body, }); return GitHubCreateUpdateFileResponseSchema.parse(response); }
  • Zod schemas for input validation: CreateOrUpdateFileSchema (public tool input) and _CreateOrUpdateFileSchema (internal with github_pat).
    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)"), }); export const _CreateOrUpdateFileSchema = CreateOrUpdateFileSchema.extend({ github_pat: z.string().describe("GitHub Personal Access Token"), });
  • src/index.ts:78-82 (registration)
    Tool registration in the ListTools response, specifying name, description, and input schema.
    { name: "create_or_update_file", description: "Create or update a single file in a GitHub repository", inputSchema: zodToJsonSchema(files.CreateOrUpdateFileSchema), },
  • src/index.ts:405-420 (registration)
    Dispatch handler in CallToolRequest that parses arguments with internal schema and calls the createOrUpdateFile function.
    case "create_or_update_file": { const args = files._CreateOrUpdateFileSchema.parse(params.arguments); const result = await files.createOrUpdateFile( args.github_pat, args.owner, args.repo, args.path, args.content, args.message, args.branch, args.sha ); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/MissionSquad/mcp-github'

If you have feedback or need assistance with the MCP directory API, please join our Discord server