typespec-http-mcp 0.9.0 → 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 CHANGED
@@ -150,20 +150,37 @@ client that did not declare the extension is answered inline, as before. See
150
150
  Nothing is invented. Every field of a tool comes from something the document already states, or from
151
151
  a published specification:
152
152
 
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.readOnlyHint` | the method's safety, per IANA's HTTP Method Registry |
161
- | `annotations.idempotentHint` | the method's idempotence, same source |
162
- | which operations are tools | `@tag`, which `@typespec/openapi3` publishes |
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 |
163
166
 
164
167
  There are **no decorators**. A decorator would let a spec state something the OpenAPI document
165
168
  cannot publish, so the emitted surface would enforce a rule no reader of the contract can see.
166
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
+
167
184
  ## Documentation
168
185
 
169
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` | no external source; see below |
13
- * | `openWorldHint` | not derivable, never written |
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 never written. Whether an operation reaches beyond this service is not
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;
@@ -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` | no external source; see below |
13
- * | `openWorldHint` | not derivable, never written |
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 never written. Whether an operation reaches beyond this service is not
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
  }
@@ -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
  *
@@ -1,4 +1,5 @@
1
1
  import { type EmittedRoute, type RouteSchemaNames } from "typespec-http-zod";
2
+ import { type DeclaredHints } from "./annotations.js";
2
3
  import type { SecurityRequirement } from "./runtime.js";
3
4
  /**
4
5
  * Where emitted output imports its runtime from: the copy this package writes beside it.
@@ -25,6 +26,8 @@ export interface ToolEntry {
25
26
  readonly requirements: readonly SecurityRequirement[];
26
27
  readonly description: string | undefined;
27
28
  readonly title: string | undefined;
29
+ /** The hints the operation states in `x-mcp-annotations`, over those its method gives. */
30
+ readonly hints: DeclaredHints;
28
31
  }
29
32
  export interface RenderedTools {
30
33
  readonly text: string;
package/dist/src/tools.js CHANGED
@@ -20,7 +20,7 @@ function renderSecurity(requirements) {
20
20
  }
21
21
  /** The annotations as a TypeScript object literal, for the generated call site. */
22
22
  function renderAnnotations(annotations) {
23
- const entries = Object.entries(annotations).map(([key, value]) => `${key}: ${String(value)}`);
23
+ const entries = Object.entries(annotations).map(([key, value]) => `${key}: ${JSON.stringify(value)}`);
24
24
  return `{ ${entries.join(", ")} }`;
25
25
  }
26
26
  /**
@@ -484,7 +484,7 @@ export function renderTools(entries, declaredOperationIds, options) {
484
484
  ${literal(entry.toolName)},
485
485
  {${title}${description}
486
486
  inputSchema: ${inputSchemaName(entry)},${outputSchema}
487
- annotations: ${renderAnnotations(annotationsFor(entry.route.verb))},
487
+ annotations: ${renderAnnotations(annotationsFor(entry.route.verb, entry.hints, entry.title))},
488
488
  },
489
489
  async (${argumentName}, context) => {
490
490
  const ctx = deps.context(context, ${literal(entry.route.authentication)});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "typespec-http-mcp",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "TypeSpec emitter: generate an MCP tool surface, and the Zod validators it publishes, from an HTTP service definition, dispatching into the same handlers a generated HTTP server calls.",
5
5
  "keywords": [
6
6
  "cloudflare-workers",