write_document
Create or replace markdown documents in the MCP Documentation Service, automatically generating directories as needed for structured documentation management.
Instructions
Create a new markdown document or completely overwrite an existing document with new content. Use with caution as it will overwrite existing documents without warning. Can create parent directories if they don't exist.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| content | Yes | ||
| createDirectories | No |
Implementation Reference
- src/handlers/documents.ts:142-172 (handler)The main handler function implementing the write_document tool logic. Validates the path, creates directories if needed, writes the file content, and returns success/error response.*/ async writeDocument( docPath: string, content: string, createDirectories = true ): Promise<ToolResponse> { try { const validPath = await this.validatePath(docPath); // Create parent directories if needed if (createDirectories) { const dirPath = path.dirname(validPath); await fs.mkdir(dirPath, { recursive: true }); } await fs.writeFile(validPath, content, "utf-8"); return { content: [{ type: "text", text: `Successfully wrote to ${docPath}` }], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { content: [ { type: "text", text: `Error writing document: ${errorMessage}` }, ], isError: true, }; } }
- src/schemas/tools.ts:32-36 (schema)Zod schema defining the input parameters for the write_document tool: path, content, and optional createDirectories flag.export const WriteDocumentSchema = ToolInputSchema.extend({ path: z.string(), content: z.string(), createDirectories: z.boolean().default(true), });
- src/index.ts:207-213 (registration)Tool registration in the ListToolsRequest handler, defining name, description, and input schema for write_document.name: "write_document", description: "Create a new markdown document or completely overwrite an existing document with new content. " + "Use with caution as it will overwrite existing documents without warning. " + "Can create parent directories if they don't exist.", inputSchema: zodToJsonSchema(WriteDocumentSchema) as any, },
- src/index.ts:321-333 (registration)Dispatch case in the CallToolRequest handler that validates input using the schema and invokes the documentHandler.writeDocument method.case "write_document": { const parsed = WriteDocumentSchema.safeParse(args); if (!parsed.success) { throw new Error( `Invalid arguments for write_document: ${parsed.error}` ); } return await documentHandler.writeDocument( parsed.data.path, parsed.data.content, parsed.data.createDirectories ); }