Skip to main content
Glama

update_draft

Modify an unpublished Substack draft by updating its title, subtitle, body in markdown, or audience.

Instructions

Update an existing draft post. Only works on unpublished drafts. Accepts markdown body.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
draft_idYesThe draft ID to update
titleNoNew title
subtitleNoNew subtitle
bodyNoNew body in markdown format
audienceNoWho can see this post

Implementation Reference

  • The handler function for the 'update_draft' tool. Accepts draft_id (required), title, subtitle, body, audience (optional). Converts markdown body to ProseMirror format via markdownToProseMirror, then calls client.updateDraft to send a PUT request to Substack API.
    server.tool(
      "update_draft",
      "Update an existing draft post. Only works on unpublished drafts. Accepts markdown body.",
      {
        draft_id: z.number().describe("The draft ID to update"),
        title: z.string().optional().describe("New title"),
        subtitle: z.string().optional().describe("New subtitle"),
        body: z.string().optional().describe("New body in markdown format"),
        audience: z
          .enum(["everyone", "only_paid", "founding", "only_free"])
          .optional()
          .describe("Who can see this post"),
      },
      async ({ draft_id, title, subtitle, body, audience }) => {
        const updates: Record<string, unknown> = {};
        if (title !== undefined) updates.draft_title = title;
        if (subtitle !== undefined) updates.draft_subtitle = subtitle;
        if (body !== undefined) updates.draft_body = markdownToProseMirror(body);
        if (audience !== undefined) updates.audience = audience;
    
        const draft = await client.updateDraft(draft_id, updates);
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                {
                  id: draft.id,
                  title: draft.draft_title,
                  message: "Draft updated successfully.",
                },
                null,
                2,
              ),
            },
          ],
        };
      },
    );
  • src/server.ts:207-245 (registration)
    Tool registration via server.tool() in the createServer function. Registers 'update_draft' with Zod schema for input validation.
    server.tool(
      "update_draft",
      "Update an existing draft post. Only works on unpublished drafts. Accepts markdown body.",
      {
        draft_id: z.number().describe("The draft ID to update"),
        title: z.string().optional().describe("New title"),
        subtitle: z.string().optional().describe("New subtitle"),
        body: z.string().optional().describe("New body in markdown format"),
        audience: z
          .enum(["everyone", "only_paid", "founding", "only_free"])
          .optional()
          .describe("Who can see this post"),
      },
      async ({ draft_id, title, subtitle, body, audience }) => {
        const updates: Record<string, unknown> = {};
        if (title !== undefined) updates.draft_title = title;
        if (subtitle !== undefined) updates.draft_subtitle = subtitle;
        if (body !== undefined) updates.draft_body = markdownToProseMirror(body);
        if (audience !== undefined) updates.audience = audience;
    
        const draft = await client.updateDraft(draft_id, updates);
        return {
          content: [
            {
              type: "text",
              text: JSON.stringify(
                {
                  id: draft.id,
                  title: draft.draft_title,
                  message: "Draft updated successfully.",
                },
                null,
                2,
              ),
            },
          ],
        };
      },
    );
  • TypeScript type definition for the payload sent to update a draft.
    export interface DraftUpdatePayload {
      draft_title?: string;
      draft_subtitle?: string;
      draft_body?: string;
      audience?: "everyone" | "only_paid" | "founding" | "only_free";
      section_id?: number | null;
      cover_image?: string | null;
    }
  • API client method that sends a PUT request to /api/v1/drafts/{id} to update a draft on Substack.
    async updateDraft(
      id: number,
      updates: DraftUpdatePayload,
    ): Promise<SubstackDraft> {
      return this.request<SubstackDraft>(
        `${this.publicationUrl}/api/v1/drafts/${id}`,
        {
          method: "PUT",
          body: JSON.stringify(updates),
        },
      );
    }
  • Utility function that converts markdown to Substack's ProseMirror JSON format, used by the update_draft handler to transform body content.
    export function markdownToProseMirror(markdown: string): string {
      const lines = markdown.split("\n");
      const nodes: PMNode[] = [];
      let i = 0;
    
      while (i < lines.length) {
        const line = lines[i];
    
        // Blank line — skip
        if (line.trim() === "") {
          i++;
          continue;
        }
    
        // Fenced code block
        if (line.trimStart().startsWith("```")) {
          const lang = line.trim().slice(3).trim();
          const codeLines: string[] = [];
          i++;
          while (i < lines.length && !lines[i].trimStart().startsWith("```")) {
            codeLines.push(lines[i]);
            i++;
          }
          i++; // skip closing ```
          nodes.push({
            type: "code_block",
            ...(lang ? { attrs: { lang } } : {}),
            content: [{ type: "text", text: codeLines.join("\n") }],
          });
          continue;
        }
    
        // Horizontal rule
        if (/^(-{3,}|\*{3,}|_{3,})\s*$/.test(line.trim())) {
          nodes.push({ type: "horizontal_rule" });
          i++;
          continue;
        }
    
        // Heading
        const headingMatch = line.match(/^(#{1,6})\s+(.+)$/);
        if (headingMatch) {
          const level = headingMatch[1].length;
          nodes.push({
            type: "heading",
            attrs: { level },
            content: parseInline(headingMatch[2]),
          });
          i++;
          continue;
        }
    
        // Blockquote
        if (line.trimStart().startsWith("> ")) {
          const quoteLines: string[] = [];
          while (i < lines.length && lines[i].trimStart().startsWith("> ")) {
            quoteLines.push(lines[i].replace(/^>\s?/, ""));
            i++;
          }
          nodes.push({
            type: "blockquote",
            content: [
              {
                type: "paragraph",
                content: parseInline(quoteLines.join(" ")),
              },
            ],
          });
          continue;
        }
    
        // Unordered list
        if (/^[\s]*[-*+]\s/.test(line)) {
          const items: PMNode[] = [];
          while (i < lines.length && /^[\s]*[-*+]\s/.test(lines[i])) {
            const itemText = lines[i].replace(/^[\s]*[-*+]\s/, "");
            items.push({
              type: "list_item",
              content: [
                { type: "paragraph", content: parseInline(itemText) },
              ],
            });
            i++;
          }
          nodes.push({
            type: "bullet_list",
            content: items,
          });
          continue;
        }
    
        // Ordered list
        if (/^[\s]*\d+\.\s/.test(line)) {
          const items: PMNode[] = [];
          while (i < lines.length && /^[\s]*\d+\.\s/.test(lines[i])) {
            const itemText = lines[i].replace(/^[\s]*\d+\.\s/, "");
            items.push({
              type: "list_item",
              content: [
                { type: "paragraph", content: parseInline(itemText) },
              ],
            });
            i++;
          }
          nodes.push({
            type: "ordered_list",
            content: items,
          });
          continue;
        }
    
        // Image (standalone line)
        const imgMatch = line.match(/^!\[([^\]]*)\]\(([^)]+)\)\s*$/);
        if (imgMatch) {
          nodes.push({
            type: "captionedImage",
            attrs: {
              src: imgMatch[2],
              alt: imgMatch[1],
              title: null,
              caption: imgMatch[1] || null,
            },
          });
          i++;
          continue;
        }
    
        // Default: paragraph — collect consecutive non-blank, non-special lines
        const paraLines: string[] = [];
        while (
          i < lines.length &&
          lines[i].trim() !== "" &&
          !lines[i].trimStart().startsWith("```") &&
          !lines[i].trimStart().startsWith("# ") &&
          !lines[i].trimStart().startsWith("## ") &&
          !lines[i].trimStart().startsWith("### ") &&
          !lines[i].trimStart().startsWith("> ") &&
          !/^(-{3,}|\*{3,}|_{3,})\s*$/.test(lines[i].trim()) &&
          !/^[\s]*[-*+]\s/.test(lines[i]) &&
          !/^[\s]*\d+\.\s/.test(lines[i]) &&
          !/^!\[/.test(lines[i])
        ) {
          paraLines.push(lines[i]);
          i++;
        }
    
        if (paraLines.length > 0) {
          const text = paraLines.join(" ");
          const inlineContent = parseInline(text);
          if (inlineContent.length > 0) {
            nodes.push({
              type: "paragraph",
              content: inlineContent,
            });
          }
        }
      }
    
      const doc: PMNode = {
        type: "doc",
        content: nodes.length > 0 ? nodes : [{ type: "paragraph" }],
      };
    
      return JSON.stringify(doc);
    }
    
    /**
     * Returns the raw ProseMirror content array (for Notes, which wrap it in their own doc envelope).
     */
    export function markdownToProseMirrorContent(markdown: string): PMNode[] {
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Description implies mutation (update) and mentions accepted format for body. No annotations provided to supplement. Lacks details on auth, error behavior, or side effects, but adequate for a simple update.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Single sentence covering key points: action, constraint, and note on body format. No wasted words, but could be more structured with bullet points.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Minimally covers what the tool does, its constraint, and body format. Lacks explanation of return value, error cases, or prerequisites. Adequate for basic understanding.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema has 100% description coverage, so description adds little beyond confirming markdown for body. Baseline of 3 applies as schema already documents all parameters.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Description clearly states verb 'update', resource 'draft post', and constraint 'only works on unpublished drafts'. Distinguishes from siblings like create_draft and get_draft.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

Explicitly states constraint 'Only works on unpublished drafts' providing clear context. Does not mention when not to use or alternatives, but constraint is sufficient for basic guidance.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/conorbronsdon/substack-mcp'

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