typespec-http-mcp 0.8.1 → 0.10.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 +65 -10
- package/dist/src/annotations.d.ts +41 -5
- package/dist/src/annotations.js +77 -5
- package/dist/src/emitter.js +17 -0
- package/dist/src/lib.d.ts +3 -0
- package/dist/src/lib.js +13 -0
- package/dist/src/runtime.d.ts +226 -3
- package/dist/src/runtime.js +175 -0
- package/dist/src/tools.d.ts +3 -0
- package/dist/src/tools.js +107 -22
- package/package.json +5 -4
- package/src/runtime.ts +389 -3
package/README.md
CHANGED
|
@@ -107,25 +107,80 @@ What the model receives follows from the document:
|
|
|
107
107
|
- `structuredContent` carries the body when its schema is the tool's `outputSchema`;
|
|
108
108
|
- response headers are carried in `_meta["typespec-http-mcp/response-headers"]`, keyed by wire name.
|
|
109
109
|
|
|
110
|
+
## Long-running tools and MCP Tasks
|
|
111
|
+
|
|
112
|
+
Some tools finish long after the request that started them: an export, a reply a person has to give,
|
|
113
|
+
a job on a queue. A client that supports the
|
|
114
|
+
[MCP Tasks extension](https://github.com/modelcontextprotocol/ext-tasks) can take a task handle at
|
|
115
|
+
once and poll for the result.
|
|
116
|
+
|
|
117
|
+
**Which tools may do that is decided where you wire them, never in the spec.** The extension has no
|
|
118
|
+
per-tool declaration, and whether work outlives a request is a fact about where you run it, not
|
|
119
|
+
about the operation. Add a `tasks` policy to `deps`, and attach the runtime's `TasksExtension`,
|
|
120
|
+
over a store of yours, to the server:
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import { registerTools, type ToolCall } from "./generated/mcp.gen.js";
|
|
124
|
+
import { TasksExtension, type ToolDeps } from "./generated/mcp-runtime.gen.js";
|
|
125
|
+
|
|
126
|
+
const tasks = new TasksExtension(store); // your TaskStore: a Durable Object, a Workflow, a table
|
|
127
|
+
|
|
128
|
+
// in the factory above
|
|
129
|
+
const deps: ToolDeps<AuthInfo, ToolCall> = {
|
|
130
|
+
// permits, context and noContext, as above, and:
|
|
131
|
+
tasks: {
|
|
132
|
+
operations: ["exportCatalog"],
|
|
133
|
+
create: (call, caller, serverContext) =>
|
|
134
|
+
tasks.create(serverContext, { context: { call, caller: caller.clientId } }),
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
const server = new McpServer({ name: "bookshop", version: "1.0.0" });
|
|
138
|
+
tasks.attach(server);
|
|
139
|
+
registerTools(server, () => handlers, deps);
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
The work behind a task runs the same handler through `runTool(handlers, call, caller)`, which
|
|
143
|
+
answers exactly what the tool would have, so a completed task holds the blocking call's result. A
|
|
144
|
+
client that did not declare the extension is answered inline, as before. See
|
|
145
|
+
[the guide](docs/guides.md#running-long-tools-as-mcp-tasks) and
|
|
146
|
+
[the reference](docs/reference.md#tasks).
|
|
147
|
+
|
|
110
148
|
## What is derived, and from what
|
|
111
149
|
|
|
112
150
|
Nothing is invented. Every field of a tool comes from something the document already states, or from
|
|
113
151
|
a published specification:
|
|
114
152
|
|
|
115
|
-
| tool field
|
|
116
|
-
|
|
|
117
|
-
| `name`
|
|
118
|
-
| `title`
|
|
119
|
-
| `description`
|
|
120
|
-
| `inputSchema`
|
|
121
|
-
| `outputSchema`
|
|
122
|
-
| `annotations.
|
|
123
|
-
| `annotations.
|
|
124
|
-
|
|
|
153
|
+
| tool field | derived from |
|
|
154
|
+
| ----------------------------- | -------------------------------------------------------------------------------- |
|
|
155
|
+
| `name` | the operation id the document publishes |
|
|
156
|
+
| `title` | `@summary` |
|
|
157
|
+
| `description` | `@doc` |
|
|
158
|
+
| `inputSchema` | the emitted Zod schema, **input** projection |
|
|
159
|
+
| `outputSchema` | the body every 2xx and 3xx status shares, unless the tool may answer with a task |
|
|
160
|
+
| `annotations.title` | `@summary`, as `title` |
|
|
161
|
+
| `annotations.readOnlyHint` | the method's safety, per IANA's HTTP Method Registry |
|
|
162
|
+
| `annotations.idempotentHint` | the method's idempotence, same source |
|
|
163
|
+
| `annotations.destructiveHint` | `true` for DELETE; otherwise what the operation states in `x-mcp-annotations` |
|
|
164
|
+
| `annotations.openWorldHint` | what the operation states in `x-mcp-annotations` |
|
|
165
|
+
| which operations are tools | `@tag`, which `@typespec/openapi3` publishes |
|
|
125
166
|
|
|
126
167
|
There are **no decorators**. A decorator would let a spec state something the OpenAPI document
|
|
127
168
|
cannot publish, so the emitted surface would enforce a rule no reader of the contract can see.
|
|
128
169
|
|
|
170
|
+
What a method can't tell, whether a write only adds and whether a tool reaches beyond the service,
|
|
171
|
+
an operation states in an OpenAPI extension, which the document publishes beside it:
|
|
172
|
+
|
|
173
|
+
```tsp
|
|
174
|
+
@put
|
|
175
|
+
@summary("Shelve a book")
|
|
176
|
+
@extension("x-mcp-annotations", #{ destructiveHint: false, openWorldHint: false })
|
|
177
|
+
op shelveBook(@body book: Book): Book;
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
It may state `destructiveHint` and `openWorldHint`, each `true` or `false`, and nothing else:
|
|
181
|
+
`readOnlyHint` and `idempotentHint` stay the method's, and a DELETE stays destructive
|
|
182
|
+
([`invalid-tool-annotations`](docs/reference.md#invalid-tool-annotations)).
|
|
183
|
+
|
|
129
184
|
## Documentation
|
|
130
185
|
|
|
131
186
|
- [`docs/reference.md`](docs/reference.md) -- every option and every diagnostic.
|
|
@@ -9,14 +9,46 @@
|
|
|
9
9
|
* | --- | --- |
|
|
10
10
|
* | `readOnlyHint` | RFC 9110 s9.2.1, safe methods |
|
|
11
11
|
* | `idempotentHint` | RFC 9110 s9.2.2, idempotent methods |
|
|
12
|
-
* | `destructiveHint` |
|
|
13
|
-
* | `openWorldHint` |
|
|
12
|
+
* | `destructiveHint` | `true` for DELETE; otherwise what the operation states in `x-mcp-annotations` |
|
|
13
|
+
* | `openWorldHint` | what the operation states in `x-mcp-annotations`; never derived |
|
|
14
|
+
* | `title` | `@summary`, as the tool's own `title` is |
|
|
15
|
+
*
|
|
16
|
+
* **What a method can't tell, an operation states in a published extension.** Whether a PUT
|
|
17
|
+
* replaces data or only adds to it, and whether an operation reaches anything beyond the service,
|
|
18
|
+
* are facts no HTTP keyword carries. An operation states them with
|
|
19
|
+
* `@extension("x-mcp-annotations", #{ destructiveHint: false, openWorldHint: false })`, which
|
|
20
|
+
* `@typespec/openapi3` publishes beside the operation, so a reader of the document sees the same
|
|
21
|
+
* fact the tool list carries: an extension, not a decorator only this emitter reads. It may state
|
|
22
|
+
* those two hints and nothing else: `readOnlyHint` and `idempotentHint` stay the method's, so an
|
|
23
|
+
* operation can't declare a GET destructive or a POST idempotent without its method saying so, and
|
|
24
|
+
* a DELETE stays destructive.
|
|
14
25
|
*/
|
|
15
26
|
export interface ToolAnnotations {
|
|
27
|
+
readonly title?: string;
|
|
16
28
|
readonly readOnlyHint?: boolean;
|
|
17
29
|
readonly idempotentHint?: boolean;
|
|
18
30
|
readonly destructiveHint?: boolean;
|
|
31
|
+
readonly openWorldHint?: boolean;
|
|
32
|
+
}
|
|
33
|
+
/** The OpenAPI extension an operation states the hints its method can't tell in. */
|
|
34
|
+
export declare const ANNOTATIONS_EXTENSION = "x-mcp-annotations";
|
|
35
|
+
/** The hints an operation may state in `x-mcp-annotations`: those no method tells. */
|
|
36
|
+
export interface DeclaredHints {
|
|
37
|
+
readonly destructiveHint?: boolean;
|
|
38
|
+
readonly openWorldHint?: boolean;
|
|
19
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* The hints an operation's `x-mcp-annotations` states, or why it can't be read: a value that isn't
|
|
42
|
+
* an object, a key that isn't one of the two it may hold, a hint the method already states, a value
|
|
43
|
+
* that isn't a boolean, `destructiveHint` on a safe method, which MCP reads only for a tool that
|
|
44
|
+
* isn't read-only, or `destructiveHint: false` on a DELETE, which removes what it targets and so is
|
|
45
|
+
* never only additive. Absent, it states nothing.
|
|
46
|
+
*/
|
|
47
|
+
export declare function declaredHints(verb: string, value: unknown): {
|
|
48
|
+
readonly hints: DeclaredHints;
|
|
49
|
+
} | {
|
|
50
|
+
readonly problem: string;
|
|
51
|
+
};
|
|
20
52
|
/**
|
|
21
53
|
* What this emitter states about a method, and what it deliberately leaves unsaid.
|
|
22
54
|
*
|
|
@@ -27,7 +59,11 @@ export interface ToolAnnotations {
|
|
|
27
59
|
* guess.
|
|
28
60
|
* - `destructiveHint: true` for DELETE only. **Omitted for POST and PATCH**, where MCP's default of
|
|
29
61
|
* `true` stands: the document gives no basis to contradict it, and an emitter must not invent one.
|
|
30
|
-
* - `openWorldHint` is
|
|
31
|
-
* something any OpenAPI keyword states.
|
|
62
|
+
* - `openWorldHint` is written only where the operation states it (`x-mcp-annotations`). Whether
|
|
63
|
+
* an operation reaches beyond this service is not something any OpenAPI keyword states.
|
|
64
|
+
* - `destructiveHint` and `openWorldHint` as the operation states them in `x-mcp-annotations`, over
|
|
65
|
+
* what the method gives (`declaredHints`).
|
|
66
|
+
* - `title` as `@summary` gives it, the same text as the tool's own `title`: MCP shows the tool's
|
|
67
|
+
* first, and a client that reads only the annotations still finds one.
|
|
32
68
|
*/
|
|
33
|
-
export declare function annotationsFor(verb: string): ToolAnnotations;
|
|
69
|
+
export declare function annotationsFor(verb: string, declared?: DeclaredHints, title?: string): ToolAnnotations;
|
package/dist/src/annotations.js
CHANGED
|
@@ -9,13 +9,75 @@
|
|
|
9
9
|
* | --- | --- |
|
|
10
10
|
* | `readOnlyHint` | RFC 9110 s9.2.1, safe methods |
|
|
11
11
|
* | `idempotentHint` | RFC 9110 s9.2.2, idempotent methods |
|
|
12
|
-
* | `destructiveHint` |
|
|
13
|
-
* | `openWorldHint` |
|
|
12
|
+
* | `destructiveHint` | `true` for DELETE; otherwise what the operation states in `x-mcp-annotations` |
|
|
13
|
+
* | `openWorldHint` | what the operation states in `x-mcp-annotations`; never derived |
|
|
14
|
+
* | `title` | `@summary`, as the tool's own `title` is |
|
|
15
|
+
*
|
|
16
|
+
* **What a method can't tell, an operation states in a published extension.** Whether a PUT
|
|
17
|
+
* replaces data or only adds to it, and whether an operation reaches anything beyond the service,
|
|
18
|
+
* are facts no HTTP keyword carries. An operation states them with
|
|
19
|
+
* `@extension("x-mcp-annotations", #{ destructiveHint: false, openWorldHint: false })`, which
|
|
20
|
+
* `@typespec/openapi3` publishes beside the operation, so a reader of the document sees the same
|
|
21
|
+
* fact the tool list carries: an extension, not a decorator only this emitter reads. It may state
|
|
22
|
+
* those two hints and nothing else: `readOnlyHint` and `idempotentHint` stay the method's, so an
|
|
23
|
+
* operation can't declare a GET destructive or a POST idempotent without its method saying so, and
|
|
24
|
+
* a DELETE stays destructive.
|
|
14
25
|
*/
|
|
15
26
|
/** Safe methods: RFC 9110 s9.2.1. A safe method is also idempotent, s9.2.2. */
|
|
16
27
|
const SAFE = new Set(["get", "head"]);
|
|
17
28
|
/** Idempotent methods: RFC 9110 s9.2.2. */
|
|
18
29
|
const IDEMPOTENT = new Set(["get", "head", "put", "delete"]);
|
|
30
|
+
/** The OpenAPI extension an operation states the hints its method can't tell in. */
|
|
31
|
+
export const ANNOTATIONS_EXTENSION = "x-mcp-annotations";
|
|
32
|
+
/** The hints `x-mcp-annotations` may hold. */
|
|
33
|
+
const DECLARABLE = new Set(["destructiveHint", "openWorldHint"]);
|
|
34
|
+
/** The hints the method states, which `x-mcp-annotations` may not. */
|
|
35
|
+
const DERIVED = new Set(["readOnlyHint", "idempotentHint"]);
|
|
36
|
+
/**
|
|
37
|
+
* The hints an operation's `x-mcp-annotations` states, or why it can't be read: a value that isn't
|
|
38
|
+
* an object, a key that isn't one of the two it may hold, a hint the method already states, a value
|
|
39
|
+
* that isn't a boolean, `destructiveHint` on a safe method, which MCP reads only for a tool that
|
|
40
|
+
* isn't read-only, or `destructiveHint: false` on a DELETE, which removes what it targets and so is
|
|
41
|
+
* never only additive. Absent, it states nothing.
|
|
42
|
+
*/
|
|
43
|
+
export function declaredHints(verb, value) {
|
|
44
|
+
if (value === undefined)
|
|
45
|
+
return { hints: {} };
|
|
46
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
47
|
+
return { problem: "it must be an object of booleans" };
|
|
48
|
+
}
|
|
49
|
+
const hints = {};
|
|
50
|
+
for (const [key, hint] of Object.entries(value)) {
|
|
51
|
+
if (DERIVED.has(key)) {
|
|
52
|
+
return {
|
|
53
|
+
problem: `'${key}' is the operation's method's to state (RFC 9110), not the extension's`,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (!DECLARABLE.has(key)) {
|
|
57
|
+
return {
|
|
58
|
+
problem: `'${key}' is not a hint it may state: only destructiveHint and openWorldHint`,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (typeof hint !== "boolean")
|
|
62
|
+
return { problem: `'${key}' must be true or false` };
|
|
63
|
+
if (key === "destructiveHint")
|
|
64
|
+
hints.destructiveHint = hint;
|
|
65
|
+
else
|
|
66
|
+
hints.openWorldHint = hint;
|
|
67
|
+
}
|
|
68
|
+
const method = verb.toLowerCase();
|
|
69
|
+
if (hints.destructiveHint !== undefined && SAFE.has(method)) {
|
|
70
|
+
return {
|
|
71
|
+
problem: `'destructiveHint' means nothing for a ${verb.toUpperCase()}, which is read-only`,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
if (hints.destructiveHint === false && method === "delete") {
|
|
75
|
+
return {
|
|
76
|
+
problem: `'destructiveHint: false' says a DELETE only adds, and a DELETE removes what it targets (RFC 9110)`,
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
return { hints };
|
|
80
|
+
}
|
|
19
81
|
/**
|
|
20
82
|
* What this emitter states about a method, and what it deliberately leaves unsaid.
|
|
21
83
|
*
|
|
@@ -26,16 +88,26 @@ const IDEMPOTENT = new Set(["get", "head", "put", "delete"]);
|
|
|
26
88
|
* guess.
|
|
27
89
|
* - `destructiveHint: true` for DELETE only. **Omitted for POST and PATCH**, where MCP's default of
|
|
28
90
|
* `true` stands: the document gives no basis to contradict it, and an emitter must not invent one.
|
|
29
|
-
* - `openWorldHint` is
|
|
30
|
-
* something any OpenAPI keyword states.
|
|
91
|
+
* - `openWorldHint` is written only where the operation states it (`x-mcp-annotations`). Whether
|
|
92
|
+
* an operation reaches beyond this service is not something any OpenAPI keyword states.
|
|
93
|
+
* - `destructiveHint` and `openWorldHint` as the operation states them in `x-mcp-annotations`, over
|
|
94
|
+
* what the method gives (`declaredHints`).
|
|
95
|
+
* - `title` as `@summary` gives it, the same text as the tool's own `title`: MCP shows the tool's
|
|
96
|
+
* first, and a client that reads only the annotations still finds one.
|
|
31
97
|
*/
|
|
32
|
-
export function annotationsFor(verb) {
|
|
98
|
+
export function annotationsFor(verb, declared = {}, title) {
|
|
33
99
|
const method = verb.toLowerCase();
|
|
34
100
|
const annotations = {};
|
|
101
|
+
if (title !== undefined)
|
|
102
|
+
annotations.title = title;
|
|
35
103
|
if (SAFE.has(method))
|
|
36
104
|
annotations.readOnlyHint = true;
|
|
37
105
|
annotations.idempotentHint = IDEMPOTENT.has(method);
|
|
38
106
|
if (method === "delete")
|
|
39
107
|
annotations.destructiveHint = true;
|
|
108
|
+
if (declared.destructiveHint !== undefined)
|
|
109
|
+
annotations.destructiveHint = declared.destructiveHint;
|
|
110
|
+
if (declared.openWorldHint !== undefined)
|
|
111
|
+
annotations.openWorldHint = declared.openWorldHint;
|
|
40
112
|
return annotations;
|
|
41
113
|
}
|
package/dist/src/emitter.js
CHANGED
|
@@ -2,6 +2,8 @@ import { readFileSync } from "node:fs";
|
|
|
2
2
|
import { fileURLToPath } from "node:url";
|
|
3
3
|
import { emitFile, NoTarget, resolvePath } from "@typespec/compiler";
|
|
4
4
|
import { getDoc, getSummary } from "@typespec/compiler";
|
|
5
|
+
import { getExtensions } from "@typespec/openapi";
|
|
6
|
+
import { ANNOTATIONS_EXTENSION, declaredHints } from "./annotations.js";
|
|
5
7
|
import { emitHttpZod } from "typespec-http-zod";
|
|
6
8
|
import { PACKAGE_NAME, reportDiagnostic } from "./lib.js";
|
|
7
9
|
import { nameTools } from "./names.js";
|
|
@@ -165,6 +167,20 @@ export async function $onEmit(context) {
|
|
|
165
167
|
});
|
|
166
168
|
continue;
|
|
167
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* **What the method can't tell, as the operation states it.** Read from the extension
|
|
172
|
+
* `@typespec/openapi3` publishes, so the tool list and the document say the same. One that
|
|
173
|
+
* can't be read is warned of, and the operation stays on the surface with the method's
|
|
174
|
+
* hints alone.
|
|
175
|
+
*/
|
|
176
|
+
const declared = declaredHints(route.verb, getExtensions(context.program, operation.operation).get(ANNOTATIONS_EXTENSION));
|
|
177
|
+
if ("problem" in declared) {
|
|
178
|
+
reportDiagnostic(context.program, {
|
|
179
|
+
code: "invalid-tool-annotations",
|
|
180
|
+
target: targetFor(emitted, route.operationId),
|
|
181
|
+
format: { operationId: route.operationId, problem: declared.problem },
|
|
182
|
+
});
|
|
183
|
+
}
|
|
168
184
|
const description = getDoc(context.program, operation.operation);
|
|
169
185
|
const title = getSummary(context.program, operation.operation);
|
|
170
186
|
const described = description !== undefined || title !== undefined;
|
|
@@ -212,6 +228,7 @@ export async function $onEmit(context) {
|
|
|
212
228
|
* model reads. A title is for display and is absent unless the spec declares one.
|
|
213
229
|
*/
|
|
214
230
|
title,
|
|
231
|
+
hints: "hints" in declared ? declared.hints : {},
|
|
215
232
|
});
|
|
216
233
|
}
|
|
217
234
|
/**
|
package/dist/src/lib.d.ts
CHANGED
|
@@ -96,6 +96,9 @@ type Diagnostics = {
|
|
|
96
96
|
"headers-only-success": {
|
|
97
97
|
readonly default: CallableMessage<["operationId"]>;
|
|
98
98
|
};
|
|
99
|
+
"invalid-tool-annotations": {
|
|
100
|
+
readonly default: CallableMessage<["operationId", "problem"]>;
|
|
101
|
+
};
|
|
99
102
|
"tool-without-description": {
|
|
100
103
|
readonly default: CallableMessage<["operationId"]>;
|
|
101
104
|
};
|
package/dist/src/lib.js
CHANGED
|
@@ -235,6 +235,19 @@ const diagnostics = {
|
|
|
235
235
|
default: paramMessage `'runtime-module' is set to '${"value"}', and typespec-http-mcp no longer substitutes its runtime: 'mcp-runtime.gen.ts' is always emitted. Remove the option and delete the module it pointed at. The caller context is inferred from 'deps.context', and a handler returns '{ status, body, headers }' for any response the document declares. See the Upgrading section of docs/guides.md.`,
|
|
236
236
|
},
|
|
237
237
|
},
|
|
238
|
+
/**
|
|
239
|
+
* An operation's `x-mcp-annotations` this emitter can't read as tool hints.
|
|
240
|
+
*
|
|
241
|
+
* A warning, as every diagnostic about the document is: an error would cost the whole OpenAPI
|
|
242
|
+
* document over one operation. The tool keeps the hints its method gives, and this says the
|
|
243
|
+
* stated ones were not read, since the MCP defaults stand in their place.
|
|
244
|
+
*/
|
|
245
|
+
"invalid-tool-annotations": {
|
|
246
|
+
severity: "warning",
|
|
247
|
+
messages: {
|
|
248
|
+
default: paramMessage `The x-mcp-annotations of '${"operationId"}' can't be read as tool hints: ${"problem"}. It may hold destructiveHint and openWorldHint, each true or false; readOnlyHint and idempotentHint come from the operation's method.`,
|
|
249
|
+
},
|
|
250
|
+
},
|
|
238
251
|
/**
|
|
239
252
|
* A selected operation with nothing for a model to read.
|
|
240
253
|
*
|
package/dist/src/runtime.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
1
|
+
import { type CallToolResult, type McpServer, type ServerContext } from "@modelcontextprotocol/server";
|
|
2
|
+
import { type ZodError, type ZodType } from "zod";
|
|
3
3
|
/**
|
|
4
4
|
* **What generated code imports at run time -- and the only thing this package puts in its output.**
|
|
5
5
|
*
|
|
@@ -179,7 +179,7 @@ export declare function toolResult(declared: ToolResponses, operationId: string,
|
|
|
179
179
|
* **`C` is the caller context, and `registerTools` INFERS it from `context`.** An application whose
|
|
180
180
|
* `context` returns a `Caller` has handlers typed `(ctx: Caller, input)`, with nothing to declare.
|
|
181
181
|
*/
|
|
182
|
-
export interface ToolDeps<C = unknown> {
|
|
182
|
+
export interface ToolDeps<C = unknown, Call extends ToolCallShape = never> {
|
|
183
183
|
/**
|
|
184
184
|
* Whether the caller this server was built for may SEE this tool.
|
|
185
185
|
*
|
|
@@ -217,4 +217,227 @@ export interface ToolDeps<C = unknown> {
|
|
|
217
217
|
readonly context: (context: ServerContext, authentication: "none" | "optional" | "required") => C | null;
|
|
218
218
|
/** The result when {@link context} returns `null`. The application's to decide. */
|
|
219
219
|
readonly noContext: (context: ServerContext) => CallToolResult;
|
|
220
|
+
/**
|
|
221
|
+
* Which tools may answer with an MCP task, and how a task is created. Absent, no tool ever does.
|
|
222
|
+
*
|
|
223
|
+
* **The application's to decide, because the document cannot say it.** The Tasks extension has
|
|
224
|
+
* no per-tool declaration: the server decides on each request whether to answer with a task.
|
|
225
|
+
* Whether a tool's work outlives a request is a fact about where the application runs it, not
|
|
226
|
+
* about the operation.
|
|
227
|
+
*
|
|
228
|
+
* **Typed by the generated `ToolCall`, so write `ToolDeps<Caller, ToolCall>` to set it.** The
|
|
229
|
+
* policy names operations, and only the generated module knows which exist. `ToolDeps<Caller>`
|
|
230
|
+
* names none, so it admits no operation, and wiring written before tasks existed compiles as it
|
|
231
|
+
* did.
|
|
232
|
+
*/
|
|
233
|
+
readonly tasks?: ToolTasks<C, Call>;
|
|
234
|
+
}
|
|
235
|
+
/** What a tool call is, to the code that runs it later: the operation and its validated input. */
|
|
236
|
+
export interface ToolCallShape {
|
|
237
|
+
readonly operationId: string;
|
|
238
|
+
readonly input: unknown;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* The application's task policy for the tools one `registerTools` call registers.
|
|
242
|
+
*
|
|
243
|
+
* **`decide` and `create` are METHOD signatures, deliberately.** That keeps the policy covariant in
|
|
244
|
+
* `Call`, so `ToolDeps<Caller>`, which lists no operation, is still a `ToolDeps<Caller, ToolCall>`.
|
|
245
|
+
* With property signatures the two would be unrelated, and every wiring written before tasks existed
|
|
246
|
+
* would stop compiling.
|
|
247
|
+
*
|
|
248
|
+
* **A request that did not declare the Tasks extension never reaches this policy.** It is answered
|
|
249
|
+
* inline, as if no policy were configured, because the extension forbids answering it with a task.
|
|
250
|
+
*/
|
|
251
|
+
export interface ToolTasks<C, Call extends ToolCallShape> {
|
|
252
|
+
/**
|
|
253
|
+
* The operations that may answer with a task.
|
|
254
|
+
*
|
|
255
|
+
* **Read at REGISTRATION, like `permits`**, because it changes what is published: a tool listed
|
|
256
|
+
* here publishes no `outputSchema`. The SDK refuses a task from a tool that publishes one, so a
|
|
257
|
+
* task tool is an ordinary tool without it, on every request.
|
|
258
|
+
*/
|
|
259
|
+
readonly operations: readonly Call["operationId"][];
|
|
260
|
+
/**
|
|
261
|
+
* Whether THIS call becomes a task or runs inline. Consulted only for a listed operation and a
|
|
262
|
+
* request that declared the extension. Absent, every such call becomes a task.
|
|
263
|
+
*
|
|
264
|
+
* Receives the validated input and the caller, so the answer can depend on the application's own
|
|
265
|
+
* state: work that is already finished can be answered at once.
|
|
266
|
+
*
|
|
267
|
+
* Type `call` as the whole `ToolCall` and narrow on `operationId`: as a method, this accepts a
|
|
268
|
+
* narrower parameter without complaint, and receives every listed operation's call regardless.
|
|
269
|
+
*/
|
|
270
|
+
decide?(call: Call, caller: C, context: ServerContext): Awaitable<"task" | "inline">;
|
|
271
|
+
/**
|
|
272
|
+
* Create the task and answer with its handle. In practice
|
|
273
|
+
* `(call, caller, context) => tasks.create(context, { context: { ...call, ... } })`, where the
|
|
274
|
+
* application chooses what the store needs to find or start the work: the call, an identifier
|
|
275
|
+
* for the caller, a service name when one store serves several services.
|
|
276
|
+
*/
|
|
277
|
+
create(call: Call, caller: C, context: ServerContext): Promise<TaskToolResult>;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* The result McpServer answers when a tool callback throws: the message as text, marked as an error.
|
|
281
|
+
*
|
|
282
|
+
* Exported so the work behind a task settles a thrown error exactly as the blocking call would have
|
|
283
|
+
* answered it. The Tasks extension requires a tool error to complete the task with that result,
|
|
284
|
+
* and reserves `failed` for a protocol error.
|
|
285
|
+
*/
|
|
286
|
+
export declare function toolError(error: unknown): CallToolResult;
|
|
287
|
+
/**
|
|
288
|
+
* Refuses a task policy on a server no {@link TasksExtension} is attached to. A tool there would
|
|
289
|
+
* answer with a task the server does not advertise and nobody can poll: `tasks/get` would be an
|
|
290
|
+
* unknown method. Called by `registerTools` when the policy lists an operation.
|
|
291
|
+
*/
|
|
292
|
+
export declare function requireTasksAttached(server: McpServer): void;
|
|
293
|
+
/**
|
|
294
|
+
* A tool's answer as the 2026-07-28 wire carries it, so as a completed task holds it: with the
|
|
295
|
+
* `resultType: "complete"` every result of that revision carries.
|
|
296
|
+
*
|
|
297
|
+
* **A task's result is exactly what the call would have answered**, and McpServer adds the
|
|
298
|
+
* discriminator to a blocking answer on its way out. A task's result reaches the client inside
|
|
299
|
+
* `tasks/get` instead, where nothing adds it, and the Tasks extension's reference client refuses a
|
|
300
|
+
* completed result without it.
|
|
301
|
+
*/
|
|
302
|
+
export declare function completed(result: CallToolResult): CallToolResult;
|
|
303
|
+
/**
|
|
304
|
+
* The MCP Tasks extension (`io.modelcontextprotocol/tasks`) lets a server answer `tools/call` with a
|
|
305
|
+
* task handle, which the client polls with `tasks/get`, answers with `tasks/update` and stops with
|
|
306
|
+
* `tasks/cancel`.
|
|
307
|
+
*
|
|
308
|
+
* **The declarations below have the shape of the SDK's own Tasks extension**, which is not yet in a
|
|
309
|
+
* release. A store written against them satisfies the SDK's `TaskStore` as well, so adopting the
|
|
310
|
+
* SDK's extension replaces one import and one `attach` call, and nothing else.
|
|
311
|
+
*/
|
|
312
|
+
export declare const TASKS_EXTENSION_ID = "io.modelcontextprotocol/tasks";
|
|
313
|
+
/**
|
|
314
|
+
* A task's state, as every task-related message carries it.
|
|
315
|
+
*
|
|
316
|
+
* **A type, not an interface, and its optional members admit `undefined`**, so a task typed by the
|
|
317
|
+
* SDK's own extension, an open object whose optional members do, is one of these and the reverse.
|
|
318
|
+
*/
|
|
319
|
+
export type Task = {
|
|
320
|
+
readonly taskId: string;
|
|
321
|
+
readonly status: "working" | "input_required" | "completed" | "cancelled" | "failed";
|
|
322
|
+
readonly statusMessage?: string | undefined;
|
|
323
|
+
/** ISO 8601. */
|
|
324
|
+
readonly createdAt: string;
|
|
325
|
+
/** ISO 8601. */
|
|
326
|
+
readonly lastUpdatedAt: string;
|
|
327
|
+
/** Retention from creation, in milliseconds; `null` is unlimited. */
|
|
328
|
+
readonly ttlMs: number | null;
|
|
329
|
+
/** How often a client should poll, in milliseconds. */
|
|
330
|
+
readonly pollIntervalMs?: number | undefined;
|
|
331
|
+
};
|
|
332
|
+
/** A request a task makes of the client while it waits, such as `elicitation/create`. */
|
|
333
|
+
export type TaskInputRequest = {
|
|
334
|
+
readonly method: string;
|
|
335
|
+
readonly params?: Readonly<Record<string, unknown>> | undefined;
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
* What `tasks/get` answers: the task, with what its status carries.
|
|
339
|
+
*
|
|
340
|
+
* **`completed` carries the `CallToolResult` the call would have returned, `isError` included.**
|
|
341
|
+
* `failed` is only for a protocol error during execution, never for a tool that answered an error.
|
|
342
|
+
*/
|
|
343
|
+
export type DetailedTask = (Task & {
|
|
344
|
+
readonly status: "working";
|
|
345
|
+
}) | (Task & {
|
|
346
|
+
readonly status: "input_required";
|
|
347
|
+
readonly inputRequests: Readonly<Record<string, TaskInputRequest>>;
|
|
348
|
+
}) | (Task & {
|
|
349
|
+
readonly status: "completed";
|
|
350
|
+
readonly result: Readonly<Record<string, unknown>>;
|
|
351
|
+
}) | (Task & {
|
|
352
|
+
readonly status: "failed";
|
|
353
|
+
readonly error: Readonly<Record<string, unknown>>;
|
|
354
|
+
}) | (Task & {
|
|
355
|
+
readonly status: "cancelled";
|
|
356
|
+
});
|
|
357
|
+
/** A tool's answer when its work continues as a task. */
|
|
358
|
+
export type CreateTaskResult = Task & {
|
|
359
|
+
readonly resultType: "task";
|
|
360
|
+
};
|
|
361
|
+
/** The handle a tool callback returns in place of a `CallToolResult`. */
|
|
362
|
+
export type TaskToolResult = CallToolResult & CreateTaskResult;
|
|
363
|
+
/** What the extension asks a store to create. */
|
|
364
|
+
export interface CreateTaskParams {
|
|
365
|
+
/** Retention from creation, in milliseconds; `null` is unlimited. */
|
|
366
|
+
readonly ttlMs: number | null;
|
|
367
|
+
readonly pollIntervalMs?: number;
|
|
368
|
+
/**
|
|
369
|
+
* The identity of the request that created the task. A store MUST refuse every later access
|
|
370
|
+
* from a different principal, answering as for an unknown task.
|
|
371
|
+
*/
|
|
372
|
+
readonly principal?: string;
|
|
373
|
+
/** The store's own: whatever its execution needs to find or start the work. */
|
|
374
|
+
readonly context?: Readonly<Record<string, unknown>>;
|
|
375
|
+
}
|
|
376
|
+
/** The identity presented on a `tasks/*` request. */
|
|
377
|
+
export interface TaskAccess {
|
|
378
|
+
readonly principal?: string;
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Where tasks live and how their work is found. The extension owns the protocol; the store owns the
|
|
382
|
+
* state, and the application owns the execution behind it: a queue, a workflow, a durable object.
|
|
383
|
+
*
|
|
384
|
+
* - `create` MUST NOT resolve before a following `get` would find the task.
|
|
385
|
+
* - `get`, `update` and `cancel` answer `undefined` or `false` for a task that is unknown, expired
|
|
386
|
+
* or another principal's, and the extension answers all three alike.
|
|
387
|
+
* - `cancel` is cooperative and idempotent on a finished task.
|
|
388
|
+
* - Task ids MUST be unguessable.
|
|
389
|
+
*/
|
|
390
|
+
export interface TaskStore {
|
|
391
|
+
create(params: CreateTaskParams): Promise<Task>;
|
|
392
|
+
get(taskId: string, access?: TaskAccess): Promise<DetailedTask | undefined>;
|
|
393
|
+
update(taskId: string, inputResponses: Readonly<Record<string, unknown>>, access?: TaskAccess): Promise<boolean>;
|
|
394
|
+
cancel(taskId: string, access?: TaskAccess): Promise<boolean>;
|
|
395
|
+
}
|
|
396
|
+
export interface TasksExtensionOptions {
|
|
397
|
+
/** Retention when `create` is given none, in milliseconds; `null` is unlimited. Default 24 hours. */
|
|
398
|
+
readonly defaultTtlMs?: number | null;
|
|
399
|
+
/** The polling hint when `create` is given none, in milliseconds. Default 5000. */
|
|
400
|
+
readonly defaultPollIntervalMs?: number;
|
|
401
|
+
/**
|
|
402
|
+
* The identity a task is bound to. Default: the OAuth client id, with the token subject when
|
|
403
|
+
* the token verifier puts one at `authInfo.extra.sub`. With no authentication it is `undefined`,
|
|
404
|
+
* and a task's id is then the only credential for it.
|
|
405
|
+
*/
|
|
406
|
+
readonly principal?: (context: ServerContext) => string | undefined;
|
|
407
|
+
}
|
|
408
|
+
export interface CreateTaskOptions {
|
|
409
|
+
readonly ttlMs?: number | null;
|
|
410
|
+
readonly pollIntervalMs?: number;
|
|
411
|
+
readonly context?: Readonly<Record<string, unknown>>;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Whether THIS request declared the Tasks extension. A task may be returned only to a request that
|
|
415
|
+
* did, whatever an earlier request declared.
|
|
416
|
+
*/
|
|
417
|
+
export declare function declaresTasks(context: ServerContext): boolean;
|
|
418
|
+
/**
|
|
419
|
+
* Serves the Tasks extension: its capability, `tasks/get`, `tasks/update` and `tasks/cancel`, over
|
|
420
|
+
* the {@link TaskStore} the application supplies. A tool answers with a task through
|
|
421
|
+
* {@link TasksExtension.create}.
|
|
422
|
+
*
|
|
423
|
+
* One instance serves any number of servers; {@link TasksExtension.attach} each one before it
|
|
424
|
+
* connects. Under `createMcpHandler`, which builds a server per request, create the extension
|
|
425
|
+
* once and attach it in the factory.
|
|
426
|
+
*/
|
|
427
|
+
export declare class TasksExtension {
|
|
428
|
+
#private;
|
|
429
|
+
readonly id = "io.modelcontextprotocol/tasks";
|
|
430
|
+
readonly store: TaskStore;
|
|
431
|
+
constructor(store: TaskStore, options?: TasksExtensionOptions);
|
|
432
|
+
/**
|
|
433
|
+
* Create a task for the current `tools/call`, bound to the caller, and return the handle the tool
|
|
434
|
+
* answers with. Refuses with `-32021` when the request did not declare the extension.
|
|
435
|
+
*/
|
|
436
|
+
create(context: ServerContext, options?: CreateTaskOptions): Promise<TaskToolResult>;
|
|
437
|
+
/**
|
|
438
|
+
* Serve the extension on `server`. Must run before it connects. Attaching this extension to the
|
|
439
|
+
* same server again does nothing; attaching a second Tasks extension throws and leaves the server
|
|
440
|
+
* as it was, because its `tasks/*` handlers would silently replace the first one's.
|
|
441
|
+
*/
|
|
442
|
+
attach(server: McpServer): void;
|
|
220
443
|
}
|