@toolgrain/mcp 0.3.0-alpha.2 → 0.3.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/CHANGELOG.md CHANGED
@@ -1,37 +1,26 @@
1
1
  # @toolgrain/mcp
2
2
 
3
- ## 0.3.0-alpha.2
3
+ ## 0.3.0
4
4
 
5
5
  ### Minor Changes
6
6
 
7
- - Add reusable fixed-URI resources with text and binary reads, request context, and endpoint authorization across both MCP revisions.
8
- - Add native MCP Apps protocol contracts and typed React widgets with shared-entry routing.
9
-
10
- ### Patch Changes
11
-
12
- - Updated dependencies
13
- - @toolgrain/protocol@0.3.0-alpha.2
14
-
15
- ## 0.3.0-alpha.1
16
-
17
- ### Patch Changes
18
-
19
- - Pace request-scoped progress notifications at 100 ms intervals while preserving ordered delivery, backpressure, and prompt cancellation. Reject duplicate active progress tokens on shared message connections, release tokens on completion or cancellation, and preserve isolation between independent HTTP response streams.
20
- - @toolgrain/protocol@0.3.0-alpha.1
21
-
22
- ## 0.3.0-alpha.0
23
-
24
- ### Minor Changes
25
-
26
- - Add request-scoped progress reporting through `ctx.reportProgress()` with stdio notifications and HTTP SSE delivery. The bound reporter honors client progress tokens, increasing values, backpressure, cancellation, and normal final tool results. HTTP opens SSE on the first progress update and retains JSON responses when no progress is sent.
27
-
7
+ - 0e1669b: Add request-scoped progress reporting through `ctx.reportProgress()` with stdio notifications and HTTP SSE delivery. The bound reporter honors client progress tokens, increasing values, backpressure, cancellation, and normal final tool results. HTTP opens SSE on the first progress update and retains JSON responses when no progress is sent.
8
+
28
9
  Custom message transports must accept progress notifications alongside protocol responses. Direct `server.handle()` integrations can supply `onNotification` for request-scoped delivery; contexts without a delivery channel use a no-op reporter.
29
-
10
+
30
11
  Keep final HTTP SSE delivery active until it completes or is cancelled, including server shutdown while the final write is blocked by backpressure.
12
+ - e4ac322: Add reusable fixed-URI resources with text and binary reads, request context, and endpoint authorization across both MCP revisions.
13
+ - 1cf25e6: Add typed content helpers for text, images, audio, resource links, and embedded files, with synchronous Uint8Array encoding and readonly authoring inputs.
14
+ - 1cde354: Add native MCP Apps protocol contracts and typed React widgets with shared-entry routing.
31
15
 
32
16
  ### Patch Changes
33
17
 
34
- - @toolgrain/protocol@0.3.0-alpha.0
18
+ - 0e1669b: Pace request-scoped progress notifications at 100 ms intervals while preserving ordered delivery, backpressure, and prompt cancellation. Reject duplicate active progress tokens on shared message connections, release tokens on completion or cancellation, and preserve isolation between independent HTTP response streams.
19
+ - Updated dependencies [414e8f5]
20
+ - Updated dependencies [1cde354]
21
+ - Updated dependencies [712e975]
22
+ - Updated dependencies [1cde354]
23
+ - @toolgrain/protocol@0.3.0
35
24
 
36
25
  ## 0.2.0
37
26
 
package/dist/index.d.ts CHANGED
@@ -14,6 +14,8 @@ export type { Resource, ResourceDefinition, ResourceMetadata, ResourceReadResult
14
14
  export { resource } from "./resources/definition.js";
15
15
  export type { HandleOptions, Server, ServerOptions } from "./server.js";
16
16
  export { createServer } from "./server.js";
17
+ export type { AudioContentOptions, BinaryContentData, ContentOptions, ImageContentOptions, ResourceContentOptions, ResourceLinkOptions, } from "./tools/content-helpers.js";
18
+ export { content } from "./tools/content-helpers.js";
17
19
  export type { ToolClient, ToolContext, ToolContextOptions, ToolRequest, } from "./tools/context.js";
18
20
  export { toolContext } from "./tools/context.js";
19
21
  export type { Tool, ToolDefinition, ToolMetadata } from "./tools/definition.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvD,YAAY,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO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AsB,EACtB,mBAAmB,GACpB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AACrD,YAAY,EACV,UAAU,EACV,WAAW,EACX,kBAAkB,EAClB,WAAW,GACZ,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,YAAY,EAAE,IAAI,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAChF,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,YAAY,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,YAAY,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EACV,cAAc,EACd,SAAS,EACT,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,YAAY,EACV,gBAAgB,EAChB,SAAS,EACT,kBAAkB,GACnB,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -7,6 +7,7 @@ export { authUrl as validateAuthUrl } from "./auth/url.js";
7
7
  export { createContext } from "./context/definition.js";
8
8
  export { resource } from "./resources/definition.js";
9
9
  export { createServer } from "./server.js";
10
+ export { content } from "./tools/content-helpers.js";
10
11
  export { toolContext } from "./tools/context.js";
11
12
  export { tool } from "./tools/definition.js";
12
13
  export { ToolNameError } from "./tools/name.js";
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;A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AA4B,CAAC;AAOrD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAShD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC"}
@@ -0,0 +1,57 @@
1
+ import type { AudioContent, BlobResourceContents, EmbeddedResource, ImageContent, ResourceLink, TextContent, TextResourceContents } from "@toolgrain/protocol";
2
+ import type { DeepReadonly } from "./json-value.js";
3
+ /** Base64-encoded data or bytes to encode synchronously; Node Buffers are accepted as Uint8Arrays. */
4
+ export type BinaryContentData = string | Uint8Array;
5
+ /** Optional content-block annotations and metadata, accepting readonly authoring values. */
6
+ export type ContentOptions = DeepReadonly<Pick<TextContent, "annotations" | "_meta">>;
7
+ /** Image content without its discriminator; data may be base64 or bytes. */
8
+ export type ImageContentOptions = Omit<DeepReadonly<ImageContent>, "type" | "data"> & {
9
+ /** Base64 image data or bytes to encode; the supplied byte view is not mutated. */
10
+ readonly data: BinaryContentData;
11
+ };
12
+ /** Audio content without its discriminator; data may be base64 or bytes. */
13
+ export type AudioContentOptions = Omit<DeepReadonly<AudioContent>, "type" | "data"> & {
14
+ /** Base64 audio data or bytes to encode; the supplied byte view is not mutated. */
15
+ readonly data: BinaryContentData;
16
+ };
17
+ /** A resource link's URI, required name, and optional descriptive fields. */
18
+ export type ResourceLinkOptions = Omit<DeepReadonly<ResourceLink>, "type">;
19
+ /** Embedded text or binary resource contents; text and blob are mutually exclusive. */
20
+ export type ResourceContentOptions = (DeepReadonly<TextResourceContents> & {
21
+ readonly blob?: never;
22
+ }) | (Omit<DeepReadonly<BlobResourceContents>, "blob"> & {
23
+ /** Base64 resource data or bytes to encode synchronously. */
24
+ readonly blob: BinaryContentData;
25
+ readonly text?: never;
26
+ });
27
+ /**
28
+ * Construct text content with optional annotations and metadata.
29
+ * Wrap the block in mcpResult() to return explicit MCP content from a tool.
30
+ */
31
+ declare function text(value: string, options?: ContentOptions): DeepReadonly<TextContent>;
32
+ /** Construct inline image content, synchronously encoding bytes when supplied. MIME type is required. */
33
+ declare function image(options: ImageContentOptions): DeepReadonly<ImageContent>;
34
+ /** Construct inline audio content, synchronously encoding bytes when supplied. MIME type is required. */
35
+ declare function audio(options: AudioContentOptions): DeepReadonly<AudioContent>;
36
+ /** Construct a link to an application-owned resource without fetching it or registering resource methods. */
37
+ declare function resourceLink(options: ResourceLinkOptions): DeepReadonly<ResourceLink>;
38
+ /**
39
+ * Embed text or binary resource contents without reading files or fetching URIs.
40
+ * The first argument's _meta belongs to the resource; options._meta belongs to the content block.
41
+ * Binary blob strings are base64; Uint8Array blobs are encoded synchronously.
42
+ */
43
+ declare function resource(value: ResourceContentOptions, options?: ContentOptions): DeepReadonly<EmbeddedResource>;
44
+ /**
45
+ * Typed constructors for mcpResult() content blocks. Accepts readonly inputs without mutating them.
46
+ * Helpers perform no I/O; the server validates and copies content during result conversion.
47
+ * Returned blocks have readonly authoring types; nested metadata is copied at server conversion.
48
+ */
49
+ export declare const content: Readonly<{
50
+ text: typeof text;
51
+ image: typeof image;
52
+ audio: typeof audio;
53
+ resourceLink: typeof resourceLink;
54
+ resource: typeof resource;
55
+ }>;
56
+ export {};
57
+ //# sourceMappingURL=content-helpers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-helpers.d.ts","sourceRoot":"","sources":["../../src/tools/content-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,oBAAoB,EACpB,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,oBAAoB,EACrB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAEpD,sGAAsG;AACtG,MAAM,MAAM,iBAAiB,GAAG,MAAM,GAAG,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AAO,EAAE,mBAAmB,GAAG,YAAY,CAAC,YAAY,CAAC,CAEvE;AAED,6GAA6G;AAC7G,iBAAS,YAAY,CACnB,OAAO,EAAE,mBAAmB,GAC3B,YAAY,CAAC,YAAY,CAAC,CAE5B;AAED;;;;GAIG;AACH,iBAAS,QAAQ,CACf,KAAK,EAAE,sBAAsB,EAC7B,OAAO,GAAE,cAAmB,GAC3B,YAAY,CAAC,gBAAgB,CAAC,CAShC;AAED;;;;GAIG;AACH,eAAO,MAAM,OAAO;;;;;;EAMlB,CAAC"}
@@ -0,0 +1,55 @@
1
+ function base64(data) {
2
+ if (typeof data === "string")
3
+ return data;
4
+ let binary = "";
5
+ // Bound the argument count for large files and encode only the supplied view.
6
+ for (let offset = 0; offset < data.length; offset += 0x8000)
7
+ binary += String.fromCharCode(...data.subarray(offset, offset + 0x8000));
8
+ return btoa(binary);
9
+ }
10
+ /**
11
+ * Construct text content with optional annotations and metadata.
12
+ * Wrap the block in mcpResult() to return explicit MCP content from a tool.
13
+ */
14
+ function text(value, options = {}) {
15
+ return { ...options, type: "text", text: value };
16
+ }
17
+ /** Construct inline image content, synchronously encoding bytes when supplied. MIME type is required. */
18
+ function image(options) {
19
+ return { ...options, type: "image", data: base64(options.data) };
20
+ }
21
+ /** Construct inline audio content, synchronously encoding bytes when supplied. MIME type is required. */
22
+ function audio(options) {
23
+ return { ...options, type: "audio", data: base64(options.data) };
24
+ }
25
+ /** Construct a link to an application-owned resource without fetching it or registering resource methods. */
26
+ function resourceLink(options) {
27
+ return { ...options, type: "resource_link" };
28
+ }
29
+ /**
30
+ * Embed text or binary resource contents without reading files or fetching URIs.
31
+ * The first argument's _meta belongs to the resource; options._meta belongs to the content block.
32
+ * Binary blob strings are base64; Uint8Array blobs are encoded synchronously.
33
+ */
34
+ function resource(value, options = {}) {
35
+ return {
36
+ ...options,
37
+ type: "resource",
38
+ resource: value.blob === undefined
39
+ ? { ...value }
40
+ : { ...value, blob: base64(value.blob) },
41
+ };
42
+ }
43
+ /**
44
+ * Typed constructors for mcpResult() content blocks. Accepts readonly inputs without mutating them.
45
+ * Helpers perform no I/O; the server validates and copies content during result conversion.
46
+ * Returned blocks have readonly authoring types; nested metadata is copied at server conversion.
47
+ */
48
+ export const content = Object.freeze({
49
+ text,
50
+ image,
51
+ audio,
52
+ resourceLink,
53
+ resource,
54
+ });
55
+ //# sourceMappingURL=content-helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content-helpers.js","sourceRoot":"","sources":["../../src/tools/content-helpers.ts"],"names":[],"mappings":"AAiDA,SAAS,MAAM,CAAC,IAAuB;IACrC,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC1C,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,8EAA8E;IAC9E,KAAK,IAAI,MAAM,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,MAAM,IAAI,MAAM;QACzD,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC;IAC3E,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;AACtB,CAAC;AAED;;;GAGG;AACH,SAAS,IAAI,CACX,KAAa,EACb,OAAO,GAAmB,EAAE;IAE5B,OAAO,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AACnD,CAAC;AAED,yGAAyG;AACzG,SAAS,KAAK,CAAC,OAA4B;IACzC,OAAO,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;AACnE,CAAC;AAED,yGAAyG;AACzG,SAAS,KAAK,CAAC,OAA4B;IACzC,OAAO,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;AACnE,CAAC;AAED,6GAA6G;AAC7G,SAAS,YAAY,CACnB,OAA4B;IAE5B,OAAO,EAAE,GAAG,OAAO,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC;AAC/C,CAAC;AAED;;;;GAIG;AACH,SAAS,QAAQ,CACf,KAA6B,EAC7B,OAAO,GAAmB,EAAE;IAE5B,OAAO;QACL,GAAG,OAAO;QACV,IAAI,EAAE,UAAU;QAChB,QAAQ,EACN,KAAK,CAAC,IAAI,KAAK,SAAS;YACtB,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE;YACd,CAAC,CAAC,EAAE,GAAG,KAAK,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,EAAE;KAC7C,CAAC;AACJ,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC;IACnC,IAAI;IACJ,KAAK;IACL,KAAK;IACL,YAAY;IACZ,QAAQ;CACT,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toolgrain/mcp",
3
- "version": "0.3.0-alpha.2",
3
+ "version": "0.3.0",
4
4
  "description": "Define reusable, typed MCP tools and servers with Zod validation.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -23,7 +23,7 @@
23
23
  "publishConfig": {
24
24
  "access": "public",
25
25
  "registry": "https://registry.npmjs.org/",
26
- "tag": "alpha"
26
+ "tag": "latest"
27
27
  },
28
28
  "type": "module",
29
29
  "exports": {
@@ -59,7 +59,7 @@
59
59
  "@toolgrain/repo-tools": "0.0.0"
60
60
  },
61
61
  "dependencies": {
62
- "@toolgrain/protocol": "0.3.0-alpha.2"
62
+ "@toolgrain/protocol": "0.3.0"
63
63
  },
64
64
  "engines": {
65
65
  "node": "^24.20.0",
package/src/index.ts CHANGED
@@ -25,6 +25,15 @@ export type {
25
25
  export { resource } from "./resources/definition.js";
26
26
  export type { HandleOptions, Server, ServerOptions } from "./server.js";
27
27
  export { createServer } from "./server.js";
28
+ export type {
29
+ AudioContentOptions,
30
+ BinaryContentData,
31
+ ContentOptions,
32
+ ImageContentOptions,
33
+ ResourceContentOptions,
34
+ ResourceLinkOptions,
35
+ } from "./tools/content-helpers.js";
36
+ export { content } from "./tools/content-helpers.js";
28
37
  export type {
29
38
  ToolClient,
30
39
  ToolContext,
@@ -0,0 +1,117 @@
1
+ import type {
2
+ AudioContent,
3
+ BlobResourceContents,
4
+ EmbeddedResource,
5
+ ImageContent,
6
+ ResourceLink,
7
+ TextContent,
8
+ TextResourceContents,
9
+ } from "@toolgrain/protocol";
10
+ import type { DeepReadonly } from "./json-value.js";
11
+
12
+ /** Base64-encoded data or bytes to encode synchronously; Node Buffers are accepted as Uint8Arrays. */
13
+ export type BinaryContentData = string | Uint8Array;
14
+
15
+ /** Optional content-block annotations and metadata, accepting readonly authoring values. */
16
+ export type ContentOptions = DeepReadonly<
17
+ Pick<TextContent, "annotations" | "_meta">
18
+ >;
19
+
20
+ /** Image content without its discriminator; data may be base64 or bytes. */
21
+ export type ImageContentOptions = Omit<
22
+ DeepReadonly<ImageContent>,
23
+ "type" | "data"
24
+ > & {
25
+ /** Base64 image data or bytes to encode; the supplied byte view is not mutated. */
26
+ readonly data: BinaryContentData;
27
+ };
28
+
29
+ /** Audio content without its discriminator; data may be base64 or bytes. */
30
+ export type AudioContentOptions = Omit<
31
+ DeepReadonly<AudioContent>,
32
+ "type" | "data"
33
+ > & {
34
+ /** Base64 audio data or bytes to encode; the supplied byte view is not mutated. */
35
+ readonly data: BinaryContentData;
36
+ };
37
+
38
+ /** A resource link's URI, required name, and optional descriptive fields. */
39
+ export type ResourceLinkOptions = Omit<DeepReadonly<ResourceLink>, "type">;
40
+
41
+ /** Embedded text or binary resource contents; text and blob are mutually exclusive. */
42
+ export type ResourceContentOptions =
43
+ | (DeepReadonly<TextResourceContents> & { readonly blob?: never })
44
+ | (Omit<DeepReadonly<BlobResourceContents>, "blob"> & {
45
+ /** Base64 resource data or bytes to encode synchronously. */
46
+ readonly blob: BinaryContentData;
47
+ readonly text?: never;
48
+ });
49
+
50
+ function base64(data: BinaryContentData): string {
51
+ if (typeof data === "string") return data;
52
+ let binary = "";
53
+ // Bound the argument count for large files and encode only the supplied view.
54
+ for (let offset = 0; offset < data.length; offset += 0x8000)
55
+ binary += String.fromCharCode(...data.subarray(offset, offset + 0x8000));
56
+ return btoa(binary);
57
+ }
58
+
59
+ /**
60
+ * Construct text content with optional annotations and metadata.
61
+ * Wrap the block in mcpResult() to return explicit MCP content from a tool.
62
+ */
63
+ function text(
64
+ value: string,
65
+ options: ContentOptions = {},
66
+ ): DeepReadonly<TextContent> {
67
+ return { ...options, type: "text", text: value };
68
+ }
69
+
70
+ /** Construct inline image content, synchronously encoding bytes when supplied. MIME type is required. */
71
+ function image(options: ImageContentOptions): DeepReadonly<ImageContent> {
72
+ return { ...options, type: "image", data: base64(options.data) };
73
+ }
74
+
75
+ /** Construct inline audio content, synchronously encoding bytes when supplied. MIME type is required. */
76
+ function audio(options: AudioContentOptions): DeepReadonly<AudioContent> {
77
+ return { ...options, type: "audio", data: base64(options.data) };
78
+ }
79
+
80
+ /** Construct a link to an application-owned resource without fetching it or registering resource methods. */
81
+ function resourceLink(
82
+ options: ResourceLinkOptions,
83
+ ): DeepReadonly<ResourceLink> {
84
+ return { ...options, type: "resource_link" };
85
+ }
86
+
87
+ /**
88
+ * Embed text or binary resource contents without reading files or fetching URIs.
89
+ * The first argument's _meta belongs to the resource; options._meta belongs to the content block.
90
+ * Binary blob strings are base64; Uint8Array blobs are encoded synchronously.
91
+ */
92
+ function resource(
93
+ value: ResourceContentOptions,
94
+ options: ContentOptions = {},
95
+ ): DeepReadonly<EmbeddedResource> {
96
+ return {
97
+ ...options,
98
+ type: "resource",
99
+ resource:
100
+ value.blob === undefined
101
+ ? { ...value }
102
+ : { ...value, blob: base64(value.blob) },
103
+ };
104
+ }
105
+
106
+ /**
107
+ * Typed constructors for mcpResult() content blocks. Accepts readonly inputs without mutating them.
108
+ * Helpers perform no I/O; the server validates and copies content during result conversion.
109
+ * Returned blocks have readonly authoring types; nested metadata is copied at server conversion.
110
+ */
111
+ export const content = Object.freeze({
112
+ text,
113
+ image,
114
+ audio,
115
+ resourceLink,
116
+ resource,
117
+ });