@spooky-sync/cli 0.0.1-canary.69 → 0.0.1-canary.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ interface ConnectedTab {
2
+ tabId: number;
3
+ url?: string;
4
+ title?: string;
5
+ }
6
+ export declare class Bridge {
7
+ private wss;
8
+ private extensionSocket;
9
+ private connectedTabs;
10
+ private pendingRequests;
11
+ private requestCounter;
12
+ private pingInterval;
13
+ get isConnected(): boolean;
14
+ getConnectedTabs(): ConnectedTab[];
15
+ start(): Promise<void>;
16
+ private startPing;
17
+ private stopPing;
18
+ private handleMessage;
19
+ request(method: string, params?: Record<string, unknown>, tabId?: number): Promise<unknown>;
20
+ private getDefaultTabId;
21
+ stop(): Promise<void>;
22
+ }
23
+ export {};
@@ -0,0 +1,155 @@
1
+ import { WebSocketServer, WebSocket } from 'ws';
2
+ import { isBridgeResponse, isBridgeNotification, BRIDGE_PORT, } from './protocol.js';
3
+ const REQUEST_TIMEOUT_MS = 10_000;
4
+ export class Bridge {
5
+ wss = null;
6
+ extensionSocket = null;
7
+ connectedTabs = new Map();
8
+ pendingRequests = new Map();
9
+ requestCounter = 0;
10
+ pingInterval = null;
11
+ get isConnected() {
12
+ return this.extensionSocket?.readyState === WebSocket.OPEN;
13
+ }
14
+ getConnectedTabs() {
15
+ return Array.from(this.connectedTabs.values());
16
+ }
17
+ start() {
18
+ const port = Number.parseInt(process.env.SP00KY_MCP_PORT || '', 10) || BRIDGE_PORT;
19
+ return new Promise((resolve, reject) => {
20
+ this.wss = new WebSocketServer({ host: '127.0.0.1', port }, () => {
21
+ process.stderr.write(`[sp00ky-mcp] Bridge listening on ws://127.0.0.1:${port}\n`);
22
+ resolve();
23
+ });
24
+ this.wss.on('error', (err) => {
25
+ process.stderr.write(`[sp00ky-mcp] Bridge error: ${err.message}\n`);
26
+ reject(err);
27
+ });
28
+ this.wss.on('connection', (ws) => {
29
+ process.stderr.write('[sp00ky-mcp] Extension connected\n');
30
+ // Only allow one extension connection at a time
31
+ if (this.extensionSocket) {
32
+ this.extensionSocket.close();
33
+ }
34
+ this.extensionSocket = ws;
35
+ // Start keepalive pings
36
+ this.startPing(ws);
37
+ ws.on('message', (data) => {
38
+ try {
39
+ const msg = JSON.parse(data.toString());
40
+ this.handleMessage(msg);
41
+ }
42
+ catch (err) {
43
+ process.stderr.write(`[sp00ky-mcp] Bad message: ${err}\n`);
44
+ }
45
+ });
46
+ ws.on('close', () => {
47
+ process.stderr.write('[sp00ky-mcp] Extension disconnected\n');
48
+ if (this.extensionSocket === ws) {
49
+ this.extensionSocket = null;
50
+ this.connectedTabs.clear();
51
+ this.stopPing();
52
+ // Reject all pending requests
53
+ for (const [id, pending] of this.pendingRequests) {
54
+ pending.reject(new Error('Extension disconnected'));
55
+ clearTimeout(pending.timer);
56
+ this.pendingRequests.delete(id);
57
+ }
58
+ }
59
+ });
60
+ ws.on('error', (err) => {
61
+ process.stderr.write(`[sp00ky-mcp] Socket error: ${err.message}\n`);
62
+ });
63
+ });
64
+ });
65
+ }
66
+ startPing(ws) {
67
+ this.stopPing();
68
+ this.pingInterval = setInterval(() => {
69
+ if (ws.readyState === WebSocket.OPEN) {
70
+ ws.ping();
71
+ }
72
+ }, 20_000);
73
+ }
74
+ stopPing() {
75
+ if (this.pingInterval) {
76
+ clearInterval(this.pingInterval);
77
+ this.pingInterval = null;
78
+ }
79
+ }
80
+ handleMessage(msg) {
81
+ // Handle response to a pending request
82
+ if (isBridgeResponse(msg)) {
83
+ const pending = this.pendingRequests.get(msg.id);
84
+ if (pending) {
85
+ clearTimeout(pending.timer);
86
+ this.pendingRequests.delete(msg.id);
87
+ if (msg.error) {
88
+ pending.reject(new Error(msg.error.message));
89
+ }
90
+ else {
91
+ pending.resolve(msg.result);
92
+ }
93
+ }
94
+ return;
95
+ }
96
+ // Handle notifications from extension
97
+ if (isBridgeNotification(msg)) {
98
+ if (msg.method === 'tabsChanged') {
99
+ this.connectedTabs.clear();
100
+ const tabs = msg.params.tabs;
101
+ for (const tab of tabs) {
102
+ this.connectedTabs.set(tab.tabId, tab);
103
+ }
104
+ }
105
+ return;
106
+ }
107
+ }
108
+ async request(method, params = {}, tabId) {
109
+ if (!this.extensionSocket || this.extensionSocket.readyState !== WebSocket.OPEN) {
110
+ throw new Error('No extension connected. Make sure the Sp00ky DevTools extension is running and has a page with Sp00ky open.');
111
+ }
112
+ const id = `mcp-${++this.requestCounter}`;
113
+ const resolvedTabId = tabId ?? this.getDefaultTabId();
114
+ const request = {
115
+ jsonrpc: '2.0',
116
+ id,
117
+ method,
118
+ params,
119
+ ...(resolvedTabId !== undefined ? { tabId: resolvedTabId } : {}),
120
+ };
121
+ return new Promise((resolve, reject) => {
122
+ const timer = setTimeout(() => {
123
+ this.pendingRequests.delete(id);
124
+ reject(new Error(`Request timed out after ${REQUEST_TIMEOUT_MS}ms: ${method}`));
125
+ }, REQUEST_TIMEOUT_MS);
126
+ this.pendingRequests.set(id, { resolve, reject, timer });
127
+ // oxlint-disable-next-line no-non-null-assertion
128
+ this.extensionSocket.send(JSON.stringify(request));
129
+ });
130
+ }
131
+ getDefaultTabId() {
132
+ const tabs = this.getConnectedTabs();
133
+ return tabs.length > 0 ? tabs[0].tabId : undefined;
134
+ }
135
+ async stop() {
136
+ this.stopPing();
137
+ for (const [id, pending] of this.pendingRequests) {
138
+ clearTimeout(pending.timer);
139
+ pending.reject(new Error('Bridge shutting down'));
140
+ this.pendingRequests.delete(id);
141
+ }
142
+ if (this.extensionSocket) {
143
+ this.extensionSocket.close();
144
+ this.extensionSocket = null;
145
+ }
146
+ return new Promise((resolve) => {
147
+ if (this.wss) {
148
+ this.wss.close(() => resolve());
149
+ }
150
+ else {
151
+ resolve();
152
+ }
153
+ });
154
+ }
155
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env node
2
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
+ import { Bridge } from './bridge.js';
4
+ import { SurrealClient } from './surreal.js';
5
+ import { createServer } from './server.js';
6
+ async function main() {
7
+ const bridge = new Bridge();
8
+ await bridge.start();
9
+ const surreal = process.env.SURREAL_URL
10
+ ? new SurrealClient({
11
+ url: process.env.SURREAL_URL,
12
+ namespace: process.env.SURREAL_NS ?? 'main',
13
+ database: process.env.SURREAL_DB ?? 'main',
14
+ username: process.env.SURREAL_USER ?? 'root',
15
+ password: process.env.SURREAL_PASS ?? 'root',
16
+ })
17
+ : null;
18
+ if (surreal) {
19
+ process.stderr.write(`[sp00ky-mcp] Direct DB mode enabled (${process.env.SURREAL_URL})\n`);
20
+ }
21
+ const server = createServer(bridge, surreal);
22
+ const transport = new StdioServerTransport();
23
+ await server.connect(transport);
24
+ process.stderr.write('[sp00ky-mcp] MCP server running on stdio\n');
25
+ // Graceful shutdown
26
+ const cleanup = async () => {
27
+ process.stderr.write('[sp00ky-mcp] Shutting down...\n');
28
+ await bridge.stop();
29
+ process.exit(0);
30
+ };
31
+ process.on('SIGINT', cleanup);
32
+ process.on('SIGTERM', cleanup);
33
+ }
34
+ main().catch((err) => {
35
+ process.stderr.write(`[sp00ky-mcp] Fatal error: ${err.message}\n`);
36
+ process.exit(1);
37
+ });
@@ -0,0 +1,34 @@
1
+ export interface BridgeRequest {
2
+ jsonrpc: '2.0';
3
+ id: string;
4
+ method: string;
5
+ params: Record<string, unknown>;
6
+ tabId?: number;
7
+ }
8
+ export interface BridgeResponse {
9
+ jsonrpc: '2.0';
10
+ id: string;
11
+ result?: unknown;
12
+ error?: {
13
+ code: number;
14
+ message: string;
15
+ };
16
+ }
17
+ export interface BridgeNotification {
18
+ jsonrpc: '2.0';
19
+ method: string;
20
+ params: Record<string, unknown>;
21
+ }
22
+ export type BridgeMessage = BridgeRequest | BridgeResponse | BridgeNotification;
23
+ export declare const BRIDGE_METHODS: {
24
+ readonly GET_STATE: "getState";
25
+ readonly RUN_QUERY: "runQuery";
26
+ readonly GET_TABLE_DATA: "getTableData";
27
+ readonly UPDATE_TABLE_ROW: "updateTableRow";
28
+ readonly DELETE_TABLE_ROW: "deleteTableRow";
29
+ readonly CLEAR_HISTORY: "clearHistory";
30
+ };
31
+ export declare const BRIDGE_PORT = 9315;
32
+ export declare function isBridgeResponse(msg: unknown): msg is BridgeResponse;
33
+ export declare function isBridgeRequest(msg: unknown): msg is BridgeRequest;
34
+ export declare function isBridgeNotification(msg: unknown): msg is BridgeNotification;
@@ -0,0 +1,37 @@
1
+ // Shared message types for MCP Server <-> Chrome Extension bridge (JSON-RPC 2.0 style)
2
+ // Methods the MCP server can call on the extension
3
+ export const BRIDGE_METHODS = {
4
+ GET_STATE: 'getState',
5
+ RUN_QUERY: 'runQuery',
6
+ GET_TABLE_DATA: 'getTableData',
7
+ UPDATE_TABLE_ROW: 'updateTableRow',
8
+ DELETE_TABLE_ROW: 'deleteTableRow',
9
+ CLEAR_HISTORY: 'clearHistory',
10
+ };
11
+ export const BRIDGE_PORT = 9315;
12
+ export function isBridgeResponse(msg) {
13
+ return (typeof msg === 'object' &&
14
+ msg !== null &&
15
+ 'jsonrpc' in msg &&
16
+ msg.jsonrpc === '2.0' &&
17
+ 'id' in msg &&
18
+ ('result' in msg || 'error' in msg));
19
+ }
20
+ export function isBridgeRequest(msg) {
21
+ return (typeof msg === 'object' &&
22
+ msg !== null &&
23
+ 'jsonrpc' in msg &&
24
+ msg.jsonrpc === '2.0' &&
25
+ 'method' in msg &&
26
+ 'id' in msg &&
27
+ !('result' in msg) &&
28
+ !('error' in msg));
29
+ }
30
+ export function isBridgeNotification(msg) {
31
+ return (typeof msg === 'object' &&
32
+ msg !== null &&
33
+ 'jsonrpc' in msg &&
34
+ msg.jsonrpc === '2.0' &&
35
+ 'method' in msg &&
36
+ !('id' in msg));
37
+ }
@@ -0,0 +1,4 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import type { Bridge } from './bridge.js';
3
+ import type { SurrealClient } from './surreal.js';
4
+ export declare function createServer(bridge: Bridge, surreal?: SurrealClient | null): McpServer;
@@ -0,0 +1,319 @@
1
+ import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { z } from 'zod';
3
+ import { BRIDGE_METHODS } from './protocol.js';
4
+ function json(data) {
5
+ return { content: [{ type: 'text', text: JSON.stringify(data, null, 2) }] };
6
+ }
7
+ export function createServer(bridge, surreal) {
8
+ const server = new McpServer({
9
+ name: 'sp00ky-devtools',
10
+ version: '0.0.1',
11
+ });
12
+ // --- Tools ---
13
+ server.tool('list_connections', 'List browser tabs connected to Sp00ky DevTools', {}, async () => {
14
+ return json({ connected: bridge.isConnected, tabs: bridge.getConnectedTabs() });
15
+ });
16
+ server.tool('get_state', 'Get the full Sp00ky DevTools state (events, queries, auth, database)', { tabId: z.number().optional().describe('Browser tab ID (uses first connected tab if omitted)') }, async ({ tabId }) => {
17
+ if (!bridge.isConnected) {
18
+ throw new Error('No extension connected. get_state requires the Sp00ky DevTools browser extension.');
19
+ }
20
+ const result = await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId);
21
+ return json(result);
22
+ });
23
+ server.tool('run_query', 'Execute a SurrealQL query against the database', {
24
+ query: z.string().describe('SurrealQL query to execute'),
25
+ target: z.enum(['local', 'remote']).optional().default('remote').describe('Query target: local or remote database'),
26
+ tabId: z.number().optional().describe('Browser tab ID'),
27
+ }, async ({ query, target, tabId }) => {
28
+ if (bridge.isConnected) {
29
+ const result = await bridge.request(BRIDGE_METHODS.RUN_QUERY, { query, target }, tabId);
30
+ return json(result);
31
+ }
32
+ if (surreal) {
33
+ const result = await surreal.query(query);
34
+ return json(result);
35
+ }
36
+ throw new Error('No extension connected and no direct database configured. Set SURREAL_URL or connect the browser extension.');
37
+ });
38
+ server.tool('list_tables', 'List all database tables', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
39
+ if (bridge.isConnected) {
40
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
41
+ const tables = state?.database?.tables ?? [];
42
+ return json(tables);
43
+ }
44
+ if (surreal) {
45
+ const result = await surreal.query('INFO FOR DB;');
46
+ const info = result;
47
+ const tables = info?.[0]?.result?.tables ?? info?.[0]?.tables ?? {};
48
+ return json(Object.keys(tables));
49
+ }
50
+ throw new Error('No extension connected and no direct database configured.');
51
+ });
52
+ server.tool('get_table_data', 'Fetch records from a database table', {
53
+ tableName: z.string().describe('Name of the table'),
54
+ limit: z.number().optional().default(100).describe('Max number of records to return'),
55
+ tabId: z.number().optional().describe('Browser tab ID'),
56
+ }, async ({ tableName, limit, tabId }) => {
57
+ if (bridge.isConnected) {
58
+ const result = await bridge.request(BRIDGE_METHODS.GET_TABLE_DATA, { tableName }, tabId);
59
+ return json(result);
60
+ }
61
+ if (surreal) {
62
+ const result = await surreal.query(`SELECT * FROM \`${tableName}\` LIMIT ${limit};`);
63
+ return json(result);
64
+ }
65
+ throw new Error('No extension connected and no direct database configured.');
66
+ });
67
+ server.tool('update_table_row', 'Update a record in a database table', {
68
+ tableName: z.string().optional().describe('Name of the table (used when browser extension is connected)'),
69
+ recordId: z.string().describe('Record ID to update (e.g. "users:abc123")'),
70
+ updates: z.record(z.unknown()).describe('Fields to update'),
71
+ tabId: z.number().optional().describe('Browser tab ID'),
72
+ }, async ({ tableName, recordId, updates, tabId }) => {
73
+ if (bridge.isConnected) {
74
+ const result = await bridge.request(BRIDGE_METHODS.UPDATE_TABLE_ROW, { tableName, recordId, updates }, tabId);
75
+ return json(result);
76
+ }
77
+ if (surreal) {
78
+ const result = await surreal.query(`UPDATE ${recordId} MERGE ${JSON.stringify(updates)};`);
79
+ return json(result);
80
+ }
81
+ throw new Error('No extension connected and no direct database configured.');
82
+ });
83
+ server.tool('delete_table_row', 'Delete a record from a database table', {
84
+ tableName: z.string().optional().describe('Name of the table (used when browser extension is connected)'),
85
+ recordId: z.string().describe('Record ID to delete (e.g. "users:abc123")'),
86
+ tabId: z.number().optional().describe('Browser tab ID'),
87
+ }, async ({ tableName, recordId, tabId }) => {
88
+ if (bridge.isConnected) {
89
+ const result = await bridge.request(BRIDGE_METHODS.DELETE_TABLE_ROW, { tableName, recordId }, tabId);
90
+ return json(result);
91
+ }
92
+ if (surreal) {
93
+ const result = await surreal.query(`DELETE ${recordId};`);
94
+ return json(result);
95
+ }
96
+ throw new Error('No extension connected and no direct database configured.');
97
+ });
98
+ server.tool('get_active_queries', 'Get all active live queries and their data', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
99
+ if (bridge.isConnected) {
100
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
101
+ return json(state?.activeQueries ?? []);
102
+ }
103
+ if (surreal) {
104
+ const result = await surreal.query('SELECT * FROM _00_query;');
105
+ return json(result);
106
+ }
107
+ throw new Error('No extension connected and no direct database configured.');
108
+ });
109
+ server.tool('get_query_timings', 'Per-query processing-time breakdown for debugging: SSP phases (parse/plan/snapshot at registration; store-apply/circuit-step/transform per ingest, plus whole-ingest `ssp` wall time), local & remote record-fetch, and frontend reconcile — each as { lastMs, p50, p90, p99, count } — sorted slowest-first. Requires the Sp00ky DevTools browser extension. The SurrealDB fallback only exposes persisted materialization percentiles (the per-phase samples are in-memory).', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
110
+ if (bridge.isConnected) {
111
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
112
+ const queries = state?.activeQueries
113
+ ? Object.values(state.activeQueries)
114
+ : [];
115
+ // Rank by the dominant phases' p90 (fall back to last) so the slowest
116
+ // query/phase surfaces first.
117
+ const scoreOf = (t) => ['ssp', 'localFetch', 'remoteFetch', 'frontend'].reduce((s, k) => s + (t?.[k]?.p90 ?? t?.[k]?.lastMs ?? 0), 0);
118
+ const rows = queries
119
+ .map((q) => ({
120
+ queryHash: q.queryHash,
121
+ query: q.query,
122
+ updateCount: q.timings?.updateCount ?? q.updateCount,
123
+ errorCount: q.timings?.errorCount,
124
+ timings: q.timings ?? null,
125
+ _score: scoreOf(q.timings),
126
+ }))
127
+ .sort((a, b) => b._score - a._score)
128
+ // oxlint-disable-next-line no-unused-vars -- strip the internal sort key
129
+ .map(({ _score, ...rest }) => rest);
130
+ return json(rows);
131
+ }
132
+ if (surreal) {
133
+ const result = await surreal.query('SELECT * FROM _00_query;');
134
+ return json(result);
135
+ }
136
+ throw new Error('No extension connected and no direct database configured.');
137
+ });
138
+ server.tool('get_events', 'Get event history, optionally filtered by type', {
139
+ eventType: z.string().optional().describe('Filter by event type'),
140
+ limit: z.number().optional().default(50).describe('Max number of events to return'),
141
+ tabId: z.number().optional().describe('Browser tab ID'),
142
+ }, async ({ eventType, limit, tabId }) => {
143
+ if (bridge.isConnected) {
144
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
145
+ let events = state?.eventsHistory ?? [];
146
+ if (eventType) {
147
+ events = events.filter((e) => e.eventType === eventType);
148
+ }
149
+ if (limit) {
150
+ events = events.slice(-limit);
151
+ }
152
+ return json(events);
153
+ }
154
+ if (surreal) {
155
+ const result = await surreal.query(`SELECT * FROM _00_events ORDER BY timestamp DESC LIMIT ${limit};`);
156
+ return json(result);
157
+ }
158
+ throw new Error('No extension connected and no direct database configured.');
159
+ });
160
+ server.tool('get_auth_state', 'Get the current authentication state', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
161
+ if (!bridge.isConnected) {
162
+ throw new Error('No extension connected. get_auth_state requires the Sp00ky DevTools browser extension.');
163
+ }
164
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
165
+ return json(state?.auth ?? null);
166
+ });
167
+ server.tool('clear_history', 'Clear the event history', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
168
+ if (!bridge.isConnected) {
169
+ throw new Error('No extension connected. clear_history requires the Sp00ky DevTools browser extension.');
170
+ }
171
+ await bridge.request(BRIDGE_METHODS.CLEAR_HISTORY, {}, tabId);
172
+ return { content: [{ type: 'text', text: 'History cleared.' }] };
173
+ });
174
+ server.tool('describe_schema', 'Describe all tables with columns, types, and sp00ky annotations (@crdt, @parent). Stitches INFO FOR DB with parsed schema metadata. With the browser extension this returns @crdt/@parent semantics; direct-DB mode returns raw column info only.', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
175
+ if (bridge.isConnected) {
176
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
177
+ const dbState = state?.database ?? {};
178
+ return json({
179
+ source: 'extension',
180
+ tables: dbState.tables ?? [],
181
+ relationships: dbState.relationships ?? [],
182
+ });
183
+ }
184
+ if (surreal) {
185
+ const dbInfo = (await surreal.query('INFO FOR DB;'));
186
+ const tablesObj = dbInfo?.[0]?.result?.tables ?? dbInfo?.[0]?.tables ?? {};
187
+ const tableNames = Object.keys(tablesObj);
188
+ const tables = await Promise.all(tableNames.map(async (name) => {
189
+ try {
190
+ const info = (await surreal.query(`INFO FOR TABLE \`${name}\`;`));
191
+ const fieldsObj = info?.[0]?.result?.fields ?? info?.[0]?.fields ?? {};
192
+ const columns = Object.entries(fieldsObj).map(([fname, def]) => ({
193
+ name: fname,
194
+ definition: typeof def === 'string' ? def : JSON.stringify(def),
195
+ }));
196
+ return { name, columns };
197
+ }
198
+ catch (e) {
199
+ return { name, columns: [], error: e instanceof Error ? e.message : String(e) };
200
+ }
201
+ }));
202
+ return json({
203
+ source: 'direct-db',
204
+ note: '@crdt / @parent annotations are not visible in direct-DB mode; connect the browser extension to see them.',
205
+ tables,
206
+ });
207
+ }
208
+ throw new Error('No extension connected and no direct database configured.');
209
+ });
210
+ server.tool('lint_query', 'Validate a SurrealQL query without running it. Sends EXPLAIN <query> through the connected channel; returns parse / plan errors with location when SurrealDB provides them.', {
211
+ query: z.string().describe('SurrealQL query to validate'),
212
+ target: z
213
+ .enum(['local', 'remote'])
214
+ .optional()
215
+ .default('remote')
216
+ .describe('When using the extension: lint against local (cache) or remote DB'),
217
+ tabId: z.number().optional().describe('Browser tab ID'),
218
+ }, async ({ query, target, tabId }) => {
219
+ const trimmed = query.trim().replace(/;\s*$/, '');
220
+ const explainQuery = /^\s*EXPLAIN\b/i.test(trimmed) ? trimmed : `EXPLAIN ${trimmed};`;
221
+ const parseError = (msg) => {
222
+ const m = msg.match(/line\s+(\d+)(?:[,\s]+col(?:umn)?\s+(\d+))?/i);
223
+ return {
224
+ ok: false,
225
+ errors: [
226
+ {
227
+ message: msg,
228
+ line: m ? Number(m[1]) : undefined,
229
+ column: m && m[2] ? Number(m[2]) : undefined,
230
+ },
231
+ ],
232
+ };
233
+ };
234
+ const inspectResult = (raw) => {
235
+ const arr = Array.isArray(raw) ? raw : [raw];
236
+ const errors = arr
237
+ .map((r) => (r && r.status === 'ERR' ? r.result ?? r.message : null))
238
+ .filter(Boolean);
239
+ if (errors.length > 0) {
240
+ return { ok: false, errors: errors.map((m) => parseError(m).errors[0]) };
241
+ }
242
+ return { ok: true, plan: arr };
243
+ };
244
+ try {
245
+ if (bridge.isConnected) {
246
+ const result = await bridge.request(BRIDGE_METHODS.RUN_QUERY, { query: explainQuery, target }, tabId);
247
+ return json(inspectResult(result));
248
+ }
249
+ if (surreal) {
250
+ const result = await surreal.query(explainQuery);
251
+ return json(inspectResult(result));
252
+ }
253
+ throw new Error('No extension connected and no direct database configured.');
254
+ }
255
+ catch (e) {
256
+ const msg = e instanceof Error ? e.message : String(e);
257
+ return json(parseError(msg));
258
+ }
259
+ });
260
+ // --- Resources ---
261
+ server.resource('state', 'sp00ky://state', { description: 'Full Sp00ky DevTools state' }, async (uri) => {
262
+ if (!bridge.isConnected) {
263
+ throw new Error('No extension connected. State resource requires the browser extension.');
264
+ }
265
+ const state = await bridge.request(BRIDGE_METHODS.GET_STATE);
266
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(state, null, 2) }] };
267
+ });
268
+ server.resource('tables', 'sp00ky://tables', { description: 'List of database tables' }, async (uri) => {
269
+ if (bridge.isConnected) {
270
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE));
271
+ const tables = state?.database?.tables ?? [];
272
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(tables, null, 2) }] };
273
+ }
274
+ if (surreal) {
275
+ const result = await surreal.query('INFO FOR DB;');
276
+ const info = result;
277
+ const tables = info?.[0]?.result?.tables ?? info?.[0]?.tables ?? {};
278
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(Object.keys(tables), null, 2) }] };
279
+ }
280
+ throw new Error('No extension connected and no direct database configured.');
281
+ });
282
+ server.resource('table-data', new ResourceTemplate('sp00ky://tables/{tableName}', { list: undefined }), { description: 'Contents of a specific database table' }, async (uri, variables) => {
283
+ const tableName = variables.tableName;
284
+ if (bridge.isConnected) {
285
+ const result = await bridge.request(BRIDGE_METHODS.GET_TABLE_DATA, { tableName });
286
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(result, null, 2) }] };
287
+ }
288
+ if (surreal) {
289
+ const result = await surreal.query(`SELECT * FROM \`${tableName}\` LIMIT 100;`);
290
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(result, null, 2) }] };
291
+ }
292
+ throw new Error('No extension connected and no direct database configured.');
293
+ });
294
+ server.resource('queries', 'sp00ky://queries', { description: 'Active live queries' }, async (uri) => {
295
+ if (bridge.isConnected) {
296
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE));
297
+ const queries = state?.activeQueries ?? [];
298
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(queries, null, 2) }] };
299
+ }
300
+ if (surreal) {
301
+ const result = await surreal.query('SELECT * FROM _00_query;');
302
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(result, null, 2) }] };
303
+ }
304
+ throw new Error('No extension connected and no direct database configured.');
305
+ });
306
+ server.resource('events', 'sp00ky://events', { description: 'Event history' }, async (uri) => {
307
+ if (bridge.isConnected) {
308
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE));
309
+ const events = state?.eventsHistory ?? [];
310
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(events, null, 2) }] };
311
+ }
312
+ if (surreal) {
313
+ const result = await surreal.query('SELECT * FROM _00_events ORDER BY timestamp DESC LIMIT 50;');
314
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(result, null, 2) }] };
315
+ }
316
+ throw new Error('No extension connected and no direct database configured.');
317
+ });
318
+ return server;
319
+ }
@@ -0,0 +1,13 @@
1
+ export interface SurrealConfig {
2
+ url: string;
3
+ namespace: string;
4
+ database: string;
5
+ username: string;
6
+ password: string;
7
+ }
8
+ export declare class SurrealClient {
9
+ private config;
10
+ private authHeader;
11
+ constructor(config: SurrealConfig);
12
+ query(surql: string): Promise<unknown[]>;
13
+ }
@@ -0,0 +1,27 @@
1
+ export class SurrealClient {
2
+ config;
3
+ authHeader;
4
+ constructor(config) {
5
+ this.config = config;
6
+ this.authHeader =
7
+ 'Basic ' + Buffer.from(`${config.username}:${config.password}`).toString('base64');
8
+ }
9
+ async query(surql) {
10
+ const res = await fetch(`${this.config.url}/sql`, {
11
+ method: 'POST',
12
+ headers: {
13
+ 'Content-Type': 'application/json',
14
+ Authorization: this.authHeader,
15
+ 'surreal-ns': this.config.namespace,
16
+ 'surreal-db': this.config.database,
17
+ Accept: 'application/json',
18
+ },
19
+ body: surql,
20
+ });
21
+ if (!res.ok) {
22
+ const text = await res.text();
23
+ throw new Error(`SurrealDB query failed (${res.status}): ${text}`);
24
+ }
25
+ return res.json();
26
+ }
27
+ }
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spooky-sync/cli",
3
- "version": "0.0.1-canary.69",
3
+ "version": "0.0.1-canary.71",
4
4
  "description": "Generate TypeScript/Dart types from SurrealDB schema files",
5
5
  "type": "module",
6
6
  "main": "./dist/syncgen.cjs",
@@ -61,10 +61,10 @@
61
61
  "vitest": "^1.0.0"
62
62
  },
63
63
  "optionalDependencies": {
64
- "@spooky-sync/cli-darwin-arm64": "0.0.1-canary.69",
65
- "@spooky-sync/cli-darwin-x64": "0.0.1-canary.69",
66
- "@spooky-sync/cli-linux-arm64": "0.0.1-canary.69",
67
- "@spooky-sync/cli-linux-x64": "0.0.1-canary.69",
68
- "@spooky-sync/cli-win32-x64": "0.0.1-canary.69"
64
+ "@spooky-sync/cli-darwin-arm64": "0.0.1-canary.71",
65
+ "@spooky-sync/cli-darwin-x64": "0.0.1-canary.71",
66
+ "@spooky-sync/cli-linux-arm64": "0.0.1-canary.71",
67
+ "@spooky-sync/cli-linux-x64": "0.0.1-canary.71",
68
+ "@spooky-sync/cli-win32-x64": "0.0.1-canary.71"
69
69
  }
70
70
  }