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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Absolute path where file should be created | |
| file_text | Yes | Content to write to the file |
Implementation Reference
- src/editor.ts:84-94 (handler)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"] } },
- src/types.ts:19-22 (schema)TypeScript interface defining the input arguments for the 'create' tool.export interface CreateArgs extends Record<string, unknown> { path: string; file_text: string; }
- src/types.ts:48-50 (schema)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"; }
- src/utils.ts:43-75 (helper)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; } } }