-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathmcp-routes.ts
More file actions
452 lines (431 loc) · 18.3 KB
/
Copy pathmcp-routes.ts
File metadata and controls
452 lines (431 loc) · 18.3 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
import type { Express } from 'express';
import fs from 'node:fs';
import { SIDECAR_ENV } from '@open-design/sidecar-proto';
import { buildMcpInstallPayload, type McpInstallPayload } from './mcp-install-info.js';
import { installCodexMcp, probeCodexInstall, uninstallCodexMcp } from './codex-cli.js';
import { MCP_TEMPLATES, readMcpConfig, writeMcpConfig } from './mcp-config.js';
import { beginAuth, exchangeCodeForToken } from './mcp-oauth.js';
import { clearToken, getToken, setToken } from './mcp-tokens.js';
import type { RouteDeps } from './server-context.js';
export interface RegisterMcpRoutesDeps extends RouteDeps<'http' | 'paths' | 'mcp'> {}
export function registerMcpRoutes(app: Express, ctx: RegisterMcpRoutesDeps) {
const { isLocalSameOrigin, resolvedPortRef, sendApiError } = ctx.http;
const { OD_BIN, RUNTIME_DATA_DIR } = ctx.paths;
const { pendingAuth } = ctx.mcp;
const getResolvedPort = () => resolvedPortRef.current;
// Surfaces the absolute paths to the daemon's Node-compatible runtime and
// CLI entry so the Settings → MCP server panel can render snippets that work
// even when `od` isn't on the user's PATH (the common case for source clones
// - and macOS/Linux ship a /usr/bin/od octal-dump tool that shadows ours
// anyway). Cached for 5s because the panel pings on every open and these
// paths cannot change without a daemon restart.
const INSTALL_INFO_TTL_MS = 5000;
let installInfoCache: { t: number; payload: object } | null = null;
// Resolve the install snippet for the current daemon. Shared by the
// public GET /api/mcp/install-info endpoint (renders TOML/JSON for
// the user to copy) and POST /api/mcp/install/codex (which feeds the
// exact same fields into `codex mcp add` so the one-click install
// configures Codex with byte-for-byte the same command as the copy
// snippet would). Keeping this in one place is the whole point of
// the factoring — divergence here would mean Codex behaves
// differently depending on which install path the user took.
function computeInstallPayload(): McpInstallPayload {
const cliPath = OD_BIN;
// The daemon was bootstrapped as a sidecar (tools-dev, packaged) iff
// bootstrapSidecarRuntime stamped OD_SIDECAR_IPC_PATH into the env.
// In sidecar mode the snippet omits --daemon-url and the spawned
// `od mcp` discovers the live URL via the concrete IPC endpoint on
// every spawn, so the client config survives ephemeral-port
// restarts. For direct `od` / `od --port X` launches there is no
// IPC socket; the helper bakes --daemon-url so custom ports keep
// working.
const sidecarIpcPath = process.env[SIDECAR_ENV.IPC_PATH];
const isSidecarMode = sidecarIpcPath != null && sidecarIpcPath.length > 0;
const sidecarEnv: Record<string, string> = {};
if (isSidecarMode) {
sidecarEnv[SIDECAR_ENV.IPC_PATH] = sidecarIpcPath;
}
// tools-dev / packaged launchers export OD_WEB_PORT so the daemon
// knows where the browser-facing Open Design studio is running.
// CLI-only / headless launches set neither and webBaseUrl falls
// through as null — MCP clients then just omit the studio deep
// link from their responses.
const webPortRaw = process.env.OD_WEB_PORT;
const webPortNum = webPortRaw ? Number(webPortRaw) : Number.NaN;
const webBaseUrl = Number.isFinite(webPortNum) && webPortNum > 0
? `http://127.0.0.1:${webPortNum}`
: null;
return buildMcpInstallPayload({
cliPath,
cliExists: fs.existsSync(cliPath),
// process.execPath is the absolute path to the Node-compatible
// runtime running the daemon RIGHT NOW. In packaged builds this
// may be Electron with ELECTRON_RUN_AS_NODE=1 rather than a
// separate bundled Node binary; the helper surfaces that env
// requirement on the command so IDE-spawned MCP clients can
// reproduce the same mode from a minimal OS launcher env.
execPath: process.execPath,
nodeExists: fs.existsSync(process.execPath),
port: getResolvedPort(),
platform: process.platform,
dataDir: RUNTIME_DATA_DIR,
electronAsNode: process.env.ELECTRON_RUN_AS_NODE === '1',
isSidecarMode,
sidecarEnv,
webBaseUrl,
});
}
app.get('/api/mcp/install-info', (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
const now = Date.now();
if (installInfoCache && now - installInfoCache.t < INSTALL_INFO_TTL_MS) {
return res.json(installInfoCache.payload);
}
const payload = computeInstallPayload();
installInfoCache = { t: now, payload };
res.json(payload);
});
// Codex one-click install. Codex CLI exposes `codex mcp add/remove/get`,
// so we shell out to it rather than rewriting ~/.codex/config.toml
// ourselves — that way we inherit Codex's merge / validation rules
// and only need to track its argv. See apps/daemon/src/codex-cli.ts.
const CODEX_MCP_NAME = 'open-design';
app.get('/api/mcp/install/codex/status', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
try {
const status = await probeCodexInstall(CODEX_MCP_NAME);
res.json(status);
} catch (err) {
sendApiError(res, 500, 'CODEX_PROBE_FAILED', err instanceof Error ? err.message : String(err));
}
});
app.post('/api/mcp/install/codex', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
const payload = computeInstallPayload();
if (!payload.cliExists || !payload.nodeExists) {
return sendApiError(res, 500, 'INSTALL_INFO_INCOMPLETE', payload.buildHint ?? 'install payload not ready');
}
try {
await installCodexMcp({
name: CODEX_MCP_NAME,
command: payload.command,
args: payload.args,
env: payload.env,
});
res.json({ ok: true });
} catch (err) {
sendApiError(res, 500, 'CODEX_INSTALL_FAILED', err instanceof Error ? err.message : String(err));
}
});
app.delete('/api/mcp/install/codex', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
try {
await uninstallCodexMcp(CODEX_MCP_NAME);
res.json({ ok: true });
} catch (err) {
sendApiError(res, 500, 'CODEX_UNINSTALL_FAILED', err instanceof Error ? err.message : String(err));
}
});
// External MCP server configuration. Open Design connects to these as a
// CLIENT and surfaces their tools to the underlying agent at spawn time.
// GET returns user-saved entries plus the built-in template list so the UI
// can render the "Add MCP server" picker without a second round-trip.
app.get('/api/mcp/servers', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
try {
const cfg = await readMcpConfig(RUNTIME_DATA_DIR);
res.json({ servers: cfg.servers, templates: MCP_TEMPLATES });
} catch (err: any) {
res
.status(500)
.json({ error: String(err && err.message ? err.message : err) });
}
});
app.put('/api/mcp/servers', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
try {
const cfg = await writeMcpConfig(RUNTIME_DATA_DIR, req.body);
res.json({ servers: cfg.servers, templates: MCP_TEMPLATES });
} catch (err: any) {
res
.status(400)
.json({ error: String(err && err.message ? err.message : err) });
}
});
// ─────────────────────────────────────────────────────────────────
// External MCP server OAuth — daemon-owned authorization flow.
//
// Replaces per-spawn `mcp-remote` subprocesses. The token is stored
// server-side in <dataDir>/mcp-tokens.json and injected as a Bearer
// header into the `.mcp.json` we write for Claude Code at spawn time.
// The redirect URI points at THIS daemon's public origin so the flow
// works the same in local dev (loopback) and in cloud deployments
// where OD_PUBLIC_BASE_URL pins the externally-routable URL.
// ─────────────────────────────────────────────────────────────────
app.post('/api/mcp/oauth/start', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
const serverId =
typeof req.body?.serverId === 'string' ? req.body.serverId.trim() : '';
if (!serverId) {
return res.status(400).json({ error: 'serverId is required' });
}
try {
const cfg = await readMcpConfig(RUNTIME_DATA_DIR);
const server = cfg.servers.find((s) => s.id === serverId);
if (!server) {
return res.status(404).json({ error: `unknown serverId ${serverId}` });
}
if (server.transport !== 'http' && server.transport !== 'sse') {
return res
.status(400)
.json({ error: 'OAuth flow only applies to http/sse transports' });
}
if (!server.url) {
return res.status(400).json({ error: 'server has no URL configured' });
}
if (server.authMode === 'none') {
return res
.status(400)
.json({ error: 'server is configured for no managed OAuth' });
}
const redirectUri = mcpOAuthCallbackUrl(req);
console.log(
`[mcp-oauth] start serverId=${serverId} url=${server.url} redirect=${redirectUri}`,
);
const result = await beginAuth({
serverId,
serverUrl: server.url,
redirectUri,
dataDir: RUNTIME_DATA_DIR,
fetchImpl: fetch,
});
pendingAuth.put(result.state, result.pending);
console.log(
`[mcp-oauth] start ok serverId=${serverId} authServer=${result.pending.authServerIssuer} clientId=${result.pending.clientId}`,
);
res.json({
authorizeUrl: result.authorizeUrl,
state: result.state,
redirectUri,
});
} catch (err: any) {
const msg = err && err.message ? err.message : String(err);
console.error(`[mcp-oauth] start failed serverId=${serverId}:`, msg);
res.status(502).json({ error: msg });
}
});
// Public endpoint — the OAuth provider's user-agent redirect lands here
// after the user approves. We deliberately do NOT enforce
// isLocalSameOrigin: in cloud the daemon IS the public origin, and even
// locally the request comes back from the OAuth provider's redirect
// (no Origin header at all on a top-level navigation).
app.get('/api/mcp/oauth/callback', async (req, res) => {
const code = typeof req.query.code === 'string' ? req.query.code : '';
const state = typeof req.query.state === 'string' ? req.query.state : '';
const error = typeof req.query.error === 'string' ? req.query.error : '';
if (error) {
return res.status(400).type('html').send(renderOAuthResultPage({
ok: false,
message: `Auth provider returned error: ${error}`,
}));
}
if (!code || !state) {
return res.status(400).type('html').send(renderOAuthResultPage({
ok: false,
message: 'Missing code or state — open Settings → External MCP servers and click Connect again.',
}));
}
const pending = pendingAuth.consume(state);
if (!pending) {
return res.status(400).type('html').send(renderOAuthResultPage({
ok: false,
message: 'Auth state expired or already used. Click Connect again.',
}));
}
try {
const tokenResp = await exchangeCodeForToken({
tokenEndpoint: pending.tokenEndpoint,
clientId: pending.clientId,
clientSecret: pending.clientSecret,
redirectUri: pending.redirectUri,
code,
codeVerifier: pending.codeVerifier,
resource: pending.resourceUrl,
});
const stored: any = {
accessToken: tokenResp.access_token,
refreshToken: tokenResp.refresh_token,
tokenType: tokenResp.token_type ?? 'Bearer',
scope: tokenResp.scope ?? pending.scope,
expiresAt:
typeof tokenResp.expires_in === 'number'
? Date.now() + tokenResp.expires_in * 1000
: undefined,
savedAt: Date.now(),
// Persist the OAuth client context so refresh-token rotation can
// hit the same client_id / token endpoint the upstream issued the
// refresh_token to. Refresh tokens are client-bound (RFC 6749 §6).
tokenEndpoint: pending.tokenEndpoint,
clientId: pending.clientId,
clientSecret: pending.clientSecret,
authServerIssuer: pending.authServerIssuer,
redirectUri: pending.redirectUri,
resourceUrl: pending.resourceUrl,
};
await setToken(RUNTIME_DATA_DIR, pending.serverId, stored);
res.type('html').send(renderOAuthResultPage({
ok: true,
serverId: pending.serverId,
}));
} catch (err: any) {
console.error(
'[mcp-oauth] callback failed:',
err && err.message ? err.message : err,
);
res.status(502).type('html').send(renderOAuthResultPage({
ok: false,
message: String(err && err.message ? err.message : err),
}));
}
});
app.get('/api/mcp/oauth/status', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
const serverId =
typeof req.query.serverId === 'string' ? req.query.serverId.trim() : '';
if (!serverId) return res.status(400).json({ error: 'serverId is required' });
try {
const tok = await getToken(RUNTIME_DATA_DIR, serverId);
if (!tok) return res.json({ connected: false });
res.json({
connected: true,
expiresAt: tok.expiresAt ?? null,
scope: tok.scope ?? null,
savedAt: tok.savedAt,
});
} catch (err: any) {
res.status(500).json({ error: String(err && err.message ? err.message : err) });
}
});
app.post('/api/mcp/oauth/disconnect', async (req, res) => {
if (!isLocalSameOrigin(req, getResolvedPort())) {
return res.status(403).json({ error: 'cross-origin request rejected' });
}
const serverId =
typeof req.body?.serverId === 'string' ? req.body.serverId.trim() : '';
if (!serverId) return res.status(400).json({ error: 'serverId is required' });
try {
await clearToken(RUNTIME_DATA_DIR, serverId);
res.json({ ok: true });
} catch (err: any) {
res.status(500).json({ error: String(err && err.message ? err.message : err) });
}
});
}
function getPublicBaseUrl(req: any) {
const env = process.env.OD_PUBLIC_BASE_URL;
if (env && /^https?:\/\//i.test(env)) {
return env.replace(/\/+$/u, '');
}
const proto = req.protocol || 'http';
const host = req.get('host');
if (!host) return `http://localhost:${process.env.OD_PORT ?? '7456'}`;
return `${proto}://${host}`;
}
function mcpOAuthCallbackUrl(req: any) {
return `${getPublicBaseUrl(req)}/api/mcp/oauth/callback`;
}
function renderOAuthResultPage(opts: any) {
const ok = Boolean(opts.ok);
const title = ok ? 'Connected' : 'Authorization failed';
const heading = ok ? '✅ Connected' : '⚠️ Authorization failed';
const body = ok
? `Your MCP server <code>${escapeHtml(opts.serverId ?? '')}</code> is now connected. You can close this tab and return to Open Design.`
: escapeHtml(opts.message ?? 'Authorization could not be completed.');
const accent = ok ? '#1a7f37' : '#cf222e';
const payload = ok
? { type: 'mcp-oauth', ok: true, serverId: opts.serverId ?? null }
: { type: 'mcp-oauth', ok: false, message: opts.message ?? null };
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>${escapeHtml(title)} — Open Design</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
:root { color-scheme: light dark; }
html, body { height: 100%; margin: 0; }
body {
display: flex; align-items: center; justify-content: center;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, sans-serif;
background: #f6f7f9; color: #1f2328; padding: 24px;
}
@media (prefers-color-scheme: dark) {
body { background: #0d1117; color: #e6edf3; }
.card { background: #161b22; border-color: #30363d; }
code { background: #1f242c; }
}
.card {
max-width: 420px; width: 100%; padding: 28px 28px 22px; border-radius: 12px;
background: white; border: 1px solid #d0d7de; box-shadow: 0 8px 24px rgba(0,0,0,.06);
text-align: left;
}
h1 { margin: 0 0 8px; font-size: 18px; color: ${accent}; }
p { margin: 0 0 16px; font-size: 14px; line-height: 1.55; }
code { background: #f3f4f6; padding: 1px 6px; border-radius: 4px; font-size: 12.5px; }
button {
appearance: none; border: 1px solid #d0d7de; background: white;
border-radius: 8px; padding: 8px 14px; font-size: 13px; cursor: pointer;
}
button:hover { background: #f6f8fa; }
@media (prefers-color-scheme: dark) {
button { background: #21262d; border-color: #30363d; color: #e6edf3; }
button:hover { background: #30363d; }
}
</style>
</head>
<body>
<div class="card">
<h1>${escapeHtml(heading)}</h1>
<p>${body}</p>
<button type="button" onclick="window.close()">Close this tab</button>
</div>
<script>
try {
var payload = ${JSON.stringify(payload)};
if (window.opener && !window.opener.closed) {
window.opener.postMessage(payload, '*');
}
if (window.BroadcastChannel) {
var bc = new BroadcastChannel('open-design-mcp-oauth');
bc.postMessage(payload);
bc.close();
}
} catch (e) { /* ignore postMessage failures */ }
</script>
</body>
</html>`;
}
function escapeHtml(s: any) {
return String(s ?? '')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}