Skip to main content
Glama

copy_file

Copy files or directories between locations with options to overwrite existing files and copy recursively.

Instructions

Copy a file or directory

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sourceYesSource path
destinationYesDestination path
overwriteNoOverwrite if exists
recursiveNoCopy directories recursively

Implementation Reference

  • The implementation of the `copy_file` logic, including path validation, directory/file handling, and error reporting.
    async function copyFileImpl(input: CopyFileInput): Promise<ToolResult> {
      try {
        const srcPath = path.resolve(input.source);
        const destPath = path.resolve(input.destination);
    
        // Check if source exists
        const srcStats = await fs.stat(srcPath);
    
        // Check if destination exists
        let destExists = false;
        try {
          await fs.access(destPath);
          destExists = true;
        } catch {
          // Destination doesn't exist
        }
    
        if (destExists && !input.overwrite) {
          return {
            isError: true,
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  code: 'ALREADY_EXISTS',
                  message: `Destination already exists and overwrite is false: ${input.destination}`,
                }),
              },
            ],
          };
        }
    
        if (srcStats.isDirectory()) {
          if (!input.recursive) {
            return {
              isError: true,
              content: [
                {
                  type: 'text',
                  text: JSON.stringify({
                    code: 'INVALID_PATH',
                    message: 'Source is a directory but recursive is false',
                  }),
                },
              ],
            };
          }
    
          // Copy directory recursively
          await copyDirRecursive(srcPath, destPath);
        } else {
          // Ensure parent directory exists
          await fs.mkdir(path.dirname(destPath), { recursive: true });
    
          // Copy file
          await fs.copyFile(srcPath, destPath);
        }
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                success: true,
                source: srcPath,
                destination: destPath,
              }),
            },
          ],
        };
      } catch (error) {
        const err = error as NodeJS.ErrnoException;
    
        if (err.code === 'ENOENT') {
          return {
            isError: true,
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  code: 'FILE_NOT_FOUND',
                  message: `Source not found: ${input.source}`,
                }),
              },
            ],
          };
        }
    
        if (err.code === 'EACCES') {
          return {
            isError: true,
            content: [
              {
                type: 'text',
                text: JSON.stringify({
                  code: 'PERMISSION_DENIED',
                  message: `Permission denied`,
                }),
              },
            ],
          };
        }
    
        return {
          isError: true,
          content: [
            {
              type: 'text',
              text: JSON.stringify({
                code: 'UNKNOWN_ERROR',
                message: `Error copying: ${err.message}`,
              }),
            },
          ],
        };
      }
    }
  • Registration of the 'copy_file' tool with the MCP server.
    // copy_file tool
    server.tool(
      'copy_file',
      'Copy a file or directory',
      {
        source: z.string().describe('Source path'),
        destination: z.string().describe('Destination path'),
        overwrite: z.boolean().optional().describe('Overwrite if exists'),
        recursive: z.boolean().optional().describe('Copy directories recursively'),
      },
      async (args) => {
        const input = CopyFileInputSchema.parse(args);
        return await copyFileImpl(input);
      }
    );
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 but offers minimal information. It mentions copying files or directories but doesn't cover critical aspects like permissions needed, whether it preserves metadata, error conditions (e.g., if source doesn't exist), or side effects. This is inadequate for a mutation tool with zero annotation coverage.

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 with zero wasted words. It's front-loaded with the core action and resource, making it easy to parse quickly. Every word earns its place by conveying essential information without 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?

Given the complexity of a file operation (a mutation with potential side effects), no annotations, and no output schema, the description is incomplete. It lacks details on return values, error handling, permissions, or how it interacts with sibling tools, leaving significant gaps for an AI agent to navigate.

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 input schema fully documents all four parameters (source, destination, overwrite, recursive). The description adds no additional parameter semantics beyond what's in the schema, but the baseline score of 3 is appropriate when the schema handles 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 'Copy a file or directory' clearly states the verb (copy) and resource (file or directory), making the purpose immediately understandable. However, it doesn't differentiate from sibling tools like 'move_file' or 'write_file' beyond the basic action name, which prevents a perfect 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 like 'move_file' (for moving instead of copying) or 'write_file' (for creating new files). There's no mention of prerequisites, typical use cases, or exclusions, leaving the agent to infer usage from context alone.

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/mcp-tool-shop-org/mcp-file-forge'

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