@zapier/connectors-sdk 0.1.0-experimental.1
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 +93 -0
- package/README.internal.md +397 -0
- package/README.md +6 -0
- package/dist/build-zapier-fetch-DWCYBAF4.js +52 -0
- package/dist/index.cjs +1039 -0
- package/dist/index.d.cts +575 -0
- package/dist/index.d.ts +575 -0
- package/dist/index.js +933 -0
- package/package.json +48 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
import * as _modelcontextprotocol_sdk_types_js from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { ToolAnnotations, Tool } from '@modelcontextprotocol/sdk/types.js';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `connection-key.ts` — runtime + type pair that names this codebase's
|
|
7
|
+
* single transform between identifier strings (kebab/lowercase) and the
|
|
8
|
+
* `SCREAMING_SNAKE_CASE` segments used to compose env-var names and
|
|
9
|
+
* programmatic-bag keys.
|
|
10
|
+
*
|
|
11
|
+
* Internal only. Not re-exported from the public SDK index — resolver
|
|
12
|
+
* authors don't compose env vars themselves; the wrappers do.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Recursive template-literal helper. Replaces every occurrence of `From`
|
|
16
|
+
* with `To` in `S`. Used only by `EnvFromKey`; not generally useful on
|
|
17
|
+
* its own (no built-in TS string-replace primitive).
|
|
18
|
+
*/
|
|
19
|
+
type Replace<S extends string, From extends string, To extends string> = S extends `${infer Head}${From}${infer Tail}` ? Replace<`${Head}${To}${Tail}`, From, To> : S;
|
|
20
|
+
/**
|
|
21
|
+
* Type-level mirror of the `envFromKey` runtime function. Used by
|
|
22
|
+
* `define-connector.ts` to compose programmatic-bag key types from each
|
|
23
|
+
* resolver's literal `name` (and suffixes).
|
|
24
|
+
*
|
|
25
|
+
* Examples:
|
|
26
|
+
* EnvFromKey<"notion"> -> "NOTION"
|
|
27
|
+
* EnvFromKey<"google-drive"> -> "GOOGLE_DRIVE"
|
|
28
|
+
* EnvFromKey<"zapier-connection-id"> -> "ZAPIER_CONNECTION_ID"
|
|
29
|
+
* EnvFromKey<"token"> -> "TOKEN"
|
|
30
|
+
*/
|
|
31
|
+
type EnvFromKey<K extends string> = Uppercase<Replace<K, "-", "_">>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A Zapier policy statement. Carried on the `ToolDefinition` for in-process
|
|
35
|
+
* governance consumers (policy engines that wrap `script.run` before HTTP).
|
|
36
|
+
* Not propagated to MCP `_meta` — only `inputDependencies` is published over
|
|
37
|
+
* the wire (`_meta["zapier:inputDependencies"]`) by `toMcpTool` /
|
|
38
|
+
* `toMcpServerTool`.
|
|
39
|
+
*/
|
|
40
|
+
interface Statement {
|
|
41
|
+
effect: "allow" | "ask" | "deny";
|
|
42
|
+
permissions: ReadonlyArray<string>;
|
|
43
|
+
resources: ReadonlyArray<string>;
|
|
44
|
+
conditions?: ReadonlyArray<{
|
|
45
|
+
path: ReadonlyArray<string>;
|
|
46
|
+
operator: string;
|
|
47
|
+
value: unknown;
|
|
48
|
+
}>;
|
|
49
|
+
label?: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Single-credential resolver: the resolver's `name` IS the credential
|
|
53
|
+
* descriptor. `resolve` receives the credential as a plain `string` —
|
|
54
|
+
* matching the `string` shorthand arm of `ConnectionValue`. Resolvers do
|
|
55
|
+
* NOT see env-var names or programmatic-bag keys; the wrapper does the
|
|
56
|
+
* key composition.
|
|
57
|
+
*
|
|
58
|
+
* Composition (single-slot, connection key "notion"):
|
|
59
|
+
*
|
|
60
|
+
* { name: "token", resolve: (token) => ... }
|
|
61
|
+
* -> env var: NOTION_TOKEN
|
|
62
|
+
* -> programmatic key: TOKEN
|
|
63
|
+
*
|
|
64
|
+
* { name: "zapier-connection-id", resolve: (id) => ... }
|
|
65
|
+
* -> env var: NOTION_ZAPIER_CONNECTION_ID
|
|
66
|
+
* -> programmatic key: ZAPIER_CONNECTION_ID
|
|
67
|
+
*/
|
|
68
|
+
interface SingleConnectionResolver<TName extends string = string> {
|
|
69
|
+
readonly name: TName;
|
|
70
|
+
readonly keySuffixes?: undefined;
|
|
71
|
+
resolve: (credential: string) => typeof globalThis.fetch | Promise<typeof globalThis.fetch>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Multi-credential resolver: requires N>1 distinct credentials. `resolve`
|
|
75
|
+
* receives a suffix-keyed bag (NOT name-prefixed) — the wrapper strips the
|
|
76
|
+
* `<NAME>_` prefix before calling.
|
|
77
|
+
*
|
|
78
|
+
* Composition (single-slot, connection key "some-api"):
|
|
79
|
+
*
|
|
80
|
+
* { name: "hmac", keySuffixes: ["KEY", "SECRET"] as const,
|
|
81
|
+
* resolve: ({ KEY, SECRET }) => ... }
|
|
82
|
+
* -> env vars: SOME_API_HMAC_KEY, SOME_API_HMAC_SECRET
|
|
83
|
+
* -> programmatic bag: { HMAC_KEY: string; HMAC_SECRET: string }
|
|
84
|
+
* -> resolve receives: { KEY: string; SECRET: string }
|
|
85
|
+
*/
|
|
86
|
+
interface MultiConnectionResolver<TName extends string = string, TSuffixes extends readonly string[] = readonly string[]> {
|
|
87
|
+
readonly name: TName;
|
|
88
|
+
readonly keySuffixes: TSuffixes;
|
|
89
|
+
resolve: (credentials: Record<TSuffixes[number], string>) => typeof globalThis.fetch | Promise<typeof globalThis.fetch>;
|
|
90
|
+
}
|
|
91
|
+
/** Either resolver shape. */
|
|
92
|
+
type ConnectionResolver = SingleConnectionResolver | MultiConnectionResolver;
|
|
93
|
+
/**
|
|
94
|
+
* Per-key resolver entry: a single resolver, or an ordered list. The
|
|
95
|
+
* wrapper walks the list in order — first resolver whose required env
|
|
96
|
+
* vars are all set wins (or, for programmatic input, the resolver whose
|
|
97
|
+
* bag-key set matches `Object.keys(value)` exactly).
|
|
98
|
+
*/
|
|
99
|
+
type ConnectionResolverEntry = ConnectionResolver | readonly ConnectionResolver[];
|
|
100
|
+
/**
|
|
101
|
+
* Map from connection-key string -> resolver entry. The keys must cover
|
|
102
|
+
* every connection key referenced by every script's `connection` /
|
|
103
|
+
* `connections` declaration.
|
|
104
|
+
*/
|
|
105
|
+
type ConnectionResolversMap = Readonly<Record<string, ConnectionResolverEntry>>;
|
|
106
|
+
/**
|
|
107
|
+
* The four input shapes a slot's caller-provided handle accepts.
|
|
108
|
+
*
|
|
109
|
+
* 1. function → pre-authed `fetch`, used as-is.
|
|
110
|
+
* 2. string → shorthand; forwarded to the FIRST single-
|
|
111
|
+
* credential resolver in the array.
|
|
112
|
+
* 3. Record<string,…> → name-prefixed bag (`<NAME>` for single,
|
|
113
|
+
* `<NAME>_<SUFFIX>` for multi). Disambiguates
|
|
114
|
+
* between same-shape resolvers in one array.
|
|
115
|
+
*/
|
|
116
|
+
type ConnectionValue = typeof globalThis.fetch | string | Record<string, string>;
|
|
117
|
+
type AnyResolverArray = readonly ConnectionResolver[];
|
|
118
|
+
/**
|
|
119
|
+
* Programmatic record-bag shape for a single resolver. Single-credential
|
|
120
|
+
* resolvers contribute `{ [EnvFromKey<TName>]: string }`; multi-credential
|
|
121
|
+
* resolvers contribute `{ [\`${EnvFromKey<TName>}_${TSuffixes[number]}\`]:
|
|
122
|
+
* string }`.
|
|
123
|
+
*/
|
|
124
|
+
type BagOf<R> = R extends SingleConnectionResolver<infer TName> ? {
|
|
125
|
+
readonly [K in EnvFromKey<TName>]: string;
|
|
126
|
+
} : R extends MultiConnectionResolver<infer TName, infer TSuffixes> ? {
|
|
127
|
+
readonly [K in `${EnvFromKey<TName>}_${TSuffixes[number]}`]: string;
|
|
128
|
+
} : never;
|
|
129
|
+
/**
|
|
130
|
+
* `ConnectionValue` narrowed to a specific resolver entry. Single-resolver
|
|
131
|
+
* forms also accept the resolver's bag shape; resolver arrays additionally
|
|
132
|
+
* union the per-resolver bag shapes; the `string` shorthand arm appears
|
|
133
|
+
* iff the entry contains at least one single-credential resolver.
|
|
134
|
+
*/
|
|
135
|
+
type ConnectionValueFor<R extends ConnectionResolverEntry> = R extends ConnectionResolver ? typeof globalThis.fetch | (R extends SingleConnectionResolver ? string : never) | BagOf<R> : R extends AnyResolverArray ? typeof globalThis.fetch | (Extract<R[number], SingleConnectionResolver> extends never ? never : string) | BagOf<R[number]> : never;
|
|
136
|
+
type AnyResolversMap = Record<string, ConnectionResolverEntry>;
|
|
137
|
+
/**
|
|
138
|
+
* Per-slot narrowing: given the connector's resolvers map and the
|
|
139
|
+
* connection key the slot references, return the slot's
|
|
140
|
+
* `ConnectionValueFor` shape (or `ConnectionValue` if the key is unknown
|
|
141
|
+
* to the type system — we still validate at runtime).
|
|
142
|
+
*/
|
|
143
|
+
type ConnectionValueForKey<TResolvers extends AnyResolversMap, TKey extends string> = TKey extends keyof TResolvers ? ConnectionValueFor<TResolvers[TKey]> : ConnectionValue;
|
|
144
|
+
/**
|
|
145
|
+
* Caller-provided run options. Three shapes (mirrors `ToolDefinition`):
|
|
146
|
+
* - none: `RunOptionsNone` — no `connection` / `connections`.
|
|
147
|
+
* - single: `RunOptionsSingle` — `connection` only.
|
|
148
|
+
* - multi: `RunOptionsMulti` — `connections` only.
|
|
149
|
+
*/
|
|
150
|
+
interface RunOptionsNone {
|
|
151
|
+
connection?: never;
|
|
152
|
+
connections?: never;
|
|
153
|
+
}
|
|
154
|
+
interface RunOptionsSingle<TValue = ConnectionValue> {
|
|
155
|
+
connection: TValue;
|
|
156
|
+
connections?: never;
|
|
157
|
+
}
|
|
158
|
+
interface RunOptionsMulti<TSlots extends string = string, TPerSlot extends Record<TSlots, ConnectionValue> = Record<TSlots, ConnectionValue>> {
|
|
159
|
+
connection?: never;
|
|
160
|
+
connections: TPerSlot;
|
|
161
|
+
}
|
|
162
|
+
type RunOptions<TSlots extends string = string, TValue = ConnectionValue, TPerSlot extends Record<TSlots, ConnectionValue> = Record<TSlots, ConnectionValue>> = RunOptionsNone | RunOptionsSingle<TValue> | RunOptionsMulti<TSlots, TPerSlot>;
|
|
163
|
+
/** Author-facing context for a single-connection script. */
|
|
164
|
+
interface ContextSingle {
|
|
165
|
+
fetch: typeof globalThis.fetch;
|
|
166
|
+
}
|
|
167
|
+
/** Author-facing context for a multi-connection script. */
|
|
168
|
+
interface ContextMulti<TSlots extends string = string> {
|
|
169
|
+
connections: Record<TSlots, typeof globalThis.fetch>;
|
|
170
|
+
}
|
|
171
|
+
type AnyInputDependencies$1 = Readonly<Record<string, unknown>>;
|
|
172
|
+
/** When authoring omits `inputDependencies`, the definition value is `undefined`. */
|
|
173
|
+
type InputDependenciesValue<T> = [T] extends [undefined] ? undefined : T;
|
|
174
|
+
interface DefineToolConfigBase<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined, TName extends string> {
|
|
175
|
+
name: TName;
|
|
176
|
+
title: string;
|
|
177
|
+
description: string;
|
|
178
|
+
inputSchema: TIn;
|
|
179
|
+
outputSchema: TOut;
|
|
180
|
+
annotations: _modelcontextprotocol_sdk_types_js.Tool["annotations"];
|
|
181
|
+
statements?: ReadonlyArray<Statement>;
|
|
182
|
+
inputDependencies?: TInputDependencies;
|
|
183
|
+
}
|
|
184
|
+
interface DefineToolConfigNone<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined, TName extends string> extends DefineToolConfigBase<TIn, TOut, TInputDependencies, TName> {
|
|
185
|
+
connection?: never;
|
|
186
|
+
connections?: never;
|
|
187
|
+
run: (input: z.infer<TIn>) => Promise<z.infer<TOut>>;
|
|
188
|
+
}
|
|
189
|
+
interface DefineToolConfigSingle<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined, TName extends string, TKey extends string> extends DefineToolConfigBase<TIn, TOut, TInputDependencies, TName> {
|
|
190
|
+
connection: TKey;
|
|
191
|
+
connections?: never;
|
|
192
|
+
run: (input: z.infer<TIn>, ctx: ContextSingle) => Promise<z.infer<TOut>>;
|
|
193
|
+
}
|
|
194
|
+
interface DefineToolConfigMulti<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined, TName extends string, TSlots extends string, TKey extends string> extends DefineToolConfigBase<TIn, TOut, TInputDependencies, TName> {
|
|
195
|
+
connection?: never;
|
|
196
|
+
connections: Record<TSlots, TKey>;
|
|
197
|
+
run: (input: z.infer<TIn>, ctx: ContextMulti<TSlots>) => Promise<z.infer<TOut>>;
|
|
198
|
+
}
|
|
199
|
+
interface ToolDefinitionBase<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined, TName extends string> {
|
|
200
|
+
readonly kind: "tool";
|
|
201
|
+
readonly name: TName;
|
|
202
|
+
readonly title: string;
|
|
203
|
+
readonly description: string;
|
|
204
|
+
inputSchema: TIn;
|
|
205
|
+
outputSchema: TOut;
|
|
206
|
+
readonly annotations: _modelcontextprotocol_sdk_types_js.Tool["annotations"];
|
|
207
|
+
readonly statements: ReadonlyArray<Statement> | undefined;
|
|
208
|
+
inputDependencies: InputDependenciesValue<TInputDependencies>;
|
|
209
|
+
}
|
|
210
|
+
interface ToolDefinitionNone<TIn extends z.ZodType = z.ZodType, TOut extends z.ZodType = z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined = undefined, TName extends string = string> extends ToolDefinitionBase<TIn, TOut, TInputDependencies, TName> {
|
|
211
|
+
readonly connection?: undefined;
|
|
212
|
+
readonly connections?: undefined;
|
|
213
|
+
/** Author's run; the wrapper passes `input` directly. */
|
|
214
|
+
run: (input: z.infer<TIn>) => Promise<z.infer<TOut>>;
|
|
215
|
+
}
|
|
216
|
+
interface ToolDefinitionSingle<TIn extends z.ZodType = z.ZodType, TOut extends z.ZodType = z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined = undefined, TName extends string = string, TKey extends string = string> extends ToolDefinitionBase<TIn, TOut, TInputDependencies, TName> {
|
|
217
|
+
readonly connection: TKey;
|
|
218
|
+
readonly connections?: undefined;
|
|
219
|
+
run: (input: z.infer<TIn>, ctx: ContextSingle) => Promise<z.infer<TOut>>;
|
|
220
|
+
}
|
|
221
|
+
interface ToolDefinitionMulti<TIn extends z.ZodType = z.ZodType, TOut extends z.ZodType = z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined = undefined, TName extends string = string, TSlots extends string = string, TKey extends string = string> extends ToolDefinitionBase<TIn, TOut, TInputDependencies, TName> {
|
|
222
|
+
readonly connection?: undefined;
|
|
223
|
+
readonly connections: Record<TSlots, TKey>;
|
|
224
|
+
run: (input: z.infer<TIn>, ctx: ContextMulti<TSlots>) => Promise<z.infer<TOut>>;
|
|
225
|
+
}
|
|
226
|
+
type ToolDefinition<TIn extends z.ZodType = z.ZodType, TOut extends z.ZodType = z.ZodType, TInputDependencies extends AnyInputDependencies$1 | undefined = undefined, TName extends string = string, TSlots extends string = string, TKey extends string = string> = ToolDefinitionNone<TIn, TOut, TInputDependencies, TName> | ToolDefinitionSingle<TIn, TOut, TInputDependencies, TName, TKey> | ToolDefinitionMulti<TIn, TOut, TInputDependencies, TName, TSlots, TKey>;
|
|
227
|
+
/** Wide author-side `run` signature for polymorphic walks. */
|
|
228
|
+
type AnyToolDefinitionRun = (input: any, ctx?: any) => Promise<any>;
|
|
229
|
+
type AnyToolDefinition = Omit<ToolDefinitionBase<z.ZodType, z.ZodType, any, string>, "run" | "inputDependencies"> & {
|
|
230
|
+
inputDependencies: AnyInputDependencies$1 | undefined;
|
|
231
|
+
run: AnyToolDefinitionRun;
|
|
232
|
+
readonly connection?: string | undefined;
|
|
233
|
+
readonly connections?: Record<string, string> | undefined;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* SDK-shipped resolvers and the `defineConnectionResolver` typed-identity
|
|
238
|
+
* helper.
|
|
239
|
+
*
|
|
240
|
+
* Resolvers are plain data objects matching one of two shapes
|
|
241
|
+
* (`SingleConnectionResolver` / `MultiConnectionResolver`). They never
|
|
242
|
+
* see env-var names, programmatic-bag keys, slot names, or connection
|
|
243
|
+
* keys — the surface wrappers (`defineConnector`, `handleIfScriptMain`)
|
|
244
|
+
* compose those from the resolver's `name` and (optional) `keySuffixes`.
|
|
245
|
+
*/
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Typed identity helper for hand-rolled resolvers. Preserves the literal
|
|
249
|
+
* `name` (and `keySuffixes` for multi-credential resolvers) on the value's
|
|
250
|
+
* type so `defineConnector` can compose programmatic-bag key types via
|
|
251
|
+
* `EnvFromKey<TName>` template literals.
|
|
252
|
+
*
|
|
253
|
+
* Examples:
|
|
254
|
+
*
|
|
255
|
+
* const myToken = defineConnectionResolver({
|
|
256
|
+
* name: "api-key",
|
|
257
|
+
* resolve: (key) => ...,
|
|
258
|
+
* }); // -> SingleConnectionResolver<"api-key">
|
|
259
|
+
*
|
|
260
|
+
* const myHmac = defineConnectionResolver({
|
|
261
|
+
* name: "hmac",
|
|
262
|
+
* keySuffixes: ["KEY", "SECRET"] as const,
|
|
263
|
+
* resolve: ({ KEY, SECRET }) => ...,
|
|
264
|
+
* }); // -> MultiConnectionResolver<"hmac", readonly ["KEY", "SECRET"]>
|
|
265
|
+
*/
|
|
266
|
+
declare function defineConnectionResolver<T extends ConnectionResolver>(resolver: T): T;
|
|
267
|
+
/**
|
|
268
|
+
* Zapier-relayed resolver. Single-credential — operators set
|
|
269
|
+
* `<SLOT>_<CONNECTION_KEY>_ZAPIER_CONNECTION_ID` in env, and Relay derives
|
|
270
|
+
* the app from the connection itself (no app arg needed).
|
|
271
|
+
*
|
|
272
|
+
* Composition (single-slot):
|
|
273
|
+
*
|
|
274
|
+
* notion -> env: NOTION_ZAPIER_CONNECTION_ID programmatic: { ZAPIER_CONNECTION_ID }
|
|
275
|
+
* github -> env: GITHUB_ZAPIER_CONNECTION_ID programmatic: { ZAPIER_CONNECTION_ID }
|
|
276
|
+
* google-drive -> env: GOOGLE_DRIVE_ZAPIER_CONNECTION_ID programmatic: { ZAPIER_CONNECTION_ID }
|
|
277
|
+
*
|
|
278
|
+
* Composition (multi-slot):
|
|
279
|
+
*
|
|
280
|
+
* { source: "notion" } -> env: SOURCE_NOTION_ZAPIER_CONNECTION_ID
|
|
281
|
+
*
|
|
282
|
+
* Exported as a value (no parameters):
|
|
283
|
+
* `connectionResolvers: { notion: zapierConnectionResolver }`. The
|
|
284
|
+
* `@zapier/zapier-sdk` import stays lazy — it only loads when this
|
|
285
|
+
* resolver actually wins resolution at run time.
|
|
286
|
+
*/
|
|
287
|
+
declare const zapierConnectionResolver: SingleConnectionResolver<"zapier-connection-id">;
|
|
288
|
+
/**
|
|
289
|
+
* Bearer-token resolver: `Authorization: <scheme> <token>`.
|
|
290
|
+
* Single-credential.
|
|
291
|
+
*
|
|
292
|
+
* Default `name: "token"` so a single-resolver setup composes to
|
|
293
|
+
* `<SLOT>_<CONNECTION_KEY>_TOKEN` (env) and `{ TOKEN }` (programmatic),
|
|
294
|
+
* matching the dominant ecosystem convention (`NOTION_TOKEN`,
|
|
295
|
+
* `GITHUB_TOKEN`, `STRIPE_SECRET_KEY` patterns). Authors who prefer
|
|
296
|
+
* `<SLOT>_<KEY>_BEARER_TOKEN` (Twitter/X-style) override:
|
|
297
|
+
* `defineBearerTokenResolver({ name: "bearer-token" })`.
|
|
298
|
+
*
|
|
299
|
+
* The `name` literal flows through the generic so `RunOptions.connection`
|
|
300
|
+
* narrows to `{ [EnvFromKey<TName>]: string }`.
|
|
301
|
+
*
|
|
302
|
+
* `scheme` defaults to `"Bearer"`; pass `"token"` for GitHub-style PATs.
|
|
303
|
+
*/
|
|
304
|
+
declare function defineBearerTokenResolver<TName extends string = "token">(opts?: {
|
|
305
|
+
scheme?: string;
|
|
306
|
+
name?: TName;
|
|
307
|
+
}): SingleConnectionResolver<TName>;
|
|
308
|
+
|
|
309
|
+
/** Chat Completions registration surface shape (`chat.completions.create({ tools })`). */
|
|
310
|
+
interface ChatCompletionFunctionTool {
|
|
311
|
+
type: "function";
|
|
312
|
+
function: {
|
|
313
|
+
name: string;
|
|
314
|
+
description: string;
|
|
315
|
+
parameters: Record<string, unknown>;
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
/** Build the Chat Completions registration surface shape from a `ToolDefinition`. */
|
|
319
|
+
declare function toChatCompletionTool(definition: AnyToolDefinition): ChatCompletionFunctionTool;
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Config object passable to `McpServer.registerTool(name, config, cb)`.
|
|
323
|
+
*
|
|
324
|
+
* Mirrors the inline shape declared by `@modelcontextprotocol/sdk`'s
|
|
325
|
+
* `McpServer.registerTool` signature: Zod schemas (not JSON Schema) for
|
|
326
|
+
* `inputSchema` / `outputSchema`, `annotations`, and an optional `_meta`
|
|
327
|
+
* passthrough. `name` is omitted — `registerTool` takes it as the first
|
|
328
|
+
* positional argument, distinct from the config object.
|
|
329
|
+
*/
|
|
330
|
+
interface McpRegisterToolConfig {
|
|
331
|
+
title?: string;
|
|
332
|
+
description?: string;
|
|
333
|
+
inputSchema?: z.ZodType;
|
|
334
|
+
outputSchema?: z.ZodType;
|
|
335
|
+
annotations?: ToolAnnotations;
|
|
336
|
+
_meta?: Record<string, unknown>;
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Build the `McpServer.registerTool` config for a `ToolDefinition`.
|
|
340
|
+
*
|
|
341
|
+
* Use this with the modern high-level API:
|
|
342
|
+
*
|
|
343
|
+
* ```ts
|
|
344
|
+
* server.registerTool(script.name, notion.toMcpServerTool(script), cb);
|
|
345
|
+
* ```
|
|
346
|
+
*
|
|
347
|
+
* For the deprecated `Server.setRequestHandler(ListToolsRequestSchema, ...)`
|
|
348
|
+
* flow (or any consumer that wants a self-contained JSON-Schema'd descriptor),
|
|
349
|
+
* use `toMcpTool` instead.
|
|
350
|
+
*/
|
|
351
|
+
declare function toMcpServerTool(definition: AnyToolDefinition): McpRegisterToolConfig;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Build the wire-format MCP `Tool` descriptor for a `ToolDefinition`.
|
|
355
|
+
*
|
|
356
|
+
* Produces a JSON-Schema'd `Tool` suitable for the deprecated
|
|
357
|
+
* `Server.setRequestHandler(ListToolsRequestSchema, ...)` flow, plus any
|
|
358
|
+
* non-`McpServer` consumer that wants a self-contained tool descriptor
|
|
359
|
+
* (catalog APIs, OpenAPI bridges, `tools/list` proxies).
|
|
360
|
+
*
|
|
361
|
+
* For the modern `McpServer.registerTool(name, config, cb)` path, use
|
|
362
|
+
* `toMcpServerTool` instead — that helper keeps `inputSchema` / `outputSchema`
|
|
363
|
+
* as Zod and matches the inline config shape `registerTool` expects.
|
|
364
|
+
*/
|
|
365
|
+
declare function toMcpTool(definition: AnyToolDefinition): Tool;
|
|
366
|
+
|
|
367
|
+
/** Responses API registration surface shape (`responses.create({ tools })`). */
|
|
368
|
+
interface ResponsesFunctionTool {
|
|
369
|
+
type: "function";
|
|
370
|
+
name: string;
|
|
371
|
+
description: string;
|
|
372
|
+
parameters: Record<string, unknown>;
|
|
373
|
+
}
|
|
374
|
+
/** Build the Responses API registration surface shape from a `ToolDefinition`. */
|
|
375
|
+
declare function toResponsesTool(definition: AnyToolDefinition): ResponsesFunctionTool;
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* `defineConnector({ scripts, connectionResolvers })` — connector
|
|
379
|
+
* `index.ts` helper.
|
|
380
|
+
*
|
|
381
|
+
* Validates that every connection key referenced by any script's
|
|
382
|
+
* `connection` / `connections` fields exists in the supplied
|
|
383
|
+
* `connectionResolvers` map, and that every resolver name within each
|
|
384
|
+
* key's array is unique. Wraps each script's author-side `.run(input,
|
|
385
|
+
* ctx)` into a consumer-facing `.run(input, opts: RunOptions) =>
|
|
386
|
+
* Promise<output>`. The wrapped form is what consumers invoke; the
|
|
387
|
+
* registration helpers (`toMcpTool`, `toMcpServerTool`, etc.) and
|
|
388
|
+
* registration surfaces (per-script CLI, dispatcher, MCP stdio) all
|
|
389
|
+
* consume the wrapped scripts unchanged.
|
|
390
|
+
*/
|
|
391
|
+
|
|
392
|
+
type AnyDef = ToolDefinition<any, any, any, string, string, string>;
|
|
393
|
+
/**
|
|
394
|
+
* Map a script's `ToolDefinition` to its consumer-facing wrapped form,
|
|
395
|
+
* narrowed against the connector's `connectionResolvers` map. The wrapped
|
|
396
|
+
* `run` takes `(input, opts: RunOptions)`; `RunOptions.connection` /
|
|
397
|
+
* `RunOptions.connections.<slot>` is narrowed per the slot's resolver
|
|
398
|
+
* array via `ConnectionValueFor`.
|
|
399
|
+
*/
|
|
400
|
+
type WrappedScript<D extends AnyDef, TResolvers extends ConnectionResolversMap> = D extends ToolDefinitionNone<infer TIn, infer TOut, infer _TInputDependencies, infer _TName> ? Omit<D, "run"> & {
|
|
401
|
+
run: (input: z.infer<TIn>, opts?: RunOptionsNone) => Promise<z.infer<TOut>>;
|
|
402
|
+
} : D extends ToolDefinitionSingle<infer TIn, infer TOut, infer _TInputDependencies, infer _TName, infer TKey> ? Omit<D, "run"> & {
|
|
403
|
+
run: (input: z.infer<TIn>, opts: RunOptionsSingle<ConnectionValueForKey<TResolvers, TKey>>) => Promise<z.infer<TOut>>;
|
|
404
|
+
} : D extends ToolDefinitionMulti<infer TIn, infer TOut, infer _TInputDependencies, infer _TName, infer TSlots, infer TKey> ? Omit<D, "run"> & {
|
|
405
|
+
run: (input: z.infer<TIn>, opts: {
|
|
406
|
+
connection?: never;
|
|
407
|
+
connections: {
|
|
408
|
+
[S in TSlots]: ConnectionValueForKey<TResolvers, TKey>;
|
|
409
|
+
};
|
|
410
|
+
}) => Promise<z.infer<TOut>>;
|
|
411
|
+
} : never;
|
|
412
|
+
type WrappedScripts<TScripts extends Record<string, AnyDef>, TResolvers extends ConnectionResolversMap> = {
|
|
413
|
+
[K in keyof TScripts]: WrappedScript<TScripts[K], TResolvers>;
|
|
414
|
+
};
|
|
415
|
+
interface ConnectorDefinition<TScripts extends Record<string, AnyDef>, TResolvers extends ConnectionResolversMap> {
|
|
416
|
+
scripts: WrappedScripts<TScripts, TResolvers>;
|
|
417
|
+
/** The `connectionResolvers` map this connector was constructed with. */
|
|
418
|
+
connectionResolvers: TResolvers;
|
|
419
|
+
toMcpTool: typeof toMcpTool;
|
|
420
|
+
toMcpServerTool: typeof toMcpServerTool;
|
|
421
|
+
toChatCompletionTool: typeof toChatCompletionTool;
|
|
422
|
+
toResponsesTool: typeof toResponsesTool;
|
|
423
|
+
/**
|
|
424
|
+
* Build `RunOptions` for a wrapped script from a process-env bag.
|
|
425
|
+
* Convenience for long-running consumers that resolve credentials once
|
|
426
|
+
* and reuse the same opts for the life of the process.
|
|
427
|
+
*/
|
|
428
|
+
buildRunOptionsFromEnv: (script: AnyToolDefinition, env: Record<string, string | undefined>) => RunOptions;
|
|
429
|
+
}
|
|
430
|
+
interface DefineConnectorConfig<TScripts extends Record<string, AnyDef>, TResolvers extends ConnectionResolversMap> {
|
|
431
|
+
scripts: TScripts;
|
|
432
|
+
connectionResolvers?: TResolvers;
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Wrap every script in the bundle with the connector's resolvers, then
|
|
436
|
+
* attach the SDK helpers consumers reach for through the connector
|
|
437
|
+
* default export.
|
|
438
|
+
*/
|
|
439
|
+
declare function defineConnector<TScripts extends Record<string, AnyDef>, const TResolvers extends ConnectionResolversMap = ConnectionResolversMap>(config: DefineConnectorConfig<TScripts, TResolvers>): ConnectorDefinition<TScripts, TResolvers>;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* `defineTool({...})` — the *authoring* surface of `@zapier/connectors-sdk`.
|
|
443
|
+
*
|
|
444
|
+
* A script's single `export default defineTool({...})` produces a
|
|
445
|
+
* `ToolDefinition` value: metadata (`kind: "tool"`, schemas, optional
|
|
446
|
+
* `connection` / `connections` string keys, …) plus an author-side `run`
|
|
447
|
+
* — `(input)` for credential-free scripts, `(input, ctx)` for those that
|
|
448
|
+
* declare connections.
|
|
449
|
+
*
|
|
450
|
+
* `defineTool` is intentionally light: it does NOT translate
|
|
451
|
+
* `RunOptions` to `Context`, does NOT read `process.env`, and does NOT
|
|
452
|
+
* compose env-var names. All of that lives on the surface wrappers
|
|
453
|
+
* (`defineConnector`, `handleIfScriptMain`) where the connector's
|
|
454
|
+
* `connectionResolvers` are attached.
|
|
455
|
+
*
|
|
456
|
+
* Three overloads (none / single / multi) only differ in the
|
|
457
|
+
* `connection` / `connections` field shape and the `ctx` shape passed
|
|
458
|
+
* to `run`. The returned definition mirrors the input one-to-one.
|
|
459
|
+
*/
|
|
460
|
+
|
|
461
|
+
type AnyInputDependencies = Readonly<Record<string, unknown>>;
|
|
462
|
+
declare function defineTool<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies | undefined = undefined, const TName extends string = string>(config: DefineToolConfigNone<TIn, TOut, TInputDependencies, TName>): ToolDefinitionNone<TIn, TOut, TInputDependencies, TName>;
|
|
463
|
+
declare function defineTool<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies | undefined = undefined, const TName extends string = string, const TKey extends string = string>(config: DefineToolConfigSingle<TIn, TOut, TInputDependencies, TName, TKey>): ToolDefinitionSingle<TIn, TOut, TInputDependencies, TName, TKey>;
|
|
464
|
+
declare function defineTool<TIn extends z.ZodType, TOut extends z.ZodType, TInputDependencies extends AnyInputDependencies | undefined = undefined, const TName extends string = string, const TSlots extends string = string, const TKey extends string = string>(config: DefineToolConfigMulti<TIn, TOut, TInputDependencies, TName, TSlots, TKey>): ToolDefinitionMulti<TIn, TOut, TInputDependencies, TName, TSlots, TKey>;
|
|
465
|
+
|
|
466
|
+
/**
|
|
467
|
+
* `handleIfScriptMain(meta, definition, opts?)` — per-script CLI execution
|
|
468
|
+
* surface (not the connector bin).
|
|
469
|
+
*
|
|
470
|
+
* Each `apps/<app>/scripts/<name>.ts` ends with
|
|
471
|
+
* `await handleIfScriptMain(import.meta, definition, { connectionResolvers })`.
|
|
472
|
+
* When `import.meta.main` is true: parses argv/stdin, builds `RunOptions`
|
|
473
|
+
* from `process.env` via `buildRunOptionsFromEnv`, calls the wrapped
|
|
474
|
+
* script, writes JSON.
|
|
475
|
+
*
|
|
476
|
+
* **Credentials are env-only by design.** The CLI takes only positional
|
|
477
|
+
* JSON (the script's `inputSchema`) and `--help`. Passing secrets via
|
|
478
|
+
* argv would leak them through shell history, `ps`, audit logs, and CI
|
|
479
|
+
* runner echo.
|
|
480
|
+
*
|
|
481
|
+
* **Error handling.** The public entry catches any thrown error, writes
|
|
482
|
+
* it to stderr, and calls `process.exit(1)`. This mirrors the explicit
|
|
483
|
+
* catch+exit in `runDispatchCli`; see that function's header for the
|
|
484
|
+
* rationale (top-level-await rejection alone can be silently swallowed
|
|
485
|
+
* in practice, e.g. when a script's `run` chain triggers a lazy dynamic
|
|
486
|
+
* `import()` inside a dependency).
|
|
487
|
+
*
|
|
488
|
+
* `handleIfScriptMainBody` rethrows so it composes cleanly: the
|
|
489
|
+
* connector bin's `runDispatchCliBody` delegates to it after argv
|
|
490
|
+
* shifting, and the dispatcher's own public entry is responsible for
|
|
491
|
+
* the single `process.exit`. Advanced programmatic consumers that
|
|
492
|
+
* embed this surface get the raw error and decide what to do.
|
|
493
|
+
*/
|
|
494
|
+
|
|
495
|
+
interface HandleIfScriptMainOptions {
|
|
496
|
+
/**
|
|
497
|
+
* Required when the script declares any connection (`connection` /
|
|
498
|
+
* `connections`); omit for credential-free scripts. Same shape as
|
|
499
|
+
* `defineConnector`'s `connectionResolvers` — single resolver or
|
|
500
|
+
* ordered array per connection key.
|
|
501
|
+
*/
|
|
502
|
+
connectionResolvers?: ConnectionResolversMap;
|
|
503
|
+
}
|
|
504
|
+
declare function handleIfScriptMain<D extends AnyToolDefinition>(meta: ImportMeta, definition: D, opts?: HandleIfScriptMainOptions): Promise<void>;
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* `runDispatchCli(meta, connector)` — connector-bin execution surface.
|
|
508
|
+
*
|
|
509
|
+
* Each app's `apps/<app>/cli.ts` ends with `await runDispatchCli(import.meta,
|
|
510
|
+
* connector)` (where `connector` is the `defineConnector` result), which:
|
|
511
|
+
*
|
|
512
|
+
* 1. Returns early when imported as a module (`import.meta.main` is false).
|
|
513
|
+
* 2. Parses argv:
|
|
514
|
+
* - `<bin> run <script> [args...]` — dispatch to a script.
|
|
515
|
+
* - `<bin> mcp` — boot a local MCP stdio
|
|
516
|
+
* server exposing every
|
|
517
|
+
* script.
|
|
518
|
+
* - `<bin>`, `<bin> --help`, `<bin> -h` — usage to stdout.
|
|
519
|
+
* - `<bin> run <script> --help` — per-script help (input
|
|
520
|
+
* schema + required env
|
|
521
|
+
* vars per slot/resolver).
|
|
522
|
+
* - `<bin> mcp --help`, `<bin> mcp -h` — `mcp` primary-command
|
|
523
|
+
* help (what the server
|
|
524
|
+
* exposes + how to set
|
|
525
|
+
* env vars).
|
|
526
|
+
* 3. Delegates to `handleIfScriptMainBody` with shifted argv, so per-script
|
|
527
|
+
* semantics (positional JSON input, stdin fallback, `--help`) are
|
|
528
|
+
* identical to the per-script `node apps/<app>/scripts/<x>.ts`
|
|
529
|
+
* invocation.
|
|
530
|
+
*
|
|
531
|
+
* `run` is required to dispatch a script — there is no shorthand
|
|
532
|
+
* `<bin> <script>` form. This was deliberately removed (originally
|
|
533
|
+
* supported, reverted in favor of one canonical grammar) because the
|
|
534
|
+
* shorthand collided with primary commands (`mcp`), forced a
|
|
535
|
+
* reserved-name carve-out for script authors, and added a second shape
|
|
536
|
+
* for agents/humans to remember without paying for itself in keystrokes.
|
|
537
|
+
*
|
|
538
|
+
* Credentials are env-only: there are no credential CLI flags. Operators
|
|
539
|
+
* set the env vars listed by `--help`.
|
|
540
|
+
*
|
|
541
|
+
* **Error handling.** The public entry catches any thrown error from the
|
|
542
|
+
* dispatch body, writes it to stderr, and calls `process.exit(1)`.
|
|
543
|
+
* Relying on Node's top-level-await unhandled-rejection diagnostic is not
|
|
544
|
+
* enough in practice: when a script's `run` triggers a lazy dynamic
|
|
545
|
+
* `import()` deep in a dependency (e.g. `@zapier/zapier-sdk`'s lazy
|
|
546
|
+
* `@zapier/zapier-sdk-cli/login` probe) the rejection raced against
|
|
547
|
+
* pending microtasks can resolve to a silent `exit 0` with no output.
|
|
548
|
+
* The explicit `try`/`catch` + `process.exit` makes the error surface
|
|
549
|
+
* deterministic across Node versions and dependency-loading topologies.
|
|
550
|
+
*
|
|
551
|
+
* `process.exit` only happens at this single boundary. The lower-level
|
|
552
|
+
* `runDispatchCliBody` rethrows so its two real consumers — the public
|
|
553
|
+
* entry above and advanced programmatic consumers (cron wrappers,
|
|
554
|
+
* custom IO, the per-script `handleIfScriptMainBody` callee chain) —
|
|
555
|
+
* can format the error their own way before exiting (or not exiting at
|
|
556
|
+
* all, for embedded use).
|
|
557
|
+
*/
|
|
558
|
+
|
|
559
|
+
type AnyConnector = ConnectorDefinition<any, any>;
|
|
560
|
+
/** Public: thin guard + delegate. App `cli.ts` files call this. */
|
|
561
|
+
declare function runDispatchCli(meta: ImportMeta, connector: AnyConnector): Promise<void>;
|
|
562
|
+
|
|
563
|
+
type AnyScripts = Record<string, any>;
|
|
564
|
+
type NamedFunctions<TScripts extends AnyScripts> = {
|
|
565
|
+
[K in keyof TScripts]: TScripts[K]["run"];
|
|
566
|
+
};
|
|
567
|
+
/**
|
|
568
|
+
* Map each wrapped script in a connector definition to its `.run`
|
|
569
|
+
* function, so a connector's package can `export const { search,
|
|
570
|
+
* createDatabaseItem } = toFunctions(connector)`. Each export is the
|
|
571
|
+
* consumer-facing `(input, opts: RunOptions) => Promise<output>`.
|
|
572
|
+
*/
|
|
573
|
+
declare function toFunctions<TScripts extends AnyScripts, TResolvers extends ConnectionResolversMap>(connector: ConnectorDefinition<TScripts, TResolvers>): NamedFunctions<ConnectorDefinition<TScripts, TResolvers>["scripts"]>;
|
|
574
|
+
|
|
575
|
+
export { type AnyToolDefinition, type ConnectorDefinition, type RunOptions, defineBearerTokenResolver, defineConnectionResolver, defineConnector, defineTool, handleIfScriptMain, runDispatchCli, toFunctions, zapierConnectionResolver };
|