@dev-crowagent/pigeon-mcp 0.0.0-stage → 1.7.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.
Files changed (136) hide show
  1. package/CHANGELOG.md +143 -0
  2. package/README.md +256 -2
  3. package/THIRD_PARTY_NOTICES/modelcontextprotocol-typescript-sdk.txt +21 -0
  4. package/dist/auth-provider.d.ts +19 -0
  5. package/dist/auth-provider.d.ts.map +1 -0
  6. package/dist/auth-provider.js +2 -0
  7. package/dist/auth-provider.js.map +1 -0
  8. package/dist/client.d.ts +193 -0
  9. package/dist/client.d.ts.map +1 -0
  10. package/dist/client.js +747 -0
  11. package/dist/client.js.map +1 -0
  12. package/dist/config-schema.d.ts +105 -0
  13. package/dist/config-schema.d.ts.map +1 -0
  14. package/dist/config-schema.js +101 -0
  15. package/dist/config-schema.js.map +1 -0
  16. package/dist/config.d.ts +57 -0
  17. package/dist/config.d.ts.map +1 -0
  18. package/dist/config.js +160 -0
  19. package/dist/config.js.map +1 -0
  20. package/dist/index.d.ts +16 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +14 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/input-schema.d.ts +7 -0
  25. package/dist/input-schema.d.ts.map +1 -0
  26. package/dist/input-schema.js +13 -0
  27. package/dist/input-schema.js.map +1 -0
  28. package/dist/model-content.d.ts +19 -0
  29. package/dist/model-content.d.ts.map +1 -0
  30. package/dist/model-content.js +67 -0
  31. package/dist/model-content.js.map +1 -0
  32. package/dist/naming.d.ts +23 -0
  33. package/dist/naming.d.ts.map +1 -0
  34. package/dist/naming.js +37 -0
  35. package/dist/naming.js.map +1 -0
  36. package/dist/oauth/auth-provider.d.ts +52 -0
  37. package/dist/oauth/auth-provider.d.ts.map +1 -0
  38. package/dist/oauth/auth-provider.js +97 -0
  39. package/dist/oauth/auth-provider.js.map +1 -0
  40. package/dist/oauth/callback.d.ts +48 -0
  41. package/dist/oauth/callback.d.ts.map +1 -0
  42. package/dist/oauth/callback.js +121 -0
  43. package/dist/oauth/callback.js.map +1 -0
  44. package/dist/oauth/client-settings.d.ts +107 -0
  45. package/dist/oauth/client-settings.d.ts.map +1 -0
  46. package/dist/oauth/client-settings.js +114 -0
  47. package/dist/oauth/client-settings.js.map +1 -0
  48. package/dist/oauth/discovery.d.ts +30 -0
  49. package/dist/oauth/discovery.d.ts.map +1 -0
  50. package/dist/oauth/discovery.js +156 -0
  51. package/dist/oauth/discovery.js.map +1 -0
  52. package/dist/oauth/errors.d.ts +32 -0
  53. package/dist/oauth/errors.d.ts.map +1 -0
  54. package/dist/oauth/errors.js +58 -0
  55. package/dist/oauth/errors.js.map +1 -0
  56. package/dist/oauth/flow.d.ts +112 -0
  57. package/dist/oauth/flow.d.ts.map +1 -0
  58. package/dist/oauth/flow.js +360 -0
  59. package/dist/oauth/flow.js.map +1 -0
  60. package/dist/oauth/index.d.ts +11 -0
  61. package/dist/oauth/index.d.ts.map +1 -0
  62. package/dist/oauth/index.js +9 -0
  63. package/dist/oauth/index.js.map +1 -0
  64. package/dist/oauth/loopback.d.ts +51 -0
  65. package/dist/oauth/loopback.d.ts.map +1 -0
  66. package/dist/oauth/loopback.js +98 -0
  67. package/dist/oauth/loopback.js.map +1 -0
  68. package/dist/oauth/provider.d.ts +62 -0
  69. package/dist/oauth/provider.d.ts.map +1 -0
  70. package/dist/oauth/provider.js +118 -0
  71. package/dist/oauth/provider.js.map +1 -0
  72. package/dist/oauth/sign-in.d.ts +73 -0
  73. package/dist/oauth/sign-in.d.ts.map +1 -0
  74. package/dist/oauth/sign-in.js +182 -0
  75. package/dist/oauth/sign-in.js.map +1 -0
  76. package/dist/oauth/types.d.ts +83 -0
  77. package/dist/oauth/types.d.ts.map +1 -0
  78. package/dist/oauth/types.js +113 -0
  79. package/dist/oauth/types.js.map +1 -0
  80. package/dist/output-limit.d.ts +24 -0
  81. package/dist/output-limit.d.ts.map +1 -0
  82. package/dist/output-limit.js +58 -0
  83. package/dist/output-limit.js.map +1 -0
  84. package/dist/protocol/content.d.ts +83 -0
  85. package/dist/protocol/content.d.ts.map +1 -0
  86. package/dist/protocol/content.js +41 -0
  87. package/dist/protocol/content.js.map +1 -0
  88. package/dist/protocol/http-headers.d.ts +31 -0
  89. package/dist/protocol/http-headers.d.ts.map +1 -0
  90. package/dist/protocol/http-headers.js +129 -0
  91. package/dist/protocol/http-headers.js.map +1 -0
  92. package/dist/protocol/jsonrpc.d.ts +64 -0
  93. package/dist/protocol/jsonrpc.d.ts.map +1 -0
  94. package/dist/protocol/jsonrpc.js +74 -0
  95. package/dist/protocol/jsonrpc.js.map +1 -0
  96. package/dist/protocol/stateless.d.ts +47 -0
  97. package/dist/protocol/stateless.d.ts.map +1 -0
  98. package/dist/protocol/stateless.js +102 -0
  99. package/dist/protocol/stateless.js.map +1 -0
  100. package/dist/protocol/types.d.ts +156 -0
  101. package/dist/protocol/types.d.ts.map +1 -0
  102. package/dist/protocol/types.js +12 -0
  103. package/dist/protocol/types.js.map +1 -0
  104. package/dist/result-schema.d.ts +10 -0
  105. package/dist/result-schema.d.ts.map +1 -0
  106. package/dist/result-schema.js +78 -0
  107. package/dist/result-schema.js.map +1 -0
  108. package/dist/testing/index.d.ts +2 -0
  109. package/dist/testing/index.d.ts.map +1 -0
  110. package/dist/testing/index.js +2 -0
  111. package/dist/testing/index.js.map +1 -0
  112. package/dist/transports/in-memory.d.ts +19 -0
  113. package/dist/transports/in-memory.d.ts.map +1 -0
  114. package/dist/transports/in-memory.js +50 -0
  115. package/dist/transports/in-memory.js.map +1 -0
  116. package/dist/transports/lines.d.ts +34 -0
  117. package/dist/transports/lines.d.ts.map +1 -0
  118. package/dist/transports/lines.js +85 -0
  119. package/dist/transports/lines.js.map +1 -0
  120. package/dist/transports/pipe.d.ts +49 -0
  121. package/dist/transports/pipe.d.ts.map +1 -0
  122. package/dist/transports/pipe.js +96 -0
  123. package/dist/transports/pipe.js.map +1 -0
  124. package/dist/transports/stdio.d.ts +29 -0
  125. package/dist/transports/stdio.d.ts.map +1 -0
  126. package/dist/transports/stdio.js +170 -0
  127. package/dist/transports/stdio.js.map +1 -0
  128. package/dist/transports/streamable-http.d.ts +113 -0
  129. package/dist/transports/streamable-http.d.ts.map +1 -0
  130. package/dist/transports/streamable-http.js +586 -0
  131. package/dist/transports/streamable-http.js.map +1 -0
  132. package/dist/transports/transport.d.ts +48 -0
  133. package/dist/transports/transport.d.ts.map +1 -0
  134. package/dist/transports/transport.js +38 -0
  135. package/dist/transports/transport.js.map +1 -0
  136. package/package.json +65 -5
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../../src/protocol/content.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,EAAE,CAAC,MAAM,GAAG,WAAW,CAAC,EAAE,CAAC;IACpC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,YAAY;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,YAAY;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,kBAAkB,CAAC;IACjC,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO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AC;IAClB,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC;AAED;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AA4B5G;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,IAAI,CAAC,cAAc,EAAE,SAAS,GAAG,mBAAmB,CAAC,GAAG,UAAU,EAAE,CAMxG"}
@@ -0,0 +1,41 @@
1
+ function blockToLlmContent(block) {
2
+ switch (block.type) {
3
+ case "text":
4
+ return { type: "text", text: block.text };
5
+ case "image":
6
+ return { type: "image", data: block.data, mimeType: block.mimeType };
7
+ case "audio":
8
+ return { type: "text", text: `[audio ${block.mimeType} omitted]` };
9
+ case "resource_link":
10
+ return { type: "text", text: `${block.name}: ${block.uri}` };
11
+ case "resource": {
12
+ const resource = block.resource;
13
+ if ("text" in resource)
14
+ return { type: "text", text: resource.text };
15
+ if (resource.mimeType?.startsWith("image/")) {
16
+ return { type: "image", data: resource.blob, mimeType: resource.mimeType };
17
+ }
18
+ return {
19
+ type: "text",
20
+ text: `[binary resource ${resource.uri} (${resource.mimeType ?? "unknown type"}) omitted]`,
21
+ };
22
+ }
23
+ default:
24
+ return { type: "text", text: `[unsupported MCP content ${block.type}]` };
25
+ }
26
+ }
27
+ /**
28
+ * Convert a tool result to text and image content for a model. Text and images pass through,
29
+ * embedded text resources become text, embedded image resources become images, and other blocks
30
+ * (audio, resource links, binary resources) become a short text placeholder. A result without
31
+ * content blocks but with `structuredContent` becomes its JSON, since servers should, but do not
32
+ * always, mirror structured results as text.
33
+ */
34
+ export function toLlmContent(result) {
35
+ const content = (result.content ?? []).map(blockToLlmContent);
36
+ if (content.length === 0 && result.structuredContent !== undefined) {
37
+ content.push({ type: "text", text: JSON.stringify(result.structuredContent, null, 2) });
38
+ }
39
+ return content;
40
+ }
41
+ //# sourceMappingURL=content.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content.js","sourceRoot":"","sources":["../../src/protocol/content.ts"],"names":[],"mappings":"AA8EA,SAAS,iBAAiB,CAAC,KAAmB;IAC7C,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,MAAM;YACV,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,CAAC;QAC3C,KAAK,OAAO;YACX,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,QAAQ,EAAE,KAAK,CAAC,QAAQ,EAAE,CAAC;QACtE,KAAK,OAAO;YACX,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,KAAK,CAAC,QAAQ,WAAW,EAAE,CAAC;QACpE,KAAK,eAAe;YACnB,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC,IAAI,KAAK,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC;QAC9D,KAAK,UAAU,EAAE,CAAC;YACjB,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,CAAC;YAChC,IAAI,MAAM,IAAI,QAAQ;gBAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,CAAC;YACrE,IAAI,QAAQ,CAAC,QAAQ,EAAE,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;gBAC7C,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC;YAC5E,CAAC;YACD,OAAO;gBACN,IAAI,EAAE,MAAM;gBACZ,IAAI,EAAE,oBAAoB,QAAQ,CAAC,GAAG,KAAK,QAAQ,CAAC,QAAQ,IAAI,cAAc,YAAY;aAC1F,CAAC;QACH,CAAC;QACD;YACC,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,4BAA6B,KAA0B,CAAC,IAAI,GAAG,EAAE,CAAC;IACjG,CAAC;AACF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,MAA6D;IACzF,MAAM,OAAO,GAAG,CAAC,MAAM,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;IAC9D,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC,IAAI,MAAM,CAAC,iBAAiB,KAAK,SAAS,EAAE,CAAC;QACpE,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,iBAAiB,EAAE,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC;IACzF,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC","sourcesContent":["export interface ContentAnnotations {\n\taudience?: (\"user\" | \"assistant\")[];\n\tpriority?: number;\n\tlastModified?: string;\n}\n\nexport interface TextContent {\n\ttype: \"text\";\n\ttext: string;\n\tannotations?: ContentAnnotations;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface ImageContent {\n\ttype: \"image\";\n\tdata: string;\n\tmimeType: string;\n\tannotations?: ContentAnnotations;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface AudioContent {\n\ttype: \"audio\";\n\tdata: string;\n\tmimeType: string;\n\tannotations?: ContentAnnotations;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface ResourceLinkContent {\n\ttype: \"resource_link\";\n\turi: string;\n\tname: string;\n\ttitle?: string;\n\tdescription?: string;\n\tmimeType?: string;\n\tsize?: number;\n\tannotations?: ContentAnnotations;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface TextResourceContents {\n\turi: string;\n\tmimeType?: string;\n\ttext: string;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface BlobResourceContents {\n\turi: string;\n\tmimeType?: string;\n\tblob: string;\n\t_meta?: Record<string, unknown>;\n}\n\nexport interface EmbeddedResourceContent {\n\ttype: \"resource\";\n\tresource: TextResourceContents | BlobResourceContents;\n\tannotations?: ContentAnnotations;\n\t_meta?: Record<string, unknown>;\n}\n\nexport type ContentBlock = TextContent | ImageContent | AudioContent | ResourceLinkContent | EmbeddedResourceContent;\n\nexport interface CallToolResult {\n\tcontent: ContentBlock[];\n\t/** Any JSON value, including arrays and `null` (MCP 2026-07-28; earlier revisions allowed only objects). */\n\tstructuredContent?: unknown;\n\tisError?: boolean;\n\t_meta?: Record<string, unknown>;\n}\n\n/**\n * Tool result content in the shape LLM APIs accept: text and base64 images. Matches the\n * `TextContent` and `ImageContent` types of `@dev-crowagent/pigeon-ai`.\n */\nexport type LlmContent = { type: \"text\"; text: string } | { type: \"image\"; data: string; mimeType: string };\n\nfunction blockToLlmContent(block: ContentBlock): LlmContent {\n\tswitch (block.type) {\n\t\tcase \"text\":\n\t\t\treturn { type: \"text\", text: block.text };\n\t\tcase \"image\":\n\t\t\treturn { type: \"image\", data: block.data, mimeType: block.mimeType };\n\t\tcase \"audio\":\n\t\t\treturn { type: \"text\", text: `[audio ${block.mimeType} omitted]` };\n\t\tcase \"resource_link\":\n\t\t\treturn { type: \"text\", text: `${block.name}: ${block.uri}` };\n\t\tcase \"resource\": {\n\t\t\tconst resource = block.resource;\n\t\t\tif (\"text\" in resource) return { type: \"text\", text: resource.text };\n\t\t\tif (resource.mimeType?.startsWith(\"image/\")) {\n\t\t\t\treturn { type: \"image\", data: resource.blob, mimeType: resource.mimeType };\n\t\t\t}\n\t\t\treturn {\n\t\t\t\ttype: \"text\",\n\t\t\t\ttext: `[binary resource ${resource.uri} (${resource.mimeType ?? \"unknown type\"}) omitted]`,\n\t\t\t};\n\t\t}\n\t\tdefault:\n\t\t\treturn { type: \"text\", text: `[unsupported MCP content ${(block as { type: string }).type}]` };\n\t}\n}\n\n/**\n * Convert a tool result to text and image content for a model. Text and images pass through,\n * embedded text resources become text, embedded image resources become images, and other blocks\n * (audio, resource links, binary resources) become a short text placeholder. A result without\n * content blocks but with `structuredContent` becomes its JSON, since servers should, but do not\n * always, mirror structured results as text.\n */\nexport function toLlmContent(result: Pick<CallToolResult, \"content\" | \"structuredContent\">): LlmContent[] {\n\tconst content = (result.content ?? []).map(blockToLlmContent);\n\tif (content.length === 0 && result.structuredContent !== undefined) {\n\t\tcontent.push({ type: \"text\", text: JSON.stringify(result.structuredContent, null, 2) });\n\t}\n\treturn content;\n}\n"]}
@@ -0,0 +1,31 @@
1
+ /** One tool argument mirrored into a header: the `properties` path to it and the header name part. */
2
+ export interface ToolHeaderBinding {
3
+ path: string[];
4
+ header: string;
5
+ }
6
+ /** A tool's header bindings, or why its `x-mcp-header` annotations are invalid. */
7
+ export type ToolHeaderBindings = {
8
+ kind: "valid";
9
+ bindings: ToolHeaderBinding[];
10
+ } | {
11
+ kind: "invalid";
12
+ reason: string;
13
+ };
14
+ /**
15
+ * A header value for `value`: as is when it is plain ASCII without leading or trailing whitespace,
16
+ * otherwise Base64 of its UTF-8 in the `=?base64?…?=` form. Values that look like that form are
17
+ * encoded too, so they are not mistaken for it.
18
+ */
19
+ export declare function encodeHeaderValue(value: string): string;
20
+ /** `Mcp-Method`, and `Mcp-Name` for the methods that name a tool, prompt, or resource. */
21
+ export declare function standardRequestHeaders(method: string, params: unknown): Record<string, string>;
22
+ /**
23
+ * The arguments a tool mirrors into headers (wire input). Annotations are valid only on `string`,
24
+ * `integer`, and `boolean` properties reached from the schema root through `properties` alone, with
25
+ * non-empty token names that are unique ignoring case. Clients on HTTP exclude tools with an invalid
26
+ * annotation.
27
+ */
28
+ export declare function toolHeaderBindings(inputSchema: Record<string, unknown>): ToolHeaderBindings;
29
+ /** `Mcp-Param-*` headers for a tool call. Arguments that are absent, null, or not primitive get no header. */
30
+ export declare function toolParamHeaders(bindings: readonly ToolHeaderBinding[], args: Record<string, unknown> | undefined): Record<string, string>;
31
+ //# sourceMappingURL=http-headers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-headers.d.ts","sourceRoot":"","sources":["../../src/protocol/http-headers.ts"],"names":[],"mappings":"AAIA,sGAAsG;AACtG,MAAM,WAAW,iBAAiB;IACjC,IAAI,EAAE,MAAM,EAAE,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CACf;AAED,mFAAmF;AACnF,MAAM,MAAM,kBAAkB,GAAG;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,iBAAiB,EAAE,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,SAAS,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAYxH;;;;GAIG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAOvD;AAED,0FAA0F;AAC1F,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAQ9F;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,kBAAkB,CAwC3F;AAmBD,8GAA8G;AAC9G,wBAAgB,gBAAgB,CAC/B,QAAQ,EAAE,SAAS,iBAAiB,EAAE,EACtC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,GACvC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CASxB"}
@@ -0,0 +1,129 @@
1
+ // HTTP headers that MCP 2026-07-28 mirrors from the request body: `Mcp-Method`, `Mcp-Name`, and
2
+ // `Mcp-Param-*` for tool arguments a tool's input schema marks with `x-mcp-header`.
3
+ import { isObject } from "./jsonrpc.js";
4
+ const HEADER_ANNOTATION = "x-mcp-header";
5
+ const BASE64_SENTINEL = /^=\?base64\?.*\?=$/s;
6
+ // RFC 9110 token characters.
7
+ const HEADER_TOKEN = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
8
+ const PRIMITIVE_TYPES = new Set(["string", "integer", "boolean"]);
9
+ // Input schemas come from the server; deeper nesting is not worth walking.
10
+ const MAX_SCHEMA_DEPTH = 64;
11
+ // Keywords whose values are instance data, not schemas, so an `x-mcp-header` key in them is no annotation.
12
+ const DATA_KEYWORDS = new Set(["const", "default", "enum", "examples"]);
13
+ /**
14
+ * A header value for `value`: as is when it is plain ASCII without leading or trailing whitespace,
15
+ * otherwise Base64 of its UTF-8 in the `=?base64?…?=` form. Values that look like that form are
16
+ * encoded too, so they are not mistaken for it.
17
+ */
18
+ export function encodeHeaderValue(value) {
19
+ let plain = value === value.trim() && !BASE64_SENTINEL.test(value);
20
+ for (let i = 0; plain && i < value.length; i++) {
21
+ const code = value.charCodeAt(i);
22
+ plain = code >= 0x20 && code <= 0x7e;
23
+ }
24
+ return plain ? value : `=?base64?${Buffer.from(value, "utf8").toString("base64")}?=`;
25
+ }
26
+ /** `Mcp-Method`, and `Mcp-Name` for the methods that name a tool, prompt, or resource. */
27
+ export function standardRequestHeaders(method, params) {
28
+ const headers = { "Mcp-Method": method };
29
+ const name = method === "resources/read"
30
+ ? isObject(params) && params.uri
31
+ : (method === "tools/call" || method === "prompts/get") && isObject(params) && params.name;
32
+ if (typeof name === "string")
33
+ headers["Mcp-Name"] = encodeHeaderValue(name);
34
+ return headers;
35
+ }
36
+ /**
37
+ * The arguments a tool mirrors into headers (wire input). Annotations are valid only on `string`,
38
+ * `integer`, and `boolean` properties reached from the schema root through `properties` alone, with
39
+ * non-empty token names that are unique ignoring case. Clients on HTTP exclude tools with an invalid
40
+ * annotation.
41
+ */
42
+ export function toolHeaderBindings(inputSchema) {
43
+ const bindings = [];
44
+ const seen = new Set();
45
+ let problem;
46
+ const visit = (schema, path, depth) => {
47
+ if (problem || depth > MAX_SCHEMA_DEPTH)
48
+ return;
49
+ if (Array.isArray(schema)) {
50
+ for (const item of schema)
51
+ visit(item, undefined, depth + 1);
52
+ return;
53
+ }
54
+ if (!isObject(schema))
55
+ return;
56
+ if (HEADER_ANNOTATION in schema) {
57
+ if (!path) {
58
+ problem = `${HEADER_ANNOTATION} outside of nested properties`;
59
+ return;
60
+ }
61
+ const annotation = readAnnotation(schema, seen);
62
+ if (annotation.kind === "invalid") {
63
+ problem = annotation.reason;
64
+ return;
65
+ }
66
+ seen.add(annotation.header.toLowerCase());
67
+ bindings.push({ path, header: annotation.header });
68
+ }
69
+ for (const [key, value] of Object.entries(schema)) {
70
+ if (key === "properties" && path && isObject(value)) {
71
+ for (const [name, property] of Object.entries(value))
72
+ visit(property, [...path, name], depth + 1);
73
+ }
74
+ else if (key !== HEADER_ANNOTATION && !DATA_KEYWORDS.has(key)) {
75
+ visit(value, undefined, depth + 1);
76
+ }
77
+ }
78
+ };
79
+ if (isObject(inputSchema.properties)) {
80
+ for (const [name, property] of Object.entries(inputSchema.properties))
81
+ visit(property, [name], 1);
82
+ }
83
+ for (const [key, value] of Object.entries(inputSchema)) {
84
+ if (key === HEADER_ANNOTATION)
85
+ problem ??= `${HEADER_ANNOTATION} on the schema root`;
86
+ else if (key !== "properties" && !DATA_KEYWORDS.has(key))
87
+ visit(value, undefined, 1);
88
+ }
89
+ return problem ? { kind: "invalid", reason: problem } : { kind: "valid", bindings };
90
+ }
91
+ /** The header name of a property's annotation, or why the annotation is invalid. */
92
+ function readAnnotation(schema, seen) {
93
+ const header = schema[HEADER_ANNOTATION];
94
+ const invalid = (reason) => ({ kind: "invalid", reason: `${HEADER_ANNOTATION} ${reason}` });
95
+ if (typeof header !== "string" || header === "")
96
+ return invalid("must be a non-empty string");
97
+ if (!HEADER_TOKEN.test(header))
98
+ return invalid(`"${header}" is not a valid header name`);
99
+ if (seen.has(header.toLowerCase()))
100
+ return invalid(`"${header}" is not unique`);
101
+ const types = Array.isArray(schema.type) ? schema.type.filter((type) => type !== "null") : [schema.type];
102
+ if (types.length !== 1 || typeof types[0] !== "string" || !PRIMITIVE_TYPES.has(types[0])) {
103
+ return invalid(`"${header}" must be on a string, integer, or boolean property`);
104
+ }
105
+ return { kind: "valid", header };
106
+ }
107
+ /** `Mcp-Param-*` headers for a tool call. Arguments that are absent, null, or not primitive get no header. */
108
+ export function toolParamHeaders(bindings, args) {
109
+ const headers = {};
110
+ for (const { path, header } of bindings) {
111
+ let value = args;
112
+ for (const key of path)
113
+ value = isObject(value) ? value[key] : undefined;
114
+ const encoded = paramHeaderValue(value);
115
+ if (encoded !== undefined)
116
+ headers[`Mcp-Param-${header}`] = encoded;
117
+ }
118
+ return headers;
119
+ }
120
+ function paramHeaderValue(value) {
121
+ if (typeof value === "string")
122
+ return encodeHeaderValue(value);
123
+ if (typeof value === "boolean")
124
+ return String(value);
125
+ if (typeof value === "number" && Number.isInteger(value))
126
+ return String(value);
127
+ return undefined;
128
+ }
129
+ //# sourceMappingURL=http-headers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"http-headers.js","sourceRoot":"","sources":["../../src/protocol/http-headers.ts"],"names":[],"mappings":"AAAA,gGAAgG;AAChG,oFAAoF;AACpF,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAWxC,MAAM,iBAAiB,GAAG,cAAc,CAAC;AACzC,MAAM,eAAe,GAAG,qBAAqB,CAAC;AAC9C,6BAA6B;AAC7B,MAAM,YAAY,GAAG,gCAAgC,CAAC;AACtD,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,CAAC,QAAQ,EAAE,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC;AAClE,2EAA2E;AAC3E,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAC5B,2GAA2G;AAC3G,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,CAAC,CAAC,C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KAAK,MAAM,GAAG,IAAI,IAAI;YAAE,KAAK,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACzE,MAAM,OAAO,GAAG,gBAAgB,CAAC,KAAK,CAAC,CAAC;QACxC,IAAI,OAAO,KAAK,SAAS;YAAE,OAAO,CAAC,aAAa,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC;IACrE,CAAC;IACD,OAAO,OAAO,CAAC;AAChB,CAAC;AAED,SAAS,gBAAgB,CAAC,KAAc;IACvC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,iBAAiB,CAAC,KAAK,CAAC,CAAC;IAC/D,IAAI,OAAO,KAAK,KAAK,SAAS;QAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IACrD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC;QAAE,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;IAC/E,OAAO,SAAS,CAAC;AAClB,CAAC","sourcesContent":["// HTTP headers that MCP 2026-07-28 mirrors from the request body: `Mcp-Method`, `Mcp-Name`, and\n// `Mcp-Param-*` for tool arguments a tool's input schema marks with `x-mcp-header`.\nimport { isObject } from \"./jsonrpc.ts\";\n\n/** One tool argument mirrored into a header: the `properties` path to it and the header name part. */\nexport interface ToolHeaderBinding {\n\tpath: string[];\n\theader: string;\n}\n\n/** A tool's header bindings, or why its `x-mcp-header` annotations are invalid. */\nexport type ToolHeaderBindings = { kind: \"valid\"; bindings: ToolHeaderBinding[] } | { kind: \"invalid\"; reason: string };\n\nconst HEADER_ANNOTATION = \"x-mcp-header\";\nconst BASE64_SENTINEL = /^=\\?base64\\?.*\\?=$/s;\n// RFC 9110 token characters.\nconst HEADER_TOKEN = /^[!#$%&'*+\\-.^_`|~0-9A-Za-z]+$/;\nconst PRIMITIVE_TYPES = new Set([\"string\", \"integer\", \"boolean\"]);\n// Input schemas come from the server; deeper nesting is not worth walking.\nconst MAX_SCHEMA_DEPTH = 64;\n// Keywords whose values are instance data, not schemas, so an `x-mcp-header` key in them is no annotation.\nconst DATA_KEYWORDS = new Set([\"const\", \"default\", \"enum\", \"examples\"]);\n\n/**\n * A header value for `value`: as is when it is plain ASCII without leading or trailing whitespace,\n * otherwise Base64 of its UTF-8 in the `=?base64?…?=` form. Values that look like that form are\n * encoded too, so they are not mistaken for it.\n */\nexport function encodeHeaderValue(value: string): string {\n\tlet plain = value === value.trim() && !BASE64_SENTINEL.test(value);\n\tfor (let i = 0; plain && i < value.length; i++) {\n\t\tconst code = value.charCodeAt(i);\n\t\tplain = code >= 0x20 && code <= 0x7e;\n\t}\n\treturn plain ? value : `=?base64?${Buffer.from(value, \"utf8\").toString(\"base64\")}?=`;\n}\n\n/** `Mcp-Method`, and `Mcp-Name` for the methods that name a tool, prompt, or resource. */\nexport function standardRequestHeaders(method: string, params: unknown): Record<string, string> {\n\tconst headers: Record<string, string> = { \"Mcp-Method\": method };\n\tconst name =\n\t\tmethod === \"resources/read\"\n\t\t\t? isObject(params) && params.uri\n\t\t\t: (method === \"tools/call\" || method === \"prompts/get\") && isObject(params) && params.name;\n\tif (typeof name === \"string\") headers[\"Mcp-Name\"] = encodeHeaderValue(name);\n\treturn headers;\n}\n\n/**\n * The arguments a tool mirrors into headers (wire input). Annotations are valid only on `string`,\n * `integer`, and `boolean` properties reached from the schema root through `properties` alone, with\n * non-empty token names that are unique ignoring case. Clients on HTTP exclude tools with an invalid\n * annotation.\n */\nexport function toolHeaderBindings(inputSchema: Record<string, unknown>): ToolHeaderBindings {\n\tconst bindings: ToolHeaderBinding[] = [];\n\tconst seen = new Set<string>();\n\tlet problem: string | undefined;\n\tconst visit = (schema: unknown, path: string[] | undefined, depth: number): void => {\n\t\tif (problem || depth > MAX_SCHEMA_DEPTH) return;\n\t\tif (Array.isArray(schema)) {\n\t\t\tfor (const item of schema) visit(item, undefined, depth + 1);\n\t\t\treturn;\n\t\t}\n\t\tif (!isObject(schema)) return;\n\t\tif (HEADER_ANNOTATION in schema) {\n\t\t\tif (!path) {\n\t\t\t\tproblem = `${HEADER_ANNOTATION} outside of nested properties`;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tconst annotation = readAnnotation(schema, seen);\n\t\t\tif (annotation.kind === \"invalid\") {\n\t\t\t\tproblem = annotation.reason;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tseen.add(annotation.header.toLowerCase());\n\t\t\tbindings.push({ path, header: annotation.header });\n\t\t}\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\tif (key === \"properties\" && path && isObject(value)) {\n\t\t\t\tfor (const [name, property] of Object.entries(value)) visit(property, [...path, name], depth + 1);\n\t\t\t} else if (key !== HEADER_ANNOTATION && !DATA_KEYWORDS.has(key)) {\n\t\t\t\tvisit(value, undefined, depth + 1);\n\t\t\t}\n\t\t}\n\t};\n\tif (isObject(inputSchema.properties)) {\n\t\tfor (const [name, property] of Object.entries(inputSchema.properties)) visit(property, [name], 1);\n\t}\n\tfor (const [key, value] of Object.entries(inputSchema)) {\n\t\tif (key === HEADER_ANNOTATION) problem ??= `${HEADER_ANNOTATION} on the schema root`;\n\t\telse if (key !== \"properties\" && !DATA_KEYWORDS.has(key)) visit(value, undefined, 1);\n\t}\n\treturn problem ? { kind: \"invalid\", reason: problem } : { kind: \"valid\", bindings };\n}\n\n/** The header name of a property's annotation, or why the annotation is invalid. */\nfunction readAnnotation(\n\tschema: Record<string, unknown>,\n\tseen: Set<string>,\n): { kind: \"valid\"; header: string } | { kind: \"invalid\"; reason: string } {\n\tconst header = schema[HEADER_ANNOTATION];\n\tconst invalid = (reason: string) => ({ kind: \"invalid\" as const, reason: `${HEADER_ANNOTATION} ${reason}` });\n\tif (typeof header !== \"string\" || header === \"\") return invalid(\"must be a non-empty string\");\n\tif (!HEADER_TOKEN.test(header)) return invalid(`\"${header}\" is not a valid header name`);\n\tif (seen.has(header.toLowerCase())) return invalid(`\"${header}\" is not unique`);\n\tconst types = Array.isArray(schema.type) ? schema.type.filter((type) => type !== \"null\") : [schema.type];\n\tif (types.length !== 1 || typeof types[0] !== \"string\" || !PRIMITIVE_TYPES.has(types[0])) {\n\t\treturn invalid(`\"${header}\" must be on a string, integer, or boolean property`);\n\t}\n\treturn { kind: \"valid\", header };\n}\n\n/** `Mcp-Param-*` headers for a tool call. Arguments that are absent, null, or not primitive get no header. */\nexport function toolParamHeaders(\n\tbindings: readonly ToolHeaderBinding[],\n\targs: Record<string, unknown> | undefined,\n): Record<string, string> {\n\tconst headers: Record<string, string> = {};\n\tfor (const { path, header } of bindings) {\n\t\tlet value: unknown = args;\n\t\tfor (const key of path) value = isObject(value) ? value[key] : undefined;\n\t\tconst encoded = paramHeaderValue(value);\n\t\tif (encoded !== undefined) headers[`Mcp-Param-${header}`] = encoded;\n\t}\n\treturn headers;\n}\n\nfunction paramHeaderValue(value: unknown): string | undefined {\n\tif (typeof value === \"string\") return encodeHeaderValue(value);\n\tif (typeof value === \"boolean\") return String(value);\n\tif (typeof value === \"number\" && Number.isInteger(value)) return String(value);\n\treturn undefined;\n}\n"]}
@@ -0,0 +1,64 @@
1
+ export type JsonRpcId = string | number;
2
+ /**
3
+ * The client's request IDs and progress tokens are integers. Some servers echo them back as strings
4
+ * ("1" for 1); map those to the integer so they match, like other MCP clients do.
5
+ */
6
+ export declare function normalizeOwnRequestId(id: JsonRpcId): JsonRpcId;
7
+ export interface JsonRpcRequest {
8
+ jsonrpc: "2.0";
9
+ id: JsonRpcId;
10
+ method: string;
11
+ params?: unknown;
12
+ }
13
+ export interface JsonRpcNotification {
14
+ jsonrpc: "2.0";
15
+ method: string;
16
+ params?: unknown;
17
+ }
18
+ export interface JsonRpcErrorObject {
19
+ code: number;
20
+ message: string;
21
+ data?: unknown;
22
+ }
23
+ export interface JsonRpcSuccessResponse {
24
+ jsonrpc: "2.0";
25
+ id: JsonRpcId;
26
+ result: unknown;
27
+ }
28
+ export interface JsonRpcErrorResponse {
29
+ jsonrpc: "2.0";
30
+ id: JsonRpcId;
31
+ error: JsonRpcErrorObject;
32
+ }
33
+ export type JsonRpcResponse = JsonRpcSuccessResponse | JsonRpcErrorResponse;
34
+ export type JsonRpcMessage = JsonRpcRequest | JsonRpcNotification | JsonRpcResponse;
35
+ export declare const JSON_RPC_ERROR_CODES: {
36
+ readonly parseError: -32700;
37
+ readonly invalidRequest: -32600;
38
+ readonly methodNotFound: -32601;
39
+ readonly invalidParams: -32602;
40
+ readonly internalError: -32603;
41
+ };
42
+ export declare class McpError extends Error {
43
+ readonly code: number;
44
+ readonly data: unknown;
45
+ constructor(code: number, message: string, data?: unknown);
46
+ }
47
+ export declare class McpConnectionClosedError extends Error {
48
+ constructor(message?: string);
49
+ }
50
+ export declare class McpTimeoutError extends Error {
51
+ readonly timeoutMs: number;
52
+ constructor(timeoutMs: number);
53
+ }
54
+ export declare class McpAbortError extends Error {
55
+ constructor(message?: string);
56
+ }
57
+ export declare function isObject(value: unknown): value is Record<string, unknown>;
58
+ export declare function toError(value: unknown): Error;
59
+ export declare function isJsonRpcId(value: unknown): value is JsonRpcId;
60
+ export declare function isJsonRpcRequest(message: unknown): message is JsonRpcRequest;
61
+ export declare function isJsonRpcNotification(message: unknown): message is JsonRpcNotification;
62
+ export declare function isJsonRpcResponse(message: unknown): message is JsonRpcResponse;
63
+ export declare function parseJsonRpcMessage(value: unknown): JsonRpcMessage;
64
+ //# sourceMappingURL=jsonrpc.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonrpc.d.ts","sourceRoot":"","sources":["../../src/protocol/jsonrpc.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,CAAC;AAExC;;;GAGG;AACH,wBAAgB,qBAAqB,CAAC,EAAE,EAAE,SAAS,GAAG,SAAS,CAE9D;AAED,MAAM,WAAW,cAAc;IAC9B,OAAO,EAAE,KAAK,CAAC;IACf,EAAE,E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AC,SAAS,EAAE,MAAM,CAAC;IAE3B,YAAY,SAAS,EAAE,MAAM,EAI5B;CACD;AAED,qBAAa,aAAc,SAAQ,KAAK;IACvC,YAAY,OAAO,SAAwB,EAG1C;CACD;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEzE;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAE7C;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAE9D;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,cAAc,CAI5E;AAED,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,mBAAmB,CAEtF;AAED,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,eAAe,CAK9E;AAED,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,cAAc,CAGlE"}
@@ -0,0 +1,74 @@
1
+ /**
2
+ * The client's request IDs and progress tokens are integers. Some servers echo them back as strings
3
+ * ("1" for 1); map those to the integer so they match, like other MCP clients do.
4
+ */
5
+ export function normalizeOwnRequestId(id) {
6
+ return typeof id === "string" && /^(0|[1-9]\d*)$/.test(id) ? Number(id) : id;
7
+ }
8
+ export const JSON_RPC_ERROR_CODES = {
9
+ parseError: -32700,
10
+ invalidRequest: -32600,
11
+ methodNotFound: -32601,
12
+ invalidParams: -32602,
13
+ internalError: -32603,
14
+ };
15
+ export class McpError extends Error {
16
+ code;
17
+ data;
18
+ constructor(code, message, data) {
19
+ super(message);
20
+ this.name = "McpError";
21
+ this.code = code;
22
+ this.data = data;
23
+ }
24
+ }
25
+ export class McpConnectionClosedError extends Error {
26
+ constructor(message = "MCP connection closed") {
27
+ super(message);
28
+ this.name = "McpConnectionClosedError";
29
+ }
30
+ }
31
+ export class McpTimeoutError extends Error {
32
+ timeoutMs;
33
+ constructor(timeoutMs) {
34
+ super(`MCP request timed out after ${timeoutMs}ms`);
35
+ this.name = "McpTimeoutError";
36
+ this.timeoutMs = timeoutMs;
37
+ }
38
+ }
39
+ export class McpAbortError extends Error {
40
+ constructor(message = "MCP request aborted") {
41
+ super(message);
42
+ this.name = "AbortError";
43
+ }
44
+ }
45
+ export function isObject(value) {
46
+ return typeof value === "object" && value !== null && !Array.isArray(value);
47
+ }
48
+ export function toError(value) {
49
+ return value instanceof Error ? value : new Error(String(value));
50
+ }
51
+ export function isJsonRpcId(value) {
52
+ return typeof value === "string" || (typeof value === "number" && Number.isFinite(value));
53
+ }
54
+ export function isJsonRpcRequest(message) {
55
+ return (isObject(message) && message.jsonrpc === "2.0" && isJsonRpcId(message.id) && typeof message.method === "string");
56
+ }
57
+ export function isJsonRpcNotification(message) {
58
+ return isObject(message) && message.jsonrpc === "2.0" && !("id" in message) && typeof message.method === "string";
59
+ }
60
+ export function isJsonRpcResponse(message) {
61
+ if (!isObject(message) || message.jsonrpc !== "2.0" || !isJsonRpcId(message.id))
62
+ return false;
63
+ if ("result" in message)
64
+ return !("error" in message);
65
+ if (!("error" in message) || !isObject(message.error))
66
+ return false;
67
+ return typeof message.error.code === "number" && typeof message.error.message === "string";
68
+ }
69
+ export function parseJsonRpcMessage(value) {
70
+ if (isJsonRpcRequest(value) || isJsonRpcNotification(value) || isJsonRpcResponse(value))
71
+ return value;
72
+ throw new McpError(JSON_RPC_ERROR_CODES.invalidRequest, "Invalid JSON-RPC message");
73
+ }
74
+ //# sourceMappingURL=jsonrpc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsonrpc.js","sourceRoot":"","sources":["../../src/protocol/jsonrpc.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,EAAa;IAClD,OAAO,OAAO,EAAE,KAAK,QAAQ,IAAI,gBAAgB,CAAC,IAAI,CA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,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACpE,OAAO,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,OAAO,CAAC,KAAK,CAAC,OAAO,KAAK,QAAQ,CAAC;AAC5F,CAAC;AAED,MAAM,UAAU,mBAAmB,CAAC,KAAc;IACjD,IAAI,gBAAgB,CAAC,KAAK,CAAC,IAAI,qBAAqB,CAAC,KAAK,CAAC,IAAI,iBAAiB,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IACtG,MAAM,IAAI,QAAQ,CAAC,oBAAoB,CAAC,cAAc,EAAE,0BAA0B,CAAC,CAAC;AACrF,CAAC","sourcesContent":["export type JsonRpcId = string | number;\n\n/**\n * The client's request IDs and progress tokens are integers. Some servers echo them back as strings\n * (\"1\" for 1); map those to the integer so they match, like other MCP clients do.\n */\nexport function normalizeOwnRequestId(id: JsonRpcId): JsonRpcId {\n\treturn typeof id === \"string\" && /^(0|[1-9]\\d*)$/.test(id) ? Number(id) : id;\n}\n\nexport interface JsonRpcRequest {\n\tjsonrpc: \"2.0\";\n\tid: JsonRpcId;\n\tmethod: string;\n\tparams?: unknown;\n}\n\nexport interface JsonRpcNotification {\n\tjsonrpc: \"2.0\";\n\tmethod: string;\n\tparams?: unknown;\n}\n\nexport interface JsonRpcErrorObject {\n\tcode: number;\n\tmessage: string;\n\tdata?: unknown;\n}\n\nexport interface JsonRpcSuccessResponse {\n\tjsonrpc: \"2.0\";\n\tid: JsonRpcId;\n\tresult: unknown;\n}\n\nexport interface JsonRpcErrorResponse {\n\tjsonrpc: \"2.0\";\n\tid: JsonRpcId;\n\terror: JsonRpcErrorObject;\n}\n\nexport type JsonRpcResponse = JsonRpcSuccessResponse | JsonRpcErrorResponse;\nexport type JsonRpcMessage = JsonRpcRequest | JsonRpcNotification | JsonRpcResponse;\n\nexport const JSON_RPC_ERROR_CODES = {\n\tparseError: -32700,\n\tinvalidRequest: -32600,\n\tmethodNotFound: -32601,\n\tinvalidParams: -32602,\n\tinternalError: -32603,\n} as const;\n\nexport class McpError extends Error {\n\treadonly code: number;\n\treadonly data: unknown;\n\n\tconstructor(code: number, message: string, data?: unknown) {\n\t\tsuper(message);\n\t\tthis.name = \"McpError\";\n\t\tthis.code = code;\n\t\tthis.data = data;\n\t}\n}\n\nexport class McpConnectionClosedError extends Error {\n\tconstructor(message = \"MCP connection closed\") {\n\t\tsuper(message);\n\t\tthis.name = \"McpConnectionClosedError\";\n\t}\n}\n\nexport class McpTimeoutError extends Error {\n\treadonly timeoutMs: number;\n\n\tconstructor(timeoutMs: number) {\n\t\tsuper(`MCP request timed out after ${timeoutMs}ms`);\n\t\tthis.name = \"McpTimeoutError\";\n\t\tthis.timeoutMs = timeoutMs;\n\t}\n}\n\nexport class McpAbortError extends Error {\n\tconstructor(message = \"MCP request aborted\") {\n\t\tsuper(message);\n\t\tthis.name = \"AbortError\";\n\t}\n}\n\nexport function isObject(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nexport function toError(value: unknown): Error {\n\treturn value instanceof Error ? value : new Error(String(value));\n}\n\nexport function isJsonRpcId(value: unknown): value is JsonRpcId {\n\treturn typeof value === \"string\" || (typeof value === \"number\" && Number.isFinite(value));\n}\n\nexport function isJsonRpcRequest(message: unknown): message is JsonRpcRequest {\n\treturn (\n\t\tisObject(message) && message.jsonrpc === \"2.0\" && isJsonRpcId(message.id) && typeof message.method === \"string\"\n\t);\n}\n\nexport function isJsonRpcNotification(message: unknown): message is JsonRpcNotification {\n\treturn isObject(message) && message.jsonrpc === \"2.0\" && !(\"id\" in message) && typeof message.method === \"string\";\n}\n\nexport function isJsonRpcResponse(message: unknown): message is JsonRpcResponse {\n\tif (!isObject(message) || message.jsonrpc !== \"2.0\" || !isJsonRpcId(message.id)) return false;\n\tif (\"result\" in message) return !(\"error\" in message);\n\tif (!(\"error\" in message) || !isObject(message.error)) return false;\n\treturn typeof message.error.code === \"number\" && typeof message.error.message === \"string\";\n}\n\nexport function parseJsonRpcMessage(value: unknown): JsonRpcMessage {\n\tif (isJsonRpcRequest(value) || isJsonRpcNotification(value) || isJsonRpcResponse(value)) return value;\n\tthrow new McpError(JSON_RPC_ERROR_CODES.invalidRequest, \"Invalid JSON-RPC message\");\n}\n"]}
@@ -0,0 +1,47 @@
1
+ import type { ClientCapabilities, DiscoverResult, Implementation, InputRequest, LoggingLevel, STATELESS_PROTOCOL_VERSION, SubscriptionFilter } from "./types.ts";
2
+ /** Error code of a request whose `Mcp-*` headers do not match its body (MCP 2026-07-28). */
3
+ export declare const HEADER_MISMATCH_ERROR_CODE = -32020;
4
+ /**
5
+ * Error codes MCP 2026-07-28 reserves: header mismatch, missing required client capability, and
6
+ * unsupported protocol version. Only servers of that revision send them.
7
+ */
8
+ export declare const STATELESS_ERROR_CODES: readonly number[];
9
+ /** What every stateless request declares about the client in its `_meta`. */
10
+ export interface StatelessRequestMeta {
11
+ protocolVersion: typeof STATELESS_PROTOCOL_VERSION;
12
+ clientInfo: Implementation;
13
+ clientCapabilities: ClientCapabilities;
14
+ logLevel: LoggingLevel | undefined;
15
+ }
16
+ /** `params` with the stateless request metadata added to its `_meta`, keeping the `_meta` keys it has. */
17
+ export declare function withStatelessMeta(params: Record<string, unknown> | undefined, meta: StatelessRequestMeta): Record<string, unknown>;
18
+ /** A result of a stateless request, by `resultType`. */
19
+ export type StatelessResult = {
20
+ kind: "complete";
21
+ result: unknown;
22
+ } | {
23
+ kind: "input_required";
24
+ inputRequests: Record<string, InputRequest>;
25
+ requestState: string | undefined;
26
+ }
27
+ /** A result type an MCP extension defines (`upload_required`, say); the whole result, unchecked. */
28
+ | {
29
+ kind: "extension";
30
+ resultType: string;
31
+ result: Record<string, unknown>;
32
+ };
33
+ /**
34
+ * Read the `resultType` of a stateless result (wire input). A result without one is complete; other
35
+ * result types than the base protocol's are left to the caller, which knows the extensions it supports.
36
+ */
37
+ export declare function readStatelessResult(method: string, value: unknown): StatelessResult;
38
+ /** Validate a `server/discover` result (wire input), with the server's name and version from its `_meta`. */
39
+ export declare function validateDiscoverResult(value: unknown): {
40
+ result: DiscoverResult;
41
+ serverInfo?: Implementation;
42
+ };
43
+ /** The subscription a notification belongs to (`_meta`), by the id of its `subscriptions/listen` request. */
44
+ export declare function subscriptionIdOf(params: unknown): unknown;
45
+ /** The notification types a `notifications/subscriptions/acknowledged` agrees to (wire input); others are left out. */
46
+ export declare function readSubscriptionFilter(params: unknown): SubscriptionFilter;
47
+ //# sourceMappingURL=stateless.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stateless.d.ts","sourceRoot":"","sources":["../../src/protocol/stateless.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACX,kBAAkB,EAClB,cAAc,EACd,cAAc,EACd,YAAY,EACZ,YAAY,EACZ,0BAA0B,EAC1B,kBAAkB,EAClB,MAAM,YAAY,CAAC;AAEpB,4FAA4F;AAC5F,eAAO,MAAM,0BAA0B,SAAS,CAAC;AAEjD;;;GAGG;AACH,eAAO,MAAM,qBAAqB,EAAE,SAAS,MAAM,EAAiD,CAAC;AAErG,6EAA6E;AAC7E,MAAM,WAAW,oBAAoB;IACpC,eAAe,EAAE,OAAO,0BAA0B,CAAC;IACnD,UAAU,EAAE,cAAc,CAAC;IAC3B,kBAAkB,EAAE,kBAAkB,CAAC;IACvC,QAAQ,EAAE,YAAY,GAAG,SAAS,CAAC;CACnC;AAED,0GAA0G;AAC1G,wBAAgB,iBAAiB,CAChC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EAC3C,IAAI,EAAE,oBAAoB,GACxB,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAWzB;AAED,wDAAwD;AACxD,MAAM,MAAM,eAAe,GACxB;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,MAAM,EAAE,OAAO,CAAA;CAAE,GACrC;IAAE,IAAI,EAAE,gBAAgB,CAAC;IAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,CAAC;IAAC,YAAY,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE;AAC3G,oGAAoG;GAClG;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAAE,CAAC;AAE9E;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,GAAG,eAAe,CAgBnF;AAYD,6GAA6G;AAC7G,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,OAAO,GAAG;IAAE,MAAM,EAAE,cAAc,CAAC;IAAC,UAAU,CAAC,EAAE,cAAc,CAAA;CAAE,CAoB9G;AAED,6GAA6G;AAC7G,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO,CAIzD;AAID,uHAAuH;AACvH,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,OAAO,GAAG,kBAAkB,CAU1E"}
@@ -0,0 +1,102 @@
1
+ // Wire rules of the stateless MCP revision (2026-07-28): request metadata, result types, and the
2
+ // `server/discover` result.
3
+ import { isObject, JSON_RPC_ERROR_CODES, McpError } from "./jsonrpc.js";
4
+ /** Error code of a request whose `Mcp-*` headers do not match its body (MCP 2026-07-28). */
5
+ export const HEADER_MISMATCH_ERROR_CODE = -32020;
6
+ /**
7
+ * Error codes MCP 2026-07-28 reserves: header mismatch, missing required client capability, and
8
+ * unsupported protocol version. Only servers of that revision send them.
9
+ */
10
+ export const STATELESS_ERROR_CODES = [HEADER_MISMATCH_ERROR_CODE, -32021, -32022];
11
+ /** `params` with the stateless request metadata added to its `_meta`, keeping the `_meta` keys it has. */
12
+ export function withStatelessMeta(params, meta) {
13
+ return {
14
+ ...params,
15
+ _meta: {
16
+ ...(isObject(params?._meta) ? params._meta : {}),
17
+ "io.modelcontextprotocol/protocolVersion": meta.protocolVersion,
18
+ "io.modelcontextprotocol/clientInfo": meta.clientInfo,
19
+ "io.modelcontextprotocol/clientCapabilities": meta.clientCapabilities,
20
+ ...(meta.logLevel === undefined ? {} : { "io.modelcontextprotocol/logLevel": meta.logLevel }),
21
+ },
22
+ };
23
+ }
24
+ /**
25
+ * Read the `resultType` of a stateless result (wire input). A result without one is complete; other
26
+ * result types than the base protocol's are left to the caller, which knows the extensions it supports.
27
+ */
28
+ export function readStatelessResult(method, value) {
29
+ if (!isObject(value))
30
+ throw invalid(`Invalid MCP ${method} result`);
31
+ const { resultType, inputRequests, requestState } = value;
32
+ if (resultType === undefined || resultType === "complete")
33
+ return { kind: "complete", result: value };
34
+ if (typeof resultType !== "string")
35
+ throw invalid(`Unsupported MCP ${method} result type: ${String(resultType)}`);
36
+ if (resultType !== "input_required")
37
+ return { kind: "extension", resultType, result: value };
38
+ if (inputRequests !== undefined && !isInputRequests(inputRequests)) {
39
+ throw invalid(`Invalid input requests in MCP ${method} result`);
40
+ }
41
+ if (requestState !== undefined && typeof requestState !== "string") {
42
+ throw invalid(`Invalid request state in MCP ${method} result`);
43
+ }
44
+ if (inputRequests === undefined && requestState === undefined) {
45
+ throw invalid(`MCP ${method} result requires input but names neither input requests nor request state`);
46
+ }
47
+ return { kind: "input_required", inputRequests: inputRequests ?? {}, requestState };
48
+ }
49
+ function isInputRequests(value) {
50
+ if (!isObject(value))
51
+ return false;
52
+ return Object.values(value).every((request) => isObject(request) &&
53
+ typeof request.method === "string" &&
54
+ (request.params === undefined || isObject(request.params)));
55
+ }
56
+ /** Validate a `server/discover` result (wire input), with the server's name and version from its `_meta`. */
57
+ export function validateDiscoverResult(value) {
58
+ if (!isObject(value) ||
59
+ !Array.isArray(value.supportedVersions) ||
60
+ !value.supportedVersions.every((version) => typeof version === "string") ||
61
+ !isObject(value.capabilities) ||
62
+ (value.instructions !== undefined && typeof value.instructions !== "string")) {
63
+ throw invalid("Invalid MCP server/discover result");
64
+ }
65
+ const result = value;
66
+ // Servers SHOULD name themselves, but need not.
67
+ const info = isObject(value._meta) ? value._meta["io.modelcontextprotocol/serverInfo"] : undefined;
68
+ if (!isObject(info) || typeof info.name !== "string" || typeof info.version !== "string")
69
+ return { result };
70
+ const serverInfo = {
71
+ name: info.name,
72
+ version: info.version,
73
+ ...(typeof info.title === "string" ? { title: info.title } : {}),
74
+ };
75
+ return { result, serverInfo };
76
+ }
77
+ /** The subscription a notification belongs to (`_meta`), by the id of its `subscriptions/listen` request. */
78
+ export function subscriptionIdOf(params) {
79
+ return isObject(params) && isObject(params._meta)
80
+ ? params._meta["io.modelcontextprotocol/subscriptionId"]
81
+ : undefined;
82
+ }
83
+ const SUBSCRIPTION_FLAGS = ["toolsListChanged", "promptsListChanged", "resourcesListChanged"];
84
+ /** The notification types a `notifications/subscriptions/acknowledged` agrees to (wire input); others are left out. */
85
+ export function readSubscriptionFilter(params) {
86
+ const value = isObject(params) ? params.notifications : undefined;
87
+ if (!isObject(value))
88
+ return {};
89
+ const filter = {};
90
+ for (const key of SUBSCRIPTION_FLAGS) {
91
+ if (typeof value[key] === "boolean")
92
+ filter[key] = value[key];
93
+ }
94
+ const uris = value.resourceSubscriptions;
95
+ if (Array.isArray(uris) && uris.every((uri) => typeof uri === "string"))
96
+ filter.resourceSubscriptions = uris;
97
+ return filter;
98
+ }
99
+ function invalid(message) {
100
+ return new McpError(JSON_RPC_ERROR_CODES.invalidRequest, message);
101
+ }
102
+ //# sourceMappingURL=stateless.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"stateless.js","sourceRoot":"","sources":["../../src/protocol/stateless.ts"],"names":[],"mappings":"AAAA,iGAAiG;AACjG,4BAA4B;AAC5B,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAC;AAWxE,4FAA4F;AAC5F,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,KAAK,CAAC;AAEjD;;;GAGG;AACH,MAAM,CAAC,MA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,CAAC,CAAC,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,SAAS,CAAC;IAClE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,EAAE,CAAC;IAChC,MAAM,MAAM,GAAuB,EAAE,CAAC;IACtC,KAAK,MAAM,GAAG,IAAI,kBAAkB,EAAE,CAAC;QACtC,IAAI,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,SAAS;YAAE,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;IAC/D,CAAC;IACD,MAAM,IAAI,GAAG,KAAK,CAAC,qBAAqB,CAAC;IACzC,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC;QAAE,MAAM,CAAC,qBAAqB,GAAG,IAAI,CAAC;IAC7G,OAAO,MAAM,CAAC;AACf,CAAC;AAED,SAAS,OAAO,CAAC,OAAe;IAC/B,OAAO,IAAI,QAAQ,CAAC,oBAAoB,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;AACnE,CAAC","sourcesContent":["// Wire rules of the stateless MCP revision (2026-07-28): request metadata, result types, and the\n// `server/discover` result.\nimport { isObject, JSON_RPC_ERROR_CODES, McpError } from \"./jsonrpc.ts\";\nimport type {\n\tClientCapabilities,\n\tDiscoverResult,\n\tImplementation,\n\tInputRequest,\n\tLoggingLevel,\n\tSTATELESS_PROTOCOL_VERSION,\n\tSubscriptionFilter,\n} from \"./types.ts\";\n\n/** Error code of a request whose `Mcp-*` headers do not match its body (MCP 2026-07-28). */\nexport const HEADER_MISMATCH_ERROR_CODE = -32020;\n\n/**\n * Error codes MCP 2026-07-28 reserves: header mismatch, missing required client capability, and\n * unsupported protocol version. Only servers of that revision send them.\n */\nexport const STATELESS_ERROR_CODES: readonly number[] = [HEADER_MISMATCH_ERROR_CODE, -32021, -32022];\n\n/** What every stateless request declares about the client in its `_meta`. */\nexport interface StatelessRequestMeta {\n\tprotocolVersion: typeof STATELESS_PROTOCOL_VERSION;\n\tclientInfo: Implementation;\n\tclientCapabilities: ClientCapabilities;\n\tlogLevel: LoggingLevel | undefined;\n}\n\n/** `params` with the stateless request metadata added to its `_meta`, keeping the `_meta` keys it has. */\nexport function withStatelessMeta(\n\tparams: Record<string, unknown> | undefined,\n\tmeta: StatelessRequestMeta,\n): Record<string, unknown> {\n\treturn {\n\t\t...params,\n\t\t_meta: {\n\t\t\t...(isObject(params?._meta) ? params._meta : {}),\n\t\t\t\"io.modelcontextprotocol/protocolVersion\": meta.protocolVersion,\n\t\t\t\"io.modelcontextprotocol/clientInfo\": meta.clientInfo,\n\t\t\t\"io.modelcontextprotocol/clientCapabilities\": meta.clientCapabilities,\n\t\t\t...(meta.logLevel === undefined ? {} : { \"io.modelcontextprotocol/logLevel\": meta.logLevel }),\n\t\t},\n\t};\n}\n\n/** A result of a stateless request, by `resultType`. */\nexport type StatelessResult =\n\t| { kind: \"complete\"; result: unknown }\n\t| { kind: \"input_required\"; inputRequests: Record<string, InputRequest>; requestState: string | undefined }\n\t/** A result type an MCP extension defines (`upload_required`, say); the whole result, unchecked. */\n\t| { kind: \"extension\"; resultType: string; result: Record<string, unknown> };\n\n/**\n * Read the `resultType` of a stateless result (wire input). A result without one is complete; other\n * result types than the base protocol's are left to the caller, which knows the extensions it supports.\n */\nexport function readStatelessResult(method: string, value: unknown): StatelessResult {\n\tif (!isObject(value)) throw invalid(`Invalid MCP ${method} result`);\n\tconst { resultType, inputRequests, requestState } = value;\n\tif (resultType === undefined || resultType === \"complete\") return { kind: \"complete\", result: value };\n\tif (typeof resultType !== \"string\") throw invalid(`Unsupported MCP ${method} result type: ${String(resultType)}`);\n\tif (resultType !== \"input_required\") return { kind: \"extension\", resultType, result: value };\n\tif (inputRequests !== undefined && !isInputRequests(inputRequests)) {\n\t\tthrow invalid(`Invalid input requests in MCP ${method} result`);\n\t}\n\tif (requestState !== undefined && typeof requestState !== \"string\") {\n\t\tthrow invalid(`Invalid request state in MCP ${method} result`);\n\t}\n\tif (inputRequests === undefined && requestState === undefined) {\n\t\tthrow invalid(`MCP ${method} result requires input but names neither input requests nor request state`);\n\t}\n\treturn { kind: \"input_required\", inputRequests: inputRequests ?? {}, requestState };\n}\n\nfunction isInputRequests(value: unknown): value is Record<string, InputRequest> {\n\tif (!isObject(value)) return false;\n\treturn Object.values(value).every(\n\t\t(request) =>\n\t\t\tisObject(request) &&\n\t\t\ttypeof request.method === \"string\" &&\n\t\t\t(request.params === undefined || isObject(request.params)),\n\t);\n}\n\n/** Validate a `server/discover` result (wire input), with the server's name and version from its `_meta`. */\nexport function validateDiscoverResult(value: unknown): { result: DiscoverResult; serverInfo?: Implementation } {\n\tif (\n\t\t!isObject(value) ||\n\t\t!Array.isArray(value.supportedVersions) ||\n\t\t!value.supportedVersions.every((version) => typeof version === \"string\") ||\n\t\t!isObject(value.capabilities) ||\n\t\t(value.instructions !== undefined && typeof value.instructions !== \"string\")\n\t) {\n\t\tthrow invalid(\"Invalid MCP server/discover result\");\n\t}\n\tconst result = value as unknown as DiscoverResult;\n\t// Servers SHOULD name themselves, but need not.\n\tconst info = isObject(value._meta) ? value._meta[\"io.modelcontextprotocol/serverInfo\"] : undefined;\n\tif (!isObject(info) || typeof info.name !== \"string\" || typeof info.version !== \"string\") return { result };\n\tconst serverInfo = {\n\t\tname: info.name,\n\t\tversion: info.version,\n\t\t...(typeof info.title === \"string\" ? { title: info.title } : {}),\n\t};\n\treturn { result, serverInfo };\n}\n\n/** The subscription a notification belongs to (`_meta`), by the id of its `subscriptions/listen` request. */\nexport function subscriptionIdOf(params: unknown): unknown {\n\treturn isObject(params) && isObject(params._meta)\n\t\t? params._meta[\"io.modelcontextprotocol/subscriptionId\"]\n\t\t: undefined;\n}\n\nconst SUBSCRIPTION_FLAGS = [\"toolsListChanged\", \"promptsListChanged\", \"resourcesListChanged\"] as const;\n\n/** The notification types a `notifications/subscriptions/acknowledged` agrees to (wire input); others are left out. */\nexport function readSubscriptionFilter(params: unknown): SubscriptionFilter {\n\tconst value = isObject(params) ? params.notifications : undefined;\n\tif (!isObject(value)) return {};\n\tconst filter: SubscriptionFilter = {};\n\tfor (const key of SUBSCRIPTION_FLAGS) {\n\t\tif (typeof value[key] === \"boolean\") filter[key] = value[key];\n\t}\n\tconst uris = value.resourceSubscriptions;\n\tif (Array.isArray(uris) && uris.every((uri) => typeof uri === \"string\")) filter.resourceSubscriptions = uris;\n\treturn filter;\n}\n\nfunction invalid(message: string): McpError {\n\treturn new McpError(JSON_RPC_ERROR_CODES.invalidRequest, message);\n}\n"]}