Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:0xranx OpenContext Store createDoc

From Leeroopedia
Knowledge Sources
Domains Knowledge_Management, API
Last Updated 2026-10-24 12:00 GMT

Overview

Concrete tool for registering documents in the OpenContext knowledge base with UUID stable identifiers, provided by the native store adapter.

Description

The createDoc function creates a new document in the knowledge base. It performs two operations atomically:

  1. Database registration: Inserts a record into the `docs` SQLite table with a UUID `stable_id`, the folder path, document name, and description.
  2. File creation: Creates the markdown file on disk at the resolved absolute path.

The `stable_id` (UUID) is the key feature: it enables `oc://doc/<stable_id>` stable links that survive future renames and moves. The stable_id is generated by the Rust core using UUID v4.

The function is used by the `oc doc create` CLI command and the `oc_create_doc` MCP tool. It is the mandatory first step for adding any document — files created directly on disk without database registration will not appear in manifests, search, or stable link resolution.

Usage

  • Document Lifecycle: Always use `createDoc` (never direct file writes) to register documents.
  • Agent workflows: Agents creating new knowledge must call `oc doc create` to ensure proper registration.
  • MCP integration: The `oc_create_doc` MCP tool wraps this function for agent access.

Code Reference

Source Location

  • Repository: OpenContext
  • File (JS adapter): src/core/store-native.js
  • File (Rust core): crates/opencontext-core/src/lib.rs
  • File (CLI): bin/oc.js
  • File (MCP): src/mcp/server.js

Signature

/**
 * Create a document
 * @param {{ folderPath: string, name: string, description?: string }} options
 * @returns {{ rel_path: string, abs_path: string, description: string, stable_id: string }}
 */
function createDoc(options) {
  return handleResult(native.get().createDoc({
    folderPath: options.folderPath,
    name: options.name,
    description: options.description,
  }));
}

Import

const { createDoc } = require('./core/store-native');

I/O Contract

Inputs

Name Type Required Description
options.folderPath string Yes Target folder path relative to contexts/
options.name string Yes Document file name (e.g., "design.md")
options.description string No Optional document description

Outputs

Name Type Description
returns.rel_path string Relative path from contexts root
returns.abs_path string Absolute filesystem path
returns.description string Document description
returns.stable_id string UUID stable identifier for oc://doc/ links

Usage Examples

Create a Document (CLI)

oc doc create projects/architecture design.md -d "System architecture overview"
# Output: Created doc at projects/architecture/design.md (stable_id: abc-123-...)

Create a Document (JavaScript)

const { createDoc } = require('./core/store-native');

const result = createDoc({
  folderPath: 'projects/architecture',
  name: 'design.md',
  description: 'System architecture overview'
});

console.log(`Created: ${result.abs_path}`);
console.log(`Stable ID: ${result.stable_id}`);
console.log(`Stable link: oc://doc/${result.stable_id}`);

MCP Tool Usage

{
  "tool": "oc_create_doc",
  "arguments": {
    "folderPath": "projects/architecture",
    "name": "design.md",
    "description": "System architecture overview"
  }
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment