@todoist/mcp-core 1.2.0 → 1.4.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/README.md CHANGED
@@ -23,17 +23,17 @@ The MCP SDK and Zod are peer dependencies, so the server and the core share one
23
23
 
24
24
  ## What's in it
25
25
 
26
- | Module | Key exports |
27
- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
28
- | `tool` | `McpTool`, `AnyMcpTool`, `ToolResult`, `ExecuteResult`, `RequiredToolAnnotations`, `ToolSchemaSource` (types) |
29
- | `register-tool` | `createToolRegistrar`, `ToolRegistrarOptions`, `RegisterTool`, `ToolFailure` |
30
- | `tool-output` | `getToolOutput`, `getErrorOutput`, `ToolOutputOptions` |
31
- | `sanitize` | `removeNullFields`, `RemoveNullFieldsOptions` |
32
- | `tool-schemas` | `getToolSchemas`, `ToolSchemas` |
33
- | `annotations` | `getMcpAnnotations`, `formatToolTitle` |
34
- | `app-meta` | `normalizeToolMeta` |
35
- | `host` (subpath) | `validateBearerToken`, `ValidateBearerTokenOptions`, `TokenValidationError`, `TokenValidationResult`, `ValidateToken`, `McpServerOptions`, `isTransientValidationError`, `buildBearerChallenge`, `createTokenVerdictCache` |
36
- | `testing` (subpath) | `describeOutputSchemaNullability`, `describeAdvertisedToolSchemas`, `describeToolAnnotations`, `describeTokenFootprint`, `measureTokenFootprint`, `findNullablePaths`, `listAdvertisedTools` |
26
+ | Module | Key exports |
27
+ | ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
28
+ | `tool` | `McpTool`, `AnyMcpTool`, `ToolResult`, `ExecuteResult`, `ToolRequest`, `Confirmation`, `InputRequiredToolResult`, `RequiredToolAnnotations`, `ToolSchemaSource` (types) |
29
+ | `register-tool` | `createToolRegistrar`, `ToolRegistrarOptions`, `RegisterTool`, `ToolFailure` |
30
+ | `tool-output` | `getToolOutput`, `getErrorOutput`, `ToolOutputOptions` |
31
+ | `sanitize` | `removeNullFields`, `RemoveNullFieldsOptions` |
32
+ | `tool-schemas` | `getToolSchemas`, `ToolSchemas` |
33
+ | `annotations` | `getMcpAnnotations`, `formatToolTitle` |
34
+ | `app-meta` | `normalizeToolMeta` |
35
+ | `host` (subpath) | `validateBearerToken`, `ValidateBearerTokenOptions`, `TokenValidationError`, `TokenValidationResult`, `ValidateToken`, `McpServerOptions`, `isTransientValidationError`, `getValidationRateLimit`, `ValidationRateLimit`, `buildBearerChallenge`, `createTokenVerdictCache` |
36
+ | `testing` (subpath) | `describeOutputSchemaNullability`, `describeAdvertisedToolSchemas`, `describeToolAnnotations`, `describeTokenFootprint`, `measureTokenFootprint`, `findNullablePaths`, `listAdvertisedTools` |
37
37
 
38
38
  ## Usage
39
39
 
@@ -100,11 +100,37 @@ For each tool this:
100
100
  - compiles its input and output schemas to JSON Schema 2020-12 once per process, however many servers register it;
101
101
  - titles it "`<titlePrefix>`: `<Title Case Name>`" and defaults `openWorldHint` to false, with the tool's own annotations winning;
102
102
  - fills in both forms of an MCP Apps UI resource (`_meta.ui.resourceUri` and `_meta['ui/resourceUri']`) when the tool sets one;
103
- - shapes every result with `getToolOutput`, below;
103
+ - passes `execute` a `ToolRequest` as its third argument, below;
104
+ - shapes every result with `getToolOutput`, below, except a question for the user (`inputRequired`), which goes out as is;
104
105
  - catches anything `execute` throws, passes it to `onError`, and returns `formatError`'s text to the model as an `isError` result.
105
106
 
106
107
  `createContext` receives the tool and the SDK's `ServerContext`, so a long-running tool can read `serverContext.mcpReq.signal` or send progress notifications.
107
108
 
109
+ ### Asking the user before acting
110
+
111
+ A tool about to do something that cannot be undone can ask the user first. `request.confirm` resolves to where the question stands. `message` is a string, or a function when building it costs a lookup:
112
+
113
+ ```ts
114
+ async execute({ id }, api, request?) {
115
+ const confirmation = (await request?.confirm({
116
+ key: 'delete',
117
+ // Built only when the question is asked: not on the retry carrying the answer, nor for
118
+ // a client that cannot ask.
119
+ message: async () => `Delete "${(await api.get(id)).title}"? It cannot be restored.`,
120
+ })) ?? { status: 'unavailable' }
121
+ if (confirmation.status === 'asking') return confirmation.result
122
+ if (confirmation.status === 'declined') return { textContent: `Kept note ${id}.` }
123
+ await api.delete(id) // 'confirmed', or 'unavailable': the client cannot ask
124
+ return { textContent: `Deleted note ${id}.` }
125
+ }
126
+ ```
127
+
128
+ - `asking`: return `result`. The client shows the question as a form with one ticked "Confirm" box, then calls the tool again with the same arguments and the answer. This works on both protocol revisions: on 2026-07-28 the client sends the call again itself; on 2025-11-25 the SDK asks the client and runs the tool again.
129
+ - `confirmed` or `declined`: the call carries the answer. Declining, dismissing the form, or unticking the box all count as declined.
130
+ - `unavailable`: the client declared no form elicitation, the request is a stateless 2025-11-25 one whose server never saw the client's capabilities, or the tool was called directly rather than over MCP (`request` is then absent). Asking anyway would fail the call, so the tool decides as it would have without asking.
131
+
132
+ Declare `request` optional (`request?`), so the tool can still be called with two arguments. Give each distinct question its own `key` (such as the item's ID when confirming several in one run): the answer is found by key, so a reused key would act on later items without asking. The tool runs from the top again once the user answers, so nothing may be written before `confirm`. The answer comes from the client: it stops a model acting alone, but is not proof the user saw the question. A `wrapExecute` that rewrites results must keep `inputRequired`.
133
+
108
134
  ### The wire shape of a result
109
135
 
110
136
  `getToolOutput` turns a `ToolResult` into a `CallToolResult`:
@@ -129,23 +155,24 @@ export const validateNotesToken: ValidateToken = (token, baseUrl = 'https://note
129
155
  validateBearerToken({ url: `${baseUrl}/api/v1/me`, token })
130
156
  ```
131
157
 
132
- | The API answers | `validateBearerToken` |
133
- | ------------------------------------- | --------------------------------------------------------------------------------------------- |
134
- | 2xx | `valid` |
135
- | 401 | `invalid`: the host sends a 401 `invalid_token` challenge, and the client refreshes its token |
136
- | 403 | `forbidden`, or whatever `classifyForbidden(response)` returns |
137
- | any other status, a redirect included | throws `TokenValidationError`, whose `httpStatusCode` lets the host treat a 5xx as transient |
138
- | nothing within `timeoutMs` (5s) | rejects with the `TimeoutError` |
139
- | a network failure | rejects with the error unchanged |
158
+ | The API answers | `validateBearerToken` |
159
+ | ------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
160
+ | 2xx | `valid` |
161
+ | 401 | `invalid`: the host sends a 401 `invalid_token` challenge, and the client refreshes its token |
162
+ | 403 | `forbidden`, or whatever `classifyForbidden(response)` returns |
163
+ | any other status, a redirect included | throws `TokenValidationError`, whose `httpStatusCode` lets the host treat a 5xx as transient and a 429 as rate-limited, with the `Retry-After` as `retryAfterSeconds` |
164
+ | nothing within `timeoutMs` (5s) | rejects with the `TimeoutError` |
165
+ | a network failure | rejects with the error unchanged |
140
166
 
141
167
  The body is drained unread, so a change to the response's shape can't misclassify a token and the connection goes back to the pool. Redirects aren't followed: a sign-in page that answers 200 would otherwise pass for an accepted token. Pass `classifyForbidden` when a 403 can also mean a refusal that consent can't lift, `headers` to tag the request, and `fetch` when the server's API client has its own transport (a proxy from `HTTPS_PROXY`, say), so validation goes the same way as every other call.
142
168
 
143
- On the host's side, three helpers turn a validator's answer into a response, whatever the HTTP framework:
169
+ On the host's side, four helpers turn a validator's answer into a response, whatever the HTTP framework:
144
170
 
145
171
  ```ts
146
172
  import {
147
173
  buildBearerChallenge,
148
174
  createTokenVerdictCache,
175
+ getValidationRateLimit,
149
176
  isTransientValidationError,
150
177
  } from '@todoist/mcp-core/host'
151
178
 
@@ -157,6 +184,12 @@ async function checkToken(token: string, baseUrl: string): Promise<Response | 'p
157
184
  try {
158
185
  result = await validateNotesToken(token, baseUrl)
159
186
  } catch (error) {
187
+ const rateLimit = getValidationRateLimit(error)
188
+ if (rateLimit) {
189
+ // Never cached, and not let through: the same limit would refuse the request.
190
+ const retryAfter = String(rateLimit.retryAfterSeconds ?? 60)
191
+ return new Response(null, { status: 429, headers: { 'Retry-After': retryAfter } })
192
+ }
160
193
  if (isTransientValidationError(error)) return 'proceed' // or a 503; never cached
161
194
  throw error
162
195
  }
@@ -178,7 +211,8 @@ async function checkToken(token: string, baseUrl: string): Promise<Response | 'p
178
211
  }
179
212
  ```
180
213
 
181
- - `isTransientValidationError`: a timeout, a dropped or refused connection, or a 5xx, the rejections worth retrying. A redirect or a 404 is not.
214
+ - `isTransientValidationError`: a timeout, a dropped or refused connection, or a 5xx, the rejections worth retrying. A redirect, a 404 or a 429 is not.
215
+ - `getValidationRateLimit`: for a 429, the API rate-limiting the token's user, `{ retryAfterSeconds }` from its `Retry-After` (undefined when it sent none, so the host picks a wait); undefined for anything else. Answer 429 with `Retry-After` rather than letting the request through, where the same limit would refuse it again.
182
216
  - `buildBearerChallenge`: `Bearer`, with `error="…"`, `resource_metadata="…"` and `scope="…"` (from `scopes`, space-separated) when given. Pass `error: 'invalid_token'` on a 401 for a token that was sent and refused, `'insufficient_scope'` on a 403, and no `error` when the request carried no token (RFC 6750 §3.1). MCP clients request the challenge's `scope` ahead of the metadata's `scopes_supported`, so pass the resource's scopes to help a client that never reads the metadata.
183
217
  - `createTokenVerdictCache`: verdicts keyed by token and base URL, kept for `ttlMs`, the oldest dropped when full.
184
218
 
@@ -1,5 +1,6 @@
1
1
  export { buildBearerChallenge, type BearerChallengeOptions } from './challenge.js';
2
2
  export type { McpServerOptions, TokenValidationResult, ValidateToken } from './server-contract.js';
3
+ export { getValidationRateLimit, type ValidationRateLimit } from './rate-limit.js';
3
4
  export { isTransientValidationError } from './transient.js';
4
5
  export { TokenValidationError, validateBearerToken, type ValidateBearerTokenOptions, } from './validate-bearer-token.js';
5
6
  export { createTokenVerdictCache, type TokenVerdictCache, type TokenVerdictCacheOptions, } from './verdict-cache.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,KAAK,sBAAsB,EAAE,MAAM,gBAAgB,CAAA;AAClF,YAAY,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAClG,OAAO,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAC3D,OAAO,EACH,oBAAoB,EACpB,mBAAmB,EACnB,KAAK,0BAA0B,GAClC,MAAM,4BAA4B,CAAA;AACnC,OAAO,EACH,uBAAuB,EACvB,KAAK,iBAAiB,EACtB,KAAK,wBAAwB,GAChC,MAAM,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,KAAK,sBAAsB,EAAE,MAAM,gBAAgB,CAAA;AAClF,YAAY,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AAClG,OAAO,EAAE,sBAAsB,EAAE,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAClF,OAAO,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAC3D,OAAO,EACH,oBAAoB,EACpB,mBAAmB,EACnB,KAAK,0BAA0B,GAClC,MAAM,4BAA4B,CAAA;AACnC,OAAO,EACH,uBAAuB,EACvB,KAAK,iBAAiB,EACtB,KAAK,wBAAwB,GAChC,MAAM,oBAAoB,CAAA"}
@@ -1,4 +1,5 @@
1
1
  export { buildBearerChallenge } from './challenge.js';
2
+ export { getValidationRateLimit } from './rate-limit.js';
2
3
  export { isTransientValidationError } from './transient.js';
3
4
  export { TokenValidationError, validateBearerToken, } from './validate-bearer-token.js';
4
5
  export { createTokenVerdictCache, } from './verdict-cache.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAA+B,MAAM,gBAAgB,CAAA;AAElF,OAAO,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAC3D,OAAO,EACH,oBAAoB,EACpB,mBAAmB,GAEtB,MAAM,4BAA4B,CAAA;AACnC,OAAO,EACH,uBAAuB,GAG1B,MAAM,oBAAoB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/host/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAA+B,MAAM,gBAAgB,CAAA;AAElF,OAAO,EAAE,sBAAsB,EAA4B,MAAM,iBAAiB,CAAA;AAClF,OAAO,EAAE,0BAA0B,EAAE,MAAM,gBAAgB,CAAA;AAC3D,OAAO,EACH,oBAAoB,EACpB,mBAAmB,GAEtB,MAAM,4BAA4B,CAAA;AACnC,OAAO,EACH,uBAAuB,GAG1B,MAAM,oBAAoB,CAAA"}
@@ -0,0 +1,16 @@
1
+ /** How a host answers a validation the API refused with a 429. */
2
+ export type ValidationRateLimit = {
3
+ /** The API's `Retry-After`, in seconds. Undefined when it sent none, so the host picks one. */
4
+ retryAfterSeconds: number | undefined;
5
+ };
6
+ /**
7
+ * Whether a `ValidateToken` rejection is the API rate-limiting the token's user (a 429), and if
8
+ * so how long the API asked the client to wait. Undefined for any other rejection.
9
+ *
10
+ * A 429 is no verdict on the token, and retrying it at once only spends more of the user's
11
+ * limit, so it is not transient either. A host answers 429 with `Retry-After` rather than
12
+ * letting the request through, where the same limit would refuse it again, and never caches it
13
+ * as a verdict.
14
+ */
15
+ export declare function getValidationRateLimit(error: unknown): ValidationRateLimit | undefined;
16
+ //# sourceMappingURL=rate-limit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limit.d.ts","sourceRoot":"","sources":["../../src/host/rate-limit.ts"],"names":[],"mappings":"AAEA,kEAAkE;AAClE,MAAM,MAAM,mBAAmB,GAAG;IAC9B,+FAA+F;IAC/F,iBAAiB,EAAE,MAAM,GAAG,SAAS,CAAA;CACxC,CAAA;AAED;;;;;;;;GAQG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG,mBAAmB,GAAG,SAAS,CAStF"}
@@ -0,0 +1,21 @@
1
+ import { statusOf } from './transient.js';
2
+ /**
3
+ * Whether a `ValidateToken` rejection is the API rate-limiting the token's user (a 429), and if
4
+ * so how long the API asked the client to wait. Undefined for any other rejection.
5
+ *
6
+ * A 429 is no verdict on the token, and retrying it at once only spends more of the user's
7
+ * limit, so it is not transient either. A host answers 429 with `Retry-After` rather than
8
+ * letting the request through, where the same limit would refuse it again, and never caches it
9
+ * as a verdict.
10
+ */
11
+ export function getValidationRateLimit(error) {
12
+ if (statusOf(error) !== 429)
13
+ return undefined;
14
+ const retryAfter = error.retryAfterSeconds;
15
+ return {
16
+ retryAfterSeconds: typeof retryAfter === 'number' && Number.isFinite(retryAfter) && retryAfter >= 0
17
+ ? retryAfter
18
+ : undefined,
19
+ };
20
+ }
21
+ //# sourceMappingURL=rate-limit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limit.js","sourceRoot":"","sources":["../../src/host/rate-limit.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAA;AAQzC;;;;;;;;GAQG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAc;IACjD,IAAI,QAAQ,CAAC,KAAK,CAAC,KAAK,GAAG;QAAE,OAAO,SAAS,CAAA;IAC7C,MAAM,UAAU,GAAI,KAAyC,CAAC,iBAAiB,CAAA;IAC/E,OAAO;QACH,iBAAiB,EACb,OAAO,UAAU,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,UAAU,IAAI,CAAC;YAC5E,CAAC,CAAC,UAAU;YACZ,CAAC,CAAC,SAAS;KACtB,CAAA;AACL,CAAC"}
@@ -14,8 +14,8 @@ export type TokenValidationResult = 'valid' | 'invalid' | 'forbidden';
14
14
  *
15
15
  * Resolves only with a verdict on the token. Anything else rejects: a `TokenValidationError`
16
16
  * for an unexpected status, or the network or timeout error unchanged. A host can then tell a
17
- * failure worth retrying (a 5xx, a timeout, a dropped connection) from a verdict, and never
18
- * caches one as the other.
17
+ * failure worth retrying (a 5xx, a timeout, a dropped connection) and a rate-limited user (a
18
+ * 429) from a verdict, and never caches either as one.
19
19
  */
20
20
  export type ValidateToken = (token: string, baseUrl?: string) => Promise<TokenValidationResult>;
21
21
  /**
@@ -1,9 +1,14 @@
1
+ /**
2
+ * The HTTP status a rejection carries, read off any object: `TokenValidationError`'s
3
+ * `httpStatusCode`, or a `statusCode`. Undefined for anything that is not an object.
4
+ */
5
+ export declare function statusOf(error: unknown): number | undefined;
1
6
  /**
2
7
  * Whether a `ValidateToken` rejection is worth retrying rather than a fault to report: a timeout,
3
8
  * a dropped or refused connection (by its `code`, on the error or on its `cause`), or a 5xx
4
9
  * (`TokenValidationError` carries it as `httpStatusCode`). A host can then let the request through or answer 503, and never caches
5
10
  * the failure as a verdict. Anything else, such as a redirect or a 404 from a misconfigured base
6
- * URL, is not transient.
11
+ * URL, is not transient. Nor is a 429, which `getValidationRateLimit` reads instead.
7
12
  */
8
13
  export declare function isTransientValidationError(error: unknown): boolean;
9
14
  //# sourceMappingURL=transient.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"transient.d.ts","sourceRoot":"","sources":["../../src/host/transient.ts"],"names":[],"mappings":"AAsBA;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAoBlE"}
1
+ {"version":3,"file":"transient.d.ts","sourceRoot":"","sources":["../../src/host/transient.ts"],"names":[],"mappings":"AAeA;;;GAGG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAM3D;AAED;;;;;;GAMG;AACH,wBAAgB,0BAA0B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAgBlE"}
@@ -11,7 +11,13 @@ const TRANSIENT_CODES = new Set([
11
11
  function codeOf(value) {
12
12
  return value?.code;
13
13
  }
14
- function statusOf(error) {
14
+ /**
15
+ * The HTTP status a rejection carries, read off any object: `TokenValidationError`'s
16
+ * `httpStatusCode`, or a `statusCode`. Undefined for anything that is not an object.
17
+ */
18
+ export function statusOf(error) {
19
+ if (error === null || typeof error !== 'object')
20
+ return undefined;
15
21
  const record = error;
16
22
  if (typeof record.httpStatusCode === 'number')
17
23
  return record.httpStatusCode;
@@ -24,7 +30,7 @@ function statusOf(error) {
24
30
  * a dropped or refused connection (by its `code`, on the error or on its `cause`), or a 5xx
25
31
  * (`TokenValidationError` carries it as `httpStatusCode`). A host can then let the request through or answer 503, and never caches
26
32
  * the failure as a verdict. Anything else, such as a redirect or a 404 from a misconfigured base
27
- * URL, is not transient.
33
+ * URL, is not transient. Nor is a 429, which `getValidationRateLimit` reads instead.
28
34
  */
29
35
  export function isTransientValidationError(error) {
30
36
  if (error instanceof Error) {
@@ -40,11 +46,7 @@ export function isTransientValidationError(error) {
40
46
  if (error.message.includes('fetch failed'))
41
47
  return true;
42
48
  }
43
- if (error !== null && typeof error === 'object') {
44
- const status = statusOf(error);
45
- if (status !== undefined && status >= 500 && status <= 599)
46
- return true;
47
- }
48
- return false;
49
+ const status = statusOf(error);
50
+ return status !== undefined && status >= 500 && status <= 599;
49
51
  }
50
52
  //# sourceMappingURL=transient.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"transient.js","sourceRoot":"","sources":["../../src/host/transient.ts"],"names":[],"mappings":"AAAA,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC;IAC5B,cAAc;IACd,YAAY;IACZ,WAAW;IACX,WAAW;IACX,WAAW;IACX,OAAO;IACP,yBAAyB;IACzB,gBAAgB;CACnB,CAAC,CAAA;AAEF,SAAS,MAAM,CAAC,KAAc;IAC1B,OAAQ,KAAwC,EAAE,IAAI,CAAA;AAC1D,CAAC;AAED,SAAS,QAAQ,CAAC,KAAa;IAC3B,MAAM,MAAM,GAAG,KAAgC,CAAA;IAC/C,IAAI,OAAO,MAAM,CAAC,cAAc,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,cAAc,CAAA;IAC3E,IAAI,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,UAAU,CA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,UAAU,QAAQ,CAAC,KAAc;IACnC,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAA;IACjE,MAAM,MAAM,GAAG,KAAgC,CAAA;IAC/C,IAAI,OAAO,MAAM,CAAC,cAAc,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,cAAc,CAAA;IAC3E,IAAI,OAAO,MAAM,CAAC,UAAU,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC,UAAU,CAAA;IACnE,OAAO,SAAS,CAAA;AACpB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,0BAA0B,CAAC,KAAc;IACrD,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QACzB,IAAI,KAAK,CAAC,IAAI,KAAK,cAAc,IAAI,KAAK,CAAC,IAAI,KAAK,YAAY;YAAE,OAAO,IAAI,CAAA;QAE7E,2FAA2F;QAC3F,gCAAgC;QAChC,KAAK,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;YACtD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC;gBAAE,OAAO,IAAI,CAAA;QAC1E,CAAC;QAED,uFAAuF;QACvF,IAAI,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC;YAAE,OAAO,IAAI,CAAA;IAC3D,CAAC;IAED,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;IAC9B,OAAO,MAAM,KAAK,SAAS,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,IAAI,GAAG,CAAA;AACjE,CAAC"}
@@ -2,11 +2,15 @@ import type { TokenValidationResult } from './server-contract.js';
2
2
  /**
3
3
  * The validation call got an answer that is no verdict on the token: a 5xx, a 429, a redirect,
4
4
  * or a 404 from a misconfigured base URL. `httpStatusCode` is what a host reads to treat a 5xx
5
- * as transient.
5
+ * as transient and a 429 as the token's user being rate-limited. `retryAfterSeconds` is the
6
+ * response's `Retry-After`, when it sent one that can be read.
6
7
  */
7
8
  export declare class TokenValidationError extends Error {
8
9
  readonly httpStatusCode: number;
9
- constructor(httpStatusCode: number);
10
+ readonly retryAfterSeconds: number | undefined;
11
+ constructor(httpStatusCode: number, { retryAfterSeconds }?: {
12
+ retryAfterSeconds?: number;
13
+ });
10
14
  }
11
15
  export type ValidateBearerTokenOptions = {
12
16
  /** An endpoint every usable token can read, ideally the cheapest one. */
@@ -36,8 +40,8 @@ export type ValidateBearerTokenOptions = {
36
40
  * Redirects are not followed: a 302 to a sign-in page that anyone can load would otherwise
37
41
  * come back 200 and pass for an accepted token.
38
42
  *
39
- * @throws {TokenValidationError} for any other status, a redirect included. Network and timeout
40
- * errors propagate unchanged.
43
+ * @throws {TokenValidationError} for any other status, a redirect included, carrying the
44
+ * response's `Retry-After`. Network and timeout errors propagate unchanged.
41
45
  */
42
46
  export declare function validateBearerToken({ url, token, headers, timeoutMs, classifyForbidden, fetch: send, }: ValidateBearerTokenOptions): Promise<TokenValidationResult>;
43
47
  //# sourceMappingURL=validate-bearer-token.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validate-bearer-token.d.ts","sourceRoot":"","sources":["../../src/host/validate-bearer-token.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAA;AAIjE;;;;GAIG;AACH,qBAAa,oBAAqB,SAAQ,KAAK;IAC3C,QAAQ,CAAC,cAAc,EAAE,MAAM,CAAA;IAE/B,YAAY,cAAc,EAAE,MAAM,EAIjC;CACJ;AAED,MAAM,MAAM,0BAA0B,GAAG;IACrC,yEAAyE;IACzE,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;IACjB,KAAK,EAAE,MAAM,CAAA;IACb,uEAAuE;IACvE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE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QAAQ,KAAK,qBAAqB,CAAA;IACjE;;;;OAIG;IACH,KAAK,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,EAAE,IAAI,EAAE,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAA;CACtE,CAAA;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,mBAAmB,CAAC,EACtC,GAAG,EACH,KAAK,EACL,OAAO,EACP,SAA8B,EAC9B,iBAAqC,EACrC,KAAK,EAAE,IAAY,GACtB,EAAE,0BAA0B,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAoB7D"}
@@ -2,16 +2,38 @@ const DEFAULT_TIMEOUT_MS = 5_000;
2
2
  /**
3
3
  * The validation call got an answer that is no verdict on the token: a 5xx, a 429, a redirect,
4
4
  * or a 404 from a misconfigured base URL. `httpStatusCode` is what a host reads to treat a 5xx
5
- * as transient.
5
+ * as transient and a 429 as the token's user being rate-limited. `retryAfterSeconds` is the
6
+ * response's `Retry-After`, when it sent one that can be read.
6
7
  */
7
8
  export class TokenValidationError extends Error {
8
9
  httpStatusCode;
9
- constructor(httpStatusCode) {
10
+ retryAfterSeconds;
11
+ constructor(httpStatusCode, { retryAfterSeconds } = {}) {
10
12
  super(`Token validation failed with status ${httpStatusCode}`);
11
13
  this.name = 'TokenValidationError';
12
14
  this.httpStatusCode = httpStatusCode;
15
+ this.retryAfterSeconds = retryAfterSeconds;
13
16
  }
14
17
  }
18
+ /**
19
+ * The seconds a `Retry-After` header asks for (RFC 9110 section 10.2.3): its delay-seconds, or the
20
+ * time until its HTTP date, never below 0. Undefined when the header is missing or unreadable.
21
+ */
22
+ function parseRetryAfter(value) {
23
+ if (value === null)
24
+ return undefined;
25
+ const trimmed = value.trim();
26
+ if (/^\d+$/.test(trimmed))
27
+ return Number(trimmed);
28
+ // An HTTP date always names its day and month. Without a letter, Date.parse would read a
29
+ // stray number such as "-5" as a year.
30
+ if (!/[a-z]/i.test(trimmed))
31
+ return undefined;
32
+ const date = Date.parse(trimmed);
33
+ if (Number.isNaN(date))
34
+ return undefined;
35
+ return Math.max(0, Math.ceil((date - Date.now()) / 1000));
36
+ }
15
37
  /**
16
38
  * Validates a bearer token with one status-only `GET`: 2xx is `valid`, 401 `invalid`, 403
17
39
  * `forbidden` (or whatever `classifyForbidden` says). The body is drained unread, so a change
@@ -20,8 +42,8 @@ export class TokenValidationError extends Error {
20
42
  * Redirects are not followed: a 302 to a sign-in page that anyone can load would otherwise
21
43
  * come back 200 and pass for an accepted token.
22
44
  *
23
- * @throws {TokenValidationError} for any other status, a redirect included. Network and timeout
24
- * errors propagate unchanged.
45
+ * @throws {TokenValidationError} for any other status, a redirect included, carrying the
46
+ * response's `Retry-After`. Network and timeout errors propagate unchanged.
25
47
  */
26
48
  export async function validateBearerToken({ url, token, headers, timeoutMs = DEFAULT_TIMEOUT_MS, classifyForbidden = () => 'forbidden', fetch: send = fetch, }) {
27
49
  const requestHeaders = new Headers(headers);
@@ -41,6 +63,8 @@ export async function validateBearerToken({ url, token, headers, timeoutMs = DEF
41
63
  return 'invalid';
42
64
  if (response.status === 403)
43
65
  return classifyForbidden(response);
44
- throw new TokenValidationError(response.status);
66
+ throw new TokenValidationError(response.status, {
67
+ retryAfterSeconds: parseRetryAfter(response.headers.get('retry-after')),
68
+ });
45
69
  }
46
70
  //# sourceMappingURL=validate-bearer-token.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"validate-bearer-token.js","sourceRoot":"","sources":["../../src/host/validate-bearer-token.ts"],"names":[],"mappings":"AAEA,MAAM,kBAAkB,GAAG,KAAK,CAAA;AAEhC;;;;GAIG;AACH,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IAClC,cAAc,CAAQ;IAE/B,YAAY,cAAsB;QAC9B,KAAK,CAAC,uCAAuC,cAAc,EAAE,CAAC,CAAA;QAC9D,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAA;QAClC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAA;IACxC,CAAC;CACJ;AAu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C,CAAA;IAEtD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,GAAG,EAAE;QAC7B,OAAO,EAAE,cAAc;QACvB,QAAQ,EAAE,QAAQ;QAClB,MAAM,EAAE,WAAW,CAAC,OAAO,CAAC,SAAS,CAAC;KACzC,CAAC,CAAA;IACF,4FAA4F;IAC5F,8FAA8F;IAC9F,sEAAsE;IACtE,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,cAAc,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAA;IAExE,IAAI,QAAQ,CAAC,EAAE;QAAE,OAAO,OAAO,CAAA;IAC/B,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG;QAAE,OAAO,SAAS,CAAA;IAC7C,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG;QAAE,OAAO,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IAC/D,MAAM,IAAI,oBAAoB,CAAC,QAAQ,CAAC,MAAM,EAAE;QAC5C,iBAAiB,EAAE,eAAe,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;KAC1E,CAAC,CAAA;AACN,CAAC"}
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { formatToolTitle, getMcpAnnotations } from './annotations.js';
2
2
  export { normalizeToolMeta } from './app-meta.js';
3
3
  export { createToolRegistrar, type RegisterTool, type ToolFailure, type ToolRegistrarOptions, } from './register-tool.js';
4
4
  export { removeNullFields, type RemoveNullFieldsOptions } from './sanitize.js';
5
- export type { AnyMcpTool, ExecuteResult, McpTool, RequiredToolAnnotations, ToolResult, ToolSchemaSource, } from './tool.js';
5
+ export type { AnyMcpTool, Confirmation, ExecuteResult, InputRequiredToolResult, McpTool, RequiredToolAnnotations, ToolRequest, ToolResult, ToolSchemaSource, } from './tool.js';
6
6
  export { getErrorOutput, getToolOutput, type ToolOutputOptions } from './tool-output.js';
7
7
  export { getToolSchemas, type ToolSchemas } from './tool-schemas.js';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EACH,mBAAmB,EACnB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,oBAAoB,GAC5B,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAC9E,YAAY,EACR,UAAU,EACV,aAAa,EACb,OAAO,EACP,uBAAuB,EACvB,UAAU,EACV,gBAAgB,GACnB,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EACH,mBAAmB,EACnB,KAAK,YAAY,EACjB,KAAK,WAAW,EAChB,KAAK,oBAAoB,GAC5B,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,MAAM,eAAe,CAAA;AAC9E,YAAY,EACR,UAAU,EACV,YAAY,EACZ,aAAa,EACb,uBAAuB,EACvB,OAAO,EACP,uBAAuB,EACvB,WAAW,EACX,UAAU,EACV,gBAAgB,GACnB,MAAM,WAAW,CAAA;AAClB,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAA"}
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EACH,mBAAmB,GAItB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAgC,MAAM,eAAe,CAAA;AAS9E,OAAO,EAAE,cAAc,EAAE,aAAa,EAA0B,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACrE,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EACH,mBAAmB,GAItB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAAE,gBAAgB,EAAgC,MAAM,eAAe,CAAA;AAY9E,OAAO,EAAE,cAAc,EAAE,aAAa,EAA0B,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAoB,MAAM,mBAAmB,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"register-tool.d.ts","sourceRoot":"","sources":["../src/register-tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAER,SAAS,EACT,aAAa,EAEhB,MAAM,8BAA8B,CAAA;AACrC,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAG5B,OAAO,EAAiC,KAAK,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAExF,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AAE,UAAU,CAAC,OAAO,CAAC,KAAK,IAAI,CAAA;AAM1F;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,EACzC,WAAW,EACX,aAAa,EACb,WAAW,EACX,WAAW,EACX,OAAwB,EACxB,GAAG,aAAa,EACnB,EAAE,oBAAoB,CAAC,OAAO,CAAC,GAAG,YAAY,CAAC,OAAO,CAAC,CA0CvD"}
@@ -1,6 +1,7 @@
1
1
  import { getMcpAnnotations } from './annotations.js';
2
2
  import { normalizeToolMeta } from './app-meta.js';
3
3
  import { getErrorOutput, getToolOutput } from './tool-output.js';
4
+ import { createToolRequest } from './tool-request.js';
4
5
  import { getToolSchemas } from './tool-schemas.js';
5
6
  function logToolFailure({ tool, error }) {
6
7
  console.error(`Error executing tool ${tool.name}:`, error);
@@ -23,8 +24,11 @@ export function createToolRegistrar({ titlePrefix, createContext, formatError, w
23
24
  const { input: inputSchema, output: outputSchema } = getToolSchemas(tool);
24
25
  const callback = async (args, serverContext) => {
25
26
  try {
26
- const run = () => execute(args, createContext(tool, serverContext));
27
+ const request = createToolRequest(server, serverContext);
28
+ const run = () => execute(args, createContext(tool, serverContext), request);
27
29
  const result = await (wrapExecute ? wrapExecute(tool, run) : run());
30
+ if (result.inputRequired)
31
+ return result.inputRequired;
28
32
  return getToolOutput(result, outputOptions);
29
33
  }
30
34
  catch (error) {
@@ -1 +1 @@
1
- {"version":3,"file":"register-tool.js","sourceRoot":"","sources":["../src/register-tool.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AACjD,OAAO,EAAE,cAAc,EAAE,aAAa,EAA0B,MAAM,kBAAkB,CAAA;AACxF,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAA;AAoClD,SAAS,cAAc,CAAC,EAAE,IAAI,EAAE,KAAK,EAA8C;IAC/E,OAAO,CAAC,KA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import type { ToolRequest } from './tool.js';
3
+ /** The `ToolRequest` a tool receives for one MCP call. */
4
+ export declare function createToolRequest(server: McpServer, serverContext: ServerContext): ToolRequest;
5
+ //# sourceMappingURL=tool-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-request.d.ts","sourceRoot":"","sources":["../src/tool-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAMH,KAAK,SAAS,EACd,KAAK,aAAa,EACrB,MAAM,8BAA8B,CAAA;AAErC,OAAO,KAAK,EAAgB,WAAW,EAAE,MAAM,WAAW,CAAA;AA2B1D,0DAA0D;AAC1D,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,SAAS,EAAE,aAAa,EAAE,aAAa,GAAG,WAAW,CAwB9F"}
@@ -0,0 +1,52 @@
1
+ import { acceptedContent, CLIENT_CAPABILITIES_META_KEY, inputRequired, } from '@modelcontextprotocol/server';
2
+ import { z } from 'zod';
3
+ const confirmationContent = z.object({ confirm: z.boolean() });
4
+ // Ticked by default, so accepting the form means yes and declining it means no.
5
+ const confirmationForm = {
6
+ type: 'object',
7
+ properties: { confirm: { type: 'boolean', title: 'Confirm', default: true } },
8
+ required: ['confirm'],
9
+ };
10
+ /**
11
+ * Whether the client can show a form. On 2026-07-28 each request declares its capabilities in
12
+ * its envelope; on 2025-11-25 they were declared once at `initialize`. A stateless 2025 request
13
+ * has neither, and so cannot be asked anything.
14
+ */
15
+ function canAskWithForm(server, serverContext) {
16
+ const envelope = serverContext.mcpReq.envelope;
17
+ const declared = envelope?.[CLIENT_CAPABILITIES_META_KEY] ??
18
+ server.server.getClientCapabilities();
19
+ const elicitation = declared?.elicitation;
20
+ if (elicitation === undefined)
21
+ return false;
22
+ // An empty object declares form mode alone; a client that names its modes has to name form.
23
+ return elicitation.form !== undefined || elicitation.url === undefined;
24
+ }
25
+ /** The `ToolRequest` a tool receives for one MCP call. */
26
+ export function createToolRequest(server, serverContext) {
27
+ return {
28
+ async confirm({ key, message }) {
29
+ const responses = serverContext.mcpReq.inputResponses;
30
+ if (responses !== undefined && key in responses) {
31
+ const answer = acceptedContent(responses, key, confirmationContent);
32
+ return answer?.confirm === true ? { status: 'confirmed' } : { status: 'declined' };
33
+ }
34
+ if (!canAskWithForm(server, serverContext))
35
+ return { status: 'unavailable' };
36
+ return {
37
+ status: 'asking',
38
+ result: {
39
+ inputRequired: inputRequired({
40
+ inputRequests: {
41
+ [key]: inputRequired.elicit({
42
+ message: typeof message === 'function' ? await message() : message,
43
+ requestedSchema: confirmationForm,
44
+ }),
45
+ },
46
+ }),
47
+ },
48
+ };
49
+ },
50
+ };
51
+ }
52
+ //# sourceMappingURL=tool-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-request.js","sourceRoot":"","sources":["../src/tool-request.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,eAAe,EACf,4BAA4B,EAG5B,aAAa,GAGhB,MAAM,8BAA8B,CAAA;AACrC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAA;AAE9D,gFAAgF;AAChF,MAAM,gBAAgB,GAA+C;IACjE,IAAI,EAAE,QAAQ;IACd,UAAU,EAAE,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;IAC7E,QAAQ,EAAE,CAAC,SAAS,CAAC;CACxB,CAAA;AAED;;;;GAIG;AACH,SAAS,cAAc,CAAC,MAAiB,EAAE,aAA4B;IACnE,MAAM,QAAQ,GAAwC,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAA;IACnF,MAAM,QAAQ,GACT,QAAQ,EAAE,CAAC,4BAA4B,CAAoC;QAC5E,MAAM,CAAC,MAAM,CAAC,qBAAqB,EAAE,CAAA;IACzC,MAAM,WAAW,GAAG,QAAQ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ock, ToolAnnotations } from '@modelcontextprotocol/server';
1
+ import type { ContentBlock, InputRequiredResult, ToolAnnotations } from '@modelcontextprotocol/server';
2
2
  import type { z } from 'zod';
3
3
  /**
4
4
  * The three hints every tool must state. The MCP defaults (`readOnlyHint: false`,
@@ -18,8 +18,63 @@ export type ToolResult<Output extends z.ZodRawShape = z.ZodRawShape> = {
18
18
  structuredContent?: z.infer<z.ZodObject<Output>>;
19
19
  /** Blocks the tool authors itself, such as images or embedded resources. Sent first. */
20
20
  contentItems?: ContentBlock[];
21
+ /**
22
+ * A question for the user instead of an answer: the client asks it and calls the tool again
23
+ * with the reply. Returned as is, so the other fields are ignored. Comes from
24
+ * `ToolRequest.confirm`; a `wrapExecute` that rewrites results must keep it.
25
+ */
26
+ inputRequired?: InputRequiredResult;
21
27
  };
22
28
  export type ExecuteResult<Output extends z.ZodRawShape = z.ZodRawShape> = Promise<ToolResult<Output>>;
29
+ /**
30
+ * A result that asks the user instead of answering. It carries nothing else, and says so in its
31
+ * type, so a tool's result can still be read as a `ToolResult` whichever branch produced it.
32
+ */
33
+ export type InputRequiredToolResult = {
34
+ inputRequired: InputRequiredResult;
35
+ textContent?: undefined;
36
+ structuredContent?: undefined;
37
+ contentItems?: undefined;
38
+ };
39
+ /**
40
+ * Where a confirmation stands for this call.
41
+ *
42
+ * - `asking`: return `result` from `execute`. The client puts the question to the user and
43
+ * calls the tool again, with the same arguments, carrying the answer.
44
+ * - `confirmed` / `declined`: the user answered. Dismissing the question counts as declining.
45
+ * - `unavailable`: the client cannot ask (it declared no form elicitation, or the call did not
46
+ * come over MCP), so the tool decides on its own, as it would have without asking.
47
+ */
48
+ export type Confirmation = {
49
+ status: 'asking';
50
+ result: InputRequiredToolResult;
51
+ } | {
52
+ status: 'confirmed';
53
+ } | {
54
+ status: 'declined';
55
+ } | {
56
+ status: 'unavailable';
57
+ };
58
+ /** The MCP request a tool call is serving, for asking the user before acting. */
59
+ export type ToolRequest = {
60
+ /**
61
+ * Asks the user to confirm before the tool acts, typically before something that cannot be
62
+ * undone. The tool runs from the top again once the user answers, so it must not write
63
+ * anything before calling this. `key` names the question within the call: a tool asking more
64
+ * than one question in a run gives each its own key (such as the item's ID), or the later ones
65
+ * find the first answer and act without asking. `message` says
66
+ * exactly what will happen. Pass `message` as a function when building it costs a lookup,
67
+ * such as the name of what is about to be deleted: it is called only when the question is
68
+ * asked, not on the call that carries the answer or when the client cannot ask.
69
+ *
70
+ * The answer comes from the client and is not proof the user saw the question: this keeps a
71
+ * model from acting alone, not a client from bypassing it.
72
+ */
73
+ confirm: (question: {
74
+ key: string;
75
+ message: string | (() => string | Promise<string>);
76
+ }) => Promise<Confirmation>;
77
+ };
23
78
  /**
24
79
  * A tool that can be registered on an MCP server or called directly by another
25
80
  * conversational AI integration.
@@ -43,7 +98,11 @@ export type McpTool<Params extends z.ZodRawShape, Output extends z.ZodRawShape =
43
98
  annotations: RequiredToolAnnotations;
44
99
  /** Sent with the tool listing, e.g. the MCP Apps `ui` resource. */
45
100
  _meta?: Record<string, unknown>;
46
- execute: (args: z.infer<z.ZodObject<Params>>, context: Context) => ExecuteResult<Output>;
101
+ /**
102
+ * `request` is set when the call came over MCP, and absent when another integration calls
103
+ * the tool directly.
104
+ */
105
+ execute: (args: z.infer<z.ZodObject<Params>>, context: Context, request?: ToolRequest) => ExecuteResult<Output>;
47
106
  };
48
107
  /**
49
108
  * A tool of any parameter and output shape.
@@ -57,7 +116,7 @@ export type McpTool<Params extends z.ZodRawShape, Output extends z.ZodRawShape =
57
116
  * through its own `satisfies McpTool<...>`.
58
117
  */
59
118
  export type AnyMcpTool<Context = unknown> = Omit<McpTool<z.ZodRawShape, z.ZodRawShape, Context>, 'execute'> & {
60
- execute: (args: never, context: Context) => ExecuteResult<z.ZodRawShape>;
119
+ execute: (args: never, context: Context, request?: ToolRequest) => ExecuteResult<z.ZodRawShape>;
61
120
  };
62
121
  /** The parts of a tool its schemas come from. Wide enough for tools not built on `McpTool`. */
63
122
  export type ToolSchemaSource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version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "Shared core for the Doist MCP servers",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",