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