Skip to main content
Glama

create_or_update_file

Create a new file or update an existing one in a GitHub repository. Provide the file path, content, commit message, and branch; include the current SHA when updating.

Instructions

Create or update a single file in a GitHub repository. If updating an existing file, you must provide the current SHA of the file (the full 40-character SHA, not a shortened version).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
ownerYesRepository owner (username or organization)
repoYesRepository name
pathYesPath where to create/update the file
contentYesContent of the file
messageYesCommit message
branchYesBranch to create/update the file in
shaNoFull SHA of the current file blob (required for updates, must be the complete 40-character SHA)

Implementation Reference

  • Registration of the create_or_update_file tool via server.tool() within the registerRepositoryTools function
    server.tool(
    	"create_or_update_file",
  • Input schema for create_or_update_file: owner, repo, path, content, message, branch, and optional sha (40-character full SHA required for updates)
    {
    	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(
    			"Full SHA of the current file blob (required for updates, must be the complete 40-character SHA)",
    		),
    },
  • Handler function for create_or_update_file: validates SHA length (must be 40 chars), calls octokit.rest.repos.createOrUpdateFileContents with base64-encoded content, formats response as markdown with file and commit details, and provides helpful error messages for SHA mismatches
    	async ({ owner, repo, path, content, message, branch, sha }) => {
    		try {
    			// If SHA is provided, validate it's the full SHA
    			if (sha && sha.length !== 40) {
    				return {
    					content: [
    						{
    							type: "text",
    							text: `Error: SHA must be the full 40-character blob SHA. Provided SHA "${sha}" is only ${sha.length} characters. Use get_file_contents to retrieve the full SHA.`,
    						},
    					],
    				}
    			}
    
    			const response = await octokit.rest.repos.createOrUpdateFileContents({
    				owner,
    				repo,
    				path,
    				message,
    				content: Buffer.from(content).toString("base64"),
    				branch,
    				sha,
    			})
    
    			// Format response as markdown
    			let markdown = `# File ${response.data.commit.message}\n\n`
    			markdown += `**Path:** ${response.data.content?.path || path}\n`
    			markdown += `**SHA:** ${response.data.content?.sha || "N/A"}\n`
    			markdown += `**Size:** ${response.data.content?.size || 0} bytes\n\n`
    			markdown += `## Commit Details\n\n`
    			markdown += `- **Commit SHA:** ${response.data.commit.sha}\n`
    			markdown += `- **Author:** ${response.data.commit.author?.name} <${response.data.commit.author?.email}>\n`
    			markdown += `- **Date:** ${response.data.commit.author?.date}\n`
    			markdown += `- **URL:** ${response.data.commit.html_url}\n`
    
    			return {
    				content: [{ type: "text", text: markdown }],
    			}
    		} catch (e: any) {
    			// Provide more helpful error messages
    			if (e.message.includes("does not match")) {
    				return {
    					content: [
    						{
    							type: "text",
    							text: `Error: SHA mismatch. The provided SHA does not match the current file's SHA. This usually means the file has been modified since you last retrieved it. Use get_file_contents to get the current SHA, then retry the update.\n\nOriginal error: ${e.message}`,
    						},
    					],
    				}
    			}
    			return {
    				content: [{ type: "text", text: `Error: ${e.message}` }],
    			}
    		}
    	},
    )
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It mentions the SHA requirement for updates but does not disclose what happens if a file already exists without SHA, permission needs, rate limits, or return behavior. Significant behavioral gaps remain.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is two short sentences, front-loading the main action and a critical requirement. Every sentence is meaningful with no redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

With 7 parameters, no output schema, many sibling tools, and no annotations, the description is insufficient. It fails to explain return values, behavior on creation vs update, and how it differs from push_files, leaving agents underinformed.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so baseline is 3. The description adds minimal value beyond repeating that SHA must be full 40-character, which is also in the schema. No other parameter details are enhanced.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool creates or updates a file in a GitHub repository. The verb 'create or update' and resource 'single file' are specific, but it does not explicitly differentiate from sibling tools like push_files, which might handle multiple files.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides guidance on when to provide SHA (for updates) but does not clarify when to use this tool versus alternatives like push_files or get_file_contents. Usage context is implied but not explicit.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/hithereiamaliff/mcp-github'

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