Skip to main content
Glama

update_section

Modify or add content to specific sections in memory documents using Markdown formatting for structured project knowledge management.

Instructions

Update an entire section of a memory document. Content supports full Markdown formatting including headings, bold, italic, code blocks, links, lists, tables, and all standard Markdown syntax.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
memory_idYesThe ID of the memory document to update
sectionYesThe section name to update
contentYesThe new content for the section. Supports full Markdown: headings (#), **bold**, *italic*, `code`, [links](url), ```code blocks```, lists, tables, etc.
modeNoWhether to append to or replace the section content (default: append)

Implementation Reference

  • The updateSectionTool function implements the core logic for the update_section tool, handling input validation, memory retrieval, section existence check, update via StorageManager, response formatting with word/line counts, and success message.
    export async function updateSectionTool(
      storageManager: StorageManager,
      args: any
    ): Promise<any> {
      const params = args as UpdateSectionParams;
    
      if (!params.memory_id || !params.section || params.content === undefined) {
        throw new Error("memory_id, section, and content are required");
      }
    
      // Validate mode
      const mode = params.mode || "append";
      if (mode !== "append" && mode !== "replace") {
        throw new Error('mode must be either "append" or "replace"');
      }
    
      // Read the memory document
      const memory = await storageManager.readMemory(params.memory_id);
      if (!memory) {
        throw new Error(`Memory document '${params.memory_id}' not found`);
      }
    
      // Check if section exists
      const existingSection = storageManager.findSection(
        memory.content,
        params.section
      );
      const sectionExists = existingSection !== null;
    
      // Update the section using the storage manager
      await storageManager.updateSection(
        params.memory_id,
        params.section,
        params.content,
        mode
      );
    
      // Format response based on what happened
      let actionText: string;
      if (!sectionExists) {
        actionText = `Created new section "${params.section}" with content`;
      } else if (mode === "replace") {
        actionText = `Replaced content in section "${params.section}"`;
      } else {
        actionText = `Appended content to section "${params.section}"`;
      }
    
      // Count words/lines for feedback
      const wordCount = params.content
        .trim()
        .split(/\s+/)
        .filter((w) => w.length > 0).length;
      const lineCount = params.content.split("\n").length;
    
      return {
        content: [
          {
            type: "text",
            text: `Successfully updated memory document '${params.memory_id}':
    
    **Action**: ${actionText}
    **Content Added**: ${wordCount} words, ${lineCount} lines
    **Mode**: ${mode}
    
    The section has been updated. You can view it using the get_section tool or check the full document summary with get_memory_summary.`,
          },
        ],
      };
    }
  • src/index.ts:142-170 (registration)
    Registration of the update_section tool in the ListToolsRequestSchema handler, including name, description, and JSON input schema definition.
      name: "update_section",
      description:
        "Update an entire section of a memory document. Content supports full Markdown formatting including headings, **bold**, *italic*, `code blocks`, [links](url), lists, tables, and all standard Markdown syntax.",
      inputSchema: {
        type: "object",
        properties: {
          memory_id: {
            type: "string",
            description: "The ID of the memory document to update",
          },
          section: {
            type: "string",
            description: "The section name to update",
          },
          content: {
            type: "string",
            description:
              "The new content for the section. Supports full Markdown: headings (#), **bold**, *italic*, `code`, [links](url), ```code blocks```, lists, tables, etc.",
          },
          mode: {
            type: "string",
            enum: ["append", "replace"],
            description:
              "Whether to append to or replace the section content (default: append)",
          },
        },
        required: ["memory_id", "section", "content"],
      },
    },
  • src/index.ts:277-278 (registration)
    Dispatch handler in the CallToolRequestSchema switch statement that routes update_section calls to the updateSectionTool function.
    case "update_section":
      return await updateSectionTool(storageManager, args);
  • TypeScript interface defining the input parameters for the update_section tool, used for type casting in the handler.
    export interface UpdateSectionParams {
      memory_id: string;
      section: string;
      content: string;
      mode?: 'replace' | 'append';
    }
  • Core helper method in StorageManager that parses sections, handles append/replace logic, rebuilds Markdown content, and persists changes to file with backup.
    async updateSection(
      memoryId: string,
      sectionName: string,
      newContent: string,
      mode: "append" | "replace" = "append"
    ): Promise<void> {
      const memory = await this.readMemory(memoryId);
      if (!memory) {
        throw new Error(`Memory document '${memoryId}' not found`);
      }
    
      const sections = this.parseSections(memory.content);
      const sectionIndex = sections.findIndex(
        (section) =>
          section.name.toLowerCase() === sectionName.toLowerCase() ||
          section.name.toLowerCase().replace(/[^a-z0-9]/g, "_") ===
            sectionName.toLowerCase()
      );
    
      if (sectionIndex === -1) {
        // Section doesn't exist, add it
        const lines = memory.content.split("\n");
        lines.push("", `## ${sectionName}`, "", newContent);
        memory.content = lines.join("\n");
      } else {
        // Section exists, update it
        const section = sections[sectionIndex];
        if (mode === "append") {
          section.content = section.content
            ? `${section.content}\n\n${newContent}`
            : newContent;
        } else {
          section.content = newContent;
        }
    
        // Rebuild the content
        memory.content = this.rebuildContent(sections);
      }
    
      await this.writeMemory(memory);
    }

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/nmeierpolys/mcp-structured-memory'

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