Self-hosted MCP server + dashboard giving Claude Code, Cursor & Hermes agents shared long-term memory over a git-backed markdown vault. Agent-native, Obsidian-compatible second brain — markdown RAG, no vector DB.
TypeScript agent-memoryai-agentsanthropicclaude-codecursorgitknowledge-baseknowledge-graphllmmarkdownmcpmcp-servermodel-context-protocolnote-takingobsidianpkmragsecond-brainself-hostedzettelkasten
feat: harness toggle (off by default) + Connect page + open local MCP
Ronalds Palacis221abd6f9ee8
diff --git a/.env.example b/.env.exampleindex 43c91e1..b815134 100644--- a/.env.example+++ b/.env.example@@ -32,7 +32,9 @@ MCP_TOKEN=""# Local/OSS single-user mode: no Google auth, MCP open. NEVER true in production.AUTH_DISABLED="false"-# ── Harness (brain_capture) ──────────────────────────────────────────────────-# Anthropic key for auto-filing rough dumps. Optional (capture disabled if unset).+# ── Harness (brain_capture) — OFF by default ─────────────────────────────────+# Optional server-side auto-filing of rough dumps. Costs extra tokens; a coding+# agent can do the same over the plain MCP tools for free. Enable both to turn on.+HARNESS_ENABLED="false"ANTHROPIC_API_KEY=""CAPTURE_MODEL="claude-haiku-4-5-20251001"diff --git a/app/(app)/connect/page.tsx b/app/(app)/connect/page.tsxnew file mode 100644index 0000000..79bc311--- /dev/null+++ b/app/(app)/connect/page.tsx@@ -0,0 +1,85 @@+"use client";++import { useEffect, useState } from "react";+import useSWR from "swr";+import { Check, Copy } from "lucide-react";+import { fetcher } from "@/lib/client";++function Copyable({ text }: { text: string }) {+ const [copied, setCopied] = useState(false);+ return (+ <div className="relative">+ <pre className="scrollbar-none overflow-x-auto whitespace-pre rounded-lg border border-border bg-muted px-4 py-3 font-mono text-xs">+ {text}+ </pre>+ <button+ onClick={() => {+ navigator.clipboard.writeText(text);+ setCopied(true);+ setTimeout(() => setCopied(false), 1200);+ }}+ className="absolute right-2 top-2 inline-flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-background hover:text-foreground"+ aria-label="Copy"+ >+ {copied ? <Check size={14} /> : <Copy size={14} />}+ </button>+ </div>+ );+}++export default function ConnectPage() {+ const [origin, setOrigin] = useState("");+ useEffect(() => setOrigin(window.location.origin), []);+ const { data: feat } = useSWR<{ mcpAuthRequired?: boolean; harness?: boolean }>("/api/features", fetcher);++ const mcp = origin ? `${origin}/api/mcp` : "…";+ const needsToken = feat?.mcpAuthRequired !== false;+ const tokenLine = needsToken ? ` \\\n --header "Authorization: Bearer <MCP_TOKEN>"` : "";+ const claudeCmd = `claude mcp add --transport http cortex ${mcp}${tokenLine}`;+ const hermes = `mcp_servers:\n cortex:\n url: ${mcp}\n headers:\n Authorization: "Bearer <MCP_TOKEN>"`;++ return (+ <div className="scrollbar-none h-full overflow-y-auto">+ <div className="mx-auto max-w-3xl px-8 py-10">+ <h1 className="text-2xl font-semibold tracking-tight">Connect an agent</h1>+ <p className="mt-2 max-w-xl text-sm text-muted-foreground">+ Point any MCP client at this brain. Agents read + write over the tools below — nothing+ keeps a local copy of the vault. MCP connection is configured on the agent, not here.+ </p>++ <section className="mt-8 space-y-2">+ <h2 className="text-sm font-medium">MCP endpoint</h2>+ <Copyable text={mcp} />+ </section>++ <section className="mt-6 space-y-2">+ <h2 className="text-sm font-medium">Claude Code</h2>+ <Copyable text={claudeCmd} />+ <p className="text-xs text-muted-foreground">Run in a terminal. Then the brain_* tools are available in your Claude Code sessions.</p>+ </section>++ <section className="mt-6 space-y-2">+ <h2 className="text-sm font-medium">Hermes — ~/.hermes/config.yaml</h2>+ <Copyable text={hermes} />+ </section>++ <section className="mt-6 space-y-2">+ <h2 className="text-sm font-medium">Cursor / other MCP clients</h2>+ <p className="text-sm text-muted-foreground">+ Add an HTTP MCP server with URL <code className="rounded bg-muted px-1">{mcp}</code>+ {needsToken && (+ <> and header <code className="rounded bg-muted px-1">Authorization: Bearer <MCP_TOKEN></code></>+ )}+ .+ </p>+ </section>++ <p className="mt-8 text-xs text-muted-foreground">+ {needsToken+ ? "The MCP requires the MCP_TOKEN bearer set on the server."+ : "Auth is off locally — no token needed. Set MCP_TOKEN before exposing this publicly."}+ </p>+ </div>+ </div>+ );+}diff --git a/app/api/features/route.ts b/app/api/features/route.tsnew file mode 100644index 0000000..02bda08--- /dev/null+++ b/app/api/features/route.ts@@ -0,0 +1,11 @@+import { AUTH_DISABLED, AUTH_SECRET, HARNESS_ENABLED, MCP_TOKEN } from "@/lib/config";++export const dynamic = "force-dynamic";++export function GET() {+ return Response.json({+ harness: HARNESS_ENABLED,+ mcpAuthRequired: MCP_TOKEN !== "",+ dashboardAuthRequired: !AUTH_DISABLED && AUTH_SECRET !== "",+ });+}diff --git a/app/api/mcp/route.ts b/app/api/mcp/route.tsindex 269254f..f530316 100644--- a/app/api/mcp/route.ts+++ b/app/api/mcp/route.ts@@ -1,4 +1,4 @@-import { AUTH_DISABLED, MCP_TOKEN } from "@/lib/config";+import { HARNESS_ENABLED, MCP_TOKEN } from "@/lib/config";import { TOOLS, TOOL_MAP } from "@/lib/mcp/tools";export const dynamic = "force-dynamic";@@ -31,10 +31,13 @@ async function handleMessage(msg: Json): Promise<Json | null> {});case "ping":return rpc(id, {});- case "tools/list":+ case "tools/list": {+ // Hide the auto-filing harness unless it's turned on (agents file notes themselves).+ const tools = TOOLS.filter((t) => t.name !== "brain_capture" || HARNESS_ENABLED);return rpc(id, {- tools: TOOLS.map((t) => ({ name: t.name, description: t.description, inputSchema: t.inputSchema })),+ tools: tools.map((t) => ({ name: t.name, description: t.description, inputSchema: t.inputSchema })),});+ }case "tools/call": {const tool = TOOL_MAP.get(params?.name);if (!tool) return rpc(id, undefined, { code: -32602, message: `unknown tool: ${params?.name}` });@@ -56,8 +59,9 @@ function jsonResponse(body: Json, status = 200) {}export async function POST(req: Request) {+ // Enforce the bearer only when a token is configured (open locally when unset).const token = (req.headers.get("authorization") || "").replace(/^Bearer\s+/i, "").trim();- if (!AUTH_DISABLED && (!MCP_TOKEN || token !== MCP_TOKEN)) {+ if (MCP_TOKEN && token !== MCP_TOKEN) {return jsonResponse(rpc(null, undefined, { code: -32001, message: "unauthorized" }), 401);}diff --git a/components/sidebar.tsx b/components/sidebar.tsxindex 2d408b8..51f898a 100644--- a/components/sidebar.tsx+++ b/components/sidebar.tsx@@ -4,7 +4,7 @@ import useSWR, { useSWRConfig } from "swr";import Link from "next/link";import { usePathname, useRouter } from "next/navigation";import { useState } from "react";-import { Search, Network, FilePlus, Zap } from "lucide-react";+import { Search, Network, FilePlus, Zap, Plug } from "lucide-react";import { fetcher, type TreeNode } from "@/lib/client";import { Tree } from "./tree";import { ThemeToggle } from "./theme-toggle";@@ -13,6 +13,7 @@ const APP_NAME = process.env.NEXT_PUBLIC_APP_NAME || "Cortex";export function Sidebar() {const { data } = useSWR<{ tree: TreeNode }>("/api/tree", fetcher, { refreshInterval: 5000 });+ const { data: feat } = useSWR<{ harness?: boolean }>("/api/features", fetcher);const { mutate } = useSWRConfig();const pathname = usePathname();const router = useRouter();@@ -91,9 +92,11 @@ export function Sidebar() {<Search size={13} /> Search<kbd className="ml-auto font-mono text-[10px] opacity-60">⌘K</kbd></button>- <button onClick={() => { setCapturing((c) => !c); setCreating(false); }} title="Quick capture" className={iconBtn}>- <Zap size={14} />- </button>+ {feat?.harness && (+ <button onClick={() => { setCapturing((c) => !c); setCreating(false); }} title="Quick capture" className={iconBtn}>+ <Zap size={14} />+ </button>+ )}<button onClick={() => { setCreating((c) => !c); setCapturing(false); }} title="New note" className={iconBtn}><FilePlus size={14} /></button>@@ -154,7 +157,14 @@ export function Sidebar() {)}</nav>- <div className="flex items-center justify-end border-t border-border px-2 py-1.5">+ <div className="flex items-center justify-between border-t border-border px-2 py-1.5">+ <Link+ href="/connect"+ title="Connect an agent"+ className="inline-flex size-8 items-center justify-center rounded-md text-muted-foreground transition-colors hover:text-foreground"+ >+ <Plug size={14} />+ </Link><ThemeToggle /></div></aside>diff --git a/lib/config.ts b/lib/config.tsindex 6ba4d39..37d27de 100644--- a/lib/config.ts+++ b/lib/config.ts@@ -45,6 +45,13 @@ export const SESSION_COOKIE = "cortex_session";/** When true, the git-sync loop commits + pushes vault changes to the remote. */export const GIT_SYNC_ENABLED = process.env.GIT_SYNC_ENABLED === "true";-/** Anthropic key for the brain_capture harness (rough dump -> filed note). Optional. */+/** Anthropic key for the brain_capture harness (rough dump -> filed note). */export const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY ?? "";export const CAPTURE_MODEL = process.env.CAPTURE_MODEL ?? "claude-haiku-4-5-20251001";++/**+ * Server-side auto-filing harness (brain_capture). OFF by default: a capable coding+ * agent can do the filing itself over the plain MCP tools with no extra server tokens.+ * Turn on only if you want the vault to file rough dumps on its own.+ */+export const HARNESS_ENABLED = process.env.HARNESS_ENABLED === "true" && ANTHROPIC_API_KEY !== "";diff --git a/lib/harness.ts b/lib/harness.tsindex eb9e44b..8c8f118 100644--- a/lib/harness.ts+++ b/lib/harness.ts@@ -1,4 +1,4 @@-import { ANTHROPIC_API_KEY, CAPTURE_MODEL } from "@/lib/config";+import { ANTHROPIC_API_KEY, CAPTURE_MODEL, HARNESS_ENABLED } from "@/lib/config";import { getTree, listNotes, readVaultFile } from "@/lib/vault/store";import { writeNote } from "@/lib/vault/write";@@ -35,7 +35,9 @@ function extractJson(text: string): { path: string; frontmatter?: Record<string,/** Take a rough dump, let the model decide the path + frontmatter, and write the note. */export async function captureNote(rough: string): Promise<CaptureResult> {- if (!ANTHROPIC_API_KEY) throw new Error("ANTHROPIC_API_KEY not set — brain_capture is disabled");+ if (!HARNESS_ENABLED)+ throw new Error("brain_capture is off — set HARNESS_ENABLED=true and ANTHROPIC_API_KEY to enable the auto-filing harness");+ if (!ANTHROPIC_API_KEY) throw new Error("ANTHROPIC_API_KEY not set");const today = new Date().toISOString().slice(0, 10);const schema = readVaultFile("SCHEMA.md") ?? "(no SCHEMA.md)";const user = `Today is ${today}.\nExisting folders: ${folderList().join(", ") || "(none yet)"}\n\nSCHEMA:\n${schema}\n\nRough note to file:\n"""\n${rough}\n"""`;diff --git a/sample-vault/inbox/test.md b/sample-vault/inbox/test.mdnew file mode 100644index 0000000..83c831f--- /dev/null+++ b/sample-vault/inbox/test.md@@ -0,0 +1 @@+# test