@todoist/mcp-core 0.0.1 → 1.0.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 +9 -0
- package/README.md +237 -32
- package/dist/annotations.d.ts +15 -0
- package/dist/annotations.d.ts.map +1 -0
- package/dist/annotations.js +23 -0
- package/dist/annotations.js.map +1 -0
- package/dist/app-meta.d.ts +8 -0
- package/dist/app-meta.d.ts.map +1 -0
- package/dist/app-meta.js +21 -0
- package/dist/app-meta.js.map +1 -0
- package/dist/host/challenge.d.ts +12 -0
- package/dist/host/challenge.d.ts.map +1 -0
- package/dist/host/challenge.js +9 -0
- package/dist/host/challenge.js.map +1 -0
- package/dist/host/index.d.ts +6 -0
- package/dist/host/index.d.ts.map +1 -0
- package/dist/host/index.js +5 -0
- package/dist/host/index.js.map +1 -0
- package/dist/host/server-contract.d.ts +31 -0
- package/dist/host/server-contract.d.ts.map +1 -0
- package/dist/host/server-contract.js +2 -0
- package/dist/host/server-contract.js.map +1 -0
- package/dist/host/transient.d.ts +9 -0
- package/dist/host/transient.d.ts.map +1 -0
- package/dist/host/transient.js +50 -0
- package/dist/host/transient.js.map +1 -0
- package/dist/host/validate-bearer-token.d.ts +43 -0
- package/dist/host/validate-bearer-token.d.ts.map +1 -0
- package/dist/host/validate-bearer-token.js +46 -0
- package/dist/host/validate-bearer-token.js.map +1 -0
- package/dist/host/verdict-cache.d.ts +21 -0
- package/dist/host/verdict-cache.d.ts.map +1 -0
- package/dist/host/verdict-cache.js +38 -0
- package/dist/host/verdict-cache.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/register-tool.d.ts +41 -0
- package/dist/register-tool.d.ts.map +1 -0
- package/dist/register-tool.js +44 -0
- package/dist/register-tool.js.map +1 -0
- package/dist/sanitize.d.ts +18 -0
- package/dist/sanitize.d.ts.map +1 -0
- package/dist/sanitize.js +44 -0
- package/dist/sanitize.js.map +1 -0
- package/dist/testing/advertised-schemas.d.ts +25 -0
- package/dist/testing/advertised-schemas.d.ts.map +1 -0
- package/dist/testing/advertised-schemas.js +68 -0
- package/dist/testing/advertised-schemas.js.map +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +5 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/nullable-output.d.ts +15 -0
- package/dist/testing/nullable-output.d.ts.map +1 -0
- package/dist/testing/nullable-output.js +57 -0
- package/dist/testing/nullable-output.js.map +1 -0
- package/dist/testing/token-footprint.d.ts +45 -0
- package/dist/testing/token-footprint.d.ts.map +1 -0
- package/dist/testing/token-footprint.js +57 -0
- package/dist/testing/token-footprint.js.map +1 -0
- package/dist/testing/tool-annotations.d.ts +24 -0
- package/dist/testing/tool-annotations.d.ts.map +1 -0
- package/dist/testing/tool-annotations.js +32 -0
- package/dist/testing/tool-annotations.js.map +1 -0
- package/dist/tool-output.d.ts +21 -0
- package/dist/tool-output.d.ts.map +1 -0
- package/dist/tool-output.js +32 -0
- package/dist/tool-output.js.map +1 -0
- package/dist/tool-schemas.d.ts +15 -0
- package/dist/tool-schemas.d.ts.map +1 -0
- package/dist/tool-schemas.js +42 -0
- package/dist/tool-schemas.js.map +1 -0
- package/dist/tool.d.ts +68 -0
- package/dist/tool.d.ts.map +1 -0
- package/dist/tool.js +2 -0
- package/dist/tool.js.map +1 -0
- package/package.json +76 -8
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Doist
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,45 +1,250 @@
|
|
|
1
1
|
# @todoist/mcp-core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared core for Todoist's MCP servers ([todoist-mcp](https://github.com/todoist/todoist-mcp), [comms-mcp](https://github.com/todoist/comms-mcp) and the Automations MCP server): the tool contract, tool registration, how a tool result is shaped for the wire, the contract a server offers the host that serves it, and the contract test suites every server runs over its own tools.
|
|
4
|
+
|
|
5
|
+
TypeScript, ESM-only, Node ≥ 24, npm ≥ 11.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @todoist/mcp-core
|
|
11
|
+
```
|
|
4
12
|
|
|
5
|
-
|
|
13
|
+
Previously published as `@doist/mcp-core`.
|
|
14
|
+
|
|
15
|
+
The MCP SDK and Zod are peer dependencies, so the server and the core share one copy of each. That matters: a host that serves several MCP servers unwraps them with `instanceof McpServer`, which fails against a second copy of the SDK.
|
|
6
16
|
|
|
7
|
-
|
|
17
|
+
| Peer | Range |
|
|
18
|
+
| ------------------------------ | -------- |
|
|
19
|
+
| `@modelcontextprotocol/server` | `^2.2.0` |
|
|
20
|
+
| `zod` | `^4.3.0` |
|
|
8
21
|
|
|
9
|
-
|
|
22
|
+
`./testing` also imports `vitest` (4.1 or later) and `@modelcontextprotocol/client` (2.2 or later), from the server's own devDependencies. They are deliberately not peers: npm installs a peer of a production dependency in production, so declaring them would put vitest in every server's `npm ci --omit=dev`.
|
|
10
23
|
|
|
11
|
-
|
|
12
|
-
1. Configure OIDC trusted publishing for the package name `@todoist/mcp-core`
|
|
13
|
-
2. Enable secure, token-less publishing from CI/CD workflows
|
|
14
|
-
3. Establish provenance for packages published under this name
|
|
24
|
+
## What's in it
|
|
15
25
|
|
|
16
|
-
|
|
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` |
|
|
17
37
|
|
|
18
|
-
|
|
38
|
+
## Usage
|
|
19
39
|
|
|
20
|
-
|
|
40
|
+
### Defining a tool
|
|
21
41
|
|
|
22
|
-
|
|
42
|
+
`McpTool<Params, Output, Context>` is generic over what each call receives, so each server picks its own context: an API client, or an object carrying one with an abort signal and a progress reporter. A server usually aliases it with its context filled in.
|
|
23
43
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
44
|
+
```ts
|
|
45
|
+
import type { McpTool } from '@todoist/mcp-core'
|
|
46
|
+
import { z } from 'zod'
|
|
47
|
+
import type { NotesApi } from './notes-api.js'
|
|
28
48
|
|
|
29
|
-
|
|
49
|
+
type NotesTool<P extends z.ZodRawShape, O extends z.ZodRawShape = Record<string, never>> = McpTool<
|
|
50
|
+
P,
|
|
51
|
+
O,
|
|
52
|
+
NotesApi
|
|
53
|
+
>
|
|
30
54
|
|
|
31
|
-
|
|
32
|
-
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
55
|
+
export const findNotes = {
|
|
56
|
+
name: 'find-notes',
|
|
57
|
+
description: 'Finds notes whose text contains the search term.',
|
|
58
|
+
parameters: { searchText: z.string().describe('Text to look for.') },
|
|
59
|
+
outputSchema: { notes: z.array(z.object({ id: z.string(), text: z.string() })) },
|
|
60
|
+
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true },
|
|
61
|
+
async execute({ searchText }, api) {
|
|
62
|
+
const notes = await api.search(searchText)
|
|
63
|
+
return { textContent: `Found ${notes.length} notes.`, structuredContent: { notes } }
|
|
64
|
+
},
|
|
65
|
+
} satisfies NotesTool<
|
|
66
|
+
{ searchText: z.ZodString },
|
|
67
|
+
{ notes: z.ZodArray<z.ZodObject<{ id: z.ZodString; text: z.ZodString }>> }
|
|
68
|
+
>
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Registering tools
|
|
72
|
+
|
|
73
|
+
`createToolRegistrar` returns a function that registers tools on a server. Everything that differs between products comes in through its options:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { McpServer } from '@modelcontextprotocol/server'
|
|
77
|
+
import { createToolRegistrar } from '@todoist/mcp-core'
|
|
78
|
+
|
|
79
|
+
export function getMcpServer({ token }: { token: string }): McpServer {
|
|
80
|
+
const server = new McpServer({ name: 'notes-mcp-server', version: '1.0.0' })
|
|
81
|
+
const api = new NotesApi(token)
|
|
82
|
+
|
|
83
|
+
const register = createToolRegistrar<NotesApi>({
|
|
84
|
+
titlePrefix: 'Notes',
|
|
85
|
+
createContext: () => api,
|
|
86
|
+
formatError: (error) => (error instanceof Error ? error.message : 'Something went wrong.'),
|
|
87
|
+
// Optional: runs around every call, for retries, request tagging or rewriting the result.
|
|
88
|
+
wrapExecute: (tool, run) => withRetry(run),
|
|
89
|
+
// Optional: replaces the default log line, which never includes the arguments.
|
|
90
|
+
onError: ({ tool, error }) => logger.error({ tool: tool.name, error }),
|
|
91
|
+
})
|
|
92
|
+
for (const tool of [findNotes]) register(server, tool)
|
|
93
|
+
|
|
94
|
+
return server
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
For each tool this:
|
|
99
|
+
|
|
100
|
+
- compiles its input and output schemas to JSON Schema 2020-12 once per process, however many servers register it;
|
|
101
|
+
- titles it "`<titlePrefix>`: `<Title Case Name>`" and defaults `openWorldHint` to false, with the tool's own annotations winning;
|
|
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;
|
|
104
|
+
- catches anything `execute` throws, passes it to `onError`, and returns `formatError`'s text to the model as an `isError` result.
|
|
105
|
+
|
|
106
|
+
`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
|
+
### The wire shape of a result
|
|
109
|
+
|
|
110
|
+
`getToolOutput` turns a `ToolResult` into a `CallToolResult`:
|
|
111
|
+
|
|
112
|
+
1. Nulls and empty objects are stripped from `structuredContent` (`removeNullFields`). Empty arrays stay. Pass `verbatimKeys` for keys whose nulls mean something, such as a stored JSON Schema.
|
|
113
|
+
2. `content` is, in order: the tool's own `contentItems`, then `structuredContent` as a JSON text block, then `textContent`. A client that reads only a text `content[0]` (the OpenAI Responses API drops `structuredContent`) still gets the payload.
|
|
114
|
+
3. `structuredContentAsText: false` leaves the JSON block out, for deployments whose clients all read `structuredContent`.
|
|
115
|
+
|
|
116
|
+
### Serving a server from a host
|
|
117
|
+
|
|
118
|
+
`@todoist/mcp-core/host` is the contract between a server package and a host that serves several of them, such as todoist-ai-integrations. Every server exports the same two shapes:
|
|
119
|
+
|
|
120
|
+
- `getMcpServer(options)`, taking `McpServerOptions` (`{ token, baseUrl? }`), or `McpServerOptions & { ... }` when it needs more;
|
|
121
|
+
- a `ValidateToken`: `(token, baseUrl?) => Promise<'valid' | 'invalid' | 'forbidden'>`.
|
|
122
|
+
|
|
123
|
+
A validator resolves only with a verdict on the token and rejects for anything else, so the host can retry a failure without caching it as a verdict. `validateBearerToken` builds one from a status-only `GET`:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { validateBearerToken, type ValidateToken } from '@todoist/mcp-core/host'
|
|
127
|
+
|
|
128
|
+
export const validateNotesToken: ValidateToken = (token, baseUrl = 'https://notes.example.com') =>
|
|
129
|
+
validateBearerToken({ url: `${baseUrl}/api/v1/me`, token })
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
| The API answers | `validateBearerToken` |
|
|
133
|
+
| ------------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
134
|
+
| 2xx | `valid` |
|
|
135
|
+
| 401 | `invalid`: the host sends a 401 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 |
|
|
140
|
+
|
|
141
|
+
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
|
+
|
|
143
|
+
On the host's side, three helpers turn a validator's answer into a response, whatever the HTTP framework:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import {
|
|
147
|
+
buildBearerChallenge,
|
|
148
|
+
createTokenVerdictCache,
|
|
149
|
+
isTransientValidationError,
|
|
150
|
+
} from '@todoist/mcp-core/host'
|
|
151
|
+
|
|
152
|
+
const verdicts = createTokenVerdictCache({ ttlMs: 120_000, maxEntries: 10_000 })
|
|
153
|
+
|
|
154
|
+
async function checkToken(token: string, baseUrl: string): Promise<Response | 'proceed'> {
|
|
155
|
+
let result = verdicts.get(token, baseUrl)
|
|
156
|
+
if (!result) {
|
|
157
|
+
try {
|
|
158
|
+
result = await validateNotesToken(token, baseUrl)
|
|
159
|
+
} catch (error) {
|
|
160
|
+
if (isTransientValidationError(error)) return 'proceed' // or a 503; never cached
|
|
161
|
+
throw error
|
|
162
|
+
}
|
|
163
|
+
verdicts.set(token, baseUrl, result)
|
|
164
|
+
}
|
|
165
|
+
if (result === 'valid') return 'proceed'
|
|
166
|
+
const forbidden = result === 'forbidden'
|
|
167
|
+
return new Response(null, {
|
|
168
|
+
status: forbidden ? 403 : 401,
|
|
169
|
+
headers: {
|
|
170
|
+
'WWW-Authenticate': buildBearerChallenge({
|
|
171
|
+
error: forbidden ? 'insufficient_scope' : undefined,
|
|
172
|
+
resourceMetadataUrl:
|
|
173
|
+
'https://notes.example.com/.well-known/oauth-protected-resource/mcp',
|
|
174
|
+
}),
|
|
175
|
+
},
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
- `isTransientValidationError`: a timeout, a dropped or refused connection, or a 5xx, the rejections worth retrying. A redirect or a 404 is not.
|
|
181
|
+
- `buildBearerChallenge`: `Bearer`, with `error="insufficient_scope"` and `resource_metadata="…"` when given.
|
|
182
|
+
- `createTokenVerdictCache`: verdicts keyed by token and base URL, kept for `ttlMs`, the oldest dropped when full.
|
|
183
|
+
|
|
184
|
+
### Contract tests
|
|
185
|
+
|
|
186
|
+
`@todoist/mcp-core/testing` holds suites a server runs over its own tool registry:
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
import {
|
|
190
|
+
describeAdvertisedToolSchemas,
|
|
191
|
+
describeOutputSchemaNullability,
|
|
192
|
+
} from '@todoist/mcp-core/testing'
|
|
193
|
+
import { getMcpServer } from './mcp-server.js'
|
|
194
|
+
import { registeredTools } from './tool-registry.js'
|
|
195
|
+
|
|
196
|
+
describeOutputSchemaNullability(registeredTools)
|
|
197
|
+
describeAdvertisedToolSchemas({
|
|
198
|
+
tools: registeredTools,
|
|
199
|
+
createServer: () => getMcpServer({ token: 'test-token' }),
|
|
200
|
+
})
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
- `describeOutputSchemaNullability`: one test per tool, failing on any output field that admits `null`.
|
|
204
|
+
- `describeAdvertisedToolSchemas`: the server advertises exactly these tools, each with JSON Schema 2020-12 schemas matching a direct Zod conversion.
|
|
205
|
+
|
|
206
|
+
Both take any `{ name, parameters, outputSchema? }`, so a server can run them before its tools move to `McpTool`.
|
|
207
|
+
|
|
208
|
+
Two more read only what a client receives, so they restate nothing about registration:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { describeTokenFootprint, describeToolAnnotations } from '@todoist/mcp-core/testing'
|
|
212
|
+
import { encode } from 'gpt-tokenizer'
|
|
213
|
+
|
|
214
|
+
const createServer = () => getMcpServer({ token: 'test-token' })
|
|
215
|
+
|
|
216
|
+
describeTokenFootprint({
|
|
217
|
+
createServer,
|
|
218
|
+
countTokens: (text) => encode(text).length,
|
|
219
|
+
budget: 13_250,
|
|
220
|
+
})
|
|
221
|
+
describeToolAnnotations({
|
|
222
|
+
createServer,
|
|
223
|
+
expected: {
|
|
224
|
+
'find-notes': {
|
|
225
|
+
title: 'Notes: Find Notes',
|
|
226
|
+
readOnlyHint: true,
|
|
227
|
+
destructiveHint: false,
|
|
228
|
+
idempotentHint: true,
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
})
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
- `describeTokenFootprint`: the fixed cost every session pays (the `tools/list` payload as one array, plus `instructions`) stays under `budget`, and either half going missing fails too. It prints the cost per tool. Treat the budget as a ratchet: a rise needs justifying in the change that causes it. `measureTokenFootprint` returns the same numbers. The tokenizer is yours to pass, so mcp-core depends on none.
|
|
235
|
+
- `describeToolAnnotations`: every advertised tool has exactly the expected annotations, title and defaults included (`openWorldHint` defaults to false), and every expected tool is advertised. Clients decide from these whether a call needs the user's confirmation.
|
|
236
|
+
|
|
237
|
+
## Rules for tools
|
|
238
|
+
|
|
239
|
+
These hold for every Doist MCP server. Where the core can enforce one, it says how.
|
|
240
|
+
|
|
241
|
+
- **Output schemas never use `.nullable()`.** Results are stripped of nulls before the SDK validates them, so a nullable field promises a value the tool can never send, and the call fails with "Output validation error" once it is stripped. Declare the field `.optional()` and leave the key out (`foo: value ?? undefined`). `describeOutputSchemaNullability` enforces this.
|
|
242
|
+
- **Annotations describe the tool's worst case.** They are per tool, not per action, so a tool that groups several operations is annotated for the riskiest one. `readOnlyHint: true` only when nothing is persisted and nothing user-visible happens (a file or a shareable link counts). `openWorldHint: true` when the tool reaches anyone outside the user's own account: notifying another person, or producing a public URL. `destructiveHint: true` when it deletes, overwrites, replaces prior state, removes access or is otherwise hard to undo; sending a notification is additive, not destructive. `RequiredToolAnnotations` makes the three hints mandatory.
|
|
243
|
+
- **Describe an output field only where the schema can't.** Output schemas are sent on every `tools/list`, and shared ones are inlined once per tool. Describe units and formats, conventions, unions, conditional presence and non-obvious meaning; leave off anything that restates the field name or lists enum values. Input descriptions are how a model learns to call the tool, and should be as full as they need to be.
|
|
244
|
+
- **Clear an optional field with a string, not `null`.** Some providers, Gemini in particular, handle a nullable input badly. Use a descriptive sentinel (`"unassign"`, `"remove"`) and document it in the field's description.
|
|
245
|
+
- **Batch write tools report per-item failures, never throw.** Settle each item on its own and return the ones that failed alongside the ones that succeeded, even when every item fails. A thrown error hides what was written, so a retry writes it twice.
|
|
246
|
+
- **Never log tool arguments by default.** Write tools carry user content in them. The default `onError` logs the tool name and the error only.
|
|
247
|
+
|
|
248
|
+
## Licence
|
|
249
|
+
|
|
250
|
+
MIT
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ToolAnnotations } from '@modelcontextprotocol/server';
|
|
2
|
+
/** "find-tasks" gives "Find Tasks". Hyphens and underscores both separate words. */
|
|
3
|
+
export declare function formatToolTitle(toolName: string): string;
|
|
4
|
+
/**
|
|
5
|
+
* The annotations a tool is registered with: a title naming the product, `openWorldHint: false`,
|
|
6
|
+
* and then the tool's own hints, which override both.
|
|
7
|
+
*
|
|
8
|
+
* `openWorldHint` defaults to false because most tools stay inside the user's own account. A tool
|
|
9
|
+
* that notifies another person or produces a public URL must set it to true itself.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getMcpAnnotations(tool: {
|
|
12
|
+
name: string;
|
|
13
|
+
annotations: ToolAnnotations;
|
|
14
|
+
}, titlePrefix: string): ToolAnnotations;
|
|
15
|
+
//# sourceMappingURL=annotations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"annotations.d.ts","sourceRoot":"","sources":["../src/annotations.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAA;AAEnE,oFAAoF;AACpF,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAMxD;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC7B,IAAI,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,eAAe,CAAA;CAAE,EACpD,WAAW,EAAE,MAAM,GACpB,eAAe,CAMjB"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** "find-tasks" gives "Find Tasks". Hyphens and underscores both separate words. */
|
|
2
|
+
export function formatToolTitle(toolName) {
|
|
3
|
+
return toolName
|
|
4
|
+
.split(/[-_]+/)
|
|
5
|
+
.filter(Boolean)
|
|
6
|
+
.map((segment) => `${segment.charAt(0).toUpperCase()}${segment.slice(1)}`)
|
|
7
|
+
.join(' ');
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The annotations a tool is registered with: a title naming the product, `openWorldHint: false`,
|
|
11
|
+
* and then the tool's own hints, which override both.
|
|
12
|
+
*
|
|
13
|
+
* `openWorldHint` defaults to false because most tools stay inside the user's own account. A tool
|
|
14
|
+
* that notifies another person or produces a public URL must set it to true itself.
|
|
15
|
+
*/
|
|
16
|
+
export function getMcpAnnotations(tool, titlePrefix) {
|
|
17
|
+
return {
|
|
18
|
+
title: `${titlePrefix}: ${formatToolTitle(tool.name)}`,
|
|
19
|
+
openWorldHint: false,
|
|
20
|
+
...tool.annotations,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=annotations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"annotations.js","sourceRoot":"","sources":["../src/annotations.ts"],"names":[],"mappings":"AAEA,oFAAoF;AACpF,MAAM,UAAU,eAAe,CAAC,QAAgB;IAC5C,OAAO,QAAQ;SACV,KAAK,CAAC,OAAO,CAAC;SACd,MAAM,CAAC,OAAO,CAAC;SACf,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC;SACzE,IAAI,CAAC,GAAG,CAAC,CAAA;AAClB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,iBAAiB,CAC7B,IAAoD,EACpD,WAAmB;IAEnB,OAAO;QACH,KAAK,EAAE,GAAG,WAAW,KAAK,eAAe,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QACtD,aAAa,EAAE,KAAK;QACpB,GAAG,IAAI,CAAC,WAAW;KACtB,CAAA;AACL,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Apps hosts read a tool's UI resource from one of two places: `_meta.ui.resourceUri`, the
|
|
3
|
+
* current form, or the flat `_meta['ui/resourceUri']` that earlier hosts read. This fills in
|
|
4
|
+
* whichever one the tool left out, so both kinds of host find the resource. Any other `_meta` is
|
|
5
|
+
* returned unchanged.
|
|
6
|
+
*/
|
|
7
|
+
export declare function normalizeToolMeta(meta: Record<string, unknown>): Record<string, unknown>;
|
|
8
|
+
//# sourceMappingURL=app-meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-meta.d.ts","sourceRoot":"","sources":["../src/app-meta.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAiBxF"}
|
package/dist/app-meta.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Apps hosts read a tool's UI resource from one of two places: `_meta.ui.resourceUri`, the
|
|
3
|
+
* current form, or the flat `_meta['ui/resourceUri']` that earlier hosts read. This fills in
|
|
4
|
+
* whichever one the tool left out, so both kinds of host find the resource. Any other `_meta` is
|
|
5
|
+
* returned unchanged.
|
|
6
|
+
*/
|
|
7
|
+
export function normalizeToolMeta(meta) {
|
|
8
|
+
const ui = typeof meta.ui === 'object' && meta.ui !== null
|
|
9
|
+
? meta.ui
|
|
10
|
+
: undefined;
|
|
11
|
+
const resourceUri = ui?.resourceUri;
|
|
12
|
+
const legacyResourceUri = meta['ui/resourceUri'];
|
|
13
|
+
if (typeof resourceUri === 'string' && typeof legacyResourceUri !== 'string') {
|
|
14
|
+
return { ...meta, 'ui/resourceUri': resourceUri };
|
|
15
|
+
}
|
|
16
|
+
if (typeof legacyResourceUri === 'string' && typeof resourceUri !== 'string') {
|
|
17
|
+
return { ...meta, ui: { ...ui, resourceUri: legacyResourceUri } };
|
|
18
|
+
}
|
|
19
|
+
return meta;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=app-meta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-meta.js","sourceRoot":"","sources":["../src/app-meta.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAA6B;IAC3D,MAAM,EAAE,GACJ,OAAO,IAAI,CAAC,EAAE,KAAK,QAAQ,IAAI,IAAI,CAAC,EAAE,KAAK,IAAI;QAC3C,CAAC,CAAE,IAAI,CAAC,EAA8B;QACtC,CAAC,CAAC,SAAS,CAAA;IACnB,MAAM,WAAW,GAAG,EAAE,EAAE,WAAW,CAAA;IACnC,MAAM,iBAAiB,GAAG,IAAI,CAAC,gBAAgB,CAAC,CAAA;IAEhD,IAAI,OAAO,WAAW,KAAK,QAAQ,IAAI,OAAO,iBAAiB,KAAK,QAAQ,EAAE,CAAC;QAC3E,OAAO,EAAE,GAAG,IAAI,EAAE,gBAAgB,EAAE,WAAW,EAAE,CAAA;IACrD,CAAC;IAED,IAAI,OAAO,iBAAiB,KAAK,QAAQ,IAAI,OAAO,WAAW,KAAK,QAAQ,EAAE,CAAC;QAC3E,OAAO,EAAE,GAAG,IAAI,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,WAAW,EAAE,iBAAiB,EAAE,EAAE,CAAA;IACrE,CAAC;IAED,OAAO,IAAI,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type BearerChallengeOptions = {
|
|
2
|
+
/**
|
|
3
|
+
* `insufficient_scope` (RFC 6750) for a 403 on a token that lacks a scope, so the client asks
|
|
4
|
+
* for consent again. Left out for a 401, where the client refreshes its token.
|
|
5
|
+
*/
|
|
6
|
+
error?: 'insufficient_scope';
|
|
7
|
+
/** The resource's protected-resource metadata (RFC 9728), where the client finds how to authorise. */
|
|
8
|
+
resourceMetadataUrl?: string;
|
|
9
|
+
};
|
|
10
|
+
/** The `WWW-Authenticate` value for a refused bearer token. */
|
|
11
|
+
export declare function buildBearerChallenge({ error, resourceMetadataUrl, }?: BearerChallengeOptions): string;
|
|
12
|
+
//# sourceMappingURL=challenge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"challenge.d.ts","sourceRoot":"","sources":["../../src/host/challenge.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,sBAAsB,GAAG;IACjC;;;OAGG;IACH,KAAK,CAAC,EAAE,oBAAoB,CAAA;IAC5B,sGAAsG;IACtG,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC/B,CAAA;AAED,+DAA+D;AAC/D,wBAAgB,oBAAoB,CAAC,EACjC,KAAK,EACL,mBAAmB,GACtB,GAAE,sBAA2B,GAAG,MAAM,CAMtC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** The `WWW-Authenticate` value for a refused bearer token. */
|
|
2
|
+
export function buildBearerChallenge({ error, resourceMetadataUrl, } = {}) {
|
|
3
|
+
const params = [
|
|
4
|
+
...(error ? [`error="${error}"`] : []),
|
|
5
|
+
...(resourceMetadataUrl ? [`resource_metadata="${resourceMetadataUrl}"`] : []),
|
|
6
|
+
];
|
|
7
|
+
return params.length > 0 ? `Bearer ${params.join(', ')}` : 'Bearer';
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=challenge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"challenge.js","sourceRoot":"","sources":["../../src/host/challenge.ts"],"names":[],"mappings":"AAUA,+DAA+D;AAC/D,MAAM,UAAU,oBAAoB,CAAC,EACjC,KAAK,EACL,mBAAmB,GACtB,GAA2B,EAAE;IAC1B,MAAM,MAAM,GAAG;QACX,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACtC,GAAG,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,sBAAsB,mBAAmB,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;KACjF,CAAA;IACD,OAAO,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,UAAU,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAA;AACvE,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { buildBearerChallenge, type BearerChallengeOptions } from './challenge.js';
|
|
2
|
+
export type { McpServerOptions, TokenValidationResult, ValidateToken } from './server-contract.js';
|
|
3
|
+
export { isTransientValidationError } from './transient.js';
|
|
4
|
+
export { TokenValidationError, validateBearerToken, type ValidateBearerTokenOptions, } from './validate-bearer-token.js';
|
|
5
|
+
export { createTokenVerdictCache, type TokenVerdictCache, type TokenVerdictCacheOptions, } from './verdict-cache.js';
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { buildBearerChallenge } from './challenge.js';
|
|
2
|
+
export { isTransientValidationError } from './transient.js';
|
|
3
|
+
export { TokenValidationError, validateBearerToken, } from './validate-bearer-token.js';
|
|
4
|
+
export { createTokenVerdictCache, } from './verdict-cache.js';
|
|
5
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What validating a bearer token against a product's API concluded.
|
|
3
|
+
*
|
|
4
|
+
* - `valid`: the token is accepted.
|
|
5
|
+
* - `invalid`: missing, expired, or minted for another audience. A host answers 401 with a
|
|
6
|
+
* challenge, so the client refreshes its token.
|
|
7
|
+
* - `forbidden`: the token authenticates but lacks a scope the tools need. A host answers 403
|
|
8
|
+
* with an `insufficient_scope` challenge, so the client asks for consent again.
|
|
9
|
+
*/
|
|
10
|
+
export type TokenValidationResult = 'valid' | 'invalid' | 'forbidden';
|
|
11
|
+
/**
|
|
12
|
+
* Validates a bearer token against the product's API at `baseUrl`, or production when it is
|
|
13
|
+
* left out.
|
|
14
|
+
*
|
|
15
|
+
* Resolves only with a verdict on the token. Anything else rejects: a `TokenValidationError`
|
|
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.
|
|
19
|
+
*/
|
|
20
|
+
export type ValidateToken = (token: string, baseUrl?: string) => Promise<TokenValidationResult>;
|
|
21
|
+
/**
|
|
22
|
+
* The options every server's `getMcpServer` accepts, so a host builds each server the same way.
|
|
23
|
+
* A server that needs more takes `McpServerOptions & { ... }`.
|
|
24
|
+
*/
|
|
25
|
+
export type McpServerOptions = {
|
|
26
|
+
/** The user's bearer token for the product's API. */
|
|
27
|
+
token: string;
|
|
28
|
+
/** The product's API base URL. Production when left out. */
|
|
29
|
+
baseUrl?: string;
|
|
30
|
+
};
|
|
31
|
+
//# sourceMappingURL=server-contract.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-contract.d.ts","sourceRoot":"","sources":["../../src/host/server-contract.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,qBAAqB,GAAG,OAAO,GAAG,SAAS,GAAG,WAAW,CAAA;AAErE;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,qBAAqB,CAAC,CAAA;AAE/F;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC3B,qDAAqD;IACrD,KAAK,EAAE,MAAM,CAAA;IACb,4DAA4D;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAA;CACnB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server-contract.js","sourceRoot":"","sources":["../../src/host/server-contract.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a `ValidateToken` rejection is worth retrying rather than a fault to report: a timeout,
|
|
3
|
+
* a dropped or refused connection (by its `code`, on the error or on its `cause`), or a 5xx
|
|
4
|
+
* (`TokenValidationError` carries it as `httpStatusCode`). A host can then let the request through or answer 503, and never caches
|
|
5
|
+
* the failure as a verdict. Anything else, such as a redirect or a 404 from a misconfigured base
|
|
6
|
+
* URL, is not transient.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isTransientValidationError(error: unknown): boolean;
|
|
9
|
+
//# sourceMappingURL=transient.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
const TRANSIENT_CODES = new Set([
|
|
2
|
+
'ECONNREFUSED',
|
|
3
|
+
'ECONNRESET',
|
|
4
|
+
'ETIMEDOUT',
|
|
5
|
+
'ENOTFOUND',
|
|
6
|
+
'EAI_AGAIN',
|
|
7
|
+
'EPIPE',
|
|
8
|
+
'UND_ERR_CONNECT_TIMEOUT',
|
|
9
|
+
'UND_ERR_SOCKET',
|
|
10
|
+
]);
|
|
11
|
+
function codeOf(value) {
|
|
12
|
+
return value?.code;
|
|
13
|
+
}
|
|
14
|
+
function statusOf(error) {
|
|
15
|
+
const record = error;
|
|
16
|
+
if (typeof record.httpStatusCode === 'number')
|
|
17
|
+
return record.httpStatusCode;
|
|
18
|
+
if (typeof record.statusCode === 'number')
|
|
19
|
+
return record.statusCode;
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Whether a `ValidateToken` rejection is worth retrying rather than a fault to report: a timeout,
|
|
24
|
+
* a dropped or refused connection (by its `code`, on the error or on its `cause`), or a 5xx
|
|
25
|
+
* (`TokenValidationError` carries it as `httpStatusCode`). A host can then let the request through or answer 503, and never caches
|
|
26
|
+
* the failure as a verdict. Anything else, such as a redirect or a 404 from a misconfigured base
|
|
27
|
+
* URL, is not transient.
|
|
28
|
+
*/
|
|
29
|
+
export function isTransientValidationError(error) {
|
|
30
|
+
if (error instanceof Error) {
|
|
31
|
+
if (error.name === 'TimeoutError' || error.name === 'AbortError')
|
|
32
|
+
return true;
|
|
33
|
+
// Node's net and dns errors carry the code themselves; undici wraps them in "fetch failed"
|
|
34
|
+
// and puts the code on `cause`.
|
|
35
|
+
for (const code of [codeOf(error), codeOf(error.cause)]) {
|
|
36
|
+
if (typeof code === 'string' && TRANSIENT_CODES.has(code))
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
// undici's own wording for a request that never got a response, when it gives no code.
|
|
40
|
+
if (error.message.includes('fetch failed'))
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
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
|
+
}
|
|
50
|
+
//# sourceMappingURL=transient.js.map
|
|
@@ -0,0 +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,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,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9C,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;QAC9B,IAAI,MAAM,KAAK,SAAS,IAAI,MAAM,IAAI,GAAG,IAAI,MAAM,IAAI,GAAG;YAAE,OAAO,IAAI,CAAA;IAC3E,CAAC;IAED,OAAO,KAAK,CAAA;AAChB,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { TokenValidationResult } from './server-contract.js';
|
|
2
|
+
/**
|
|
3
|
+
* The validation call got an answer that is no verdict on the token: a 5xx, a 429, a redirect,
|
|
4
|
+
* or a 404 from a misconfigured base URL. `httpStatusCode` is what a host reads to treat a 5xx
|
|
5
|
+
* as transient.
|
|
6
|
+
*/
|
|
7
|
+
export declare class TokenValidationError extends Error {
|
|
8
|
+
readonly httpStatusCode: number;
|
|
9
|
+
constructor(httpStatusCode: number);
|
|
10
|
+
}
|
|
11
|
+
export type ValidateBearerTokenOptions = {
|
|
12
|
+
/** An endpoint every usable token can read, ideally the cheapest one. */
|
|
13
|
+
url: string | URL;
|
|
14
|
+
token: string;
|
|
15
|
+
/** Sent with the request, for example to tag it for usage tracking. */
|
|
16
|
+
headers?: Record<string, string>;
|
|
17
|
+
/** How long to wait before giving up with a `TimeoutError`. Defaults to 5 seconds. */
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Classifies a 403. Defaults to `forbidden`. A product whose 403 can also mean a refusal that
|
|
21
|
+
* consent cannot lift reads the response here, such as its `WWW-Authenticate` challenge.
|
|
22
|
+
*/
|
|
23
|
+
classifyForbidden?: (response: Response) => TokenValidationResult;
|
|
24
|
+
/**
|
|
25
|
+
* Sends the request. Defaults to the global `fetch`. A server whose API client has its own
|
|
26
|
+
* transport passes it here, so validation takes the same route as every other call: through
|
|
27
|
+
* a proxy from `HTTPS_PROXY`, for example.
|
|
28
|
+
*/
|
|
29
|
+
fetch?: (url: string | URL, init: RequestInit) => Promise<Response>;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Validates a bearer token with one status-only `GET`: 2xx is `valid`, 401 `invalid`, 403
|
|
33
|
+
* `forbidden` (or whatever `classifyForbidden` says). The body is drained unread, so a change
|
|
34
|
+
* to the response's shape can never misclassify a token.
|
|
35
|
+
*
|
|
36
|
+
* Redirects are not followed: a 302 to a sign-in page that anyone can load would otherwise
|
|
37
|
+
* come back 200 and pass for an accepted token.
|
|
38
|
+
*
|
|
39
|
+
* @throws {TokenValidationError} for any other status, a redirect included. Network and timeout
|
|
40
|
+
* errors propagate unchanged.
|
|
41
|
+
*/
|
|
42
|
+
export declare function validateBearerToken({ url, token, headers, timeoutMs, classifyForbidden, fetch: send, }: ValidateBearerTokenOptions): Promise<TokenValidationResult>;
|
|
43
|
+
//# sourceMappingURL=validate-bearer-token.d.ts.map
|
|
@@ -0,0 +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,MAAM,CAAC,CAAA;IAChC,sFAAsF;IACtF,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB;;;OAGG;IACH,iBAAiB,CAAC,EAAE,CAAC,QAAQ,EAAE,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,CAkB7D"}
|