hmcp-gateway 0.0.2 → 0.1.0
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 +77 -0
- package/README.md +41 -9
- package/bin/hmcp-gateway.js +20 -0
- package/catalog/atlassian.yaml +41 -0
- package/catalog/brave-search.yaml +57 -0
- package/catalog/context7.yaml +42 -0
- package/catalog/fetch.yaml +37 -0
- package/catalog/figma.yaml +48 -0
- package/catalog/filesystem.yaml +48 -0
- package/catalog/git.yaml +48 -0
- package/catalog/github.yaml +103 -0
- package/catalog/huggingface.yaml +59 -0
- package/catalog/linear.yaml +47 -0
- package/catalog/memory.yaml +47 -0
- package/catalog/notion.yaml +41 -0
- package/catalog/playwright.yaml +38 -0
- package/catalog/postgres.yaml +66 -0
- package/catalog/sentry.yaml +39 -0
- package/catalog/sequential-thinking.yaml +29 -0
- package/catalog/stripe.yaml +41 -0
- package/catalog/supabase.yaml +45 -0
- package/catalog/time.yaml +49 -0
- package/dist/adopt-cli.d.ts +351 -0
- package/dist/adopt-cli.js +1857 -0
- package/dist/adopt-emit.d.ts +64 -0
- package/dist/adopt-emit.js +248 -0
- package/dist/adopt-plan.d.ts +108 -0
- package/dist/adopt-plan.js +386 -0
- package/dist/catalog.d.ts +140 -0
- package/dist/catalog.js +250 -0
- package/dist/classify.d.ts +23 -0
- package/dist/classify.js +47 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +535 -0
- package/dist/client-allow.d.ts +42 -0
- package/dist/client-allow.js +95 -0
- package/dist/client-config.d.ts +200 -0
- package/dist/client-config.js +784 -0
- package/dist/config-source.d.ts +106 -0
- package/dist/config-source.js +156 -0
- package/dist/config.d.ts +159 -0
- package/dist/config.js +330 -0
- package/dist/connect-client.d.ts +49 -0
- package/dist/connect-client.js +249 -0
- package/dist/contracts.d.ts +251 -0
- package/dist/contracts.js +9 -0
- package/dist/credential.d.ts +63 -0
- package/dist/credential.js +136 -0
- package/dist/draft.d.ts +71 -0
- package/dist/draft.js +596 -0
- package/dist/exposure-cli.d.ts +28 -0
- package/dist/exposure-cli.js +119 -0
- package/dist/gateway.d.ts +756 -0
- package/dist/gateway.js +2106 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +26 -0
- package/dist/notify.d.ts +44 -0
- package/dist/notify.js +93 -0
- package/dist/oauth.d.ts +139 -0
- package/dist/oauth.js +641 -0
- package/dist/observe.d.ts +10 -0
- package/dist/observe.js +26 -0
- package/dist/open.d.ts +24 -0
- package/dist/open.js +31 -0
- package/dist/packs.d.ts +119 -0
- package/dist/packs.js +131 -0
- package/dist/pin-cli.d.ts +28 -0
- package/dist/pin-cli.js +103 -0
- package/dist/probe-upstream.d.ts +31 -0
- package/dist/probe-upstream.js +377 -0
- package/dist/registry-map.d.ts +39 -0
- package/dist/registry-map.js +390 -0
- package/dist/rule-cli.d.ts +51 -0
- package/dist/rule-cli.js +412 -0
- package/dist/serve-http.d.ts +67 -0
- package/dist/serve-http.js +478 -0
- package/dist/snippet.d.ts +20 -0
- package/dist/snippet.js +826 -0
- package/dist/status-cli.d.ts +61 -0
- package/dist/status-cli.js +195 -0
- package/dist/suggest-cli.d.ts +43 -0
- package/dist/suggest-cli.js +279 -0
- package/dist/toolname.d.ts +47 -0
- package/dist/toolname.js +149 -0
- package/dist/upstream-errors.d.ts +54 -0
- package/dist/upstream-errors.js +330 -0
- package/dist/upstream.d.ts +128 -0
- package/dist/upstream.js +413 -0
- package/dist/version.d.ts +10 -0
- package/dist/version.js +11 -0
- package/package.json +50 -6
- package/packs/brave-search.yaml +20 -0
- package/packs/context7.yaml +13 -0
- package/packs/fetch.yaml +14 -0
- package/packs/filesystem.yaml +79 -0
- package/packs/git.yaml +29 -0
- package/packs/github.yaml +47 -0
- package/packs/huggingface.yaml +16 -0
- package/packs/memory.yaml +23 -0
- package/packs/playwright.yaml +41 -0
- package/packs/sequential-thinking.yaml +13 -0
- package/packs/time.yaml +14 -0
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared shapes for the "add any server" work. CONTRACT (phase 0): changed
|
|
3
|
+
* only by the supervisor. Implementations live in:
|
|
4
|
+
* - oauth.ts UpstreamTokenSource (workstream C)
|
|
5
|
+
* - catalog.ts / draft.ts CatalogEntry, materializeDraft (workstream D)
|
|
6
|
+
* - registry-map.ts mapServerJson (workstream D)
|
|
7
|
+
* - snippet.ts parseSnippet (workstream D)
|
|
8
|
+
* - upstream-errors.ts classifyUpstreamError (workstream B)
|
|
9
|
+
* - adopt-cli.ts probeUpstream (workstream F exports; E calls)
|
|
10
|
+
*/
|
|
11
|
+
import type { SecretValue, UpstreamProblem } from "hmcp-core";
|
|
12
|
+
import type { Upstream } from "./config.js";
|
|
13
|
+
/**
|
|
14
|
+
* Where an HTTP upstream's Authorization header comes from when it is
|
|
15
|
+
* configured with `oauth:`. The gateway asks for a header before each request;
|
|
16
|
+
* on a 401 it calls onUnauthorized() once and retries if that returns true.
|
|
17
|
+
*/
|
|
18
|
+
export interface UpstreamTokenSource {
|
|
19
|
+
/** "Bearer <token>", refreshing first if it is about to expire. */
|
|
20
|
+
header(): Promise<string | undefined>;
|
|
21
|
+
/** Force a refresh after a 401. False when a person must sign in again. */
|
|
22
|
+
onUnauthorized(): Promise<boolean>;
|
|
23
|
+
}
|
|
24
|
+
/** Thrown by a token source when nobody can fix it but a person signing in. */
|
|
25
|
+
export declare class NeedsSignIn extends Error {
|
|
26
|
+
readonly secretName: string;
|
|
27
|
+
constructor(secretName: string, reason: string);
|
|
28
|
+
}
|
|
29
|
+
export type CatalogCategory = "files" | "developer" | "productivity" | "data" | "web" | "knowledge" | "design";
|
|
30
|
+
export type Runtime = "node" | "uv" | "docker" | "python" | "git";
|
|
31
|
+
export type FieldKind = "secret" | "folder" | "folders" | "url" | "text" | "choice" | "toggle";
|
|
32
|
+
export interface CatalogField {
|
|
33
|
+
id: string;
|
|
34
|
+
kind: FieldKind;
|
|
35
|
+
/** Plain words: "Your GitHub key". */
|
|
36
|
+
label: string;
|
|
37
|
+
help?: string;
|
|
38
|
+
placeholder?: string;
|
|
39
|
+
required?: boolean;
|
|
40
|
+
default?: string | boolean | string[];
|
|
41
|
+
/** choice only. */
|
|
42
|
+
choices?: {
|
|
43
|
+
value: string;
|
|
44
|
+
label: string;
|
|
45
|
+
}[];
|
|
46
|
+
validate?: {
|
|
47
|
+
pattern: string;
|
|
48
|
+
message: string;
|
|
49
|
+
};
|
|
50
|
+
/** secret only: the env var / header name, shown under "technical". */
|
|
51
|
+
technical?: string;
|
|
52
|
+
}
|
|
53
|
+
export interface GuideStep {
|
|
54
|
+
title: string;
|
|
55
|
+
/** Plain words, short paragraphs; may contain **bold** and `code`. */
|
|
56
|
+
body: string;
|
|
57
|
+
link?: {
|
|
58
|
+
label: string;
|
|
59
|
+
url: string;
|
|
60
|
+
};
|
|
61
|
+
/** Optional illustration path under catalog/images/. */
|
|
62
|
+
image?: string;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A launch template. String values may contain `{{fields.<id>}}`. A secret
|
|
66
|
+
* field may only appear as `{ field: <id>, prefix? }` -- never inlined into a
|
|
67
|
+
* string -- so it always becomes a `{ secret }` reference.
|
|
68
|
+
* `{{...fields.<id>}}` as a whole args item expands a `folders` field into
|
|
69
|
+
* one argument per folder.
|
|
70
|
+
*/
|
|
71
|
+
export type TemplateValue = string | {
|
|
72
|
+
field: string;
|
|
73
|
+
prefix?: string;
|
|
74
|
+
};
|
|
75
|
+
export type LaunchTemplate = {
|
|
76
|
+
transport: "stdio";
|
|
77
|
+
command: string;
|
|
78
|
+
args?: string[];
|
|
79
|
+
env?: Record<string, TemplateValue>;
|
|
80
|
+
} | {
|
|
81
|
+
transport: "http";
|
|
82
|
+
url: string;
|
|
83
|
+
headers?: Record<string, TemplateValue>;
|
|
84
|
+
/** Sign in with OAuth instead of a key. */
|
|
85
|
+
oauth?: boolean;
|
|
86
|
+
};
|
|
87
|
+
export interface CatalogVariant {
|
|
88
|
+
id: string;
|
|
89
|
+
/** "On this computer" / "GitHub's hosted service". */
|
|
90
|
+
label: string;
|
|
91
|
+
summary?: string;
|
|
92
|
+
launch: LaunchTemplate;
|
|
93
|
+
/** Fields this variant uses (ids from the entry's `fields`). */
|
|
94
|
+
fields?: string[];
|
|
95
|
+
requires?: Runtime[];
|
|
96
|
+
}
|
|
97
|
+
export interface CatalogEntry {
|
|
98
|
+
id: string;
|
|
99
|
+
name: string;
|
|
100
|
+
category: CatalogCategory;
|
|
101
|
+
/** One sentence in plain words: what it lets the AI do. */
|
|
102
|
+
summary: string;
|
|
103
|
+
/** A few sentences: what it is, what the AI can then do, what it cannot. */
|
|
104
|
+
description?: string;
|
|
105
|
+
publisher: {
|
|
106
|
+
name: string;
|
|
107
|
+
official: boolean;
|
|
108
|
+
};
|
|
109
|
+
homepage: string;
|
|
110
|
+
/** The MCP Registry name, so registry results can defer to this entry. */
|
|
111
|
+
registry_name?: string;
|
|
112
|
+
requires?: Runtime[];
|
|
113
|
+
variants: CatalogVariant[];
|
|
114
|
+
fields: CatalogField[];
|
|
115
|
+
guide: GuideStep[];
|
|
116
|
+
/** Rule pack id (packs/<id>.yaml), only when verified against real tool names. */
|
|
117
|
+
pack?: string;
|
|
118
|
+
/** The folders field a filesystem-like pack is bounded to. */
|
|
119
|
+
pack_within_field?: string;
|
|
120
|
+
/** Suggested first thing to ask the AI once connected. */
|
|
121
|
+
try_prompt?: string;
|
|
122
|
+
/** Short words used by search ("files", "repo", "issues"). */
|
|
123
|
+
keywords?: string[];
|
|
124
|
+
/**
|
|
125
|
+
* A harmless call Test it makes after listing the tools, so a wrong key or
|
|
126
|
+
* an unusable place shows up now rather than the first time the AI uses it.
|
|
127
|
+
* Read-only and cheap; a server that needs none has none.
|
|
128
|
+
*/
|
|
129
|
+
check?: {
|
|
130
|
+
tool: string;
|
|
131
|
+
args: Record<string, unknown>;
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** Where a draft came from, for labelling ("From the public list -- not checked by us"). */
|
|
135
|
+
export type DraftSource = {
|
|
136
|
+
kind: "catalog";
|
|
137
|
+
id: string;
|
|
138
|
+
variant: string;
|
|
139
|
+
} | {
|
|
140
|
+
kind: "registry";
|
|
141
|
+
name: string;
|
|
142
|
+
version?: string;
|
|
143
|
+
} | {
|
|
144
|
+
kind: "paste";
|
|
145
|
+
} | {
|
|
146
|
+
kind: "manual";
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* A server the user is filling in: launch template plus fields. Catalog,
|
|
150
|
+
* registry, paste and manual all produce this, and the UI renders one form
|
|
151
|
+
* for all of them.
|
|
152
|
+
*/
|
|
153
|
+
export interface DraftServer {
|
|
154
|
+
source: DraftSource;
|
|
155
|
+
/** Suggested upstream name (lower-case, digits, underscores). */
|
|
156
|
+
name: string;
|
|
157
|
+
displayName: string;
|
|
158
|
+
launch: LaunchTemplate;
|
|
159
|
+
fields: CatalogField[];
|
|
160
|
+
guide: GuideStep[];
|
|
161
|
+
requires: Runtime[];
|
|
162
|
+
pack?: string;
|
|
163
|
+
pack_within_field?: string;
|
|
164
|
+
/** Plain-words cautions to show before testing. */
|
|
165
|
+
warnings: string[];
|
|
166
|
+
}
|
|
167
|
+
/** What the user typed. Secret fields carry the value only on the way in. */
|
|
168
|
+
export type FieldValues = Record<string, string | boolean | string[]>;
|
|
169
|
+
export interface MaterializedDraft {
|
|
170
|
+
/** Ready for gateway.yaml: secret fields are `{ secret }` references. */
|
|
171
|
+
upstream: Upstream;
|
|
172
|
+
/** Values to write to the locked-keys file before the upstream is saved. */
|
|
173
|
+
secretsToCreate: {
|
|
174
|
+
name: string;
|
|
175
|
+
label: string;
|
|
176
|
+
value: string;
|
|
177
|
+
}[];
|
|
178
|
+
/** Secret names the upstream refers to that already exist (re-used). */
|
|
179
|
+
secretsReferenced: string[];
|
|
180
|
+
/** Hosts the network rules must allow (http url host; oauth adds token host later). */
|
|
181
|
+
egressHosts: string[];
|
|
182
|
+
/** True when the url points at this computer or a private network. */
|
|
183
|
+
localNetwork: boolean;
|
|
184
|
+
packId?: string;
|
|
185
|
+
/** Folders a path-bounded pack is limited to. */
|
|
186
|
+
within?: string[];
|
|
187
|
+
/** Field id -> problem, when values are incomplete or invalid. */
|
|
188
|
+
problems: Record<string, string>;
|
|
189
|
+
}
|
|
190
|
+
export interface ParsedDraft {
|
|
191
|
+
draft: DraftServer;
|
|
192
|
+
/** Values found in the snippet (literal keys go here, never into YAML). */
|
|
193
|
+
values: FieldValues;
|
|
194
|
+
}
|
|
195
|
+
export interface ParsedSnippet {
|
|
196
|
+
servers: ParsedDraft[];
|
|
197
|
+
/** Plain words: what was ignored and why. */
|
|
198
|
+
warnings: string[];
|
|
199
|
+
/** What the text looked like: "mcpServers JSON", "a command line", ... */
|
|
200
|
+
recognisedAs: string | null;
|
|
201
|
+
}
|
|
202
|
+
export interface RegistryHit {
|
|
203
|
+
name: string;
|
|
204
|
+
title?: string;
|
|
205
|
+
description: string;
|
|
206
|
+
version: string;
|
|
207
|
+
repository?: string;
|
|
208
|
+
/** When the name matches a catalog entry, the UI opens that instead. */
|
|
209
|
+
catalogId?: string;
|
|
210
|
+
/** Null when the server cannot be used (sse only, unknown package type). */
|
|
211
|
+
draft: DraftServer | null;
|
|
212
|
+
unsupported?: string;
|
|
213
|
+
/**
|
|
214
|
+
* Plain words, when a stranger's server asks for a well-known service's
|
|
215
|
+
* key ("This isn't made by GitHub, but asks for your GitHub key.").
|
|
216
|
+
* Also added to the draft's warnings.
|
|
217
|
+
*/
|
|
218
|
+
caution?: string;
|
|
219
|
+
}
|
|
220
|
+
export type ProbeVerdict = "run" | "ask" | "blocked";
|
|
221
|
+
export interface ProbeTool {
|
|
222
|
+
/** The upstream's own name. */
|
|
223
|
+
name: string;
|
|
224
|
+
/** As the AI app will see it: <upstream>__<tool>. */
|
|
225
|
+
exposedName: string;
|
|
226
|
+
title?: string;
|
|
227
|
+
/** Scanned and, if needed, stripped. */
|
|
228
|
+
description: string;
|
|
229
|
+
effect: "read" | "write" | "destructive" | "unknown";
|
|
230
|
+
verdict: ProbeVerdict;
|
|
231
|
+
/** The pack rule that decided it, if any. */
|
|
232
|
+
packRule?: boolean;
|
|
233
|
+
/** Withheld from the AI app, with the reason. */
|
|
234
|
+
withheld?: string;
|
|
235
|
+
}
|
|
236
|
+
export interface ProbeOutcome {
|
|
237
|
+
ok: boolean;
|
|
238
|
+
problem?: UpstreamProblem;
|
|
239
|
+
tools: ProbeTool[];
|
|
240
|
+
resources: number;
|
|
241
|
+
prompts: number;
|
|
242
|
+
/** Scanner findings in plain words. */
|
|
243
|
+
findings: {
|
|
244
|
+
severity: "high" | "medium" | "low";
|
|
245
|
+
message: string;
|
|
246
|
+
tool?: string;
|
|
247
|
+
}[];
|
|
248
|
+
/** How long connecting took, ms. */
|
|
249
|
+
elapsedMs: number;
|
|
250
|
+
}
|
|
251
|
+
export type { SecretValue };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Thrown by a token source when nobody can fix it but a person signing in. */
|
|
2
|
+
export class NeedsSignIn extends Error {
|
|
3
|
+
secretName;
|
|
4
|
+
constructor(secretName, reason) {
|
|
5
|
+
super(`sign-in for "${secretName}" needs to be done again: ${reason}`);
|
|
6
|
+
this.secretName = secretName;
|
|
7
|
+
this.name = "NeedsSignIn";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { EgressGuard } from "hmcp-core";
|
|
2
|
+
import type { Upstream } from "./config.js";
|
|
3
|
+
import { type UpstreamTokenSource } from "./contracts.js";
|
|
4
|
+
/**
|
|
5
|
+
* Sending the caller's own credential to the upstream.
|
|
6
|
+
*
|
|
7
|
+
* The gateway's position is that it should never hold a customer's secret.
|
|
8
|
+
* The upstream API already authenticates and already scopes data to whoever
|
|
9
|
+
* the token belongs to — that is its job and it has been doing it since
|
|
10
|
+
* before MCP existed — so the gateway's job is the orthogonal one: deciding
|
|
11
|
+
* whether this operation should happen at all, holding it for a human,
|
|
12
|
+
* bounding it, recording it. Passing the credential straight through keeps
|
|
13
|
+
* those two jobs separate, and means a compromise here leaks nothing, because
|
|
14
|
+
* there is nothing stored to leak.
|
|
15
|
+
*
|
|
16
|
+
* The obstacle was that an upstream MCP client is created once, at startup,
|
|
17
|
+
* with its headers baked into `requestInit` for the life of the process. The
|
|
18
|
+
* way out is the SDK's `fetch` option, which is documented as covering every
|
|
19
|
+
* network request the transport makes and does: one client, headers decided
|
|
20
|
+
* at send time.
|
|
21
|
+
*
|
|
22
|
+
* Which leaves the question of how a per-request credential reaches a
|
|
23
|
+
* function called deep inside the transport. `AsyncLocalStorage` carries it,
|
|
24
|
+
* and the store is entered as narrowly as possible — around the upstream call
|
|
25
|
+
* itself, not around the HTTP handler. That is deliberate. A wider scope
|
|
26
|
+
* would put a tenant's credential in context during startup discovery and
|
|
27
|
+
* during `tools/list`, where it has no business being, and would depend on
|
|
28
|
+
* the store surviving the transport's own server-to-client plumbing. Narrow
|
|
29
|
+
* means a leak is structurally impossible rather than merely unlikely.
|
|
30
|
+
*/
|
|
31
|
+
export interface UpstreamCredential {
|
|
32
|
+
/** The caller's bearer token, exactly as presented. */
|
|
33
|
+
readonly token: string;
|
|
34
|
+
/** For the error message when an upstream rejects it. */
|
|
35
|
+
readonly tenant: string | undefined;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Runs `fn` as a tool call, with this caller's credential (if any) available
|
|
39
|
+
* to upstream requests made inside it.
|
|
40
|
+
*
|
|
41
|
+
* Always enters the store, even with no credential, so that the requests the
|
|
42
|
+
* gateway makes on its own behalf stay distinguishable from a call made
|
|
43
|
+
* without one.
|
|
44
|
+
*/
|
|
45
|
+
export declare function withCredential<T>(credential: UpstreamCredential | undefined, fn: () => Promise<T>): Promise<T>;
|
|
46
|
+
/** The credential for the call in progress, if there is one. */
|
|
47
|
+
export declare function currentCredential(): UpstreamCredential | undefined;
|
|
48
|
+
/** Whether we are inside a tool call at all, credential or not. */
|
|
49
|
+
export declare function inCall(): boolean;
|
|
50
|
+
export declare class UpstreamCredentialRejected extends Error {
|
|
51
|
+
constructor(upstream: string);
|
|
52
|
+
}
|
|
53
|
+
export declare function credentialFetch(spec: Extract<Upstream, {
|
|
54
|
+
transport: "http";
|
|
55
|
+
}>, egress: EgressGuard,
|
|
56
|
+
/**
|
|
57
|
+
* For an upstream configured with `oauth:`. Asked for the Authorization
|
|
58
|
+
* header before every request, so a token refreshed by another gateway
|
|
59
|
+
* process (or by this one a moment ago) is the one sent. The config refuses
|
|
60
|
+
* `oauth` together with pass-through or a static authorization header, so
|
|
61
|
+
* there is never a question of which credential wins.
|
|
62
|
+
*/
|
|
63
|
+
tokenSource?: UpstreamTokenSource): (url: string | URL, init?: RequestInit) => Promise<Response>;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import { NeedsSignIn } from "./contracts.js";
|
|
3
|
+
const store = new AsyncLocalStorage();
|
|
4
|
+
/**
|
|
5
|
+
* Runs `fn` as a tool call, with this caller's credential (if any) available
|
|
6
|
+
* to upstream requests made inside it.
|
|
7
|
+
*
|
|
8
|
+
* Always enters the store, even with no credential, so that the requests the
|
|
9
|
+
* gateway makes on its own behalf stay distinguishable from a call made
|
|
10
|
+
* without one.
|
|
11
|
+
*/
|
|
12
|
+
export function withCredential(credential, fn) {
|
|
13
|
+
return store.run({ credential }, fn);
|
|
14
|
+
}
|
|
15
|
+
/** The credential for the call in progress, if there is one. */
|
|
16
|
+
export function currentCredential() {
|
|
17
|
+
return store.getStore()?.credential;
|
|
18
|
+
}
|
|
19
|
+
/** Whether we are inside a tool call at all, credential or not. */
|
|
20
|
+
export function inCall() {
|
|
21
|
+
return store.getStore() !== undefined;
|
|
22
|
+
}
|
|
23
|
+
export class UpstreamCredentialRejected extends Error {
|
|
24
|
+
constructor(upstream) {
|
|
25
|
+
super(`upstream "${upstream}" rejected the passed-through credential with 401. Check the token's audience ` +
|
|
26
|
+
`and scopes for that upstream. If the upstream binds its MCP session to the token that initialized ` +
|
|
27
|
+
`it, one shared connection cannot carry per-caller credentials. A "per-tenant" session mode is ` +
|
|
28
|
+
`reserved for that and is not implemented yet; until it is, such an upstream needs a static ` +
|
|
29
|
+
`credential under "headers" instead.`);
|
|
30
|
+
this.name = "UpstreamCredentialRejected";
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A `fetch` for the SDK's HTTP client transport that attaches the caller's
|
|
35
|
+
* credential when there is one, and the configured static headers when there
|
|
36
|
+
* is not.
|
|
37
|
+
*
|
|
38
|
+
* The "when there is not" case is the important default. Connect, tool
|
|
39
|
+
* discovery and the session DELETE all run outside any call, so they get the
|
|
40
|
+
* configuration's own headers and never a customer's token.
|
|
41
|
+
*/
|
|
42
|
+
/**
|
|
43
|
+
* Reads a refused response to its end before it is dropped. Read rather than
|
|
44
|
+
* cancelled: the egress guard pipes every body through its size cap, and
|
|
45
|
+
* cancelling the outer stream rejects that pipe with nobody listening.
|
|
46
|
+
*/
|
|
47
|
+
async function drain(response) {
|
|
48
|
+
await response.arrayBuffer().catch(() => undefined);
|
|
49
|
+
}
|
|
50
|
+
export function credentialFetch(spec, egress,
|
|
51
|
+
/**
|
|
52
|
+
* For an upstream configured with `oauth:`. Asked for the Authorization
|
|
53
|
+
* header before every request, so a token refreshed by another gateway
|
|
54
|
+
* process (or by this one a moment ago) is the one sent. The config refuses
|
|
55
|
+
* `oauth` together with pass-through or a static authorization header, so
|
|
56
|
+
* there is never a question of which credential wins.
|
|
57
|
+
*/
|
|
58
|
+
tokenSource) {
|
|
59
|
+
const passthrough = spec.credential_passthrough;
|
|
60
|
+
return async (url, init) => {
|
|
61
|
+
const target = typeof url === "string" ? url : url.toString();
|
|
62
|
+
/*
|
|
63
|
+
* Only two things are ever on an upstream request: the headers this
|
|
64
|
+
* upstream was configured with, and — inside a tool call — the one header
|
|
65
|
+
* carrying the caller's token. The caller's other headers are not
|
|
66
|
+
* filtered out here because they never arrive: nothing copies the inbound
|
|
67
|
+
* request's headers onto the outbound one, so a cookie or an x-api-key
|
|
68
|
+
* the caller sent has no path to a different origin. That is a stronger
|
|
69
|
+
* guarantee than a deny-list, which has to be kept complete to be worth
|
|
70
|
+
* anything, and `passthrough.test.ts` holds it in place.
|
|
71
|
+
*/
|
|
72
|
+
const headers = new Headers(init?.headers);
|
|
73
|
+
const credential = currentCredential();
|
|
74
|
+
if (passthrough.enabled && credential) {
|
|
75
|
+
headers.set(passthrough.header, `Bearer ${credential.token}`);
|
|
76
|
+
}
|
|
77
|
+
else if (passthrough.enabled && passthrough.required && inCall()) {
|
|
78
|
+
// Only inside a call: the gateway's own connect and discovery requests
|
|
79
|
+
// have no caller by definition, and refusing those would mean a
|
|
80
|
+
// `required` upstream could never be reached at all.
|
|
81
|
+
throw new Error(`upstream "${spec.name}" requires the caller's credential, and this call arrived without one. ` +
|
|
82
|
+
`Configure this gateway with a tenant source that authenticates the caller.`);
|
|
83
|
+
}
|
|
84
|
+
/*
|
|
85
|
+
* Through the guard, not the global fetch.
|
|
86
|
+
*
|
|
87
|
+
* Egress used to run only at connect time, so an upstream allowlisted
|
|
88
|
+
* once was then free to be redirected anywhere for the rest of the
|
|
89
|
+
* process, with none of the timeouts or caps that `policy.yaml` promises.
|
|
90
|
+
* `streamingFetch` applies what can soundly be applied to a transport
|
|
91
|
+
* that holds an event stream open -- the pre-flight, DNS verification,
|
|
92
|
+
* connection pinning, a connect and headers timeout, redirect refusal,
|
|
93
|
+
* and the response cap on everything that is not itself a stream.
|
|
94
|
+
*/
|
|
95
|
+
const send = () => egress.streamingFetch(target, {
|
|
96
|
+
method: (init?.method ?? "POST").toUpperCase(),
|
|
97
|
+
headers: Object.fromEntries(headers.entries()),
|
|
98
|
+
...(init?.body !== undefined && init?.body !== null ? { body: init.body } : {}),
|
|
99
|
+
...(init?.signal ? { signal: init.signal } : {})
|
|
100
|
+
});
|
|
101
|
+
if (tokenSource) {
|
|
102
|
+
// Throws NeedsSignIn when there is no token a refresh could produce.
|
|
103
|
+
const header = await tokenSource.header();
|
|
104
|
+
if (header)
|
|
105
|
+
headers.set("authorization", header);
|
|
106
|
+
else
|
|
107
|
+
headers.delete("authorization");
|
|
108
|
+
}
|
|
109
|
+
let response = await send();
|
|
110
|
+
if (response.status === 401 && passthrough.enabled && credential) {
|
|
111
|
+
throw new UpstreamCredentialRejected(spec.name);
|
|
112
|
+
}
|
|
113
|
+
if (response.status === 401 && tokenSource) {
|
|
114
|
+
/*
|
|
115
|
+
* Once. A token can be revoked or rotated by another process between
|
|
116
|
+
* our expiry check and the server's, and a forced refresh fixes that;
|
|
117
|
+
* a second 401 straight after a fresh token means the grant itself is
|
|
118
|
+
* gone, and only a person signing in again fixes that. The body of a
|
|
119
|
+
* request the SDK built is a string, so it can be sent again as is.
|
|
120
|
+
*/
|
|
121
|
+
await drain(response);
|
|
122
|
+
if (!(await tokenSource.onUnauthorized())) {
|
|
123
|
+
throw new NeedsSignIn(spec.oauth?.secret ?? spec.name, "the server no longer accepts the saved sign-in");
|
|
124
|
+
}
|
|
125
|
+
const fresh = await tokenSource.header();
|
|
126
|
+
if (fresh)
|
|
127
|
+
headers.set("authorization", fresh);
|
|
128
|
+
response = await send();
|
|
129
|
+
if (response.status === 401) {
|
|
130
|
+
await drain(response);
|
|
131
|
+
throw new NeedsSignIn(spec.oauth?.secret ?? spec.name, "the server refused a freshly refreshed sign-in");
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return response;
|
|
135
|
+
};
|
|
136
|
+
}
|
package/dist/draft.d.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { CatalogEntry, CatalogField, DraftServer, FieldValues, MaterializedDraft } from "./contracts.js";
|
|
2
|
+
/** The value key a sign-in's locked-key name travels under (set by the UI). */
|
|
3
|
+
export declare const SIGN_IN_VALUE = "__signin";
|
|
4
|
+
/**
|
|
5
|
+
* The value key that says "use this saved locked key for this field":
|
|
6
|
+
* `__saved.<field id>` set to the key's name. The person picks it; it is
|
|
7
|
+
* never inferred for a server from the public list, a paste or the form.
|
|
8
|
+
*/
|
|
9
|
+
export declare const SAVED_KEY_PREFIX = "__saved.";
|
|
10
|
+
/**
|
|
11
|
+
* The value key that says "this address is on my own computer or network,
|
|
12
|
+
* test it": needed before a server from the public list or a paste is
|
|
13
|
+
* tested at a private or loopback address, because testing lifts the
|
|
14
|
+
* private-address protection for that one host.
|
|
15
|
+
*/
|
|
16
|
+
export declare const LOCAL_OK_VALUE = "__local_ok";
|
|
17
|
+
/** Words for a stranger's draft that points at this computer or the local network. */
|
|
18
|
+
export declare function localAddressWarning(kind: AddressKind): string | null;
|
|
19
|
+
/** A catalog entry's variant as a draft the UI can render. */
|
|
20
|
+
export declare function draftFromCatalog(entry: CatalogEntry, variantId?: string): DraftServer;
|
|
21
|
+
export interface MaterializeOptions {
|
|
22
|
+
/** Upstream names already in the gateway, to pick a free one. */
|
|
23
|
+
takenNames?: string[];
|
|
24
|
+
/** Override the suggested upstream name. */
|
|
25
|
+
name?: string;
|
|
26
|
+
/** Secret names that exist already (so a reused key is not re-created). */
|
|
27
|
+
existingSecrets?: string[];
|
|
28
|
+
/**
|
|
29
|
+
* The label each existing key was saved with. When given, a catalog
|
|
30
|
+
* server only reuses a saved key whose label is the field's own.
|
|
31
|
+
*/
|
|
32
|
+
existingSecretLabels?: Record<string, string | undefined>;
|
|
33
|
+
/** Absolute paths for runtimes, from the machine check ("npx" -> "/usr/local/bin/npx"). */
|
|
34
|
+
commandPaths?: Record<string, string>;
|
|
35
|
+
/**
|
|
36
|
+
* Where a field's format check comes from. A draft sent by a browser is
|
|
37
|
+
* not trusted to carry a regular expression (one built to backtrack would
|
|
38
|
+
* freeze the console), so the console passes the catalog's own checks
|
|
39
|
+
* here. Without it, only a catalog draft's own `validate` is used.
|
|
40
|
+
*/
|
|
41
|
+
validateFor?: (fieldId: string) => CatalogField["validate"] | undefined;
|
|
42
|
+
}
|
|
43
|
+
export declare function materializeDraft(draft: DraftServer, values: FieldValues, options?: MaterializeOptions): MaterializedDraft;
|
|
44
|
+
/** The address with any name, password or key-named query value taken out, for messages. */
|
|
45
|
+
export declare function withoutKeys(url: string): string;
|
|
46
|
+
/**
|
|
47
|
+
* Whether an address carries a key where it can't be locked: a name and
|
|
48
|
+
* password before the host, or a query parameter named like a key.
|
|
49
|
+
*/
|
|
50
|
+
export declare function addressHoldsKey(url: string): "userinfo" | "query" | "path" | null;
|
|
51
|
+
/** Lower-case, digits and underscores: what gateway.yaml accepts as an upstream name. */
|
|
52
|
+
export declare function safeName(raw: string): string;
|
|
53
|
+
/** The locked-key name a field's value is stored under: "github.token". */
|
|
54
|
+
export declare function secretNameFor(upstreamName: string, field: CatalogField): string;
|
|
55
|
+
/** The locked-key name a sign-in is stored under; the UI derives the same one. */
|
|
56
|
+
export declare function signInSecretName(upstreamName: string): string;
|
|
57
|
+
/** Why a file can't be kept at this place, in plain words, or null when it can. */
|
|
58
|
+
export declare function filePlaceProblem(path: string): string | null;
|
|
59
|
+
/**
|
|
60
|
+
* Whether the egress guard refuses this host without its private-address
|
|
61
|
+
* protection loosened. The same test as adopt-plan.ts `isLocalAddress`, which
|
|
62
|
+
* is not exported: `localhost` counts because it resolves to loopback.
|
|
63
|
+
*/
|
|
64
|
+
export declare function isLocalAddress(hostname: string): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Where an address points, in the words the add flow needs: this computer,
|
|
67
|
+
* the local network, the link-local range that holds cloud metadata services
|
|
68
|
+
* (refused outright for a server we didn't vet), or anywhere else.
|
|
69
|
+
*/
|
|
70
|
+
export type AddressKind = "loopback" | "private" | "link-local" | "public";
|
|
71
|
+
export declare function addressKind(hostname: string): AddressKind;
|