-
Notifications
You must be signed in to change notification settings - Fork 849
Expand file tree
/
Copy pathindex.ts
More file actions
643 lines (582 loc) · 21.1 KB
/
Copy pathindex.ts
File metadata and controls
643 lines (582 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StreamableHTTPTransport } from "@hono/mcp";
import { Hono } from "hono";
import { z } from "zod";
import { createClient } from "@supabase/supabase-js";
const SUPABASE_URL = Deno.env.get("SUPABASE_URL")!;
const SUPABASE_SERVICE_ROLE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY")!;
const OPENROUTER_API_KEY = Deno.env.get("OPENROUTER_API_KEY")!;
const MCP_ACCESS_KEY = Deno.env.get("MCP_ACCESS_KEY")!;
const OPENROUTER_BASE = "https://openrouter.ai/api/v1";
const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY);
type ThoughtMatch = {
id: string;
content: string;
metadata: Record<string, unknown>;
similarity: number;
created_at: string;
};
type ThoughtRecord = {
id: string;
content: string;
metadata: Record<string, unknown>;
created_at: string;
updated_at?: string | null;
};
const CITATION_BASE_URL =
Deno.env.get("OPEN_BRAIN_CITATION_BASE_URL") || "https://openbrain.local/thoughts";
function thoughtTitle(content: string, createdAt?: string): string {
const firstLine = content.replace(/\s+/g, " ").trim().slice(0, 80);
const datePrefix = createdAt ? new Date(createdAt).toLocaleDateString() : "Open Brain";
return firstLine ? `${datePrefix} - ${firstLine}` : `${datePrefix} thought`;
}
function thoughtUrl(id: string): string {
return `${CITATION_BASE_URL.replace(/\/$/, "")}/${id}`;
}
async function getEmbedding(text: string): Promise<number[]> {
const r = await fetch(`${OPENROUTER_BASE}/embeddings`, {
method: "POST",
headers: {
Authorization: `Bearer ${OPENROUTER_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openai/text-embedding-3-small",
input: text,
}),
});
if (!r.ok) {
const msg = await r.text().catch(() => "");
throw new Error(`OpenRouter embeddings failed: ${r.status} ${msg}`);
}
const d = await r.json();
return d.data[0].embedding;
}
async function extractMetadata(text: string): Promise<Record<string, unknown>> {
const r = await fetch(`${OPENROUTER_BASE}/chat/completions`, {
method: "POST",
headers: {
Authorization: `Bearer ${OPENROUTER_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openai/gpt-4o-mini",
response_format: { type: "json_object" },
messages: [
{
role: "system",
content: `Extract metadata from the user's captured thought. Return JSON with:
- "people": array of people mentioned (empty if none)
- "action_items": array of implied to-dos (empty if none)
- "dates_mentioned": array of dates YYYY-MM-DD (empty if none)
- "topics": array of 1-3 short topic tags (always at least one)
- "type": one of "observation", "task", "idea", "reference", "person_note"
Only extract what's explicitly there.`,
},
{ role: "user", content: text },
],
}),
});
const d = await r.json();
try {
return JSON.parse(d.choices[0].message.content);
} catch {
return { topics: ["uncategorized"], type: "observation" };
}
}
// --- MCP Server Setup ---
function buildServer(): McpServer {
const server = new McpServer({
name: "open-brain",
version: "1.0.0",
});
// ChatGPT compatibility: restricted connector surfaces, company knowledge, and deep
// research look for exact read-only `search` and `fetch` tool shapes.
server.registerTool(
"search",
{
title: "Search Open Brain",
description:
"Search Open Brain memories by meaning. Use this read-only compatibility tool when ChatGPT needs search/fetch-style access to stored thoughts.",
annotations: {
readOnlyHint: true,
},
inputSchema: {
query: z.string().describe("The search query to run against Open Brain thoughts"),
},
},
async ({ query }) => {
try {
const qEmb = await getEmbedding(query);
const { data, error } = await supabase.rpc("match_thoughts", {
query_embedding: qEmb,
match_threshold: 0.5,
match_count: 10,
filter: {},
});
if (error) {
return {
content: [{ type: "text" as const, text: `Search error: ${error.message}` }],
isError: true,
};
}
const results = ((data || []) as ThoughtMatch[]).map((t) => ({
id: t.id,
title: thoughtTitle(t.content, t.created_at),
url: thoughtUrl(t.id),
}));
return {
content: [{ type: "text" as const, text: JSON.stringify({ results }) }],
};
} catch (err: unknown) {
return {
content: [{ type: "text" as const, text: `Error: ${(err as Error).message}` }],
isError: true,
};
}
}
);
server.registerTool(
"fetch",
{
title: "Fetch Open Brain Thought",
description:
"Fetch one Open Brain thought by ID after using search. Use this read-only compatibility tool to retrieve the full text and metadata for citation.",
annotations: {
readOnlyHint: true,
},
inputSchema: {
id: z.string().describe("The Open Brain thought ID returned by the search tool"),
},
},
async ({ id }) => {
try {
const { data, error } = await supabase
.from("thoughts")
.select("id, content, metadata, created_at, updated_at")
.eq("id", id)
.single();
if (error) {
return {
content: [{ type: "text" as const, text: `Fetch error: ${error.message}` }],
isError: true,
};
}
const thought = data as ThoughtRecord;
const document = {
id: thought.id,
title: thoughtTitle(thought.content, thought.created_at),
text: thought.content,
url: thoughtUrl(thought.id),
metadata: {
...thought.metadata,
created_at: thought.created_at,
updated_at: thought.updated_at,
},
};
return {
content: [{ type: "text" as const, text: JSON.stringify(document) }],
};
} catch (err: unknown) {
return {
content: [{ type: "text" as const, text: `Error: ${(err as Error).message}` }],
isError: true,
};
}
}
);
// Tool 1: Semantic Search
server.registerTool(
"search_thoughts",
{
title: "Search Thoughts",
description:
"Search captured thoughts by meaning. Use this when the user asks about a topic, person, or idea they've previously captured.",
annotations: {
readOnlyHint: true,
},
inputSchema: {
query: z.string().describe("What to search for"),
limit: z.number().optional().default(10),
threshold: z.number().optional().default(0.5),
},
},
async ({ query, limit, threshold }) => {
try {
const qEmb = await getEmbedding(query);
const { data, error } = await supabase.rpc("match_thoughts", {
query_embedding: qEmb,
match_threshold: threshold,
match_count: limit,
filter: {},
});
if (error) {
return {
content: [{ type: "text" as const, text: `Search error: ${error.message}` }],
isError: true,
};
}
if (!data || data.length === 0) {
return {
content: [{ type: "text" as const, text: `No thoughts found matching "${query}".` }],
};
}
const results = data.map(
(
t: ThoughtMatch,
i: number
) => {
const m = t.metadata || {};
const parts = [
`--- Result ${i + 1} (${(t.similarity * 100).toFixed(1)}% match) ---`,
`Captured: ${new Date(t.created_at).toLocaleDateString()}`,
`Type: ${m.type || "unknown"}`,
];
if (Array.isArray(m.topics) && m.topics.length)
parts.push(`Topics: ${(m.topics as string[]).join(", ")}`);
if (Array.isArray(m.people) && m.people.length)
parts.push(`People: ${(m.people as string[]).join(", ")}`);
if (Array.isArray(m.action_items) && m.action_items.length)
parts.push(`Actions: ${(m.action_items as string[]).join("; ")}`);
parts.push(`\n${t.content}`);
return parts.join("\n");
}
);
return {
content: [
{
type: "text" as const,
text: `Found ${data.length} thought(s):\n\n${results.join("\n\n")}`,
},
],
};
} catch (err: unknown) {
return {
content: [{ type: "text" as const, text: `Error: ${(err as Error).message}` }],
isError: true,
};
}
}
);
// Tool 2: List Recent
server.registerTool(
"list_thoughts",
{
title: "List Recent Thoughts",
description:
"List recently captured thoughts with optional filters by type, topic, person, or time range.",
annotations: {
readOnlyHint: true,
},
inputSchema: {
limit: z.number().optional().default(10),
type: z.string().optional().describe("Filter by type: observation, task, idea, reference, person_note"),
topic: z.string().optional().describe("Filter by topic tag"),
person: z.string().optional().describe("Filter by person mentioned"),
days: z.number().optional().describe("Only thoughts from the last N days"),
},
},
async ({ limit, type, topic, person, days }) => {
try {
let q = supabase
.from("thoughts")
.select("content, metadata, created_at")
.order("created_at", { ascending: false })
.limit(limit);
if (type) q = q.contains("metadata", { type });
if (topic) q = q.contains("metadata", { topics: [topic] });
if (person) q = q.contains("metadata", { people: [person] });
if (days) {
const since = new Date();
since.setDate(since.getDate() - days);
q = q.gte("created_at", since.toISOString());
}
const { data, error } = await q;
if (error) {
return {
content: [{ type: "text" as const, text: `Error: ${error.message}` }],
isError: true,
};
}
if (!data || !data.length) {
return { content: [{ type: "text" as const, text: "No thoughts found." }] };
}
const results = data.map(
(
t: { content: string; metadata: Record<string, unknown>; created_at: string },
i: number
) => {
const m = t.metadata || {};
const tags = Array.isArray(m.topics) ? (m.topics as string[]).join(", ") : "";
return `${i + 1}. [${new Date(t.created_at).toLocaleDateString()}] (${m.type || "??"}${tags ? " - " + tags : ""})\n ${t.content}`;
}
);
return {
content: [
{
type: "text" as const,
text: `${data.length} recent thought(s):\n\n${results.join("\n\n")}`,
},
],
};
} catch (err: unknown) {
return {
content: [{ type: "text" as const, text: `Error: ${(err as Error).message}` }],
isError: true,
};
}
}
);
// Tool 3: Stats
server.registerTool(
"thought_stats",
{
title: "Thought Statistics",
description: "Get a summary of all captured thoughts: totals, types, top topics, and people.",
annotations: {
readOnlyHint: true,
},
inputSchema: {},
},
async () => {
try {
const { count } = await supabase
.from("thoughts")
.select("*", { count: "exact", head: true });
const { data } = await supabase
.from("thoughts")
.select("metadata, created_at")
.order("created_at", { ascending: false });
const types: Record<string, number> = {};
const topics: Record<string, number> = {};
const people: Record<string, number> = {};
for (const r of data || []) {
const m = (r.metadata || {}) as Record<string, unknown>;
if (m.type) types[m.type as string] = (types[m.type as string] || 0) + 1;
if (Array.isArray(m.topics))
for (const t of m.topics) topics[t as string] = (topics[t as string] || 0) + 1;
if (Array.isArray(m.people))
for (const p of m.people) people[p as string] = (people[p as string] || 0) + 1;
}
const sort = (o: Record<string, number>): [string, number][] =>
Object.entries(o)
.sort((a, b) => b[1] - a[1])
.slice(0, 10);
const lines: string[] = [
`Total thoughts: ${count}`,
`Date range: ${
data?.length
? new Date(data[data.length - 1].created_at).toLocaleDateString() +
" → " +
new Date(data[0].created_at).toLocaleDateString()
: "N/A"
}`,
"",
"Types:",
...sort(types).map(([k, v]) => ` ${k}: ${v}`),
];
if (Object.keys(topics).length) {
lines.push("", "Top topics:");
for (const [k, v] of sort(topics)) lines.push(` ${k}: ${v}`);
}
if (Object.keys(people).length) {
lines.push("", "People mentioned:");
for (const [k, v] of sort(people)) lines.push(` ${k}: ${v}`);
}
return { content: [{ type: "text" as const, text: lines.join("\n") }] };
} catch (err: unknown) {
return {
content: [{ type: "text" as const, text: `Error: ${(err as Error).message}` }],
isError: true,
};
}
}
);
// Tool 4: Capture Thought
server.registerTool(
"capture_thought",
{
title: "Capture Thought",
description:
"Save a new thought to the Open Brain. Generates an embedding and extracts metadata automatically. Use this when the user wants to save something to their brain directly from any AI client — notes, insights, decisions, or migrated content from other systems.",
annotations: {
readOnlyHint: false,
openWorldHint: false,
destructiveHint: false,
idempotentHint: false,
},
inputSchema: {
content: z.string().describe("The thought to capture — a clear, standalone statement that will make sense when retrieved later by any AI"),
},
},
async ({ content }) => {
try {
const [embedding, metadata] = await Promise.all([
getEmbedding(content),
extractMetadata(content),
]);
const { data: upsertResult, error: upsertError } = await supabase.rpc("upsert_thought", {
p_content: content,
p_payload: { metadata: { ...metadata, source: "mcp" } },
});
if (upsertError) {
return {
content: [{ type: "text" as const, text: `Failed to capture: ${upsertError.message}` }],
isError: true,
};
}
const thoughtId = upsertResult?.id;
const { error: embError } = await supabase
.from("thoughts")
.update({ embedding })
.eq("id", thoughtId);
if (embError) {
return {
content: [{ type: "text" as const, text: `Failed to save embedding: ${embError.message}` }],
isError: true,
};
}
const meta = metadata as Record<string, unknown>;
let confirmation = `Captured as ${meta.type || "thought"}`;
if (Array.isArray(meta.topics) && meta.topics.length)
confirmation += ` — ${(meta.topics as string[]).join(", ")}`;
if (Array.isArray(meta.people) && meta.people.length)
confirmation += ` | People: ${(meta.people as string[]).join(", ")}`;
if (Array.isArray(meta.action_items) && meta.action_items.length)
confirmation += ` | Actions: ${(meta.action_items as string[]).join("; ")}`;
return {
content: [{ type: "text" as const, text: confirmation }],
};
} catch (err: unknown) {
return {
content: [{ type: "text" as const, text: `Error: ${(err as Error).message}` }],
isError: true,
};
}
}
);
return server;
}
// --- Hono App with Auth + CORS ---
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type, x-brain-key, accept, mcp-session-id, mcp-protocol-version, last-event-id",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS, DELETE",
};
// JSON-RPC error code for unauthorized requests.
// Per the JSON-RPC 2.0 spec, the range -32099 to -32000 is reserved for
// implementation-defined server errors. -32001 is the conventional
// "Unauthorized" code used by MCP clients/servers in the wild.
//
// Why a JSON-RPC envelope (HTTP 200) instead of a bare HTTP 401?
// Strict MCP hosts (Codex CLI, Claude Code) treat bare HTTP 4xx responses
// as transport-level failures and tear the connection down rather than
// surfacing the failure to the application layer. Wrapping the auth
// rejection in a JSON-RPC error keeps the connection alive and lets
// clients recover (e.g. prompt the user for a new key, refetch a stale
// cache) instead of dying.
const JSON_RPC_UNAUTHORIZED_CODE = -32001;
const UNAUTHORIZED_MESSAGE = "Unauthorized: missing or invalid authentication.";
/**
* Read the request body as text without consuming the original request's
* body stream for downstream handlers. Returns null on bodyless methods
* or read failure.
*/
async function readBodyText(req: Request): Promise<string | null> {
if (req.method === "GET" || req.method === "HEAD" || req.method === "DELETE") {
return null;
}
try {
return await req.text();
} catch {
return null;
}
}
/**
* Best-effort extraction of the JSON-RPC `id` from a raw request body.
* Returns null when the body is missing, not JSON, or not a JSON-RPC
* shape with an id. Per the JSON-RPC 2.0 spec, id may be a string,
* number, or null — we preserve any of those; anything else becomes null.
*/
function extractJsonRpcId(bodyText: string | null): string | number | null {
if (!bodyText) return null;
try {
const parsed = JSON.parse(bodyText);
if (parsed && typeof parsed === "object" && "id" in parsed) {
const id = (parsed as { id: unknown }).id;
if (typeof id === "string" || typeof id === "number" || id === null) {
return id;
}
}
} catch {
// fall through — malformed body
}
return null;
}
/**
* Build a JSON-RPC 2.0 error envelope response for auth failures.
* Returns HTTP 200 — the JSON-RPC layer expresses the error so that
* strict MCP clients keep the connection alive instead of treating
* the failure as a transport-level fault.
*/
function unauthorizedResponse(id: string | number | null): Response {
const body = {
jsonrpc: "2.0",
error: {
code: JSON_RPC_UNAUTHORIZED_CODE,
message: UNAUTHORIZED_MESSAGE,
},
id,
};
return new Response(JSON.stringify(body), {
status: 200,
headers: {
"Content-Type": "application/json",
...corsHeaders,
},
});
}
const app = new Hono();
// CORS preflight — required for browser/Electron-based clients (Claude Desktop, claude.ai)
app.options("*", (c) => {
return c.text("ok", 200, corsHeaders);
});
app.all("*", async (c) => {
// Accept access key via header OR URL query parameter
const provided = c.req.header("x-brain-key") || new URL(c.req.url).searchParams.get("key");
if (!provided || provided !== MCP_ACCESS_KEY) {
// Return a JSON-RPC 2.0 error envelope (HTTP 200) instead of a bare
// HTTP 401 so strict MCP hosts treat this as an application-level
// error rather than a transport fault and keep the connection alive.
// Best-effort echo of the inbound request id keeps the response
// correlated; malformed/missing bodies fall back to id: null.
const bodyText = await readBodyText(c.req.raw);
const id = extractJsonRpcId(bodyText);
return unauthorizedResponse(id);
}
// Fix: Claude Desktop connectors don't send the Accept header that
// StreamableHTTPTransport requires. Build a patched request if missing.
// See: https://github.com/NateBJones-Projects/OB1/issues/33
if (!c.req.header("accept")?.includes("text/event-stream")) {
const headers = new Headers(c.req.raw.headers);
headers.set("Accept", "application/json, text/event-stream");
const patched = new Request(c.req.raw.url, {
method: c.req.raw.method,
headers,
body: c.req.raw.body,
// @ts-ignore -- duplex required for streaming body in Deno
duplex: "half",
});
Object.defineProperty(c.req, "raw", { value: patched, writable: true });
}
const server = buildServer();
const transport = new StreamableHTTPTransport();
await server.connect(transport);
const response = await transport.handleRequest(c);
if (!response) return c.json({ error: "No response from MCP transport" }, 500, corsHeaders);
response.headers.delete("mcp-session-id");
for (const [k, v] of Object.entries(corsHeaders)) response.headers.set(k, v);
return response;
});
Deno.serve(app.fetch);