clay-server 5.2.0-beta.2 → 5.2.0-beta.4
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.
- package/lib/builtin-mates.js +2 -2
- package/lib/bundled-skills/clay-agent-browser/SKILL.md +6 -2
- package/lib/dm-digest-cursor.js +120 -0
- package/lib/knowledge-db-import-validation.js +46 -0
- package/lib/knowledge-db-import.js +98 -0
- package/lib/knowledge-db-service.js +18 -30
- package/lib/knowledge-db-storage-nedb-maintenance.js +99 -0
- package/lib/knowledge-db-storage-nedb.js +66 -0
- package/lib/knowledge-db-storage.js +72 -0
- package/lib/knowledge-db-validation.js +0 -0
- package/lib/knowledge-file-store.js +7 -1
- package/lib/mate-avatar-catalog.js +12 -0
- package/lib/mate-avatar-migration.js +41 -0
- package/lib/mate-knowledge-mate-bound.js +53 -0
- package/lib/mate-knowledge-mcp-server.js +55 -8
- package/lib/mate-knowledge-service.js +9 -30
- package/lib/mate-knowledge-text-tools.js +106 -0
- package/lib/mates.js +14 -3
- package/lib/mcp-agent-tools.js +117 -0
- package/lib/mcp-connection-store.js +119 -15
- package/lib/mcp-import-credentials.js +31 -0
- package/lib/mcp-local-transport.js +13 -0
- package/lib/mcp-oauth.js +8 -6
- package/lib/mcp-query-bridge.js +30 -0
- package/lib/mcp-remote-connections.js +74 -20
- package/lib/mcp-source-adapters.js +91 -0
- package/lib/mcp-source-context.js +42 -0
- package/lib/mcp-source-discovery.js +169 -0
- package/lib/mcp-source-plugins.js +68 -0
- package/lib/mcp-source-restrictions.js +47 -0
- package/lib/mcp-tool-policy.js +34 -0
- package/lib/project-ask-user.js +6 -4
- package/lib/project-debate.js +1 -0
- package/lib/project-mate-interaction.js +40 -62
- package/lib/project-mcp.js +54 -16
- package/lib/project-session-document.js +8 -3
- package/lib/project-session-github.js +16 -7
- package/lib/project-session-linear.js +14 -6
- package/lib/project-session-notes.js +7 -3
- package/lib/project-sessions.js +6 -2
- package/lib/project-shared-browser.js +56 -12
- package/lib/project.js +49 -34
- package/lib/public/app.js +5 -0
- package/lib/public/appearance-bootstrap.js +14 -0
- package/lib/public/css/clay-workspace.css +29 -0
- package/lib/public/css/home-debate-planning.css +2 -0
- package/lib/public/css/mate-avatar-gallery.css +12 -0
- package/lib/public/css/mate-conversation.css +10 -5
- package/lib/public/css/session-github.css +43 -1
- package/lib/public/css/shared-browser.css +35 -1
- package/lib/public/index.html +10 -4
- package/lib/public/mate-avatars/B01.png +0 -0
- package/lib/public/mate-avatars/B02.png +0 -0
- package/lib/public/mate-avatars/B03.png +0 -0
- package/lib/public/mate-avatars/B04.png +0 -0
- package/lib/public/mate-avatars/B05.png +0 -0
- package/lib/public/mate-avatars/B06.png +0 -0
- package/lib/public/mate-avatars/B07.png +0 -0
- package/lib/public/mate-avatars/B08.png +0 -0
- package/lib/public/mate-avatars/B09.png +0 -0
- package/lib/public/mate-avatars/B10.png +0 -0
- package/lib/public/mate-avatars/B11.png +0 -0
- package/lib/public/mate-avatars/B12.png +0 -0
- package/lib/public/mate-avatars/B13.png +0 -0
- package/lib/public/mate-avatars/B14.png +0 -0
- package/lib/public/mate-avatars/B15.png +0 -0
- package/lib/public/mate-avatars/B16.png +0 -0
- package/lib/public/mate-avatars/B17.png +0 -0
- package/lib/public/mate-avatars/B18.png +0 -0
- package/lib/public/mate-avatars/B19.png +0 -0
- package/lib/public/mate-avatars/B20.png +0 -0
- package/lib/public/mate-avatars/B21.png +0 -0
- package/lib/public/mate-avatars/B22.png +0 -0
- package/lib/public/mate-avatars/B23.png +0 -0
- package/lib/public/mate-avatars/B24.png +0 -0
- package/lib/public/mate-avatars/B25.png +0 -0
- package/lib/public/mate-avatars/B26.png +0 -0
- package/lib/public/mate-avatars/B27.png +0 -0
- package/lib/public/mate-avatars/B28.png +0 -0
- package/lib/public/mate-avatars/B29.png +0 -0
- package/lib/public/mate-avatars/B30.png +0 -0
- package/lib/public/mate-avatars/B31.png +0 -0
- package/lib/public/mate-avatars/B32.png +0 -0
- package/lib/public/mate-avatars/B33.png +0 -0
- package/lib/public/mate-avatars/B34.png +0 -0
- package/lib/public/mate-avatars/B35.png +0 -0
- package/lib/public/mate-avatars/B36.png +0 -0
- package/lib/public/mate-avatars/B37.png +0 -0
- package/lib/public/mate-avatars/B38.png +0 -0
- package/lib/public/mate-avatars/B39.png +0 -0
- package/lib/public/mate-avatars/B40.png +0 -0
- package/lib/public/mate-avatars/B41.png +0 -0
- package/lib/public/mate-avatars/B42.png +0 -0
- package/lib/public/mate-avatars/B43.png +0 -0
- package/lib/public/mate-avatars/B44.png +0 -0
- package/lib/public/mate-avatars/B45.png +0 -0
- package/lib/public/mate-avatars/B46.png +0 -0
- package/lib/public/mate-avatars/B47.png +0 -0
- package/lib/public/mate-avatars/B48.png +0 -0
- package/lib/public/mate-avatars/C01.png +0 -0
- package/lib/public/mate-avatars/C02.png +0 -0
- package/lib/public/mate-avatars/C03.png +0 -0
- package/lib/public/mate-avatars/C04.png +0 -0
- package/lib/public/mate-avatars/C05.png +0 -0
- package/lib/public/mate-avatars/C06.png +0 -0
- package/lib/public/mate-avatars/C07.png +0 -0
- package/lib/public/mate-avatars/C08.png +0 -0
- package/lib/public/mate-avatars/C09.png +0 -0
- package/lib/public/mate-avatars/C10.png +0 -0
- package/lib/public/mate-avatars/C11.png +0 -0
- package/lib/public/mate-avatars/C12.png +0 -0
- package/lib/public/mate-avatars/C13.png +0 -0
- package/lib/public/mate-avatars/C14.png +0 -0
- package/lib/public/mate-avatars/C15.png +0 -0
- package/lib/public/mate-avatars/C16.png +0 -0
- package/lib/public/mate-avatars/C17.png +0 -0
- package/lib/public/mate-avatars/C18.png +0 -0
- package/lib/public/mate-avatars/C19.png +0 -0
- package/lib/public/mate-avatars/C20.png +0 -0
- package/lib/public/mate-avatars/C21.png +0 -0
- package/lib/public/mate-avatars/C22.png +0 -0
- package/lib/public/mate-avatars/C23.png +0 -0
- package/lib/public/mate-avatars/C24.png +0 -0
- package/lib/public/mate-avatars/C25.png +0 -0
- package/lib/public/mate-avatars/C26.png +0 -0
- package/lib/public/mate-avatars/C27.png +0 -0
- package/lib/public/mate-avatars/C28.png +0 -0
- package/lib/public/mate-avatars/C29.png +0 -0
- package/lib/public/mate-avatars/C30.png +0 -0
- package/lib/public/mate-avatars/C31.png +0 -0
- package/lib/public/mate-avatars/C32.png +0 -0
- package/lib/public/mate-avatars/C33.png +0 -0
- package/lib/public/mate-avatars/C34.png +0 -0
- package/lib/public/mate-avatars/C35.png +0 -0
- package/lib/public/mate-avatars/C36.png +0 -0
- package/lib/public/mate-avatars/C37.png +0 -0
- package/lib/public/mate-avatars/C38.png +0 -0
- package/lib/public/mate-avatars/C39.png +0 -0
- package/lib/public/mate-avatars/C40.png +0 -0
- package/lib/public/mate-avatars/C41.png +0 -0
- package/lib/public/mate-avatars/C42.png +0 -0
- package/lib/public/mate-avatars/C43.png +0 -0
- package/lib/public/mate-avatars/C44.png +0 -0
- package/lib/public/mate-avatars/C45.png +0 -0
- package/lib/public/mate-avatars/C46.png +0 -0
- package/lib/public/mate-avatars/C47.png +0 -0
- package/lib/public/mate-avatars/C48.png +0 -0
- package/lib/public/mate-avatars/catalog.json +482 -0
- package/lib/public/mate-avatars/lead-clay.png +0 -0
- package/lib/public/modules/app-message-router.js +4 -0
- package/lib/public/modules/app-messages.js +13 -2
- package/lib/public/modules/app-rendering.js +1 -1
- package/lib/public/modules/avatar.js +15 -41
- package/lib/public/modules/chat-render-runtime.js +10 -1
- package/lib/public/modules/clay-browser-links.js +47 -0
- package/lib/public/modules/clay-log-links.js +1 -1
- package/lib/public/modules/command-palette.js +1 -1
- package/lib/public/modules/debate.js +1 -0
- package/lib/public/modules/history-replay-batch.js +5 -1
- package/lib/public/modules/home-debate-live.js +2 -0
- package/lib/public/modules/home-debate-planning.js +7 -7
- package/lib/public/modules/home-mate-avatar-editor.js +127 -208
- package/lib/public/modules/home-mate-chat.js +6 -6
- package/lib/public/modules/home-mate-settings.js +19 -12
- package/lib/public/modules/markdown.js +4 -0
- package/lib/public/modules/mate-avatar-catalog.js +97 -0
- package/lib/public/modules/mate-management.js +40 -7
- package/lib/public/modules/mcp-connection-view.js +31 -13
- package/lib/public/modules/mcp-discovery-view.js +31 -0
- package/lib/public/modules/mcp-ui.js +23 -4
- package/lib/public/modules/mention.js +3 -0
- package/lib/public/modules/pane-browser-bridge.js +20 -0
- package/lib/public/modules/profile.js +1 -238
- package/lib/public/modules/project-mate-navigation.js +99 -14
- package/lib/public/modules/search-clay-chat.js +16 -2
- package/lib/public/modules/session-github.js +23 -9
- package/lib/public/modules/session-work-primary.js +51 -0
- package/lib/public/modules/shared-browser-settings.js +112 -0
- package/lib/public/modules/shared-browser-tabs.js +13 -3
- package/lib/public/modules/shared-browser.js +27 -6
- package/lib/public/modules/split-view.js +2 -0
- package/lib/public/modules/theme.js +8 -1
- package/lib/public/modules/tools.js +2 -1
- package/lib/public/style.css +1 -0
- package/lib/public/sw.js +3 -1
- package/lib/sdk-bridge.js +9 -3
- package/lib/server-appearance-settings.js +13 -1
- package/lib/server-dm.js +1 -0
- package/lib/server-home-chat-events.js +14 -4
- package/lib/server-home-chat.js +10 -2
- package/lib/server-home-mcp-consent.js +14 -0
- package/lib/server-mates.js +6 -1
- package/lib/server-settings.js +4 -1
- package/lib/server.js +45 -12
- package/lib/session-work-primary.js +50 -0
- package/lib/sessions.js +3 -0
- package/lib/shared-browser-cookie-sync.js +32 -0
- package/lib/shared-browser-cookies.js +79 -0
- package/lib/shared-browser-runtime.js +25 -9
- package/lib/shared-browser-storage-sync.js +43 -0
- package/lib/shared-browser-storage.js +101 -0
- package/lib/shared-browser-tools.js +2 -1
- package/lib/shared-browser-worker.js +46 -10
- package/lib/yoke/acp-agent-profiles.js +2 -2
- package/package.json +5 -1
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Agent import consent is server-owned, owner-only, exact-query and single-use.
|
|
2
|
+
var crypto = require('crypto');
|
|
3
|
+
var userInput = require('./yoke/user-input');
|
|
4
|
+
var createProxy = require('./mcp-proxy').createProxy;
|
|
5
|
+
var SERVER = 'clay-mcp-connections';
|
|
6
|
+
function result(value, error) { return { content: [{ type: 'text', text: JSON.stringify(value) }], isError: !!error }; }
|
|
7
|
+
function attachMcpAgentTools(ctx) {
|
|
8
|
+
var viewers = new Map(), pending = new Map(), used = new WeakMap(), closed = false;
|
|
9
|
+
function ownerOf(session) { return ctx.isMultiUser() ? String(session.ownerId) : 'default'; }
|
|
10
|
+
function authorized(session, owner, generation) {
|
|
11
|
+
if (closed || !session || session.youCurator || session._mcpForeignInput || session.sessionVisibility === 'shared' || !ctx.canUseSession(session) || ownerOf(session) !== owner || Number(session._sdkQueryGeneration || 0) !== generation || session.taskStopRequested || session.abortController && session.abortController.signal.aborted) throw new Error('MCP management requires the current authorized private query.');
|
|
12
|
+
ctx.authorizeOwner(owner);
|
|
13
|
+
}
|
|
14
|
+
function canRespond(ws, session, owner) {
|
|
15
|
+
try {
|
|
16
|
+
if (!ws || ws.readyState !== 1 || ctx.ownerFor(ws) !== owner) return false;
|
|
17
|
+
return ctx.clients.has(ws) && ws._clayActiveSession === session.localId || viewers.has(ws) && viewers.get(ws).has(session);
|
|
18
|
+
} catch (error) { return false; }
|
|
19
|
+
}
|
|
20
|
+
function humans(session, owner) {
|
|
21
|
+
return Array.from(ctx.clients).concat(Array.from(viewers.keys())).some(function (ws) { return canRespond(ws, session, owner); });
|
|
22
|
+
}
|
|
23
|
+
function registerViewer(ws, session) {
|
|
24
|
+
var owner = ownerOf(session);
|
|
25
|
+
if (closed || !session || !ctx.canUseSession(session) || session._mcpForeignInput || session.sessionVisibility === 'shared') throw new Error('Consent viewer requires an authorized private session.');
|
|
26
|
+
ctx.authorizeOwner(owner);
|
|
27
|
+
if (ctx.ownerFor(ws) !== owner || ws.readyState !== 1) throw new Error('Consent viewer must be the authenticated owner.');
|
|
28
|
+
var sessions = viewers.get(ws) || new Map(), detached = false;
|
|
29
|
+
sessions.set(session, (sessions.get(session) || 0) + 1); viewers.set(ws, sessions);
|
|
30
|
+
return function () {
|
|
31
|
+
if (detached) return; detached = true;
|
|
32
|
+
var count = sessions.get(session) || 0;
|
|
33
|
+
if (count <= 1) sessions.delete(session); else sessions.set(session, count - 1);
|
|
34
|
+
if (!sessions.size && viewers.get(ws) === sessions) viewers.delete(ws);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function review(candidate, owner, config) {
|
|
38
|
+
var restrictions = config.enabledTools === null ? 'Tool allowlist: unrestricted' : 'Tool allowlist: ' + JSON.stringify(config.enabledTools || []);
|
|
39
|
+
restrictions += '\nExcluded tools: ' + JSON.stringify(config.disabledTools || []);
|
|
40
|
+
if (config.toolPatterns && config.toolPatterns.length) restrictions += '\nExcluded vendor patterns: ' + JSON.stringify(config.toolPatterns);
|
|
41
|
+
var question = 'Import "' + candidate.name + '" from ' + candidate.vendor + ' (' + candidate.scope + ') ?\nSource: ' + candidate.file + '\nTransport: ' + candidate.transport + '\nDependency: ' + candidate.dependency +
|
|
42
|
+
'\nDestination: owner ' + owner + ' — private MCP catalog shared across your Clay sessions, project sessions and Mates; other accounts cannot use it.\n' + restrictions + '\nRequired credential names: ' + (candidate.references.join(', ') || 'None') +
|
|
43
|
+
'\nThe imported copy will be DISABLED. This approval only saves that exact copy. Enable, Test and Sign in require separate actions. Original vendor files stay unchanged. Enter credentials only in MCP management, never in chat.';
|
|
44
|
+
if (question.length > 2000) throw new Error('This candidate needs a larger policy review than the consent card supports. Review it in MCP management; no import was made.');
|
|
45
|
+
return question;
|
|
46
|
+
}
|
|
47
|
+
function definitions(session) {
|
|
48
|
+
var owner = ownerOf(session), generation = Number(session._sdkQueryGeneration || 0);
|
|
49
|
+
function authorize() { authorized(session, owner, generation); }
|
|
50
|
+
async function discover(args) {
|
|
51
|
+
authorize();
|
|
52
|
+
if (Object.keys(args).some(function (key) { return key !== 'vendor' && key !== 'file'; })) throw new Error('Unsupported discovery input.');
|
|
53
|
+
if (!!args.file !== !!args.vendor) throw new Error('Configured discovery requires both vendor and absolute file path.');
|
|
54
|
+
var scan = ctx.discovery.scan(owner, session, args.file ? { vendor: args.vendor, file: args.file } : null);
|
|
55
|
+
authorize(); return result(Object.assign({ status: 'discovered', imported: false }, scan));
|
|
56
|
+
}
|
|
57
|
+
async function propose(args) {
|
|
58
|
+
authorize();
|
|
59
|
+
if (Object.keys(args).some(function (key) { return key !== 'scanId' && key !== 'candidateId'; })) throw new Error('Import approval cannot be supplied by an agent.');
|
|
60
|
+
var selected = ctx.discovery.selected(owner, session, args);
|
|
61
|
+
var key = JSON.stringify([owner, session.localId, generation, args.scanId, args.candidateId]);
|
|
62
|
+
var attempts = used.get(session);
|
|
63
|
+
if (!attempts || attempts.scanId !== args.scanId || attempts.generation !== generation) { attempts = { scanId: args.scanId, generation: generation, candidates: new Set() }; used.set(session, attempts); }
|
|
64
|
+
if (attempts.candidates.has(args.candidateId) || pending.has(key)) throw new Error('This import proposal has already been requested. Scan again for a new review.');
|
|
65
|
+
var candidate = ctx.discovery.candidate(owner, session, args);
|
|
66
|
+
if (!humans(session, owner) || session.loop && session.loop.active && session.loop.role !== 'crafting') return result({ status: 'consent_unavailable', imported: false, message: 'An authenticated owner must open this conversation to approve an import. Scheduled or unattended runs cannot approve for the user.' });
|
|
67
|
+
var question = review(candidate, owner, selected.config), requestId = 'mcp_import_' + crypto.randomUUID();
|
|
68
|
+
var controller = new AbortController(), timer, timedOut = false, sessionSignal = session.abortController && session.abortController.signal;
|
|
69
|
+
var entry = { controller: controller, requestId: requestId };
|
|
70
|
+
pending.set(key, entry); attempts.candidates.add(args.candidateId);
|
|
71
|
+
var parentAbort = function () { controller.abort(); };
|
|
72
|
+
if (sessionSignal) sessionSignal.addEventListener('abort', parentAbort, { once: true });
|
|
73
|
+
try {
|
|
74
|
+
timer = setTimeout(function () { timedOut = true; controller.abort(); }, Math.min(ctx.consentTimeout || 45000, 45000));
|
|
75
|
+
var handler = ctx.userInput && ctx.userInput(session);
|
|
76
|
+
var answer = await userInput.dispatchUserInput(function (request, respond) {
|
|
77
|
+
request.authorizeResponse = function (ws) {
|
|
78
|
+
try { authorize(); return pending.get(key) === entry && canRespond(ws, session, owner); } catch (error) { return false; }
|
|
79
|
+
};
|
|
80
|
+
if (typeof handler !== 'function') return respond.cancel('Structured owner consent is unavailable.');
|
|
81
|
+
return handler(request, respond);
|
|
82
|
+
}, { questions: [{ id: 'mcp_import', header: 'Import MCP', question: question, allowOther: false, options: [{ label: 'Keep unchanged' }, { label: 'Import disabled copy' }] }] }, { source: 'mcp-import', requestId: requestId, signal: controller.signal });
|
|
83
|
+
if (answer.status !== 'submitted' || !answer.answers.mcp_import || answer.answers.mcp_import.length !== 1 || answer.answers.mcp_import[0] !== 'Import disabled copy') return result({ status: timedOut ? 'expired' : answer.status === 'submitted' ? 'declined' : answer.status, imported: false, message: 'No connection was imported.' });
|
|
84
|
+
authorize();
|
|
85
|
+
if (pending.get(key) !== entry || !humans(session, owner)) throw new Error('Import consent is no longer available.');
|
|
86
|
+
// Consume before entering the queue. Queued mutation revalidates everything again.
|
|
87
|
+
pending.delete(key);
|
|
88
|
+
function authorizeImport() { authorize(); if (controller.signal.aborted) throw new Error('Import consent expired before the copy could be saved. Scan again.'); }
|
|
89
|
+
var id = await ctx.importConnection(owner, function () {
|
|
90
|
+
authorizeImport();
|
|
91
|
+
if (!humans(session, owner)) throw new Error('The owner disconnected before import.');
|
|
92
|
+
var live = ctx.discovery.selected(owner, session, args);
|
|
93
|
+
if (live.signature !== selected.signature || live.revision !== selected.revision) throw new Error('The reviewed candidate changed. Scan again.');
|
|
94
|
+
return live;
|
|
95
|
+
}, authorizeImport);
|
|
96
|
+
return result({ status: 'imported', imported: true, id: id, enabled: false, label: candidate.name, message: 'Saved a disabled copy in your private catalog. Configure credentials in MCP management; activation, testing and login are separate actions.' });
|
|
97
|
+
} finally {
|
|
98
|
+
clearTimeout(timer); pending.delete(key);
|
|
99
|
+
if (sessionSignal) sessionSignal.removeEventListener('abort', parentAbort);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
function wrap(handler) { return function (args) { return Promise.resolve().then(function () { return handler(args || {}); }).catch(function (error) { return result({ status: 'refused', imported: false, message: error.message }, true); }); }; }
|
|
103
|
+
return [{ name: 'discover_mcp_connections', description: 'Read-only inspection of your vendor MCP sources. Returns redacted candidate IDs and provenance. Never executes commands, imports, activates, logs in or reads vendor OAuth stores. Use credential entry only in MCP management.', inputSchema: { type: 'object', properties: { vendor: { type: 'string' }, file: { type: 'string' } }, additionalProperties: false }, handler: wrap(discover) },
|
|
104
|
+
{ name: 'propose_mcp_import', description: 'Present one exact discovered candidate to the authenticated owner for structured approval, then save a disabled copy only after explicit approval. Denial, cancellation, timeout or absent owner causes no import. Pass only scanId and candidateId; agent-provided approval is never accepted.', inputSchema: { type: 'object', properties: { scanId: { type: 'string' }, candidateId: { type: 'string' } }, required: ['scanId', 'candidateId'], additionalProperties: false }, handler: wrap(propose) }];
|
|
105
|
+
}
|
|
106
|
+
function getServer(session) {
|
|
107
|
+
try {
|
|
108
|
+
authorized(session, ownerOf(session), Number(session._sdkQueryGeneration || 0));
|
|
109
|
+
if (ctx.available && !ctx.available(session)) return null;
|
|
110
|
+
var tools = definitions(session);
|
|
111
|
+
return createProxy(SERVER, tools, function (name, args) { var tool = tools.find(function (item) { return item.name === name; }); return tool.handler(args); });
|
|
112
|
+
} catch (error) { return null; }
|
|
113
|
+
}
|
|
114
|
+
function destroy() { closed = true; pending.forEach(function (entry) { entry.controller.abort(); }); pending.clear(); used = new WeakMap(); viewers.clear(); }
|
|
115
|
+
return { getServer: getServer, registerViewer: registerViewer, destroy: destroy };
|
|
116
|
+
}
|
|
117
|
+
module.exports = { attachMcpAgentTools: attachMcpAgentTools, SERVER: SERVER };
|
|
@@ -1,39 +1,143 @@
|
|
|
1
|
-
// Owner
|
|
1
|
+
// Owner-scoped remote records and project-scoped computer preferences with encrypted credentials; never sent to the UI.
|
|
2
2
|
var fs = require('fs');
|
|
3
3
|
var path = require('path');
|
|
4
4
|
var crypto = require('crypto');
|
|
5
|
-
|
|
5
|
+
var equal = require('util').isDeepStrictEqual;
|
|
6
|
+
|
|
7
|
+
function createConnectionStore(root, project, options) {
|
|
6
8
|
var directory = path.join(root, 'mcp-connections');
|
|
9
|
+
var shared = !!(options && options.shared);
|
|
7
10
|
function scope(owner) {
|
|
8
11
|
if (typeof owner !== 'string' || !owner || owner.length > 160) throw new Error('MCP account owner is required.');
|
|
9
|
-
return JSON.stringify([project, owner]);
|
|
12
|
+
return shared ? JSON.stringify([owner]) : JSON.stringify([project, owner]);
|
|
10
13
|
}
|
|
11
14
|
function filename(owner) { return path.join(directory, crypto.createHash('sha256').update(scope(owner)).digest('hex') + '.json'); }
|
|
15
|
+
function legacyFilename(owner) { return path.join(directory, crypto.createHash('sha256').update(JSON.stringify([project, owner])).digest('hex') + '.json'); }
|
|
12
16
|
function key() {
|
|
13
17
|
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
14
|
-
var file = path.join(directory, '.key');
|
|
15
|
-
try { fs.
|
|
16
|
-
catch (error) {
|
|
17
|
-
|
|
18
|
+
var file = path.join(directory, '.key'), bytes;
|
|
19
|
+
try { bytes = fs.readFileSync(file); }
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error.code !== 'ENOENT') throw error;
|
|
22
|
+
var temp = file + '.' + crypto.randomBytes(8).toString('hex') + '.tmp';
|
|
23
|
+
try {
|
|
24
|
+
fs.writeFileSync(temp, crypto.randomBytes(32), { flag: 'wx', mode: 0o600 });
|
|
25
|
+
try { fs.linkSync(temp, file); } catch (publishError) { if (publishError.code !== 'EEXIST') throw publishError; }
|
|
26
|
+
} finally { fs.unlinkSync(temp); }
|
|
27
|
+
bytes = fs.readFileSync(file);
|
|
28
|
+
}
|
|
29
|
+
if (bytes.length !== 32) throw new Error('MCP encryption key is invalid.');
|
|
30
|
+
return bytes;
|
|
18
31
|
}
|
|
19
|
-
function
|
|
32
|
+
function decode(file, aad) {
|
|
20
33
|
var data;
|
|
21
|
-
try { data = JSON.parse(fs.readFileSync(
|
|
22
|
-
catch (error) { if (error.code === 'ENOENT') return []; throw new Error('Could not read MCP connections.'); }
|
|
34
|
+
try { data = JSON.parse(fs.readFileSync(file, 'utf8')); }
|
|
35
|
+
catch (error) { if (error.code === 'ENOENT') return { records: [], revision: null, migratedProjects: [], retired: [] }; throw new Error('Could not read MCP connections.'); }
|
|
23
36
|
var parts = data.encrypted.split('.').map(function (part) { return Buffer.from(part, 'base64'); });
|
|
24
37
|
var cipher = crypto.createDecipheriv('aes-256-gcm', key(), parts[0]);
|
|
25
|
-
cipher.setAAD(Buffer.from(
|
|
26
|
-
|
|
38
|
+
cipher.setAAD(Buffer.from(aad)); cipher.setAuthTag(parts[1]);
|
|
39
|
+
var value = JSON.parse(Buffer.concat([cipher.update(parts[2]), cipher.final()]).toString('utf8'));
|
|
40
|
+
if (Array.isArray(value)) return { records: value, revision: null, migratedProjects: [], retired: [] };
|
|
41
|
+
return { records: Array.isArray(value.records) ? value.records : [], revision: value.revision || null, migratedProjects: Array.isArray(value.migratedProjects) ? value.migratedProjects : [], retired: value.retired || [] };
|
|
27
42
|
}
|
|
28
|
-
function
|
|
43
|
+
function encode(owner, state, expectedRevision) {
|
|
44
|
+
var current = decode(filename(owner), scope(owner));
|
|
45
|
+
if (expectedRevision !== undefined && current.revision !== expectedRevision) throw new Error('MCP connections changed. Please retry.');
|
|
46
|
+
var next = { records: state.records, revision: crypto.randomBytes(16).toString('hex'), migratedProjects: state.migratedProjects || [], retired: state.retired || [] };
|
|
29
47
|
var iv = crypto.randomBytes(12), cipher = crypto.createCipheriv('aes-256-gcm', key(), iv);
|
|
30
48
|
cipher.setAAD(Buffer.from(scope(owner)));
|
|
31
|
-
var bytes = Buffer.concat([cipher.update(JSON.stringify(
|
|
49
|
+
var bytes = Buffer.concat([cipher.update(JSON.stringify(next), 'utf8'), cipher.final()]);
|
|
32
50
|
var value = { encrypted: [iv, cipher.getAuthTag(), bytes].map(function (part) { return part.toString('base64'); }).join('.') };
|
|
33
51
|
var file = filename(owner), temp = file + '.' + crypto.randomBytes(8).toString('hex') + '.tmp';
|
|
34
52
|
try { fs.writeFileSync(temp, JSON.stringify(value), { mode: 0o600 }); fs.renameSync(temp, file); }
|
|
35
53
|
finally { try { fs.unlinkSync(temp); } catch (error) { if (error.code !== 'ENOENT') throw error; } }
|
|
54
|
+
return next.revision;
|
|
36
55
|
}
|
|
37
|
-
return {
|
|
56
|
+
function visible(records) { if (!shared) return records; return records.filter(function (record) { return record.source !== 'computer' || record.project === project; }); }
|
|
57
|
+
function connectionConflict(left, right) { return left.id && left.id === right.id || (left.url && right.url && left.url === right.url); }
|
|
58
|
+
function migrate(owner, state) {
|
|
59
|
+
if (!shared || state.migratedProjects.indexOf(project) >= 0) return state;
|
|
60
|
+
var legacy = decode(legacyFilename(owner), JSON.stringify([project, owner])).records;
|
|
61
|
+
var merged = state.records.slice();
|
|
62
|
+
legacy.forEach(function (record) {
|
|
63
|
+
var copy = Object.assign({}, record);
|
|
64
|
+
if (copy.source === 'computer') copy.project = project;
|
|
65
|
+
else if ((state.retired || []).some(function (removed) { return connectionConflict(removed, copy); })) return;
|
|
66
|
+
var existing = merged.filter(function (current) { return current.source === copy.source && (copy.source === 'computer' ? current.project === project && current.name === copy.name : connectionConflict(current, copy)); });
|
|
67
|
+
if (existing.length && existing.some(function (current) { return !equal(current, copy); })) throw new Error('MCP connection migration conflict. Resolve the legacy project connection before retrying.');
|
|
68
|
+
if (!existing.length) merged.push(copy);
|
|
69
|
+
});
|
|
70
|
+
state.records = merged;
|
|
71
|
+
state.migratedProjects = state.migratedProjects.concat([project]);
|
|
72
|
+
return state;
|
|
73
|
+
}
|
|
74
|
+
function readUnlocked(owner) {
|
|
75
|
+
var state = decode(filename(owner), scope(owner));
|
|
76
|
+
if (shared) {
|
|
77
|
+
var unique = [];
|
|
78
|
+
state.records.forEach(function (record) {
|
|
79
|
+
var existing = unique.find(function (current) {
|
|
80
|
+
return record.source === 'computer' ? current.source === 'computer' && record.project === current.project && record.name === current.name : current.source !== 'computer' && connectionConflict(record, current);
|
|
81
|
+
});
|
|
82
|
+
if (existing && !equal(existing, record)) throw new Error('Conflicting duplicate MCP records require resolution.');
|
|
83
|
+
if (!existing) unique.push(record);
|
|
84
|
+
});
|
|
85
|
+
if (unique.length !== state.records.length) { state.records = unique; state.revision = encode(owner, state, state.revision); }
|
|
86
|
+
}
|
|
87
|
+
if (shared && state.migratedProjects.indexOf(project) < 0) { state = migrate(owner, state); state.revision = encode(owner, state, state.revision); }
|
|
88
|
+
return state;
|
|
89
|
+
}
|
|
90
|
+
// Fail closed on contention. The lock covers migration and the full CAS read/write,
|
|
91
|
+
// including callers in a second process using the same storage root.
|
|
92
|
+
function locked(owner, operation) {
|
|
93
|
+
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
94
|
+
var file = filename(owner) + '.lock', fd;
|
|
95
|
+
try { fd = fs.openSync(file, 'wx', 0o600); }
|
|
96
|
+
catch (error) { if (error.code === 'EEXIST') throw new Error('MCP connections are busy. Please retry.'); throw error; }
|
|
97
|
+
try { fs.writeFileSync(fd, String(process.pid)); return operation(); }
|
|
98
|
+
finally { fs.closeSync(fd); fs.unlinkSync(file); }
|
|
99
|
+
}
|
|
100
|
+
function snapshot(owner) {
|
|
101
|
+
return locked(owner, function () {
|
|
102
|
+
var state = readUnlocked(owner);
|
|
103
|
+
return { records: visible(state.records), revision: state.revision };
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
function load(owner) { return snapshot(owner).records; }
|
|
107
|
+
function revision(owner) { return snapshot(owner).revision; }
|
|
108
|
+
function validate(records) {
|
|
109
|
+
if (!Array.isArray(records)) throw new Error('MCP connection snapshot must be an array.');
|
|
110
|
+
records.forEach(function (record, index) {
|
|
111
|
+
if (!record || typeof record !== 'object') throw new Error('Invalid MCP connection record.');
|
|
112
|
+
if (records.slice(0, index).some(function (previous) {
|
|
113
|
+
return record.id && previous.id === record.id ||
|
|
114
|
+
record.source === 'computer' && previous.source === 'computer' && record.name === previous.name ||
|
|
115
|
+
record.source !== 'computer' && previous.source !== 'computer' && record.url && record.url === previous.url;
|
|
116
|
+
})) throw new Error('Duplicate MCP connection in snapshot.');
|
|
117
|
+
if (shared && record.source === 'computer' && record.project && record.project !== project) throw new Error('Foreign project computer preference.');
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
// Remote rows are the complete owner snapshot. Computer rows replace only this
|
|
121
|
+
// project's preferences; omitted remote rows are deleted, never merged back.
|
|
122
|
+
function save(owner, records, expectedRevision) {
|
|
123
|
+
validate(records);
|
|
124
|
+
return locked(owner, function () {
|
|
125
|
+
var state = readUnlocked(owner);
|
|
126
|
+
if (expectedRevision !== undefined && state.revision !== expectedRevision) throw new Error('MCP connections changed. Please retry.');
|
|
127
|
+
if (!shared) return encode(owner, { records: records, migratedProjects: [] }, state.revision);
|
|
128
|
+
var incomingRemote = records.filter(function (record) { return record.source !== 'computer'; });
|
|
129
|
+
var currentComputer = records.filter(function (record) { return record.source === 'computer'; }).map(function (record) { return Object.assign({}, record, { project: project }); });
|
|
130
|
+
var otherComputer = state.records.filter(function (record) { return record.source === 'computer' && record.project !== project; });
|
|
131
|
+
var removed = state.records.filter(function (record) {
|
|
132
|
+
return record.source !== 'computer' && !incomingRemote.some(function (incoming) { return connectionConflict(record, incoming); });
|
|
133
|
+
}).map(function (record) { return { id: record.id, url: record.url }; });
|
|
134
|
+
state.retired = (state.retired || []).concat(removed).filter(function (record, index, all) {
|
|
135
|
+
return all.findIndex(function (other) { return other.id === record.id && other.url === record.url; }) === index;
|
|
136
|
+
});
|
|
137
|
+
state.records = incomingRemote.concat(otherComputer, currentComputer);
|
|
138
|
+
return encode(owner, state, state.revision);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return { load: load, save: save, revision: revision, snapshot: snapshot, namespace: fs.existsSync(root) ? fs.realpathSync(root) : path.resolve(root) };
|
|
38
142
|
}
|
|
39
143
|
module.exports = { createConnectionStore: createConnectionStore };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function requiredNames(row) {
|
|
2
|
+
var names = (row.environmentNames || []).filter(function (name) { return !Object.prototype.hasOwnProperty.call(row.env || {}, name); });
|
|
3
|
+
(row.references || []).forEach(function (reference) {
|
|
4
|
+
var match = /^(?:\$\{?|\{env:)([A-Za-z_][A-Za-z0-9_]*)/.exec(reference);
|
|
5
|
+
var name = match ? match[1] : /^[A-Za-z_][A-Za-z0-9_]*$/.test(reference) ? reference : 'Unsupported reference';
|
|
6
|
+
names.push(name);
|
|
7
|
+
});
|
|
8
|
+
return Array.from(new Set(names));
|
|
9
|
+
}
|
|
10
|
+
function applyCredentials(row, values) {
|
|
11
|
+
var names = requiredNames(row);
|
|
12
|
+
if (names.indexOf('Unsupported reference') >= 0) throw new Error('This source requires a vendor-specific helper or file reference. Configure a direct Clay connection instead.');
|
|
13
|
+
names.forEach(function (name) {
|
|
14
|
+
if (!Object.prototype.hasOwnProperty.call(values, name) || typeof values[name] !== 'string' || !values[name] || values[name].length > 8192 || /[\r\n\0]/.test(values[name])) throw new Error('Enter each required credential value.');
|
|
15
|
+
});
|
|
16
|
+
function expand(value) {
|
|
17
|
+
if (typeof value === 'string') return value.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-[^}]*)?\}|\$([A-Za-z_][A-Za-z0-9_]*)|\{env:([^}]+)\}/g, function (all, first, second, third) {
|
|
18
|
+
var name = first || second || third; return Object.prototype.hasOwnProperty.call(values, name) ? values[name] : all;
|
|
19
|
+
});
|
|
20
|
+
if (Array.isArray(value)) return value.map(expand);
|
|
21
|
+
if (value && typeof value === 'object') { var next = {}; Object.keys(value).forEach(function (key) { next[key] = expand(value[key]); }); return next; }
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
var next = {};
|
|
25
|
+
['command', 'args', 'env', 'headers', 'url'].forEach(function (field) { if (row[field] !== undefined) next[field] = expand(row[field]); });
|
|
26
|
+
next.headers = next.headers || {}; next.env = next.env || {};
|
|
27
|
+
Object.keys(row.envHeaders || {}).forEach(function (name) { next.headers[name] = (name === 'Authorization' && row.bearerTokenEnv === row.envHeaders[name] ? 'Bearer ' : '') + values[row.envHeaders[name]]; });
|
|
28
|
+
(row.environmentNames || []).forEach(function (name) { if (values[name] !== undefined) next.env[name] = values[name]; });
|
|
29
|
+
next.envHeaders = {}; next.references = []; return next;
|
|
30
|
+
}
|
|
31
|
+
module.exports = { requiredNames: requiredNames, applyCredentials: applyCredentials };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var StdioClientTransport = require('@modelcontextprotocol/sdk/client/stdio.js').StdioClientTransport;
|
|
2
|
+
var wrapSpawnAsUser = require('./os-users').wrapSpawnAsUser;
|
|
3
|
+
var os = require('os');
|
|
4
|
+
function localTransport(row, identity) {
|
|
5
|
+
if ((row.references || []).length || (row.environmentNames || []).some(function (name) { return !Object.prototype.hasOwnProperty.call(row.env || {}, name); })) throw new Error('Configure required environment values before connecting.');
|
|
6
|
+
var home = identity ? identity.home : os.homedir();
|
|
7
|
+
var env = Object.assign({ HOME: home, USER: identity ? identity.user : os.userInfo().username, LOGNAME: identity ? identity.user : os.userInfo().username, SHELL: identity ? identity.shell || '/bin/sh' : '/bin/sh', PATH: '/usr/local/bin:/usr/bin:/bin:/opt/homebrew/bin' }, row.env || {});
|
|
8
|
+
var wrapped = wrapSpawnAsUser(row.command, row.args, identity ? { uid: identity.uid, gid: identity.gid, env: env } : { env: env });
|
|
9
|
+
// The SDK cannot apply uid/gid itself; never fall back to spawning as the daemon.
|
|
10
|
+
if (wrapped.options.uid != null || wrapped.options.gid != null) throw new Error('Install util-linux setpriv to run MCP as the account OS user.');
|
|
11
|
+
return new StdioClientTransport({ command: wrapped.command, args: wrapped.args, env: env, cwd: row.cwd, stderr: 'ignore' });
|
|
12
|
+
}
|
|
13
|
+
module.exports = { localTransport: localTransport };
|
package/lib/mcp-oauth.js
CHANGED
|
@@ -2,21 +2,21 @@ var crypto = require('crypto');
|
|
|
2
2
|
var http = require('http');
|
|
3
3
|
var assertPublicUrl = require('./mcp-network').assertPublicUrl;
|
|
4
4
|
|
|
5
|
-
function createOAuthProvider(record, persist, flow) {
|
|
5
|
+
function createOAuthProvider(record, persist, flow, authorize) {
|
|
6
6
|
record.auth = record.auth || {};
|
|
7
7
|
var auth = record.auth;
|
|
8
|
-
function guard() { if (flow && flow.cancelled) throw new Error('Sign-in expired.'); }
|
|
8
|
+
function guard() { if (flow && flow.cancelled) throw new Error('Sign-in expired.'); if (authorize) authorize(); }
|
|
9
9
|
function save() { persist(); }
|
|
10
10
|
return {
|
|
11
11
|
get redirectUrl() { return flow ? flow.redirectUrl : auth.redirectUrl; },
|
|
12
12
|
get clientMetadata() { return { client_name: 'Clay', redirect_uris: [flow ? flow.redirectUrl : auth.redirectUrl],
|
|
13
13
|
grant_types: ['authorization_code', 'refresh_token'], response_types: ['code'], token_endpoint_auth_method: 'none' }; },
|
|
14
14
|
state: function () { return flow ? flow.state : crypto.randomBytes(32).toString('hex'); },
|
|
15
|
-
clientInformation: function () { return auth.client; },
|
|
15
|
+
clientInformation: function () { guard(); return auth.client; },
|
|
16
16
|
saveClientInformation: function (value) { guard(); auth.client = value; save(); },
|
|
17
|
-
tokens: function () { return auth.tokens; },
|
|
17
|
+
tokens: function () { guard(); return auth.tokens; },
|
|
18
18
|
saveTokens: function (value) { guard(); auth.tokens = value; save(); },
|
|
19
|
-
saveCodeVerifier: function (value) { if (flow) flow.verifier = value; },
|
|
19
|
+
saveCodeVerifier: function (value) { guard(); if (flow) flow.verifier = value; },
|
|
20
20
|
codeVerifier: function () { if (!flow || !flow.verifier) throw new Error('Sign in again.'); return flow.verifier; },
|
|
21
21
|
redirectToAuthorization: async function (url) {
|
|
22
22
|
if (!flow) { var required = new Error('Sign in to connect this server.'); required.name = 'UnauthorizedError'; throw required; }
|
|
@@ -25,13 +25,15 @@ function createOAuthProvider(record, persist, flow) {
|
|
|
25
25
|
if (url.searchParams.get('state') !== flow.state || url.searchParams.get('redirect_uri') !== flow.redirectUrl || !url.searchParams.get('code_challenge')) throw new Error('Invalid sign-in request.');
|
|
26
26
|
flow.authorizationUrl = url.href;
|
|
27
27
|
},
|
|
28
|
-
discoveryState: function () { return auth.discovery; },
|
|
28
|
+
discoveryState: function () { guard(); return auth.discovery; },
|
|
29
29
|
saveDiscoveryState: function (value) {
|
|
30
|
+
guard();
|
|
30
31
|
// Bind client credentials to the discovered issuer.
|
|
31
32
|
if (auth.discovery && auth.discovery.authorizationServerUrl !== value.authorizationServerUrl) { delete auth.client; delete auth.tokens; }
|
|
32
33
|
guard(); auth.discovery = value; save();
|
|
33
34
|
},
|
|
34
35
|
invalidateCredentials: function (which) {
|
|
36
|
+
guard();
|
|
35
37
|
if (which === 'all' || which === 'client') delete auth.client;
|
|
36
38
|
if (which === 'all' || which === 'tokens') delete auth.tokens;
|
|
37
39
|
if (which === 'all' || which === 'discovery') delete auth.discovery;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
var descriptors = require('./sdk-mcp-descriptors').extractMcpDescriptors;
|
|
2
|
+
function createQueryBridge(ctx) {
|
|
3
|
+
function valid() {
|
|
4
|
+
if (!ctx.valid()) throw new Error('MCP tools require the current authorized query.');
|
|
5
|
+
}
|
|
6
|
+
function list() {
|
|
7
|
+
if (!ctx.valid()) return [];
|
|
8
|
+
var servers = descriptors(ctx.getServers()) || [], tools = [];
|
|
9
|
+
servers.forEach(function (server) {
|
|
10
|
+
server.tools.forEach(function (tool) { tools.push(Object.assign({ server: server.serverName }, tool)); });
|
|
11
|
+
});
|
|
12
|
+
return tools;
|
|
13
|
+
}
|
|
14
|
+
async function call(serverName, toolName, args) {
|
|
15
|
+
valid();
|
|
16
|
+
var server = ctx.getServers()[serverName], tool = server && server.instance && server.instance._registeredTools[toolName];
|
|
17
|
+
if (!tool || typeof (tool.handler || tool.callback) !== 'function') throw new Error('MCP tool is unavailable.');
|
|
18
|
+
var result = await (tool.handler || tool.callback)(args || {});
|
|
19
|
+
valid(); return result;
|
|
20
|
+
}
|
|
21
|
+
function dynamicDefs() {
|
|
22
|
+
return list().map(function (tool) {
|
|
23
|
+
return { name: 'mcp__' + tool.server + '__' + tool.name, permissionName: 'mcp__' + tool.server + '__' + tool.name,
|
|
24
|
+
description: tool.description, inputSchema: tool.inputSchema, queryBound: true,
|
|
25
|
+
handler: function (args) { return call(tool.server, tool.name, args); } };
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
return { list: list, call: call, dynamicDefs: dynamicDefs };
|
|
29
|
+
}
|
|
30
|
+
module.exports = { createQueryBridge: createQueryBridge };
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
var crypto = require('crypto');
|
|
2
2
|
var Client = require('@modelcontextprotocol/sdk/client/index.js').Client;
|
|
3
|
+
var SSEClientTransport = require('@modelcontextprotocol/sdk/client/sse.js').SSEClientTransport;
|
|
4
|
+
var localTransport = require('./mcp-local-transport').localTransport;
|
|
3
5
|
var StreamableHTTPClientTransport = require('@modelcontextprotocol/sdk/client/streamableHttp.js').StreamableHTTPClientTransport;
|
|
4
6
|
var network = require('./mcp-network');
|
|
5
7
|
var oauth = require('./mcp-oauth');
|
|
@@ -9,12 +11,33 @@ function createRemoteConnections(ctx) {
|
|
|
9
11
|
var accounts = new Map(), live = new Map(), flows = new Map(), queues = new Map();
|
|
10
12
|
var remoteFetch = ctx.remoteFetch || network.createRemoteFetch();
|
|
11
13
|
var closed = false;
|
|
14
|
+
function sameRuntimeConfig(left, right) {
|
|
15
|
+
return left && right && left.url === right.url && left.enabled === right.enabled && left.authMode === right.authMode && left.token === right.token && JSON.stringify(left.auth || null) === JSON.stringify(right.auth || null) && JSON.stringify([left.transport, left.command, left.args, left.env, left.cwd, left.headers, left.envHeaders, left.references, left.disabledTools, left.enabledTools, left.toolPatterns, left.toolPrefix]) === JSON.stringify([right.transport, right.command, right.args, right.env, right.cwd, right.headers, right.envHeaders, right.references, right.disabledTools, right.enabledTools, right.toolPatterns, right.toolPrefix]);
|
|
16
|
+
}
|
|
17
|
+
function invalidate(owner, previous, current) {
|
|
18
|
+
var byId = new Map(current.filter(function (row) { return row.source !== 'computer'; }).map(function (row) { return [row.id, row]; }));
|
|
19
|
+
previous.filter(function (row) { return row.source !== 'computer'; }).forEach(function (row) {
|
|
20
|
+
var next = byId.get(row.id);
|
|
21
|
+
if (!next || !sameRuntimeConfig(row, next)) disconnect(owner, row.id);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
12
24
|
function records(owner) {
|
|
13
25
|
ctx.authorize(owner);
|
|
14
|
-
|
|
15
|
-
|
|
26
|
+
var account = accounts.get(owner), snapshot = typeof ctx.storage.snapshot === 'function' ? ctx.storage.snapshot(owner) : { records: ctx.storage.load(owner), revision: typeof ctx.storage.revision === 'function' ? ctx.storage.revision(owner) : null };
|
|
27
|
+
var loaded = snapshot.records, revision = snapshot.revision;
|
|
28
|
+
if (!account) accounts.set(owner, account = { records: loaded, revision: revision });
|
|
29
|
+
else if (account.revision !== revision) { invalidate(owner, account.records, loaded); account.records = loaded; account.revision = revision; }
|
|
30
|
+
return account.records;
|
|
31
|
+
}
|
|
32
|
+
function persist(owner) {
|
|
33
|
+
ctx.authorize(owner); if (closed) throw new Error('MCP connections closed.');
|
|
34
|
+
var account = accounts.get(owner);
|
|
35
|
+
if (!account) throw new Error('MCP connection snapshot is unavailable.');
|
|
36
|
+
var revision;
|
|
37
|
+
try { revision = ctx.storage.save(owner, account.records, account.revision); }
|
|
38
|
+
catch (error) { accounts.delete(owner); invalidate(owner, account.records, []); throw error; }
|
|
39
|
+
if (account) account.revision = revision;
|
|
16
40
|
}
|
|
17
|
-
function persist(owner) { ctx.authorize(owner); if (closed) throw new Error('MCP connections closed.'); ctx.storage.save(owner, records(owner)); }
|
|
18
41
|
function find(owner, id) {
|
|
19
42
|
var row = records(owner).find(function (item) { return item.id === id; });
|
|
20
43
|
if (!row) throw new Error('Connection is unavailable.');
|
|
@@ -23,14 +46,15 @@ function createRemoteConnections(ctx) {
|
|
|
23
46
|
function key(owner, id) { return JSON.stringify([owner, id]); }
|
|
24
47
|
function check(owner, row) {
|
|
25
48
|
if (closed || find(owner, row.id) !== row) throw new Error('Connection changed.');
|
|
49
|
+
if (row.source === 'local' && ctx.ownerIdentity) ctx.ownerIdentity(owner);
|
|
26
50
|
}
|
|
27
51
|
function changed(owner) { if (!closed) ctx.changed(owner); }
|
|
28
52
|
function view(owner) {
|
|
29
|
-
return records(owner).filter(function (row) { return row.source
|
|
53
|
+
return records(owner).filter(function (row) { return row.source !== 'computer'; }).map(function (row) {
|
|
30
54
|
var entry = live.get(key(owner, row.id));
|
|
31
|
-
return { id: row.id, name: row.id, label: row.label, source:
|
|
55
|
+
return { id: row.id, name: row.id, label: row.label, source: row.source, transport: row.transport || 'http', url: row.url ? new URL(row.url).origin + new URL(row.url).pathname : null, provenance: row.provenance, references: require('./mcp-import-credentials').requiredNames(row),
|
|
32
56
|
projectEnabled: !!row.enabled, extensionEnabled: true, tools: row.tools || [], toolCount: (row.tools || []).length,
|
|
33
|
-
status: entry ? entry.status : (row.tools ? 'idle' : 'unavailable'), error: entry ? entry.error : '', authMode: row.authMode || 'auto' };
|
|
57
|
+
status: entry ? entry.status : (row.provenance || row.tools ? 'idle' : 'unavailable'), error: entry ? entry.error : '', authMode: row.authMode || 'auto' };
|
|
34
58
|
});
|
|
35
59
|
}
|
|
36
60
|
async function disconnect(owner, id) {
|
|
@@ -39,7 +63,7 @@ function createRemoteConnections(ctx) {
|
|
|
39
63
|
if (entry && entry.client) await entry.client.close().catch(function () {});
|
|
40
64
|
}
|
|
41
65
|
function provider(owner, row, flow) {
|
|
42
|
-
return oauth.createOAuthProvider(row, function () { check(owner, row); persist(owner); }, flow);
|
|
66
|
+
return oauth.createOAuthProvider(row, function () { check(owner, row); persist(owner); }, flow, function () { check(owner, row); });
|
|
43
67
|
}
|
|
44
68
|
async function connect(owner, row, flow) {
|
|
45
69
|
check(owner, row);
|
|
@@ -47,17 +71,20 @@ function createRemoteConnections(ctx) {
|
|
|
47
71
|
check(owner, row);
|
|
48
72
|
if (flow && flow.cancelled) throw new Error('Sign-in expired.');
|
|
49
73
|
var client = new Client({ name: 'clay', version: '1.0.0' }, { capabilities: {} });
|
|
50
|
-
|
|
51
|
-
|
|
74
|
+
if (row.source !== 'local' && ((row.references || []).length || Object.keys(row.envHeaders || {}).length)) throw new Error('Configure required credentials in Clay before connecting.');
|
|
75
|
+
var options = { fetch: async function (input, init) { check(owner, row); var response = await remoteFetch.fetch(input, init); check(owner, row); return response; }, requestInit: { headers: row.headers || {} } };
|
|
76
|
+
if (row.authMode === 'token' && row.token) options.requestInit = { headers: Object.assign({}, row.headers || {}, { Authorization: 'Bearer ' + row.token }) };
|
|
52
77
|
else if (flow || (row.auth && row.auth.tokens)) options.authProvider = provider(owner, row, flow);
|
|
53
|
-
var transport = new StreamableHTTPClientTransport(new URL(row.url), options);
|
|
78
|
+
var transport = row.source === 'local' ? localTransport(row, ctx.ownerIdentity ? ctx.ownerIdentity(owner) : null) : (row.transport === 'sse' ? new SSEClientTransport(new URL(row.url), options) : new StreamableHTTPClientTransport(new URL(row.url), options));
|
|
54
79
|
var entry = { client: client, transport: transport, status: 'connecting', error: '' };
|
|
55
80
|
live.set(key(owner, row.id), entry); changed(owner);
|
|
56
81
|
try {
|
|
57
82
|
await client.connect(transport, { timeout: 30000 });
|
|
83
|
+
check(owner, row);
|
|
58
84
|
var tools = [], cursor, seen = new Set();
|
|
59
85
|
do {
|
|
60
86
|
var page = await client.listTools(cursor ? { cursor: cursor } : {}, { timeout: 30000 });
|
|
87
|
+
check(owner, row);
|
|
61
88
|
tools = tools.concat(page.tools);
|
|
62
89
|
if (tools.length > 1000 || seen.size >= 100 || (page.nextCursor && seen.has(page.nextCursor))) throw new Error('Tool catalog exceeds the supported limit.');
|
|
63
90
|
cursor = page.nextCursor; seen.add(cursor);
|
|
@@ -65,13 +92,16 @@ function createRemoteConnections(ctx) {
|
|
|
65
92
|
if (JSON.stringify(tools).length > 2 * 1024 * 1024) throw new Error('Tool catalog exceeds the supported limit.');
|
|
66
93
|
check(owner, row);
|
|
67
94
|
if (live.get(key(owner, row.id)) !== entry || (flow && flow.cancelled)) throw new Error('Connection changed.');
|
|
95
|
+
tools = tools.filter(function (tool) {
|
|
96
|
+
return require('./mcp-tool-policy').permits(row, tool.name);
|
|
97
|
+
});
|
|
68
98
|
require('./mcp-proxy').validateDefinitions(tools);
|
|
69
99
|
row.tools = tools; entry.status = 'ready'; persist(owner);
|
|
70
100
|
return entry;
|
|
71
101
|
} catch (error) {
|
|
72
102
|
var requiresAuth = error.code === 401 || error.code === 403 || error.name === 'UnauthorizedError' || (flow && flow.authorizationUrl);
|
|
73
103
|
entry.status = requiresAuth ? 'auth_required' : 'unavailable';
|
|
74
|
-
entry.error = requiresAuth ? 'Sign in or provide an access token to connect.' : 'Could not connect. Check the address and try again.';
|
|
104
|
+
entry.error = requiresAuth ? 'Sign in or provide an access token to connect.' : row.source === 'local' ? 'Could not start the command. Check its dependency and working directory.' : 'Could not connect. Check the address and try again.';
|
|
75
105
|
if (!flow) await client.close().catch(function () {});
|
|
76
106
|
return entry;
|
|
77
107
|
} finally { changed(owner); }
|
|
@@ -101,6 +131,7 @@ function createRemoteConnections(ctx) {
|
|
|
101
131
|
if (ready.status !== 'ready') throw new Error('Connection failed after sign-in.');
|
|
102
132
|
setTimeout(function () { if (flows.get(key(owner, row.id)) === flow) { cancelLogin(owner, row.id); changed(owner); } }, 500);
|
|
103
133
|
});
|
|
134
|
+
try { check(owner, row); } catch (error) { flow.close(); throw error; }
|
|
104
135
|
flows.set(key(owner, row.id), flow);
|
|
105
136
|
flow.timer = setTimeout(function () { cancelLogin(owner, row.id); }, 10 * 60 * 1000); flow.timer.unref();
|
|
106
137
|
row.authMode = 'auto'; delete row.token;
|
|
@@ -123,40 +154,61 @@ function createRemoteConnections(ctx) {
|
|
|
123
154
|
pending.finally(function () { if (queues.get(owner) === pending) queues.delete(owner); }).catch(function () {});
|
|
124
155
|
return pending;
|
|
125
156
|
}
|
|
157
|
+
function commitImport(owner, select, authorize) {
|
|
158
|
+
authorize();
|
|
159
|
+
var imported = select();
|
|
160
|
+
records(owner);
|
|
161
|
+
if (accounts.get(owner).revision !== imported.revision) throw new Error('Connections changed. Scan again.');
|
|
162
|
+
if (records(owner).some(function (item) { return item.importSignature === imported.signature || imported.config.url && item.url === imported.config.url; })) throw new Error('This server is already imported.');
|
|
163
|
+
authorize();
|
|
164
|
+
var row = Object.assign({}, imported.config, { id: 'remote-' + crypto.randomBytes(8).toString('hex'), importSignature: imported.signature, enabled: false });
|
|
165
|
+
records(owner).push(row); persist(owner);
|
|
166
|
+
return row;
|
|
167
|
+
}
|
|
168
|
+
function importConnection(owner, select, authorize) {
|
|
169
|
+
return serial(owner, function () { var row = commitImport(owner, select, authorize); changed(owner); return row.id; });
|
|
170
|
+
}
|
|
126
171
|
function command(owner, msg, ws) {
|
|
127
172
|
// Revocation takes effect before any queued network work completes.
|
|
128
173
|
if (msg.action === 'remove' || (msg.action === 'enable' && msg.enabled !== true)) {
|
|
129
174
|
try {
|
|
130
175
|
var revoked = find(owner, msg.id);
|
|
131
|
-
if (revoked.source
|
|
176
|
+
if (revoked.source === 'computer') throw new Error('Not a remote connection.');
|
|
132
177
|
revoked.enabled = false; cancelLogin(owner, revoked.id); persist(owner);
|
|
133
178
|
disconnect(owner, revoked.id); changed(owner);
|
|
134
179
|
} catch (error) { return Promise.reject(error); }
|
|
135
180
|
}
|
|
136
181
|
return serial(owner, async function () {
|
|
137
182
|
var row;
|
|
138
|
-
if (msg.action === '
|
|
183
|
+
if (msg.action === 'import') {
|
|
184
|
+
row = commitImport(owner, function () { return ctx.selectImport(owner, ws, msg); }, function () { if (!ctx.canAccess(ws, owner)) throw new Error('MCP account is unavailable.'); });
|
|
185
|
+
} else if (msg.action === 'add') {
|
|
139
186
|
var url = network.remoteUrl(msg.url).href;
|
|
140
|
-
if (records(owner).filter(function (item) { return item.source
|
|
187
|
+
if (records(owner).filter(function (item) { return item.source !== 'computer'; }).length >= 30) throw new Error('You can add up to 30 remote connections per project.');
|
|
141
188
|
if (records(owner).some(function (item) { return item.url === url; })) throw new Error('This server is already connected. Open it from your connections.');
|
|
142
189
|
row = { id: 'remote-' + crypto.randomBytes(8).toString('hex'), source: 'url', url: url, label: new URL(url).hostname, enabled: false };
|
|
143
190
|
records(owner).push(row); persist(owner);
|
|
144
191
|
await connect(owner, row);
|
|
145
192
|
} else {
|
|
146
193
|
row = find(owner, msg.id);
|
|
147
|
-
if (row.source
|
|
148
|
-
if (msg.action === 'enable') { row.enabled = msg.enabled === true; persist(owner); }
|
|
194
|
+
if (row.source === 'computer') throw new Error('Not a remote connection.');
|
|
195
|
+
if (msg.action === 'enable') { row.enabled = msg.enabled === true; persist(owner); if (row.enabled && row.provenance) { try { await connect(owner, row); } catch (error) { row.enabled = false; persist(owner); throw error; } } }
|
|
149
196
|
else if (msg.action === 'rename') {
|
|
150
197
|
if (typeof msg.label !== 'string' || !msg.label.trim() || msg.label.length > 100) throw new Error('Use a name between 1 and 100 characters.');
|
|
151
198
|
row.label = msg.label.trim(); persist(owner);
|
|
152
199
|
} else if (msg.action === 'remove') {
|
|
153
200
|
cancelLogin(owner, row.id); await disconnect(owner, row.id);
|
|
154
|
-
|
|
201
|
+
var account = accounts.get(owner);
|
|
202
|
+
accounts.set(owner, { records: records(owner).filter(function (item) { return item !== row; }), revision: account && account.revision }); persist(owner);
|
|
155
203
|
} else if (msg.action === 'test') { cancelLogin(owner, row.id); await connect(owner, row); }
|
|
156
|
-
else if (msg.action === '
|
|
204
|
+
else if (msg.action === 'credentials') {
|
|
205
|
+
if (!msg.values || typeof msg.values !== 'object' || Array.isArray(msg.values)) throw new Error('Enter required credential values.');
|
|
206
|
+
var next = require('./mcp-import-credentials').applyCredentials(row, msg.values);
|
|
207
|
+
cancelLogin(owner, row.id); Object.assign(row, next); persist(owner); await disconnect(owner, row.id);
|
|
208
|
+
} else if (msg.action === 'token') {
|
|
157
209
|
if (typeof msg.token !== 'string' || !msg.token.trim() || msg.token.length > 8192 || /[\r\n]/.test(msg.token)) throw new Error('Enter a valid access token.');
|
|
158
210
|
cancelLogin(owner, row.id); row.token = msg.token.trim(); row.authMode = 'token'; delete row.auth; persist(owner); await connect(owner, row);
|
|
159
|
-
} else if (msg.action === 'signin') await signIn(owner, row, ws);
|
|
211
|
+
} else if (msg.action === 'signin') { if (row.source === 'local') throw new Error('This server uses process environment credentials.'); await signIn(owner, row, ws); }
|
|
160
212
|
else throw new Error('Unsupported connection action.');
|
|
161
213
|
}
|
|
162
214
|
changed(owner);
|
|
@@ -174,6 +226,7 @@ function createRemoteConnections(ctx) {
|
|
|
174
226
|
return serial(owner, async function () {
|
|
175
227
|
authorize(); var row = find(owner, id);
|
|
176
228
|
if (!row.enabled) throw new Error('Connection is disabled.');
|
|
229
|
+
if (!require('./mcp-tool-policy').permits(row, name)) throw new Error('Tool is excluded by the imported vendor policy.');
|
|
177
230
|
var entry = live.get(key(owner, id));
|
|
178
231
|
if (!entry || entry.status !== 'ready') entry = await connect(owner, row);
|
|
179
232
|
authorize(); check(owner, row);
|
|
@@ -183,6 +236,7 @@ function createRemoteConnections(ctx) {
|
|
|
183
236
|
var result;
|
|
184
237
|
try { result = await entry.client.callTool({ name: name, arguments: args || {} }, undefined, { timeout: 60000 }); }
|
|
185
238
|
catch (error) {
|
|
239
|
+
authorize(); check(owner, row);
|
|
186
240
|
if (live.get(key(owner, id)) === entry) {
|
|
187
241
|
entry.status = error.code === 401 || error.code === 403 || error.name === 'UnauthorizedError' ? 'auth_required' : 'unavailable';
|
|
188
242
|
entry.error = entry.status === 'auth_required' ? 'Sign in again to reconnect.' : 'The tool call could not complete. Check the service before retrying.';
|
|
@@ -209,7 +263,7 @@ function createRemoteConnections(ctx) {
|
|
|
209
263
|
closed = true; live.forEach(function (entry) { entry.client.close().catch(function () {}); }); live.clear();
|
|
210
264
|
remoteFetch.close();
|
|
211
265
|
}
|
|
212
|
-
return { view: view, command: command, call: call, handleBrowser: handleBrowser, destroy: destroy,
|
|
266
|
+
return { view: view, command: command, importConnection: importConnection, call: call, handleBrowser: handleBrowser, destroy: destroy,
|
|
213
267
|
computerEnabled: computerEnabled, toggleComputer: toggleComputer };
|
|
214
268
|
}
|
|
215
269
|
module.exports = { createRemoteConnections: createRemoteConnections };
|