AI AgentStructured Tool Call

Markdown HTML AI Integration Guide

Convert Markdown and HTML inside AI workflows while keeping output deterministic and preview-safe.

1. Function Schema

Import directly into Dify, Coze, Cursor, or any tool-calling agent runtime.

Function Schema
{
  "name": "markdown_html_convert",
  "description": "Convert Markdown to HTML or HTML back to Markdown.",
  "parameters": {
    "type": "object",
    "properties": {
      "mode": {
        "type": "string",
        "enum": ["md_to_html", "html_to_md"]
      },
      "input": {
        "type": "string",
        "description": "Markdown or HTML input string."
      }
    },
    "required": ["mode", "input"]
  }
}

2. System Prompt / SOP

Paste this into your system prompt to keep the generated tool implementation aligned with the contract.

System Prompt SOP
# Task Constraints: Markdown HTML Converter

When converting Markdown and HTML for users, you MUST preserve structure and avoid unsafe shortcuts.

## Core Prohibitions
- NEVER execute scripts from user-provided HTML.
- NEVER strip content silently just because it looks unfamiliar.
- NEVER claim round-trip conversion is lossless for every HTML construct.

## Standard Implementation
1. For md_to_html, use a Markdown parser to generate HTML.
2. For html_to_md, use a dedicated HTML-to-Markdown converter.
3. Return the converted string only. Keep rendering or preview as a separate concern.
4. If the input is empty, return a validation error.

## Communication Rules
- Be explicit that complex embedded HTML, scripts, and custom widgets may not round-trip exactly.
- If sanitization is required for rendering, do it in the UI layer, not in the conversion contract.

3. Core Script

This contract assumes markdownToHtml() and htmlToMarkdown() helpers are provided by the host environment, matching the same parser pair your UI already uses.

Core Script
function main(args) {
  const { mode, input } = args;

  if (!input) {
    return { success: false, result: "", error: "input is required" };
  }

  try {
    if (mode === "md_to_html") {
      return { success: true, result: markdownToHtml(input) };
    }

    if (mode === "html_to_md") {
      return { success: true, result: htmlToMarkdown(input) };
    }

    return { success: false, result: "", error: "Unknown mode" };
  } catch (error) {
    return {
      success: false,
      result: "",
      error: "Conversion failed: " + (error instanceof Error ? error.message : String(error)),
    };
  }
}