feat(sdk,mcp): programmatic + agent access to OKF export/import#105
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Makes OKF export/import reachable from the SDK and the MCP server, not just the CLI. All three surfaces now share one output-free core, so they can't drift.
Shared core. OKF orchestration is extracted out of the CLI commands into
runOkfExport(root, opts)andrunOkfImport(root, dir, opts)— output-free functions that return structured reports. Warnings and skips are returned as data rather than printed (the read/collision/validate stages no longer write to stdout), so a library or an MCP stdio server gets clean results. The CLI commands now delegate to these cores and keep their existing presentation.SDK (
createWiki()):wiki.exportOkf({ out? })→{ outDir, writtenPaths }wiki.importOkf(dir, { trusted?, dryRun? })→{ mode, pages, skipped, warnings, nextAction }trusted:false,dryRun:false. As application code, an SDK caller owns the explicittrusted:trueescape hatch (writes live and runs the full refresh — links/index/MOC/embeddings). Report types and the typed errors are exported from the package entry point.MCP (new
src/mcp/okf-tools.ts):export_okf— export the wiki as a bundle.import_okf— staging-only: imports are always staged as review candidates (notrustedsurface is exposed to agents), so external knowledge always passes a human review/approval gate before it can enter the wiki. SupportsdryRunpreviews and returns anextActionthat states the review requirement explicitly.Trust boundary
Import treats bundles as untrusted, and the boundary is uniform across surfaces: nothing reaches the live wiki without either an explicit caller
trusted:true(SDK/CLI — caller-owned) or human review (MCP and the default path). The MCP tools add agent-surface safety: a shared path-confinement primitive (confineUnderRoot) keeps an agent-supplied path inside the project root for both tools (rejecting.., absolute, and symlinked-ancestor escapes), import staging is bounded by a pending-candidate cap, and both handlers run silently so they can never corrupt the JSON-RPC stdio stream. Lock contention and a full queue surface as typed errors mapped to clean tool results.Test plan
npx tsc --noEmit,npm run build,npm test(1637 passed),npx fallow(0 above threshold), dupes cleanrunOkfExport/runOkfImportemit zero stdout/stderr on the staged/dry-run paths (asserted via a sharedassertNoOutputhelper), incl. a wiki with a missing-source citation that would otherwise printconfineUnderRootdirect tests:.., absolute-outside, existing-symlink-escape, fresh-child-under-symlinked-parent, and valid-inside/fresh-defaultexportOkfwrites a bundle;importOkfstages by default;dryRunstages nothing;trustedwrites liveexport_okfconfined (rejects out-of-root);import_okfstaging-only (notrustedreachable), confined (rejects dir-out-of-root), and refused past the candidate cap; a real stdio JSON-RPC round-trip drives the built serverokf-import-*/okf-export-clitests stay green;src/mcp/tools.tsuntouched