Skip to main content
Glama

webdav_create_remote_file

Create a new file on a remote WebDAV server at a specified path with content, optionally overwriting existing files.

Instructions

Create a new file on a remote WebDAV server at the specified path

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contentYes
overwriteNo
pathYes

Implementation Reference

  • The core handler function that implements the tool logic. It checks if the file exists (respecting overwrite flag), writes the content using WebDAVService, and returns appropriate success or error messages in MCP content format.
    }, async ({ path, content, overwrite }) => { try { // Check if file exists and respect overwrite flag const exists = await webdavService.exists(path); if (exists && !overwrite) { return { content: [{ type: "text", text: `Error: File already exists at ${path}. Use overwrite=true to replace it.`, }], isError: true, }; } await webdavService.writeFile(path, content); return { content: [{ type: "text", text: `File created successfully at ${path}`, }], }; } catch (error) { return { content: [{ type: "text", text: `Error creating file: ${(error as Error).message}`, }], isError: true, }; } },
  • Zod input schema defining parameters: path (required string), content (string), overwrite (optional boolean default false). Used for validation in the tool registration.
    { path: z.string().min(1, "Path must not be empty"), content: z.string(), overwrite: z.boolean().optional().default(false), },
  • MCP tool registration via server.tool(), including name, description, schema, and handler reference. This is called within setupToolHandlers which is invoked during server setup.
    server.tool( "webdav_create_remote_file", "Create a new file on a remote WebDAV server at the specified path", { path: z.string().min(1, "Path must not be empty"), content: z.string(), overwrite: z.boolean().optional().default(false), }, async ({ path, content, overwrite }) => { try { // Check if file exists and respect overwrite flag const exists = await webdavService.exists(path); if (exists && !overwrite) { return { content: [{ type: "text", text: `Error: File already exists at ${path}. Use overwrite=true to replace it.`, }], isError: true, }; } await webdavService.writeFile(path, content); return { content: [{ type: "text", text: `File created successfully at ${path}`, }], }; } catch (error) { return { content: [{ type: "text", text: `Error creating file: ${(error as Error).message}`, }], isError: true, }; } }, );
  • Supporting utility in WebDAVService class that performs the actual remote file write operation using the WebDAV client's putFileContents method, with path normalization, logging, and error handling.
    async writeFile(path: string, content: string | Buffer): Promise<void> { const fullPath = this.getFullPath(path); const contentLength = typeof content === "string" ? content.length : content.length; logger.debug(`Writing file: ${fullPath}`, { contentLength }); try { // putFileContents in v5.x returns a boolean indicating success const result = await this.client.putFileContents(fullPath, content); // Check result based on type if (typeof result === "boolean" && !result) { throw new Error("Failed to write file: server returned failure status"); } else if ( this.isResponseData(result) && result.status !== undefined && result.status !== 201 && result.status !== 204 ) { throw new Error( `Failed to write file: server returned status ${result.status}`, ); } logger.debug(`Successfully wrote file: ${fullPath}`); } catch (error) { logger.error(`Error writing to file ${fullPath}:`, error); throw new Error(`Failed to write file: ${(error as Error).message}`); }
  • Helper method used by the handler to check if the target file already exists before writing.
    async exists(path: string): Promise<boolean> { const fullPath = this.getFullPath(path); logger.debug(`Checking if exists: ${fullPath}`); try { const result = await this.client.exists(fullPath); // Handle both boolean and object responses let exists = false; if (typeof result === "boolean") { exists = result; } else if (result && typeof result === "object") { // Use type guard for better type safety const responseData = result as ResponseData; if (responseData.status !== undefined) { exists = responseData.status < 400; // If status is less than 400, the resource exists } } logger.debug(`Exists check for ${fullPath}: ${exists}`); return exists; } catch (error) { logger.error(`Error checking existence of ${fullPath}:`, error); return false; } }

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/masx200/mcp-webdav-server'

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