write_document
Generate or overwrite markdown documents, including creating necessary directories, using the MCP Documentation Service for precise document 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 |
|---|---|---|---|
| content | Yes | ||
| createDirectories | No | ||
| path | Yes |
Implementation Reference
- src/handlers/documents.ts:143-172 (handler)Core handler implementation for the write_document tool. Validates path, creates directories if specified, writes content to file, 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 (string), content (string), createDirectories (boolean, default true).export const WriteDocumentSchema = ToolInputSchema.extend({ path: z.string(), content: z.string(), createDirectories: z.boolean().default(true), });
- src/index.ts:206-213 (registration)Tool registration in the ListToolsRequestHandler, 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 logic in CallToolRequestHandler switch statement that parses input with schema and calls 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 ); }