Skip to content

Add create_nodes_from_markdown MCP method #296

Closed

Description

@malibio

Overview

Implement create_nodes_from_markdown MCP method that parses markdown content and creates hierarchical NodeSpace nodes with proper parent/sibling relationships.

Use Case

AI agents (e.g., Claude Code) send markdown content via MCP stdio → NodeSpace creates structured node hierarchy → User sees real-time updates in UI.

Dependencies

Blocked by #112 - Requires MCP server foundation

Implementation

Backend (Rust)

  • Add pulldown-cmark = "0.11" dependency to Cargo.toml
  • Create src-tauri/src/mcp/handlers/markdown.rs
  • Implement markdown parser with heading hierarchy tracking
    • Track heading level stack (h1 → h2 → h3 parent-child)
    • Pop stack when encountering same/lower level heading
  • Implement list hierarchy with indentation tracking
    • Track indentation depth
    • Create parent-child based on indent level
  • Create nodes via NodeService
    • Set correct parent_id for hierarchy
    • Set before_sibling_id for top-to-bottom ordering
  • Create container node with provided title
  • Emit Tauri events for each created node (reactivity)

Node Type Mapping (Content-Based, No Properties)

Headers - Store markdown with hashtags in content:

  • # Heading → header node, content: "# Heading"
  • ## Heading → header node, content: "## Heading"
  • ### Heading → header node, content: "### Heading"

Code blocks - Store with language fence in content:

  • rust\ncode\n```` → `code-block` node, content: `"rust\ncode\n```"`

Ordered lists - Store with number prefix in content:

  • 1. First → ordered-list node, content: "1. First"
  • 2. Second → ordered-list node, content: "2. Second"

Tasks - Store with markdown checkbox in content:

  • - [ ] Task → task node, content: "- [ ] Task" (status: pending)
  • - [~] Working → task node, content: "- [~] Working" (status: inProgress)
  • - [x] Done → task node, content: "- [x] Done" (status: completed)

Other:

  • Paragraph text → text node, content: "paragraph text"
  • - List item → text node, content: "- List item" (nested via parent_id)
  • > Quote → quote-block node, content: "> Quote"

Key principle: Store content AS-IS from markdown. Let the frontend components parse and render appropriately.

MCP Interface

Request:

{
  "jsonrpc": "2.0",
  "method": "create_nodes_from_markdown",
  "params": {
    "markdown_content": "# Title\n## Subtitle\n- Item",
    "container_title": "Meeting Notes"
  }
}

Response:

{
  "jsonrpc": "2.0",
  "result": {
    "success": true,
    "container_node_id": "uuid-123",
    "nodes_created": 15,
    "node_ids": ["uuid-123", "uuid-124", ...]
  }
}

Acceptance Criteria

  • Markdown parser correctly identifies headings, lists, code blocks, quotes
  • Heading hierarchy preserved (h1 → h2 → h3 parent-child relationships)
  • List indentation creates nested child nodes
  • Sibling ordering is top-to-bottom as markdown appears
  • Container node created with provided title
  • All nodes have correct parent_id and before_sibling_id
  • Content stored AS-IS from markdown (headers with #, tasks with [ ], etc.)
  • Tauri events emitted → UI updates in real-time
  • Test with sample document
  • MCP response includes all created node IDs

Testing

  • Unit tests: Parse various markdown structures (headings, nested lists, code blocks)
  • Integration test: Import /Users/malibio/nodespace/data/sample-nodespace-data.md
  • Manual test: Claude Code imports markdown → NodeSpace UI shows nodes immediately

Sample Document

Test with cleaned sample-nodespace-data.md (Logseq collapsed:: true markers removed)

Path: /Users/malibio/nodespace/data/sample-nodespace-data.md ✅ File exists (8740 bytes)

Implementation Q&A

Q1: Ordered List Node Type

A: Use ordered-list node type. Content includes number: "1. Item"

Q2: Event Emission

A: Emit events directly in markdown handler after each node creation:

app.emit("node-created", json!({ 
    "node_id": node_id,
    "node_type": node.node_type 
}))?;

Event names: node-created, node-updated, node-deleted

Q3: Header Content Format

A: Store headers WITH hashtags in content: content: "## Heading Text"

Frontend parses the hashtags to determine level. Do NOT use properties.

Q4: List Items

A: Unordered lists (- Item) → text node, content: "- Item"

Tasks (- [ ] Task) → task node, content: "- [ ] Task"

Hierarchy via parent_id/before_sibling_id.

Q5: Task Status

A: Three task states based on checkbox content:

  • - [ ] Task → pending
  • - [~] Task or - [o] Task → inProgress
  • - [x] Task or - [X] Task → completed

See: task-node.svelte:62-78 for parsing logic

Q6: Code Block Content

A: Store WITH language fence: content: "```rust\ncode\n```"

Frontend extracts language from fence. Do NOT use properties.

Q7: Sample Document Path

A: File exists: /Users/malibio/nodespace/data/sample-nodespace-data.md

Q8: Tauri Integration

A: MCP-only for now. Keep parser logic reusable.

References

  • MCP Integration: mcp-integration.md
  • Architecture: ADR-021
  • Node Types: packages/desktop-app/src-tauri/src/constants.rs
  • Task Status Parsing: packages/desktop-app/src/lib/design/components/task-node.svelte:62-78

Activity

  1. added
    enhancement
    on Oct 19, 2025
  2. self-assigned this
    on Oct 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementfoundationmcp

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions