Implementation:0xranx OpenContext Store createDoc
| 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:
- Database registration: Inserts a record into the `docs` SQLite table with a UUID `stable_id`, the folder path, document name, and description.
- 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"
}
}