@yaks/connections 0.0.0-stage → 0.2.36

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.
Files changed (184) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +609 -2
  3. package/_dist/authorize.d.ts +38 -0
  4. package/_dist/authorize.d.ts.map +1 -0
  5. package/_dist/authorize_cli.d.ts +12 -0
  6. package/_dist/authorize_cli.d.ts.map +1 -0
  7. package/_dist/brokers.d.ts +66 -0
  8. package/_dist/brokers.d.ts.map +1 -0
  9. package/_dist/catalog.d.ts +33 -0
  10. package/_dist/catalog.d.ts.map +1 -0
  11. package/_dist/cli.d.ts +3 -0
  12. package/_dist/cli.d.ts.map +1 -0
  13. package/_dist/clients.d.ts +18 -0
  14. package/_dist/clients.d.ts.map +1 -0
  15. package/_dist/connections.d.ts +296 -0
  16. package/_dist/connections.d.ts.map +1 -0
  17. package/_dist/effects.d.ts +15 -0
  18. package/_dist/effects.d.ts.map +1 -0
  19. package/_dist/field.d.ts +17 -0
  20. package/_dist/field.d.ts.map +1 -0
  21. package/_dist/form.d.ts +49 -0
  22. package/_dist/form.d.ts.map +1 -0
  23. package/_dist/given.d.ts +10 -0
  24. package/_dist/given.d.ts.map +1 -0
  25. package/_dist/graph.d.ts +15 -0
  26. package/_dist/graph.d.ts.map +1 -0
  27. package/_dist/history.d.ts +4 -0
  28. package/_dist/history.d.ts.map +1 -0
  29. package/_dist/identity.d.ts +9 -0
  30. package/_dist/identity.d.ts.map +1 -0
  31. package/_dist/integrations.d.ts +196 -0
  32. package/_dist/integrations.d.ts.map +1 -0
  33. package/_dist/loopback.d.ts +7 -0
  34. package/_dist/loopback.d.ts.map +1 -0
  35. package/_dist/mcp-retirement.d.ts +33 -0
  36. package/_dist/mcp-retirement.d.ts.map +1 -0
  37. package/_dist/mcp.d.ts +179 -0
  38. package/_dist/mcp.d.ts.map +1 -0
  39. package/_dist/mod.d.ts +70 -0
  40. package/_dist/mod.d.ts.map +1 -0
  41. package/_dist/paste.d.ts +13 -0
  42. package/_dist/paste.d.ts.map +1 -0
  43. package/_dist/requests.d.ts +30 -0
  44. package/_dist/requests.d.ts.map +1 -0
  45. package/_dist/services.d.ts +25 -0
  46. package/_dist/services.d.ts.map +1 -0
  47. package/_dist/sign-in.d.ts +9 -0
  48. package/_dist/sign-in.d.ts.map +1 -0
  49. package/_dist/signin-mail.d.ts +12 -0
  50. package/_dist/signin-mail.d.ts.map +1 -0
  51. package/_dist/signin.d.ts +19 -0
  52. package/_dist/signin.d.ts.map +1 -0
  53. package/_dist/sources.d.ts +61 -0
  54. package/_dist/sources.d.ts.map +1 -0
  55. package/_dist/tools.d.ts +3 -0
  56. package/_dist/tools.d.ts.map +1 -0
  57. package/_dist/views.d.ts +3 -0
  58. package/_dist/views.d.ts.map +1 -0
  59. package/_dist/vocab.d.ts +6 -0
  60. package/_dist/vocab.d.ts.map +1 -0
  61. package/_dist/yaks-app.d.ts +29 -0
  62. package/_dist/yaks-app.d.ts.map +1 -0
  63. package/anthropic.json +12 -0
  64. package/auth_command.js +69 -0
  65. package/auth_command.js.map +1 -0
  66. package/auth_command.ts +74 -0
  67. package/authorize.js +89 -0
  68. package/authorize.js.map +1 -0
  69. package/authorize.ts +138 -0
  70. package/authorize_cli.js +167 -0
  71. package/authorize_cli.js.map +1 -0
  72. package/authorize_cli.ts +211 -0
  73. package/brokers.js +318 -0
  74. package/brokers.js.map +1 -0
  75. package/brokers.ts +423 -0
  76. package/canva.json +32 -0
  77. package/catalog.js +178 -0
  78. package/catalog.js.map +1 -0
  79. package/catalog.ts +216 -0
  80. package/cli.js +30 -0
  81. package/cli.js.map +1 -0
  82. package/cli.ts +26 -0
  83. package/clients.js +34 -0
  84. package/clients.js.map +1 -0
  85. package/clients.ts +65 -0
  86. package/connections.js +1961 -0
  87. package/connections.js.map +1 -0
  88. package/connections.ts +2561 -0
  89. package/deno.json +1 -0
  90. package/discord-account.json +22 -0
  91. package/discord-bot.json +20 -0
  92. package/discord.json +26 -0
  93. package/effects.js +168 -0
  94. package/effects.js.map +1 -0
  95. package/effects.ts +160 -0
  96. package/elevenlabs.json +18 -0
  97. package/facebook.json +18 -0
  98. package/field.js +19 -0
  99. package/field.js.map +1 -0
  100. package/field.ts +31 -0
  101. package/form.js +112 -0
  102. package/form.js.map +1 -0
  103. package/form.ts +178 -0
  104. package/git-config.js +44 -0
  105. package/git-config.js.map +1 -0
  106. package/git-config.ts +26 -0
  107. package/given.js +83 -0
  108. package/given.js.map +1 -0
  109. package/given.ts +88 -0
  110. package/gmail-mailbox.json +29 -0
  111. package/gmail.json +31 -0
  112. package/google-calendar.json +31 -0
  113. package/google-drive.json +31 -0
  114. package/google-search-console.json +29 -0
  115. package/graph.js +36 -0
  116. package/graph.js.map +1 -0
  117. package/graph.ts +52 -0
  118. package/history.js +31 -0
  119. package/history.js.map +1 -0
  120. package/history.ts +32 -0
  121. package/identity.js +15 -0
  122. package/identity.js.map +1 -0
  123. package/identity.ts +18 -0
  124. package/integrations.js +493 -0
  125. package/integrations.js.map +1 -0
  126. package/integrations.ts +725 -0
  127. package/loopback.js +72 -0
  128. package/loopback.js.map +1 -0
  129. package/loopback.ts +89 -0
  130. package/lulu.json +32 -0
  131. package/mcp-retirement.js +133 -0
  132. package/mcp-retirement.js.map +1 -0
  133. package/mcp-retirement.ts +200 -0
  134. package/mcp-signin.js +14 -0
  135. package/mcp-signin.js.map +1 -0
  136. package/mcp-signin.ts +13 -0
  137. package/mcp.js +1036 -0
  138. package/mcp.js.map +1 -0
  139. package/mcp.ts +1283 -0
  140. package/mod.js +70 -0
  141. package/mod.js.map +1 -0
  142. package/mod.ts +216 -0
  143. package/openai-api.json +12 -0
  144. package/openai.json +25 -0
  145. package/openrouter.json +14 -0
  146. package/package.json +89 -4
  147. package/paste.js +36 -0
  148. package/paste.js.map +1 -0
  149. package/paste.ts +55 -0
  150. package/requests.js +137 -0
  151. package/requests.js.map +1 -0
  152. package/requests.ts +200 -0
  153. package/services.js +84 -0
  154. package/services.js.map +1 -0
  155. package/services.ts +111 -0
  156. package/sign-in.js +36 -0
  157. package/sign-in.js.map +1 -0
  158. package/sign-in.ts +53 -0
  159. package/signin-mail.js +29 -0
  160. package/signin-mail.js.map +1 -0
  161. package/signin-mail.ts +56 -0
  162. package/signin.js +68 -0
  163. package/signin.js.map +1 -0
  164. package/signin.ts +109 -0
  165. package/slack.json +90 -0
  166. package/sources.js +178 -0
  167. package/sources.js.map +1 -0
  168. package/sources.ts +237 -0
  169. package/tools.js +39 -0
  170. package/tools.js.map +1 -0
  171. package/tools.ts +46 -0
  172. package/views.js +85 -0
  173. package/views.js.map +1 -0
  174. package/views.ts +89 -0
  175. package/visitor-defaults.json +8 -0
  176. package/vocab.js +17 -0
  177. package/vocab.js.map +1 -0
  178. package/vocab.json +863 -0
  179. package/vocab.ts +18 -0
  180. package/voipms.json +32 -0
  181. package/yaks-app.js +159 -0
  182. package/yaks-app.js.map +1 -0
  183. package/yaks-app.ts +177 -0
  184. package/youtube.json +26 -0
package/mcp.js ADDED
@@ -0,0 +1,1036 @@
1
+ // Person-owned remote commands. The host supplies graph, vault and transport;
2
+ // OAuth and invocation remain in the existing connections and MCP packages.
3
+ //
4
+ // A server is connected in a space (`mcp_server.space`), never for the whole
5
+ // account, either for the person who connected it there or for everyone the
6
+ // space's Operator works with (`mcp_server.everyone`), whose calls then go
7
+ // with the credential of the person who connected it. One kept from before
8
+ // servers had a space reaches its person in every space, and at their own
9
+ // connector, which names no space.
10
+ //
11
+ // A server is reached one of two ways. One on the public internet is an HTTPS
12
+ // address its person signs in to. One on a machine (`mcp_server.machine`, the
13
+ // space whose tunnel goes there) is a path on that machine's own server, asked
14
+ // through the tunnel with no sign-in: the host answers the tunnel as a `fetch`
15
+ // to the space's owner alone, so such a server is its person's only while the
16
+ // space is, and to anyone else it is not connected.
17
+ import { line } from "@yaks/query";
18
+ import { mint } from "@yaks/graph";
19
+ import { token } from "@yaks/graph/sha256";
20
+ import { after } from "@yaks/fp";
21
+ import { CallError } from "@yaks/tools/errors";
22
+ import { checkNamespaces, checkToolNames, connect as client, MACHINE, MCPAuthorizationRequired, MCPError, nameOf, unsaid } from "@yaks/mcp-client";
23
+ import { AuthorizationError, discover } from "@yaks/mcp-client/oauth";
24
+ import { credentialHeader, serverOf } from "@yaks/mcp-client/servers";
25
+ import { connect, credential, need, pick } from "./connections.js";
26
+ import { integrationEid } from "./identity.js";
27
+ import { registration } from "./clients.js";
28
+ import { keyed, known } from "./integrations.js";
29
+ import { mcpIntegration as integration } from "./mcp-retirement.js";
30
+ import { mcpAlternative, useIntegration } from "./mcp-signin.js";
31
+ let refuse = (code, text)=>new CallError(code, text);
32
+ let writing = (s)=>s == 'owns' || s == 'writes';
33
+ /** How much of what one server says to read before its tools an Operator is
34
+ * told, in characters: about 16k tokens. A server's instructions are its own
35
+ * text, which a stranger's server writes, and they are sent with every ask of
36
+ * the Operator they reach. */ export let SAID = 64_000;
37
+ /** How much of what a server says of one tool a model is told, in
38
+ * characters: the server writes it, and it rides with every turn its tool is
39
+ * listed in. */ export let DESCRIBED = 4_000;
40
+ /** How much of a tool's own title is kept, in characters: a name, not room
41
+ * for words to the model. */ export let TITLED = 80;
42
+ let cut = (text, most = SAID)=>text.length > most ? `${text.slice(0, most)}\n\n[Cut at ${most} of the ${text.length} characters the server said.]` : text;
43
+ // A tool's title, as the server's own name for it: one line, cut short.
44
+ let titled = (title)=>{
45
+ let line = title.replace(/\s+/g, ' ').trim();
46
+ return line.length > TITLED ? `${line.slice(0, TITLED)}…` : line;
47
+ };
48
+ // A server on a machine that did not answer as one: the machine is off, its
49
+ // tunnel has no gateway, or it opened no such path. Its owner sees that, and
50
+ // it is no fault of the platform's to report.
51
+ let away = (s, e)=>!!s.server.machine && e instanceof MCPError;
52
+ // A challenge to sign in, which only a server on the public internet makes: a
53
+ // machine's tunnel is its owner's already.
54
+ let challenged = (s, e)=>!s.server.machine && e instanceof MCPAuthorizationRequired;
55
+ // The address of a server on the public internet, as it is kept.
56
+ let https = (said)=>{
57
+ let url;
58
+ try {
59
+ url = new URL(said);
60
+ } catch {
61
+ throw refuse('arguments', 'An MCP server needs an HTTPS URL.');
62
+ }
63
+ if (url.protocol != 'https:' || url.username || url.password) {
64
+ throw refuse('arguments', 'An MCP server needs an HTTPS URL without credentials.');
65
+ }
66
+ return url.href;
67
+ };
68
+ let comp = (row, name)=>row?.[name] ?? {};
69
+ let input = {
70
+ type: 'object',
71
+ properties: {}
72
+ };
73
+ // How a call to a server's sign-in or status reads to the person.
74
+ let checking = (label)=>({
75
+ doing: `Checking your connection to ${label}…`,
76
+ done: `Checked your connection to ${label}`
77
+ });
78
+ let status = (s, description)=>({
79
+ at: integration(s.id),
80
+ name: 'connection_status',
81
+ namespace: s.server.namespace,
82
+ title: `Check ${s.label}`,
83
+ step: checking(s.label),
84
+ description,
85
+ readOnly: true,
86
+ input
87
+ });
88
+ // A server's schema as this host reads it. A graph's own MCP door says `ref`
89
+ // on each argument that takes an id, naming a kind in its graph, and a host
90
+ // resolves every `ref` it reads in its own (@yaks/tools `resolved`): left on,
91
+ // an id only the server knows is refused here before the call leaves.
92
+ let foreign = (schema)=>Array.isArray(schema) ? schema.map(foreign) : schema && typeof schema == 'object' ? Object.fromEntries(Object.entries(schema).filter(([k, v])=>k != 'ref' || typeof v != 'string').map(([k, v])=>[
93
+ k,
94
+ foreign(v)
95
+ ])) : schema;
96
+ let described = (s, tools)=>tools.map((t)=>{
97
+ nameOf(s.server.namespace, t.name);
98
+ return {
99
+ at: integration(s.id),
100
+ name: t.name,
101
+ namespace: s.server.namespace,
102
+ ...s.server.machine ? {
103
+ machine: s.server.machine
104
+ } : {},
105
+ title: `${s.label}: ${titled(t.title ?? t.name)}`,
106
+ step: {
107
+ doing: `Using ${s.label}…`,
108
+ done: `Used ${s.label}`
109
+ },
110
+ description: `[What the MCP server ${s.label} says of its tool, in its own words] ${cut(t.description ?? t.name, DESCRIBED)}`,
111
+ readOnly: t.annotations?.readOnlyHint === true,
112
+ input: foreign(t.inputSchema)
113
+ };
114
+ });
115
+ /** Whether the person's own server `row` is reached in `space`: one
116
+ * connected there, or one kept without a space, which reaches its person
117
+ * everywhere. */ export let reachedIn = (row, space)=>{
118
+ let at = comp(row, 'mcp_server').space;
119
+ return !at || at == space;
120
+ };
121
+ // What a server is called where it says nothing: its host, less the label
122
+ // that only says it serves MCP, or its path on a machine.
123
+ let hostOf = (url, machine)=>{
124
+ let u = new URL(url, MACHINE);
125
+ return machine ? u.pathname.replace(/^\/+|\/+$/g, '') || 'Machine' : u.hostname.replace(/^(mcp|www|api)\./, '');
126
+ };
127
+ // `name`, or the first of `name 2`, `name 3`… whose tools' prefix no server
128
+ // in `rows` already has.
129
+ let distinct = (name, rows)=>{
130
+ let space = (n)=>n.trim().replace(/[^a-zA-Z0-9_-]+/g, '_');
131
+ let taken = new Set(rows.map((r)=>space(String(comp(r, 'mcp_server').name ?? ''))));
132
+ let n = 1;
133
+ let next = name;
134
+ while(taken.has(space(next)))next = `${name} ${++n}`;
135
+ return next;
136
+ };
137
+ /** The credential of the first connected server among `servers`
138
+ * (`mcp_server` rows) whose sign-in or key goes to `host`: what a client of
139
+ * the same service's own API asks it with, as speech asks ElevenLabs with the
140
+ * sign-in its MCP server took. A server turned off, in the trash or on a
141
+ * machine gives none. */ export let hostKey = async (c, servers, host)=>{
142
+ for (let row of servers){
143
+ let s = comp(row, 'mcp_server');
144
+ if (row.trashed || s.enabled === false || s.machine) continue;
145
+ let name = integration(row.entity.eid);
146
+ let i = await known(c.graph.read, name);
147
+ if (!i?.hosts.includes(host)) continue;
148
+ let held = await pick(c.graph.read, String(s.owner), name);
149
+ if (comp(held, 'connection').status != 'connected') continue;
150
+ let key = await credential(c, held.entity.eid);
151
+ if (key) {
152
+ return {
153
+ key,
154
+ header: keyed(i) && typeof s.header == 'string' ? s.header : null
155
+ };
156
+ }
157
+ }
158
+ return undefined;
159
+ };
160
+ export let sharing = (read, person, space)=>after(read(line`.mcp_server.space=${space}&.mcp_server.owner=${person}&.mcp_server.everyone=true ?trashed`), (rows)=>rows.filter((r)=>!r.trashed && !comp(r, 'mcp_server').machine).map((r)=>String(comp(r, 'mcp_server').name)).sort());
161
+ /** Each operation reads ownership anew; no roster retains authority. */ export let remoteCommands = (person, host, options = {})=>{
162
+ // Every server the person keeps in every space, those in the trash
163
+ // (`trashed{}`) too; what is asked of one by its id is theirs wherever it is.
164
+ let mine = async ()=>person ? await host.graph.read(line`.mcp_server.owner=${person}&*`) : [];
165
+ // Those reached where the host asks.
166
+ let here = (r)=>host.every || reachedIn(r, host.space);
167
+ let kept = async ()=>(await mine()).filter(here);
168
+ let rows = async ()=>(await kept()).filter((r)=>!r.trashed);
169
+ let live = async ()=>(await mine()).filter((r)=>!r.trashed);
170
+ // Who connected each server reached, whose credential its calls carry.
171
+ let owners = new Map();
172
+ let whose = (s)=>owners.get(s.id) ?? person;
173
+ // Those another person connected in the space for everyone in it, while
174
+ // they own it: owning it is what let them share it, and their calls carry
175
+ // that person's credential. One who leaves or is demoted shares nothing
176
+ // there, and owning it again shares it again; the row stays as it was.
177
+ let shared = async ()=>{
178
+ if (!host.space || !person || host.every) return [];
179
+ let found = (await host.graph.read(line`.mcp_server.space=${host.space}&.mcp_server.everyone=true&*`)).filter((r)=>!r.trashed && comp(r, 'mcp_server').owner != person && !comp(r, 'mcp_server').machine);
180
+ let by = [
181
+ ...new Set(found.map((r)=>String(comp(r, 'mcp_server').owner)))
182
+ ];
183
+ let seats = await Promise.all(by.map((p)=>host.seat?.(p) ?? null));
184
+ let owning = new Set(by.filter((_, i)=>seats[i] == 'owns'));
185
+ return found.filter((r)=>owning.has(String(comp(r, 'mcp_server').owner)));
186
+ };
187
+ // In a space, a person with no seat there reaches no server, their own
188
+ // neither, and only one who writes there reaches those shared with
189
+ // everyone.
190
+ let seat = async ()=>host.every || !host.space ? 'writes' : await host.seat?.(person) ?? null;
191
+ let reachable = async ()=>{
192
+ let held = await seat();
193
+ let [own, theirs] = await Promise.all([
194
+ held ? rows() : [],
195
+ writing(held) ? shared() : []
196
+ ]);
197
+ for (let r of [
198
+ ...own,
199
+ ...theirs
200
+ ]){
201
+ owners.set(r.entity.eid, String(comp(r, 'mcp_server').owner));
202
+ }
203
+ return [
204
+ ...own,
205
+ ...theirs
206
+ ];
207
+ };
208
+ let reached = async (s)=>{
209
+ if (!s.server.machine) return s;
210
+ let fetch = await host.machine?.(s.server.machine);
211
+ return fetch ? {
212
+ ...s,
213
+ fetch
214
+ } : null;
215
+ };
216
+ let servers = async ()=>{
217
+ let all = (await Promise.all((await reachable()).map(serverOf).filter((s)=>s != null).map(reached))).filter((s)=>s != null);
218
+ checkNamespaces(all.map((s)=>s.server));
219
+ return all;
220
+ };
221
+ let owned = async (id)=>{
222
+ let found = (await servers()).find((s)=>s.id == id);
223
+ if (!found) {
224
+ throw refuse('access', 'This MCP server is not connected for you.');
225
+ }
226
+ return found;
227
+ };
228
+ let afterServer = (id, owner, rows)=>host.graph.apply([
229
+ {
230
+ entity: {
231
+ eid: id
232
+ },
233
+ $was: {
234
+ mcp_server: {
235
+ owner: token(owner)
236
+ },
237
+ tombstone: {
238
+ '': null
239
+ }
240
+ }
241
+ },
242
+ ...rows
243
+ ]);
244
+ let writingFor = (s, rows)=>afterServer(s.id, whose(s), rows);
245
+ // The server's integration as one a person gives a key to: no sign-in, and
246
+ // the key sent to the server's host alone. One it signed in through before
247
+ // becomes this, and its connection asks for a key.
248
+ let keying = (s)=>writingFor(s, [
249
+ {
250
+ entity: {
251
+ eid: integrationEid(integration(s.id))
252
+ },
253
+ integration: {
254
+ name: integration(s.id),
255
+ owner: whose(s),
256
+ title: s.label,
257
+ resource: s.server.url,
258
+ hosts: [
259
+ new URL(s.server.url).hostname
260
+ ],
261
+ authorize: null,
262
+ token: null,
263
+ client_id: null,
264
+ client: null,
265
+ scopes: null,
266
+ issuer: null,
267
+ userinfo: null,
268
+ auth: null
269
+ }
270
+ }
271
+ ]);
272
+ // The person's connection to a server's integration, asked for where they
273
+ // hold none.
274
+ let holding = async (s)=>{
275
+ let name = integration(s.id);
276
+ let b = await pick(host.graph.read, whose(s), name);
277
+ if (b) return b;
278
+ await writingFor(s, await need(host.graph.read, {
279
+ owner: whose(s),
280
+ integration: name
281
+ }));
282
+ return await pick(host.graph.read, whose(s), name);
283
+ };
284
+ let authorization = async (s, challenge = {})=>{
285
+ await owned(s.id);
286
+ let alternative = !s.server.machine && mcpAlternative(s.server.url);
287
+ if (alternative) throw refuse('rejected', useIntegration(alternative));
288
+ let name = integration(s.id);
289
+ let i = await known(host.graph.read, name);
290
+ if (i && (i.owner != whose(s) || i.resource != s.server.url)) {
291
+ throw refuse('access', 'This MCP credential belongs to another connection.');
292
+ }
293
+ let found = i ? undefined : await discover(s.server.url, challenge, options.fetch, host.document).catch((e)=>{
294
+ // A server that asks for a credential and names no sign-in takes a key.
295
+ if (e instanceof AuthorizationError) return undefined;
296
+ throw e;
297
+ });
298
+ if (!i && !found) await keying(s);
299
+ if (found) {
300
+ let client = s.server.oauth?.clientId ?? await found.register(host.redirect);
301
+ await writingFor(s, [
302
+ {
303
+ entity: {
304
+ eid: integrationEid(name)
305
+ },
306
+ integration: {
307
+ ...found.integration,
308
+ name,
309
+ owner: whose(s),
310
+ title: s.label,
311
+ client_id: client,
312
+ auth: 'post'
313
+ }
314
+ }
315
+ ]);
316
+ } else if (i && !keyed(i) && !i.client && s.server.oauth?.clientId && i.client_id != s.server.oauth.clientId) {
317
+ await writingFor(s, [
318
+ {
319
+ entity: {
320
+ eid: integrationEid(name)
321
+ },
322
+ integration: {
323
+ client_id: s.server.oauth.clientId
324
+ }
325
+ }
326
+ ]);
327
+ }
328
+ let held = await holding(s);
329
+ if (held && (comp(held, 'connection').status == 'connected' || challenge.scope)) {
330
+ let scopes = comp(held, 'connection').scopes ?? [];
331
+ await writingFor(s, [
332
+ {
333
+ entity: held.entity,
334
+ connection: {
335
+ ...comp(held, 'connection').status == 'connected' ? {
336
+ status: 'broken'
337
+ } : {},
338
+ ...challenge.scope ? {
339
+ scopes: [
340
+ ...new Set([
341
+ ...scopes,
342
+ ...challenge.scope.split(' ')
343
+ ])
344
+ ]
345
+ } : {}
346
+ }
347
+ }
348
+ ]);
349
+ }
350
+ let page = typeof host.signIn == 'string' ? host.signIn : await host.signIn();
351
+ let link = `${page}#${encodeURIComponent(name)}`;
352
+ let key = i ? keyed(i) : !found;
353
+ let them = whose(s) != person;
354
+ return {
355
+ text: `${them ? `${s.label} needs ${key ? 'its key' : 'a sign-in'} from whoever connected it in this space, at` : key ? `${s.label} takes a key: give it at` : `Sign in to ${s.label} at`} ${link}, then ask again. No call was retried.`,
356
+ value: {
357
+ authorization: {
358
+ server: s.id,
359
+ connection: held?.entity.eid,
360
+ url: link
361
+ }
362
+ }
363
+ };
364
+ };
365
+ // A connection to the server; `every` lists its tools past its allow list,
366
+ // for the person choosing them.
367
+ let using = async (s, run, every = false)=>{
368
+ let c = client(every ? {
369
+ ...s.server,
370
+ allow: undefined
371
+ } : s.server, s.fetch ? {
372
+ ...options,
373
+ fetch: s.fetch
374
+ } : {
375
+ ...options,
376
+ token: async ()=>{
377
+ let current = await owned(s.id);
378
+ if (JSON.stringify(current.server) != JSON.stringify(s.server)) {
379
+ throw refuse('access', 'The MCP connection changed; discover its tools again.');
380
+ }
381
+ let i = await known(host.graph.read, integration(s.id));
382
+ if (i && (i.owner != whose(s) || i.resource != s.server.url)) {
383
+ throw refuse('access', 'This MCP credential belongs to another connection.');
384
+ }
385
+ let held = await pick(host.graph.read, whose(s), integration(s.id));
386
+ if (!held || comp(held, 'connection').status != 'connected') {
387
+ return null;
388
+ }
389
+ return await credential(host, held.entity.eid) ?? null;
390
+ }
391
+ });
392
+ try {
393
+ return await run(c);
394
+ } finally{
395
+ await c.close().catch(host.report);
396
+ }
397
+ };
398
+ let listing = async (s)=>{
399
+ try {
400
+ return await using(s, async (c)=>described(s, await c.list()));
401
+ } catch (e) {
402
+ let auth = challenged(s, e);
403
+ let description = `${s.label} is unavailable. Check the connection to discover its tools again.`;
404
+ if (challenged(s, e)) {
405
+ try {
406
+ description = (await authorization(s, e.challenge)).text;
407
+ } catch (failure) {
408
+ host.report(failure);
409
+ auth = false;
410
+ }
411
+ } else if (!away(s, e)) host.report(e);
412
+ return [
413
+ {
414
+ at: integration(s.id),
415
+ name: auth ? 'sign_in' : 'connection_status',
416
+ namespace: s.server.namespace,
417
+ title: auth ? `Sign in to ${s.label}` : `Check ${s.label}`,
418
+ step: checking(s.label),
419
+ description,
420
+ readOnly: true,
421
+ input
422
+ }
423
+ ];
424
+ }
425
+ };
426
+ let list = async (at = '')=>{
427
+ let all = (await servers()).filter((s)=>!at || integration(s.id) == at);
428
+ let commands = (await Promise.all(all.map(listing))).flat();
429
+ let names = commands.map((c)=>nameOf(c.namespace, c.name));
430
+ try {
431
+ checkToolNames(names.map((name)=>({
432
+ name
433
+ })));
434
+ return commands;
435
+ } catch (e) {
436
+ host.report(e);
437
+ let duplicates = new Set(names.filter((name, i)=>names.indexOf(name) != i));
438
+ let affected = new Set(commands.filter((_, i)=>duplicates.has(names[i])).map((c)=>c.at));
439
+ return [
440
+ ...commands.filter((c)=>!affected.has(c.at)),
441
+ ...all.filter((s)=>affected.has(integration(s.id))).map((s)=>status(s, `${s.label}'s tool names collide with another server. Use mcp_connect to give it a distinct name.`))
442
+ ];
443
+ }
444
+ };
445
+ // One ask on a connection already open, sent as it was asked: the server's
446
+ // tools are listed only for its own status, or to say why a call failed,
447
+ // so a call through a tunnel costs one request on the connection, not two.
448
+ let asked = async (s, c, { name, args })=>{
449
+ if (name == 'connection_status') {
450
+ let tools = await c.list();
451
+ if (!tools.some((t)=>t.name == name)) {
452
+ let commands = described(s, tools);
453
+ return {
454
+ text: '',
455
+ value: {
456
+ commands
457
+ }
458
+ };
459
+ }
460
+ }
461
+ let reply;
462
+ try {
463
+ reply = await c.call(name, args);
464
+ } catch (e) {
465
+ let tools = await c.list().catch(()=>null);
466
+ if (tools && !tools.some((t)=>t.name == name)) {
467
+ throw refuse('missing', `${s.label} has no command ${name}.`);
468
+ }
469
+ throw e;
470
+ }
471
+ // The reply's text blocks are its words; the value keeps the rest of the
472
+ // reply (its structured answer, isError, images) so the words ride once.
473
+ let blocks = reply.content;
474
+ let words = blocks.filter((b)=>b.type == 'text');
475
+ let text = words.map((b)=>b.text).join('\n') || unsaid(reply) || '';
476
+ let content = blocks.filter((b)=>b.type != 'text');
477
+ return {
478
+ text,
479
+ value: {
480
+ ...reply,
481
+ content
482
+ }
483
+ };
484
+ };
485
+ // What an ask that failed answers: a sign-in, a server that is away, or
486
+ // the failure itself.
487
+ let failed = async (s, name, e)=>{
488
+ if (challenged(s, e)) return await authorization(s, e.challenge);
489
+ if (name != 'connection_status') {
490
+ throw away(s, e) ? refuse('unavailable', `${s.label}: ${e.message}`) : e;
491
+ }
492
+ if (!away(s, e)) host.report(e);
493
+ return {
494
+ text: `${s.label} is unavailable; try this command again later.`,
495
+ value: {
496
+ unavailable: true
497
+ }
498
+ };
499
+ };
500
+ // Several asks of one server, all at once on one connection, each answered
501
+ // or failed on its own: a page reading three lists from a machine opens
502
+ // the tunnel's connection once, not three times.
503
+ let calls = async (at, asks)=>{
504
+ let s = await owned(at.slice('mcp:'.length));
505
+ let settled = await using(s, (c)=>Promise.allSettled(asks.map((ask)=>asked(s, c, ask))));
506
+ return Promise.all(settled.map((r, i)=>r.status == 'fulfilled' ? {
507
+ out: r.value
508
+ } : failed(s, asks[i].name, r.reason).then((out)=>({
509
+ out
510
+ }), (error)=>({
511
+ error
512
+ }))));
513
+ };
514
+ let call = async (at, name, args)=>{
515
+ let [r] = await calls(at, [
516
+ {
517
+ name,
518
+ args
519
+ }
520
+ ]);
521
+ if ('error' in r) throw r.error;
522
+ return r.out;
523
+ };
524
+ // Each server the person reaches, with what it says to read before its
525
+ // tools (the protocol's `instructions`). One that cannot be reached is not
526
+ // told of; its tools say why where they are listed.
527
+ let told = async ()=>(await Promise.all((await servers()).map(async (s)=>{
528
+ try {
529
+ let text = (await using(s, (c)=>c.instructions()))?.trim() ?? '';
530
+ return [
531
+ {
532
+ label: s.label,
533
+ namespace: s.server.namespace,
534
+ url: s.server.machine ? new URL(s.server.url, MACHINE).pathname : s.server.url,
535
+ ...s.server.machine ? {
536
+ machine: s.server.machine
537
+ } : {},
538
+ by: whose(s),
539
+ text: text && cut(text)
540
+ }
541
+ ];
542
+ } catch (e) {
543
+ if (!challenged(s, e) && !away(s, e)) host.report(e);
544
+ return [];
545
+ }
546
+ }))).flat();
547
+ let configure = async (settings)=>{
548
+ if (!person) throw refuse('access', 'Sign in to connect an MCP server.');
549
+ let { machine } = settings;
550
+ if (machine && !await host.machine?.(machine)) {
551
+ let owned = (await host.owned?.() ?? []).sort();
552
+ throw refuse('access', `Only the owner of a space connects an MCP server on its machine, and ${machine} is not a space you own${owned.length ? `: name one of ${owned.join(', ')}` : '; you own no space'}.`);
553
+ }
554
+ if (machine && settings.header) {
555
+ throw refuse('arguments', 'A server on a machine is reached through its tunnel and takes no key.');
556
+ }
557
+ if (machine && settings.everyone) {
558
+ throw refuse('arguments', 'A server on a machine is its owner’s alone, never everyone’s in a space.');
559
+ }
560
+ let url = machine ? settings.url : https(settings.url);
561
+ let alternative = !machine && mcpAlternative(url);
562
+ if (alternative) throw refuse('rejected', useIntegration(alternative));
563
+ let all = [
564
+ ...await rows(),
565
+ ...await shared()
566
+ ];
567
+ // The same server again, one in the trash too, which this brings back;
568
+ // one kept without a space moves to this one.
569
+ let same = (await kept()).find((r)=>{
570
+ let c = comp(r, 'mcp_server');
571
+ return c.url == url && (c.machine ?? undefined) == machine;
572
+ });
573
+ let others = all.filter((r)=>r.entity.eid != same?.entity.eid);
574
+ let name = settings.name?.trim() || (same ? String(comp(same, 'mcp_server').name) : distinct(await called(url, machine) ?? hostOf(url, machine), others));
575
+ // Unsaid for a new server, who it is for waits on what it turns out to
576
+ // take; until then it is the person's alone.
577
+ let decided = settings.everyone ?? (same ? comp(same, 'mcp_server').everyone === true : !!settings.header || undefined);
578
+ let row = {
579
+ entity: {
580
+ eid: same?.entity.eid ?? mint()
581
+ },
582
+ mcp_server: {
583
+ owner: person,
584
+ ...host.space ? {
585
+ space: host.space
586
+ } : {},
587
+ name,
588
+ url,
589
+ ...machine ? {
590
+ machine
591
+ } : {},
592
+ enabled: settings.enabled ?? true,
593
+ everyone: !!host.space && !machine && !!decided,
594
+ ...settings.allow ? {
595
+ allow: JSON.stringify(settings.allow)
596
+ } : {},
597
+ ...settings.client_id ? {
598
+ client_id: settings.client_id
599
+ } : {},
600
+ ...settings.header !== undefined ? {
601
+ header: settings.header?.trim() || null
602
+ } : {}
603
+ },
604
+ ...settings.mentions !== undefined ? {
605
+ mentions: settings.mentions ? {
606
+ url: settings.mentions
607
+ } : null
608
+ } : {},
609
+ ...same?.trashed ? {
610
+ trashed: null
611
+ } : {}
612
+ };
613
+ let next;
614
+ try {
615
+ next = serverOf(row);
616
+ if (next) nameOf(next.server.namespace, 'connection_status');
617
+ checkNamespaces([
618
+ ...others.map(serverOf).filter((s)=>s != null).map((s)=>s.server),
619
+ ...next ? [
620
+ next.server
621
+ ] : []
622
+ ]);
623
+ } catch (e) {
624
+ throw refuse('arguments', e instanceof Error ? e.message : String(e));
625
+ }
626
+ // The server's lists, where they were said, are these and no others.
627
+ let lists = settings.lists ? [
628
+ ...(await host.graph.read(line`.mcp_list.server=${row.entity.eid}`)).map((b)=>({
629
+ entity: b.entity,
630
+ $delete: true
631
+ })),
632
+ ...settings.lists.map((l, ord)=>({
633
+ entity: {
634
+ eid: mint()
635
+ },
636
+ doc: {
637
+ title: l.title
638
+ },
639
+ mcp_list: {
640
+ server: row.entity.eid,
641
+ tool: l.tool,
642
+ args: JSON.stringify(l.args),
643
+ waits: l.waits,
644
+ ord
645
+ }
646
+ }))
647
+ ] : [];
648
+ await host.graph.apply([
649
+ row,
650
+ ...lists
651
+ ]);
652
+ // A server said to take a key in a header asks for one now, rather
653
+ // than at its first refusal.
654
+ if (next && row.mcp_server.header) {
655
+ let i = await known(host.graph.read, integration(next.id));
656
+ if (!i || !keyed(i)) await keying(next);
657
+ await holding(next);
658
+ }
659
+ // Listing its tools is what finds what it takes: a refusal naming OAuth
660
+ // metadata registers this host as its client and waits on a sign-in, and
661
+ // one naming none waits on a key (`authorization`).
662
+ let commands = row.mcp_server.enabled ? await list(integration(row.entity.eid)) : [];
663
+ let needs = next && !machine ? await waiting(next) : undefined;
664
+ if (decided === undefined && host.space && !machine) {
665
+ await afterServer(row.entity.eid, person, [
666
+ {
667
+ entity: row.entity,
668
+ mcp_server: {
669
+ everyone: needs != 'sign-in'
670
+ }
671
+ }
672
+ ]);
673
+ }
674
+ return {
675
+ server: row.entity.eid,
676
+ name,
677
+ commands,
678
+ ...needs ? {
679
+ needs
680
+ } : {}
681
+ };
682
+ };
683
+ // What the server `s` waits on before it answers: nothing where it took
684
+ // no credential or holds one that works.
685
+ let waiting = async (s)=>{
686
+ let name = integration(s.id);
687
+ let held = await pick(host.graph.read, whose(s), name);
688
+ if (!held || comp(held, 'connection').status == 'connected') return;
689
+ let i = await known(host.graph.read, name);
690
+ return i && !keyed(i) ? 'sign-in' : 'key';
691
+ };
692
+ // What the server at `url` calls itself, asked with no credential: a
693
+ // server that wants a sign-in first says nothing.
694
+ let called = async (url, machine)=>{
695
+ let fetch = machine ? await host.machine?.(machine) : undefined;
696
+ let c = client({
697
+ name: 'new',
698
+ url: machine ? new URL(url, MACHINE).href : url,
699
+ ...machine ? {
700
+ machine
701
+ } : {}
702
+ }, {
703
+ ...options,
704
+ ...fetch ? {
705
+ fetch
706
+ } : {},
707
+ token: undefined,
708
+ timeout: 10_000
709
+ });
710
+ try {
711
+ return await c.about();
712
+ } catch {
713
+ return undefined;
714
+ } finally{
715
+ await c.close().catch(()=>{});
716
+ }
717
+ };
718
+ /** Keep `key` as the credential of the person's server `id`, which it
719
+ * then takes instead of a sign-in. */ let give = async (id, key)=>{
720
+ let row = (await live()).find((r)=>r.entity.eid == id);
721
+ if (!row) {
722
+ throw refuse('missing', 'This MCP server is not connected for you.');
723
+ }
724
+ let s = serverOf({
725
+ ...row,
726
+ mcp_server: {
727
+ ...comp(row, 'mcp_server'),
728
+ enabled: true
729
+ }
730
+ });
731
+ if (s.server.machine) {
732
+ throw refuse('arguments', 'A server on a machine is reached through its tunnel and takes no key.');
733
+ }
734
+ let i = await known(host.graph.read, integration(id));
735
+ if (!i || !keyed(i)) await keying(s);
736
+ let b = await holding(s);
737
+ await connect(host, b.entity.eid, {
738
+ key
739
+ });
740
+ };
741
+ /** Sign in to the person's server `id` as the OAuth client `client`, kept
742
+ * in the vault (@yaks/connections `registration`), rather than one this
743
+ * host registers: for a server that registers no client for this host, or
744
+ * one that takes only clients it knows. Its id and secret never reach the
745
+ * graph or a tool's arguments. */ let signsInAs = async (id, given)=>{
746
+ let row = (await live()).find((r)=>r.entity.eid == id);
747
+ if (!row) {
748
+ throw refuse('missing', 'This MCP server is not connected for you.');
749
+ }
750
+ let s = serverOf({
751
+ ...row,
752
+ mcp_server: {
753
+ ...comp(row, 'mcp_server'),
754
+ enabled: true
755
+ }
756
+ });
757
+ if (s.server.machine) {
758
+ throw refuse('arguments', 'A server on a machine is reached through its tunnel and takes no sign-in.');
759
+ }
760
+ let name = integration(id);
761
+ let i = await known(host.graph.read, name);
762
+ if (!i || keyed(i)) {
763
+ throw refuse('arguments', `${s.label} names no sign-in, so it takes no OAuth client.`);
764
+ }
765
+ await writingFor(s, [
766
+ registration(name, given),
767
+ {
768
+ entity: {
769
+ eid: integrationEid(name)
770
+ },
771
+ integration: {
772
+ client: name,
773
+ client_id: null
774
+ }
775
+ },
776
+ {
777
+ entity: row.entity,
778
+ mcp_server: {
779
+ client_id: given.id
780
+ }
781
+ }
782
+ ]);
783
+ };
784
+ /** Put the person's server `id` in the trash: its tools and lists go with
785
+ * it, and its credential stays, so `restore` brings it all back.
786
+ * One already in the trash stays there, and the call is done. */ let remove = async (id)=>{
787
+ let row = (await mine()).find((r)=>r.entity.eid == id);
788
+ if (!row) {
789
+ throw refuse('missing', 'This MCP server is not connected for you.');
790
+ }
791
+ if (!row.trashed) {
792
+ await host.graph.apply([
793
+ {
794
+ entity: row.entity,
795
+ trashed: {}
796
+ }
797
+ ]);
798
+ }
799
+ };
800
+ /** Bring the person's server `id` back out of the trash. */ let restore = async (id)=>{
801
+ let row = (await mine()).find((r)=>r.entity.eid == id && r.trashed);
802
+ if (!row) throw refuse('missing', 'This MCP server is not in your trash.');
803
+ let back = serverOf(row);
804
+ try {
805
+ checkNamespaces([
806
+ ...(await rows()).map(serverOf).filter((s)=>s != null),
807
+ ...back ? [
808
+ back
809
+ ] : []
810
+ ].map((s)=>s.server));
811
+ } catch {
812
+ throw refuse('conflict', `Another of your MCP servers has the name ${comp(row, 'mcp_server').name}; rename one of them first.`);
813
+ }
814
+ await host.graph.apply([
815
+ {
816
+ entity: row.entity,
817
+ trashed: null
818
+ }
819
+ ]);
820
+ };
821
+ /** Turn the person's server `id` on or off, or say which of its tools are
822
+ * offered: `allow` null offers every one. */ let set = async (id, patch)=>{
823
+ let row = (await live()).find((r)=>r.entity.eid == id);
824
+ if (!row) {
825
+ throw refuse('missing', 'This MCP server is not connected for you.');
826
+ }
827
+ let c = comp(row, 'mcp_server');
828
+ let name = patch.name?.trim();
829
+ if (patch.name !== undefined && !name) {
830
+ throw refuse('arguments', 'Give it a name.');
831
+ }
832
+ if (name && name != c.name) {
833
+ let others = (await rows()).filter((r)=>r.entity.eid != id);
834
+ if (distinct(name, others) != name) {
835
+ throw refuse('conflict', `Another of your MCP servers has the name ${name}; choose another.`);
836
+ }
837
+ }
838
+ let header = patch.header === undefined ? undefined : patch.header?.trim() || null;
839
+ if (header && (c.machine || !credentialHeader(header))) {
840
+ throw refuse('arguments', c.machine ? 'A server on a machine is reached through its tunnel and takes no key.' : `${header} is not a header a key can go in; name one such as X-API-Key.`);
841
+ }
842
+ await host.graph.apply([
843
+ {
844
+ entity: row.entity,
845
+ mcp_server: {
846
+ ...patch.enabled != null ? {
847
+ enabled: patch.enabled
848
+ } : {},
849
+ ...patch.everyone != null ? {
850
+ everyone: patch.everyone && !c.machine && !!c.space
851
+ } : {},
852
+ ...patch.allow !== undefined ? {
853
+ allow: patch.allow && JSON.stringify(patch.allow)
854
+ } : {},
855
+ ...name ? {
856
+ name
857
+ } : {},
858
+ ...header !== undefined ? {
859
+ header
860
+ } : {}
861
+ }
862
+ }
863
+ ]);
864
+ };
865
+ /** Every tool the person's server `id` offers, those its allow list leaves
866
+ * out too, each with whether it is offered. */ let tools = async (id)=>{
867
+ let s = await owned(id);
868
+ let allow = s.server.allow;
869
+ return (await using(s, (c)=>c.list(), true)).map((t)=>({
870
+ name: t.name,
871
+ title: t.title ?? t.name,
872
+ description: t.description ?? '',
873
+ allowed: !allow || allow.includes(t.name)
874
+ }));
875
+ };
876
+ /** Each server the person keeps, as their page shows it. */ let shown = async (rows)=>await Promise.all((rows ?? await mine()).filter(here).map(async (row)=>{
877
+ let c = comp(row, 'mcp_server');
878
+ let allow = c.allow == null ? undefined : JSON.parse(String(c.allow));
879
+ let name = integration(row.entity.eid);
880
+ let [held, i] = c.machine ? [] : await Promise.all([
881
+ pick(host.graph.read, person, name),
882
+ known(host.graph.read, name)
883
+ ]);
884
+ let status = comp(held, 'connection').status ?? 'open';
885
+ let alternative = !c.machine && status != 'connected' && !c.header && (!i || !keyed(i)) && mcpAlternative(String(c.url));
886
+ return {
887
+ id: row.entity.eid,
888
+ name: String(c.name),
889
+ url: String(c.url),
890
+ ...c.machine ? {
891
+ machine: String(c.machine)
892
+ } : {},
893
+ enabled: c.enabled !== false,
894
+ everyone: c.everyone === true,
895
+ ...allow ? {
896
+ allow
897
+ } : {},
898
+ ...c.header ? {
899
+ header: String(c.header)
900
+ } : {},
901
+ trashed: !!row.trashed,
902
+ status: c.machine ? 'machine' : alternative ? 'unavailable' : status,
903
+ ...alternative ? {
904
+ alternative
905
+ } : {},
906
+ ...held ? {
907
+ connection: held.entity.eid
908
+ } : {},
909
+ ...i ? {
910
+ keyed: keyed(i)
911
+ } : {}
912
+ };
913
+ }));
914
+ return {
915
+ set,
916
+ tools,
917
+ shown,
918
+ list,
919
+ call,
920
+ calls,
921
+ told,
922
+ configure,
923
+ give,
924
+ signsInAs,
925
+ remove,
926
+ restore,
927
+ kept
928
+ };
929
+ };
930
+ // What adding a server says, by where it stands: one still waiting on its
931
+ // key or sign-in says which, and one that refused its key says so.
932
+ let addedAs = (name, one)=>one?.status == 'needed' ? `${name} is added. ${one.keyed ? 'Paste its key' : `Sign in to ${name}`} to connect it.` : one?.status == 'broken' ? `${name} is added, but it refused the key.` : `${name} is connected.`;
933
+ /** What a form on a person's connections page does to their MCP servers,
934
+ * named by `do` (`mcp-add`, `mcp-key`, `mcp-client`, `mcp-name`, `mcp-tools`, `mcp-everyone`,
935
+ * `mcp-mine`, `mcp-on`, `mcp-off`, `mcp-remove`, `mcp-restore`), with the server it is about in `server`. It
936
+ * answers what the page says, and null for a form about something else. */ export let acted = async (remote, act, field, fields)=>{
937
+ let id = field('server');
938
+ if (act == 'mcp-add') {
939
+ let url = field('url');
940
+ let machine = field('machine');
941
+ let alternative = !machine && mcpAlternative(https(url));
942
+ if (alternative) return {
943
+ alternative
944
+ };
945
+ // Only the address is asked; the rest is the Advanced settings. Unchosen,
946
+ // who it is for follows what it turns out to take (`Settings.everyone`).
947
+ let chosen = field('for');
948
+ let made = await remote.configure({
949
+ url,
950
+ ...field('name') ? {
951
+ name: field('name')
952
+ } : {},
953
+ ...machine ? {
954
+ machine
955
+ } : {},
956
+ ...field('client_id') ? {
957
+ client_id: field('client_id')
958
+ } : {},
959
+ ...field('header') ? {
960
+ header: field('header')
961
+ } : {},
962
+ ...chosen ? {
963
+ everyone: chosen == 'everyone'
964
+ } : field('key') ? {
965
+ everyone: true
966
+ } : {}
967
+ });
968
+ if (field('key')) await remote.give(made.server, field('key'));
969
+ if (field('client_secret') && field('client_id')) {
970
+ await remote.signsInAs(made.server, {
971
+ id: field('client_id'),
972
+ secret: field('client_secret')
973
+ });
974
+ }
975
+ let [one] = await remote.shown((await remote.kept()).filter((r)=>r.entity.eid == made.server));
976
+ let say = addedAs(made.name, one);
977
+ return !field('key') && made.needs == 'sign-in' && one?.connection ? {
978
+ say,
979
+ signIn: one.connection
980
+ } : say;
981
+ }
982
+ if (act == 'mcp-key') {
983
+ if (!field('key')) throw refuse('arguments', 'Paste the key.');
984
+ await remote.set(id, {
985
+ header: field('header') || null
986
+ });
987
+ await remote.give(id, field('key'));
988
+ return 'Saved. The key is kept safe and never shown again.';
989
+ }
990
+ if (act == 'mcp-client') {
991
+ if (!field('client_id')) throw refuse('arguments', 'Give its client id.');
992
+ await remote.signsInAs(id, {
993
+ id: field('client_id'),
994
+ ...field('client_secret') ? {
995
+ secret: field('client_secret')
996
+ } : {}
997
+ });
998
+ return 'Saved. Sign in to connect it.';
999
+ }
1000
+ if (act == 'mcp-name') {
1001
+ await remote.set(id, {
1002
+ name: field('name')
1003
+ });
1004
+ return `Renamed ${field('name').trim()}.`;
1005
+ }
1006
+ if (act == 'mcp-tools') {
1007
+ let every = (await remote.tools(id)).map((t)=>t.name);
1008
+ let chosen = fields('tool').filter((t)=>every.includes(t));
1009
+ await remote.set(id, {
1010
+ allow: chosen.length == every.length ? null : chosen
1011
+ });
1012
+ return chosen.length == every.length ? 'Every tool it offers is offered to your agents and apps.' : `${chosen.length} of its ${every.length} tools are offered.`;
1013
+ }
1014
+ if (act == 'mcp-everyone' || act == 'mcp-mine') {
1015
+ await remote.set(id, {
1016
+ everyone: act == 'mcp-everyone'
1017
+ });
1018
+ return act == 'mcp-everyone' ? 'The space’s Operator uses it with everyone it works with now.' : 'The space’s Operator uses it only with you now.';
1019
+ }
1020
+ if (act == 'mcp-on' || act == 'mcp-off') {
1021
+ await remote.set(id, {
1022
+ enabled: act == 'mcp-on'
1023
+ });
1024
+ return act == 'mcp-on' ? 'Turned on.' : 'Turned off.';
1025
+ }
1026
+ if (act == 'mcp-remove') {
1027
+ await remote.remove(id);
1028
+ return 'Moved to the trash. Restore it from the trash below.';
1029
+ }
1030
+ if (act == 'mcp-restore') {
1031
+ await remote.restore(id);
1032
+ return 'Restored.';
1033
+ }
1034
+ return null;
1035
+ };
1036
+ //# sourceMappingURL=mcp.js.map