engram

Protocol

Unix socket JSON protocol and trainer stdout protocol

Protocol

Engram uses two JSON protocols: Unix socket for MCP server -> Rust core communication, and stdout JSON Lines for Rust core -> trainer communication.

Unix socket protocol

Communication between the MCP server and Rust core happens via Unix socket with newline-delimited JSON.

Request format

{
  "id": "request-uuid",
  "method": "memory_search",
  "params": {
    "query": "auth middleware",
    "limit": 10
  }
}
FieldTypeDescription
idstringUUID for request-response correlation
methodstringMethod name (matches MCP tool name)
paramsobjectMethod parameters

Response format (success)

{
  "id": "request-uuid",
  "ok": true,
  "data": {
    "memories": [],
    "count": 0
  },
  "error": null
}

Response format (error)

{
  "id": "request-uuid",
  "ok": false,
  "data": null,
  "error": {
    "code": 1002,
    "message": "[1002] not found: memory abc-123"
  }
}
FieldTypeDescription
idstringRequest UUID
okbooleanOperation success
dataobject/nullData on success
errorobject/nullError on failure
error.codenumberNumeric error code
error.messagestringError description

Delimiter

Each message (request or response) is terminated by a \n (newline) character. One line = one JSON object.

Available methods

MethodDescription
memory_storeStore a record
memory_searchSearch records
memory_judgeRate a record
memory_statusSystem status
memory_configRead configuration
memory_exportExport records
memory_importImport records
memory_consolidate_previewConsolidation preview
memory_consolidateLLM analysis of candidates
memory_consolidate_applyApply recommendations
memory_train_generateGenerate insights
memory_train_listList insights
memory_train_deleteDelete an insight

Trainer stdout protocol

The trainer (Python) outputs results to stdout as JSON Lines. Each line is a JSON object with a required type field.

progress

Stage execution progress.

{
  "type": "progress",
  "stage": "clustering",
  "percent": 45.0
}

insight

Generated insight to be saved as a memory record.

{
  "type": "insight",
  "id": "uuid",
  "context": "Found cluster of 5 records about auth error handling",
  "action": "Use a unified middleware for token validation",
  "result": "60% reduction in code duplication",
  "insight_type": "cluster",
  "tags": "auth,middleware",
  "source_ids": "id1,id2,id3"
}

recommendation

Recommendation for an existing record.

{
  "type": "recommendation",
  "target_id": "existing-record-uuid",
  "action": "merge",
  "reasoning": "Records describe the same decision from different angles"
}

metric

Analysis metric.

{
  "type": "metric",
  "name": "cluster_count",
  "value": 12.0
}

artifact

Information about a created file (ONNX model, tokenizer).

{
  "type": "artifact",
  "path": "/home/user/.engram/models/text_generator.onnx",
  "size_bytes": 15728640
}

complete

Trainer execution completion.

{
  "type": "complete",
  "insights_generated": 7,
  "duration_secs": 42.5
}

Routing

The Rust core uses dispatch.rs for request routing by the method field. Unknown methods return error 6007 (DispatchError).

The trainer is invoked synchronously via tokio::process::Command with a timeout. Stdout is parsed line-by-line via serde_json::from_str with tagged deserialization (#[serde(tag = "type")]).