Skip to main content
Glama

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
NameRequiredDescriptionDefault
pathYes
contentYes
createDirectoriesNo

Implementation Reference

  • 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, }; } }
  • 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 ); }

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/alekspetrov/mcp-docs-service'

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