Skip to main content
Glama

create

Create a new file with specified content at a designated path to enable file manipulation through client-approved operations.

Instructions

Create a new file with specified content

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesAbsolute path where file should be created
file_textYesContent to write to the file

Implementation Reference

  • The primary handler function for the 'create' tool. It validates the path, writes the file content using writeFile, updates the file history, and returns a success message.
    async create(args: CreateArgs): Promise<string> {
        await validatePath('create', args.path);
        await writeFile(args.path, args.file_text);
    
        if (!this.fileHistory[args.path]) {
            this.fileHistory[args.path] = [];
        }
        this.fileHistory[args.path].push(args.file_text);
    
        return `File created successfully at: ${args.path}`;
    }
  • src/server.ts:63-80 (registration)
    Registers the 'create' tool in the MCP server's ListTools response, providing name, description, and JSON input schema.
    {
        name: "create",
        description: "Create a new file with specified content",
        inputSchema: {
            type: "object",
            properties: {
                path: {
                    type: "string",
                    description: "Absolute path where file should be created"
                },
                file_text: {
                    type: "string",
                    description: "Content to write to the file"
                }
            },
            required: ["path", "file_text"]
        }
    },
  • TypeScript interface defining the input arguments for the 'create' tool.
    export interface CreateArgs extends Record<string, unknown> {
        path: string;
        file_text: string;
    }
  • Type guard function used to validate input arguments for the 'create' tool before execution.
    export function isCreateArgs(args: Record<string, unknown>): args is CreateArgs {
        return typeof args.path === "string" && typeof args.file_text === "string";
    }
  • Utility function called by the 'create' handler to validate the file path, ensuring it's absolute, not a directory, doesn't exist yet for create, etc.
    export async function validatePath(command: string, filePath: string): Promise<void> {
        const absolutePath = path.isAbsolute(filePath) ?
            filePath :
            path.join(process.cwd(), filePath);
    
        if (!path.isAbsolute(filePath)) {
            throw new ToolError(
                `The path ${filePath} is not an absolute path, it should start with '/'. Maybe you meant ${absolutePath}?`
            );
        }
    
        try {
            const stats = await fs.stat(filePath);
            if (stats.isDirectory() && command !== 'view') {
                throw new ToolError(
                    `The path ${filePath} is a directory and only the \`view\` command can be used on directories`
                );
            }
            if (command === 'create' && stats.isFile()) {
                throw new ToolError(
                    `File already exists at: ${filePath}. Cannot overwrite files using command \`create\``
                );
            }
        } catch (e: unknown) {
            const error = e instanceof Error ? e : new Error('Unknown error');
            if ('code' in error && error.code === 'ENOENT' && command !== 'create') {
                throw new ToolError(`The path ${filePath} does not exist. Please provide a valid path.`);
            }
            if (command !== 'create') {
                throw error;
            }
        }
    }
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/arathald/mcp-editor'

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