@todoist/mcp-core 1.3.0 → 1.5.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 +40 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/register-tool.d.ts.map +1 -1
- package/dist/register-tool.js +5 -1
- package/dist/register-tool.js.map +1 -1
- package/dist/tool-request.d.ts +5 -0
- package/dist/tool-request.d.ts.map +1 -0
- package/dist/tool-request.js +58 -0
- package/dist/tool-request.js.map +1 -0
- package/dist/tool.d.ts +69 -3
- package/dist/tool.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ The MCP SDK and Zod are peer dependencies, so the server and the core share one
|
|
|
25
25
|
|
|
26
26
|
| Module | Key exports |
|
|
27
27
|
| ------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
28
|
-
| `tool` | `McpTool`, `AnyMcpTool`, `ToolResult`, `ExecuteResult`, `RequiredToolAnnotations`, `ToolSchemaSource` (types)
|
|
28
|
+
| `tool` | `McpTool`, `AnyMcpTool`, `ToolResult`, `ExecuteResult`, `ToolRequest`, `Confirmation`, `InputRequiredToolResult`, `RequiredToolAnnotations`, `ToolSchemaSource` (types) |
|
|
29
29
|
| `register-tool` | `createToolRegistrar`, `ToolRegistrarOptions`, `RegisterTool`, `ToolFailure` |
|
|
30
30
|
| `tool-output` | `getToolOutput`, `getErrorOutput`, `ToolOutputOptions` |
|
|
31
31
|
| `sanitize` | `removeNullFields`, `RemoveNullFieldsOptions` |
|
|
@@ -100,11 +100,49 @@ 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
|
-
-
|
|
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
|
+
|
|
134
|
+
When whether to ask can change between the call that asks and the one that carries the answer (a post that asks only above some audience size, say), read `request.answer(key)` first. It returns `'confirmed'` or `'declined'` when the call carries an answer, and `undefined` otherwise, and never asks, so an answer the user gave still stands when the condition no longer holds:
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
const given = request?.answer('notify')
|
|
138
|
+
if (given === 'declined') return { textContent: 'Not posted.' }
|
|
139
|
+
if (given === undefined && (await api.audience(channelId)) >= 10) {
|
|
140
|
+
const confirmation = await request?.confirm({ key: 'notify', message: 'Notify everyone?' })
|
|
141
|
+
if (confirmation?.status === 'asking') return confirmation.result
|
|
142
|
+
if (confirmation?.status === 'declined') return { textContent: 'Not posted.' }
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
108
146
|
### The wire shape of a result
|
|
109
147
|
|
|
110
148
|
`getToolOutput` turns a `ToolResult` into a `CallToolResult`:
|
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
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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;
|
|
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,
|
|
1
|
+
{"version":3,"file":"register-tool.d.ts","sourceRo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,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"}
|
package/dist/register-tool.js
CHANGED
|
@@ -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
|
|
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":"
|
|
1
|
+
{"version":3,"file":"register-tool.js","sourceRoot":"","sources":["../src/register-tool.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;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a,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;YAC/C,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACb,OAAO,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC,CAAA;gBAC9B,OAAO,cAAc,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAA;YAC7C,CAAC;QACL,CAAC,CAAA;QAED,MAAM,CAAC,YAAY,CACf,IAAI,CAAC,IAAI,EACT;YACI,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,WAAW;YACX,GAAG,CAAC,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YACzC,WAAW,EAAE,iBAAiB,CAAC,IAAI,EAAE,WAAW,CAAC;YACjD,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,iBAAiB,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAClE,EACD,QAAQ,CACX,CAAA;IACL,CAAC,CAAA;AACL,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type McpServer, type ServerContext } from '@modelcontextprotocol/server';
|
|
2
|
+
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,CA6B9F"}
|
|
@@ -0,0 +1,58 @@
|
|
|
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
|
+
function answer(key) {
|
|
28
|
+
const responses = serverContext.mcpReq.inputResponses;
|
|
29
|
+
if (responses === undefined || !(key in responses))
|
|
30
|
+
return undefined;
|
|
31
|
+
const content = acceptedContent(responses, key, confirmationContent);
|
|
32
|
+
return content?.confirm === true ? 'confirmed' : 'declined';
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
answer,
|
|
36
|
+
async confirm({ key, message }) {
|
|
37
|
+
const given = answer(key);
|
|
38
|
+
if (given)
|
|
39
|
+
return { status: given };
|
|
40
|
+
if (!canAskWithForm(server, serverContext))
|
|
41
|
+
return { status: 'unavailable' };
|
|
42
|
+
return {
|
|
43
|
+
status: 'asking',
|
|
44
|
+
result: {
|
|
45
|
+
inputRequired: inputRequired({
|
|
46
|
+
inputRequests: {
|
|
47
|
+
[key]: inputRequired.elicit({
|
|
48
|
+
message: typeof message === 'function' ? await message() : message,
|
|
49
|
+
requestedSchema: confirmationForm,
|
|
50
|
+
}),
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
//# 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,4BA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,UAAU,CAAC,CAAC,CAAC,MAAM,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO;gCAClE,eAAe,EAAE,gBAAgB;6BACpC,CAAC;yBACL;qBACJ,CAAC;iBACL;aACJ,CAAA;QACL,CAAC;KACJ,CAAA;AACL,CAAC"}
|
package/dist/tool.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ContentBlock, 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,70 @@ 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
|
+
/**
|
|
78
|
+
* The user's answer to the question named `key` when this call carries one, without ever
|
|
79
|
+
* asking. A tool whose decision to ask can change between the call that asks and the one that
|
|
80
|
+
* carries the answer (a post that asks only above some audience size, say) reads this first,
|
|
81
|
+
* so an answer the user gave still stands when the condition no longer holds.
|
|
82
|
+
*/
|
|
83
|
+
answer: (key: string) => 'confirmed' | 'declined' | undefined;
|
|
84
|
+
};
|
|
23
85
|
/**
|
|
24
86
|
* A tool that can be registered on an MCP server or called directly by another
|
|
25
87
|
* conversational AI integration.
|
|
@@ -43,7 +105,11 @@ export type McpTool<Params extends z.ZodRawShape, Output extends z.ZodRawShape =
|
|
|
43
105
|
annotations: RequiredToolAnnotations;
|
|
44
106
|
/** Sent with the tool listing, e.g. the MCP Apps `ui` resource. */
|
|
45
107
|
_meta?: Record<string, unknown>;
|
|
46
|
-
|
|
108
|
+
/**
|
|
109
|
+
* `request` is set when the call came over MCP, and absent when another integration calls
|
|
110
|
+
* the tool directly.
|
|
111
|
+
*/
|
|
112
|
+
execute: (args: z.infer<z.ZodObject<Params>>, context: Context, request?: ToolRequest) => ExecuteResult<Output>;
|
|
47
113
|
};
|
|
48
114
|
/**
|
|
49
115
|
* A tool of any parameter and output shape.
|
|
@@ -57,7 +123,7 @@ export type McpTool<Params extends z.ZodRawShape, Output extends z.ZodRawShape =
|
|
|
57
123
|
* through its own `satisfies McpTool<...>`.
|
|
58
124
|
*/
|
|
59
125
|
export type AnyMcpTool<Context = unknown> = Omit<McpTool<z.ZodRawShape, z.ZodRawShape, Context>, 'execute'> & {
|
|
60
|
-
execute: (args: never, context: Context) => ExecuteResult<z.ZodRawShape>;
|
|
126
|
+
execute: (args: never, context: Context, request?: ToolRequest) => ExecuteResult<z.ZodRawShape>;
|
|
61
127
|
};
|
|
62
128
|
/** The parts of a tool its schemas come from. Wide enough for tools not built on `McpTool`. */
|
|
63
129
|
export type ToolSchemaSource = {
|
package/dist/tool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../src/tool.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,
|
|
1
|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