@spooky-sync/devtools-mcp 0.0.1-canary.20

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 {};
package/dist/bridge.js ADDED
@@ -0,0 +1,154 @@
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 = parseInt(process.env.SPOOKY_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(`[spooky-mcp] Bridge listening on ws://127.0.0.1:${port}\n`);
22
+ resolve();
23
+ });
24
+ this.wss.on('error', (err) => {
25
+ process.stderr.write(`[spooky-mcp] Bridge error: ${err.message}\n`);
26
+ reject(err);
27
+ });
28
+ this.wss.on('connection', (ws) => {
29
+ process.stderr.write('[spooky-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(`[spooky-mcp] Bad message: ${err}\n`);
44
+ }
45
+ });
46
+ ws.on('close', () => {
47
+ process.stderr.write('[spooky-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(`[spooky-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 Spooky DevTools extension is running and has a page with Spooky 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
+ this.extensionSocket.send(JSON.stringify(request));
128
+ });
129
+ }
130
+ getDefaultTabId() {
131
+ const tabs = this.getConnectedTabs();
132
+ return tabs.length > 0 ? tabs[0].tabId : undefined;
133
+ }
134
+ async stop() {
135
+ this.stopPing();
136
+ for (const [id, pending] of this.pendingRequests) {
137
+ clearTimeout(pending.timer);
138
+ pending.reject(new Error('Bridge shutting down'));
139
+ this.pendingRequests.delete(id);
140
+ }
141
+ if (this.extensionSocket) {
142
+ this.extensionSocket.close();
143
+ this.extensionSocket = null;
144
+ }
145
+ return new Promise((resolve) => {
146
+ if (this.wss) {
147
+ this.wss.close(() => resolve());
148
+ }
149
+ else {
150
+ resolve();
151
+ }
152
+ });
153
+ }
154
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+ import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
3
+ import { Bridge } from './bridge.js';
4
+ import { createServer } from './server.js';
5
+ async function main() {
6
+ const bridge = new Bridge();
7
+ await bridge.start();
8
+ const server = createServer(bridge);
9
+ const transport = new StdioServerTransport();
10
+ await server.connect(transport);
11
+ process.stderr.write('[spooky-mcp] MCP server running on stdio\n');
12
+ // Graceful shutdown
13
+ const cleanup = async () => {
14
+ process.stderr.write('[spooky-mcp] Shutting down...\n');
15
+ await bridge.stop();
16
+ process.exit(0);
17
+ };
18
+ process.on('SIGINT', cleanup);
19
+ process.on('SIGTERM', cleanup);
20
+ }
21
+ main().catch((err) => {
22
+ process.stderr.write(`[spooky-mcp] Fatal error: ${err.message}\n`);
23
+ process.exit(1);
24
+ });
@@ -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,3 @@
1
+ import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { Bridge } from './bridge.js';
3
+ export declare function createServer(bridge: Bridge): McpServer;
package/dist/server.js ADDED
@@ -0,0 +1,120 @@
1
+ import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
2
+ import { z } from 'zod';
3
+ import { BRIDGE_METHODS } from './protocol.js';
4
+ export function createServer(bridge) {
5
+ const server = new McpServer({
6
+ name: 'spooky-devtools',
7
+ version: '0.0.1',
8
+ });
9
+ // --- Tools ---
10
+ server.tool('list_connections', 'List browser tabs connected to Spooky DevTools', {}, async () => {
11
+ const tabs = bridge.getConnectedTabs();
12
+ return {
13
+ content: [
14
+ {
15
+ type: 'text',
16
+ text: JSON.stringify({
17
+ connected: bridge.isConnected,
18
+ tabs,
19
+ }, null, 2),
20
+ },
21
+ ],
22
+ };
23
+ });
24
+ server.tool('get_state', 'Get the full Spooky DevTools state (events, queries, auth, database)', { tabId: z.number().optional().describe('Browser tab ID (uses first connected tab if omitted)') }, async ({ tabId }) => {
25
+ const result = await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId);
26
+ return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
27
+ });
28
+ server.tool('run_query', 'Execute a SurrealQL query against the database', {
29
+ query: z.string().describe('SurrealQL query to execute'),
30
+ target: z.enum(['local', 'remote']).optional().default('remote').describe('Query target: local or remote database'),
31
+ tabId: z.number().optional().describe('Browser tab ID'),
32
+ }, async ({ query, target, tabId }) => {
33
+ const result = await bridge.request(BRIDGE_METHODS.RUN_QUERY, { query, target }, tabId);
34
+ return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
35
+ });
36
+ server.tool('list_tables', 'List all database tables', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
37
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
38
+ const tables = state?.database?.tables ?? [];
39
+ return { content: [{ type: 'text', text: JSON.stringify(tables, null, 2) }] };
40
+ });
41
+ server.tool('get_table_data', 'Fetch all records from a database table', {
42
+ tableName: z.string().describe('Name of the table'),
43
+ tabId: z.number().optional().describe('Browser tab ID'),
44
+ }, async ({ tableName, tabId }) => {
45
+ const result = await bridge.request(BRIDGE_METHODS.GET_TABLE_DATA, { tableName }, tabId);
46
+ return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
47
+ });
48
+ server.tool('update_table_row', 'Update a record in a database table', {
49
+ tableName: z.string().describe('Name of the table'),
50
+ recordId: z.string().describe('Record ID to update'),
51
+ updates: z.record(z.unknown()).describe('Fields to update'),
52
+ tabId: z.number().optional().describe('Browser tab ID'),
53
+ }, async ({ tableName, recordId, updates, tabId }) => {
54
+ const result = await bridge.request(BRIDGE_METHODS.UPDATE_TABLE_ROW, { tableName, recordId, updates }, tabId);
55
+ return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
56
+ });
57
+ server.tool('delete_table_row', 'Delete a record from a database table', {
58
+ tableName: z.string().describe('Name of the table'),
59
+ recordId: z.string().describe('Record ID to delete'),
60
+ tabId: z.number().optional().describe('Browser tab ID'),
61
+ }, async ({ tableName, recordId, tabId }) => {
62
+ const result = await bridge.request(BRIDGE_METHODS.DELETE_TABLE_ROW, { tableName, recordId }, tabId);
63
+ return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] };
64
+ });
65
+ server.tool('get_active_queries', 'Get all active live queries and their data', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
66
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
67
+ const queries = state?.activeQueries ?? [];
68
+ return { content: [{ type: 'text', text: JSON.stringify(queries, null, 2) }] };
69
+ });
70
+ server.tool('get_events', 'Get event history, optionally filtered by type', {
71
+ eventType: z.string().optional().describe('Filter by event type'),
72
+ limit: z.number().optional().describe('Max number of events to return'),
73
+ tabId: z.number().optional().describe('Browser tab ID'),
74
+ }, async ({ eventType, limit, tabId }) => {
75
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
76
+ let events = state?.eventsHistory ?? [];
77
+ if (eventType) {
78
+ events = events.filter((e) => e.eventType === eventType);
79
+ }
80
+ if (limit) {
81
+ events = events.slice(-limit);
82
+ }
83
+ return { content: [{ type: 'text', text: JSON.stringify(events, null, 2) }] };
84
+ });
85
+ server.tool('get_auth_state', 'Get the current authentication state', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
86
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE, {}, tabId));
87
+ const auth = state?.auth ?? null;
88
+ return { content: [{ type: 'text', text: JSON.stringify(auth, null, 2) }] };
89
+ });
90
+ server.tool('clear_history', 'Clear the event history', { tabId: z.number().optional().describe('Browser tab ID') }, async ({ tabId }) => {
91
+ await bridge.request(BRIDGE_METHODS.CLEAR_HISTORY, {}, tabId);
92
+ return { content: [{ type: 'text', text: 'History cleared.' }] };
93
+ });
94
+ // --- Resources ---
95
+ server.resource('state', 'spooky://state', { description: 'Full Spooky DevTools state' }, async (uri) => {
96
+ const state = await bridge.request(BRIDGE_METHODS.GET_STATE);
97
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(state, null, 2) }] };
98
+ });
99
+ server.resource('tables', 'spooky://tables', { description: 'List of database tables' }, async (uri) => {
100
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE));
101
+ const tables = state?.database?.tables ?? [];
102
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(tables, null, 2) }] };
103
+ });
104
+ server.resource('table-data', new ResourceTemplate('spooky://tables/{tableName}', { list: undefined }), { description: 'Contents of a specific database table' }, async (uri, variables) => {
105
+ const tableName = variables.tableName;
106
+ const result = await bridge.request(BRIDGE_METHODS.GET_TABLE_DATA, { tableName });
107
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(result, null, 2) }] };
108
+ });
109
+ server.resource('queries', 'spooky://queries', { description: 'Active live queries' }, async (uri) => {
110
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE));
111
+ const queries = state?.activeQueries ?? [];
112
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(queries, null, 2) }] };
113
+ });
114
+ server.resource('events', 'spooky://events', { description: 'Event history' }, async (uri) => {
115
+ const state = (await bridge.request(BRIDGE_METHODS.GET_STATE));
116
+ const events = state?.eventsHistory ?? [];
117
+ return { contents: [{ uri: uri.href, mimeType: 'application/json', text: JSON.stringify(events, null, 2) }] };
118
+ });
119
+ return server;
120
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@spooky-sync/devtools-mcp",
3
+ "version": "0.0.1-canary.20",
4
+ "description": "MCP server for Spooky Sync devtools",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "bin": {
8
+ "spooky-mcp": "./dist/index.js"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "dev": "tsc --watch"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "https://github.com/mono424/spooky.git",
20
+ "directory": "apps/devtools-mcp"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "dependencies": {
26
+ "@modelcontextprotocol/sdk": "^1.12.1",
27
+ "ws": "^8.18.0",
28
+ "zod": "^3.24.0"
29
+ },
30
+ "devDependencies": {
31
+ "@types/ws": "^8.18.0",
32
+ "typescript": "^5.6.2"
33
+ }
34
+ }