Skip to main content
Glama

create_or_update_file

Create or update files in GitHub repositories by specifying content, path, and commit details to manage repository files directly.

Instructions

Create or update a single file in a GitHub repository

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
shaNoSHA of the file being replaced (required when updating existing files)

Implementation Reference

  • Implements the core logic for creating or updating a file in a GitHub repository. Checks if the file exists to get SHA for updates, encodes content to base64, and uses GitHub Contents API PUT endpoint.
    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 schema defining the input parameters for the create_or_update_file tool, used for validation and JSON schema generation.
    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)"),
    });
  • src/index.ts:79-82 (registration)
    Registers the tool in the listTools handler by providing 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)
    Dispatches calls to the createOrUpdateFile handler function in the main CallToolRequestSchema handler.
    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) }],
      };
    }
  • Extended schema including github_pat, used internally for parsing arguments in the dispatcher.
    export const _CreateOrUpdateFileSchema = CreateOrUpdateFileSchema.extend({
      github_pat: z.string().describe("GitHub Personal Access Token"),
    });
Behavior2/5

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

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action but doesn't mention critical behavioral aspects like required permissions (e.g., write access to the repo), whether it's idempotent, how conflicts are handled, or what happens on failure. For a mutation tool with zero annotation coverage, this is a significant gap.

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 a single, efficient sentence that directly states the tool's purpose without unnecessary words. It's front-loaded with the core action and resource, making it easy to parse quickly.

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?

For a complex mutation tool with 7 parameters, no annotations, and no output schema, the description is inadequate. It doesn't address behavioral traits (permissions, idempotency, error handling), usage context relative to siblings, or what the tool returns. The 100% schema coverage helps with parameters but doesn't compensate for other gaps.

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 description coverage is 100%, so the schema already documents all 7 parameters thoroughly. The description doesn't add any parameter-specific information beyond what's in the schema (e.g., it doesn't explain the relationship between 'sha' and update operations). Baseline 3 is appropriate when the schema does the heavy lifting.

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 action ('create or update') and resource ('a single file in a GitHub repository'), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'push_files' or 'get_file_contents', which could handle similar file operations, so it doesn't reach the highest score.

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

Usage Guidelines2/5

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

The description provides no guidance on when to use this tool versus alternatives. With sibling tools like 'push_files' (for multiple files) and 'get_file_contents' (for reading), there's no indication of when this single-file create/update operation is preferred, leaving usage context unclear.

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

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