Skip to main content
Glama

format_content

Convert markdown text to Ed Discussion XML format for previewing content before posting to course discussions.

Instructions

Convert markdown text to Ed Discussion XML format (preview, no API call)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
markdownYesMarkdown text to convert

Implementation Reference

  • Tool registration and handler for 'format_content' which calls 'markdownToEdXml'.
    server.tool(
      "format_content",
      "Convert markdown text to Ed Discussion XML format (preview, no API call)",
      { markdown: z.string().describe("Markdown text to convert") },
      async ({ markdown }) => {
        return msg(markdownToEdXml(markdown));
      }
    );
  • Implementation of the 'markdownToEdXml' function used by 'format_content'.
    export function markdownToEdXml(text: string): string {
      const lines = text.split("\n");
      const parts: string[] = [];
      let i = 0;
    
      while (i < lines.length) {
        const line = lines[i];
    
        // Code block
        if (line.startsWith("```")) {
          const lang = line.slice(3).trim();
          const codeLines: string[] = [];
          i++;
          while (i < lines.length && !lines[i].startsWith("```")) {
            codeLines.push(lines[i]);
            i++;
          }
          i++; // skip closing ```
          const code = escapeXml(codeLines.join("\n"));
          if (lang) {
            parts.push(`<snippet language="${escapeXml(lang)}" runnable="false">${code}</snippet>`);
          } else {
            parts.push(`<pre>${code}</pre>`);
          }
          continue;
        }
    
        // Empty line
        if (line.trim() === "") {
          i++;
          continue;
        }
    
        // Heading
        const headingMatch = line.match(/^(#{1,6})\s+(.+)$/);
        if (headingMatch) {
          const level = headingMatch[1].length;
          parts.push(`<heading level="${level}">${formatInline(headingMatch[2])}</heading>`);
          i++;
          continue;
        }
    
        // Callout (> [!info], > [!warning], etc.)
        const calloutMatch = line.match(/^>\s*\[!(success|info|warning|error)\]\s*(.*)$/i);
        if (calloutMatch) {
          const type = calloutMatch[1].toLowerCase();
          const calloutLines: string[] = [];
          if (calloutMatch[2]) calloutLines.push(calloutMatch[2]);
          i++;
          while (i < lines.length && lines[i].startsWith("> ")) {
            calloutLines.push(lines[i].slice(2));
            i++;
          }
          parts.push(
            `<callout type="${type}"><paragraph>${formatInline(calloutLines.join(" "))}</paragraph></callout>`
          );
          continue;
        }
    
        // Bullet list
        if (line.match(/^[-*]\s+/)) {
          const items: string[] = [];
          while (i < lines.length && lines[i].match(/^[-*]\s+/)) {
            items.push(lines[i].replace(/^[-*]\s+/, ""));
            i++;
          }
          const listItems = items
            .map((item) => `<list-item><paragraph>${formatInline(item)}</paragraph></list-item>`)
            .join("");
          parts.push(`<list style="bullet">${listItems}</list>`);
          continue;
        }
    
        // Numbered list
        if (line.match(/^\d+\.\s+/)) {
          const items: string[] = [];
          while (i < lines.length && lines[i].match(/^\d+\.\s+/)) {
            items.push(lines[i].replace(/^\d+\.\s+/, ""));
            i++;
          }
          const listItems = items
            .map((item) => `<list-item><paragraph>${formatInline(item)}</paragraph></list-item>`)
            .join("");
          parts.push(`<list style="number">${listItems}</list>`);
          continue;
        }
    
        // Regular paragraph
        parts.push(`<paragraph>${formatInline(line)}</paragraph>`);
        i++;
      }
    
      return `<document version="2.0">${parts.join("")}</document>`;
    }
    
    function formatInline(text: string): string {
      // Extract inline code spans first so their contents are not mangled by
      // XML escaping or bold/italic/link regex transformations.
      const codeSpans: string[] = [];
      text = text.replace(/`([^`]+)`/g, (_match, code: string) => {
        codeSpans.push(`<code>${escapeXml(code)}</code>`);
        return `\x00CODE${codeSpans.length - 1}\x00`;
      });
    
      // Escape XML special characters in the remaining (non-code) text
      text = escapeXml(text);
    
      // Bold (non-greedy to allow single * inside bold, e.g. **2*3**)
      text = text.replace(/\*\*(.+?)\*\*/g, "<bold>$1</bold>");
      // Italic (single *)
      text = text.replace(/(?<!\*)\*(.+?)\*(?!\*)/g, "<italic>$1</italic>");
      // Links — href value is already XML-escaped by escapeXml above
      text = text.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<link href="$2">$1</link>');
      // LaTeX
      text = text.replace(/\$([^$]+)\$/g, "<math>$1</math>");
    
      // Restore code span placeholders
      text = text.replace(/\x00CODE(\d+)\x00/g, (_match, idx: string) => codeSpans[parseInt(idx, 10)]);
    
      return text;
    }
    
    function escapeXml(text: string): string {
      return text
        .replace(/&/g, "&")
        .replace(/</g, "<")
        .replace(/>/g, ">")
        .replace(/"/g, """);
    }
    
    /**
     * Returns content as Ed XML. If the input already starts with `<document`,
     * it is returned as-is (raw XML passthrough); otherwise it is converted
Behavior4/5

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

No annotations provided, so description carries full burden. It successfully discloses key traits: 'no API call' indicates local processing, and 'preview' clarifies the non-destructive nature. Lacks details on output format, size limits, or specific markdown syntax supported, but covers the essential safety/scope profile.

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

Conciseness5/5

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

Single sentence with zero waste. Front-loaded verb ('Convert') followed by critical distinguishing parenthetical. Every word—including 'preview' and 'no API call'—earns its place by aiding tool selection.

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

Completeness4/5

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

Adequate for a single-parameter utility with full schema coverage. The description explains the transformation and side-effect profile sufficiently. Minor gap: does not specify return value type (XML string?), though this is partially mitigated by the explicit format naming.

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 ('Markdown text to convert'), so the description appropriately relies on the schema rather than repeating parameter details. Baseline 3 is correct when structured documentation is complete.

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?

States specific action (Convert), source format (markdown text), and target format (Ed Discussion XML). The parenthetical '(preview, no API call)' effectively distinguishes this utility from sibling posting tools like post_thread and post_comment.

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?

Provides clear behavioral context through 'preview' and 'no API call', implicitly signaling this is for local formatting verification rather than persistence. However, it does not explicitly name alternatives (e.g., 'use post_thread to actually publish') or state prerequisites.

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/rob-9/edstem-mcp'

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