-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathdaemon.ts
More file actions
230 lines (205 loc) · 5.25 KB
/
daemon.ts
File metadata and controls
230 lines (205 loc) · 5.25 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
#!/usr/bin/env node
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import fs from 'node:fs';
import {createServer, type Server} from 'node:net';
import path from 'node:path';
import process from 'node:process';
import {Client} from '@modelcontextprotocol/sdk/client/index.js';
import {StdioClientTransport} from '@modelcontextprotocol/sdk/client/stdio.js';
import {logger} from '../logger.js';
import {PipeTransport} from '../third_party/index.js';
import {VERSION} from '../version.js';
import type {DaemonMessage} from './types.js';
import {
getDaemonPid,
getPidFilePath,
getSocketPath,
INDEX_SCRIPT_PATH,
IS_WINDOWS,
isDaemonRunning,
} from './utils.js';
const pid = getDaemonPid();
if (isDaemonRunning(pid)) {
logger('Another daemon process is running.');
process.exit(1);
}
const pidFilePath = getPidFilePath();
fs.mkdirSync(path.dirname(pidFilePath), {
recursive: true,
});
fs.writeFileSync(pidFilePath, process.pid.toString());
logger(`Writing ${process.pid.toString()} to ${pidFilePath}`);
const socketPath = getSocketPath();
let mcpClient: Client | null = null;
let mcpTransport: StdioClientTransport | null = null;
let server: Server | null = null;
async function setupMCPClient() {
console.log('Setting up MCP client connection...');
const args = process.argv.slice(2);
// Create stdio transport for chrome-devtools-mcp
mcpTransport = new StdioClientTransport({
command: process.execPath,
args: [INDEX_SCRIPT_PATH, ...args],
env: process.env as Record<string, string>,
});
mcpClient = new Client(
{
name: 'chrome-devtools-cli-daemon',
version: VERSION,
},
{
capabilities: {},
},
);
await mcpClient.connect(mcpTransport);
console.log('MCP client connected');
}
interface McpContent {
type: string;
text?: string;
}
interface McpResult {
content?: McpContent[] | string;
text?: string;
}
async function handleRequest(msg: DaemonMessage) {
try {
if (msg.method === 'invoke_tool') {
if (!mcpClient) {
throw new Error('MCP client not initialized');
}
const {tool, args} = msg;
const result = (await mcpClient.callTool({
name: tool,
arguments: args || {},
})) as McpResult | McpContent[];
return {
success: true,
result: JSON.stringify(result),
};
} else if (msg.method === 'stop') {
// Ensure we are not interrupting in-progress starting.
await started;
// Trigger cleanup asynchronously.
setImmediate(() => {
void cleanup();
});
return {
success: true,
message: 'stopping',
};
} else {
return {
success: false,
error: `Unknown method: ${JSON.stringify(msg, null, 2)}`,
};
}
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
return {
success: false,
error: errorMessage,
};
}
}
async function startSocketServer() {
// Remove existing socket file if it exists (only on non-Windows)
if (!IS_WINDOWS) {
try {
fs.unlinkSync(socketPath);
} catch {
// ignore errors.
}
}
return await new Promise<void>((resolve, reject) => {
server = createServer(socket => {
const transport = new PipeTransport(socket, socket);
transport.onmessage = async (message: string) => {
logger('onmessage', message);
const response = await handleRequest(JSON.parse(message));
transport.send(JSON.stringify(response));
socket.end();
};
socket.on('error', error => {
logger('Socket error:', error);
});
});
server.listen(
{
path: socketPath,
readableAll: false,
writableAll: false,
},
async () => {
console.log(`Daemon server listening on ${socketPath}`);
try {
// Setup MCP client
await setupMCPClient();
resolve();
} catch (err) {
reject(err);
}
},
);
server.on('error', error => {
logger('Server error:', error);
reject(error);
});
});
}
async function cleanup() {
console.log('Cleaning up daemon...');
try {
await mcpClient?.close();
} catch (error) {
logger('Error closing MCP client:', error);
}
try {
await mcpTransport?.close();
} catch (error) {
logger('Error closing MCP transport:', error);
}
if (server) {
await new Promise<void>(resolve => {
server!.close(() => resolve());
});
}
if (!IS_WINDOWS) {
try {
fs.unlinkSync(socketPath);
} catch {
// ignore errors
}
}
logger(`unlinking ${pidFilePath}`);
if (fs.existsSync(pidFilePath)) {
fs.unlinkSync(pidFilePath);
}
process.exit(0);
}
// Handle shutdown signals
process.on('SIGTERM', () => {
void cleanup();
});
process.on('SIGINT', () => {
void cleanup();
});
process.on('SIGHUP', () => {
void cleanup();
});
// Handle uncaught errors
process.on('uncaughtException', error => {
logger('Uncaught exception:', error);
});
process.on('unhandledRejection', error => {
logger('Unhandled rejection:', error);
});
// Start the server
const started = startSocketServer().catch(error => {
logger('Failed to start daemon server:', error);
process.exit(1);
});