@acid-cat/mcp-handshake 0.16.0 → 0.18.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
@@ -1,8 +1,8 @@
1
1
  # @acid-cat/mcp-handshake
2
2
 
3
- `@acid-cat/mcp-handshake` lets an MCP server that speaks only revision `2026-07-28` also serve clients that open with the [`initialize` handshake of earlier revisions](../../spec/bindings/mcp.md#earlier-revisions-through-http) (`2025-11-25` and `2025-06-18`). Many MCP clients still use that handshake.
3
+ `@acid-cat/mcp-handshake` lets an MCP server that speaks only revision `2026-07-28` also serve clients that open with the [`initialize` handshake of earlier revisions](../../spec/surfaces/mcp.md#earlier-revisions-through-http) (`2025-11-25` and `2025-06-18`). Many MCP clients still use that handshake.
4
4
 
5
- `mcpHandshake(handler)` wraps a web handler of the [MCP binding through HTTP](../../spec/bindings/mcp.md#http) and returns a new web handler. The wrapper keeps no state. It works in front of any web handler that serves revision `2026-07-28`.
5
+ `mcpHandshake(handler)` wraps a web handler of the [MCP surface through HTTP](../../spec/surfaces/mcp.md#http) and returns a new web handler. The wrapper keeps no state. It works in front of any web handler that serves revision `2026-07-28`.
6
6
 
7
7
  The package is part of the TypeScript reference implementation. You usually get it through a core: [`@acid-cat/core-ts`](../core-ts) re-exports it as `mcpHandshake`, and [`@acid-cat/core-effect`](../core-effect) as `Mcp.withHandshake`.
8
8
 
@@ -34,9 +34,11 @@ const mcp = Mcp.withHandshake(Mcp.toWebHandler(app, layer, options));
34
34
 
35
35
  ## How it works
36
36
 
37
- - A request of revision `2026-07-28`, and any request that is not `POST`, goes to the handler unchanged.
37
+ - A request of revision `2026-07-28`, and any other request that is not `POST`, goes to the handler unchanged.
38
38
  - The wrapper turns each handshake request into a revision `2026-07-28` request for the handler: `tools/list` and `tools/call` pass as they are, and every other method becomes `server/discover`. The handler therefore still screens each request's origin, media type and size.
39
+ - Each request gets the [headers](../../spec/surfaces/mcp.md#headers) that the handler checks: `Mcp-Method`, and for `tools/call` `Mcp-Name` and one `Mcp-Param-*` header for each argument whose property has `x-mcp-header`. To find these properties, the wrapper first asks the handler for `tools/list`, because it keeps no state.
39
40
  - A response that is not 2xx comes back unchanged.
41
+ - A `GET` request of the [Server Card](../../spec/surfaces/mcp.md#server-card) (`<MCP URL>/server-card`) goes to the handler. The wrapper adds `2025-11-25` and `2025-06-18` to the `supportedProtocolVersions` of each remote of the card, gives the card a new `ETag`, and answers `If-None-Match` itself.
40
42
  - `initialize` returns the result of `server/discover` in the handshake's shape. `ping` returns an empty result. A notification gets 202. Any other method gets the JSON-RPC error `-32601`.
41
43
  - Results carry no `_meta`, `resultType`, `ttlMs` or `cacheScope` member. A JSON-RPC error has HTTP status 200.
42
44
 
package/dist/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export interface McpHandshakeOptions {
5
5
  }
6
6
  /**
7
7
  * A web handler that serves the `initialize` handshake of revisions 2025-11-25 and 2025-06-18, and gives all other
8
- * requests to `handler` unchanged.
8
+ * requests to `handler`. It adds these revisions to the `supportedProtocolVersions` of the Server Card of `handler`
9
+ * (spec/surfaces/mcp.md#server-card).
9
10
  */
10
11
  export declare function mcpHandshake(handler: WebHandler, options?: McpHandshakeOptions): WebHandler;
package/dist/index.js CHANGED
@@ -1,9 +1,6 @@
1
- import { MCP_EVENTS_EXTENSION, MCP_PROTOCOL_VERSION, isJsonObject, readBounded } from "@acid-cat/spec-ts";
1
+ import { MCP_EVENTS_EXTENSION, MCP_PROTOCOL_VERSION, MCP_PROTOCOL_VERSION_KEY, MCP_SERVER_CARD_MEDIA_TYPE, isJsonObject, mcpDiscovered, mcpParamHeaders, mcpServerCardResponse, mcpServerUrlOfCardRequest, mcpWrappedRequest, readBounded } from "@acid-cat/spec-ts";
2
2
  // src/index.ts
3
- /** The `initialize` handshake of earlier MCP revisions through HTTP (spec/bindings/mcp.md#earlier-revisions-through-http). */
4
- const PROTOCOL_VERSION_KEY = "io.modelcontextprotocol/protocolVersion";
5
- const CLIENT_CAPABILITIES_KEY = "io.modelcontextprotocol/clientCapabilities";
6
- const SERVER_INFO_KEY = "io.modelcontextprotocol/serverInfo";
3
+ /** The `initialize` handshake of earlier MCP revisions through HTTP (spec/surfaces/mcp.md#earlier-revisions-through-http). */
7
4
  const HANDSHAKE_VERSIONS = ["2025-11-25", "2025-06-18"];
8
5
  const [LATEST_HANDSHAKE_VERSION] = HANDSHAKE_VERSIONS;
9
6
  const CURRENT_ONLY_MEMBERS = /* @__PURE__ */ new Set([
@@ -14,15 +11,17 @@ const CURRENT_ONLY_MEMBERS = /* @__PURE__ */ new Set([
14
11
  ]);
15
12
  /**
16
13
  * A web handler that serves the `initialize` handshake of revisions 2025-11-25 and 2025-06-18, and gives all other
17
- * requests to `handler` unchanged.
14
+ * requests to `handler`. It adds these revisions to the `supportedProtocolVersions` of the Server Card of `handler`
15
+ * (spec/surfaces/mcp.md#server-card).
18
16
  */
19
17
  function mcpHandshake(handler, options = {}) {
20
18
  const maximumBytes = options.maximumBytes ?? 1048576;
21
19
  return async (request) => {
20
+ if (request.method === "GET" && mcpServerUrlOfCardRequest(request) !== void 0) return cardWithHandshake(handler, request);
22
21
  const header = request.headers.get("MCP-Protocol-Version");
23
22
  if (request.method !== "POST" || header === MCP_PROTOCOL_VERSION) return handler(request);
24
23
  const message = await readMessage(request.clone(), maximumBytes);
25
- if (message === void 0 || !isHandshakeRequest(header, metaOf(message)[PROTOCOL_VERSION_KEY])) return handler(request);
24
+ if (message === void 0 || !isHandshakeRequest(header, metaOf(message)[MCP_PROTOCOL_VERSION_KEY])) return handler(request);
26
25
  return answer(handler, request, message);
27
26
  };
28
27
  }
@@ -30,7 +29,8 @@ async function answer(handler, request, message) {
30
29
  const method = typeof message["method"] === "string" ? message["method"] : "";
31
30
  const params = isJsonObject(message["params"]) ? message["params"] : {};
32
31
  const ownMethod = method === "tools/list" || method === "tools/call" ? method : "server/discover";
33
- const response = await handler(asCurrentRevisionRequest(request, ownMethod, ownMethod === "server/discover" ? {} : params));
32
+ const paramHeaders = ownMethod === "tools/call" ? mcpParamHeaders(await inputSchemaOf(handler, request, params["name"]), params["arguments"]) : {};
33
+ const response = await handler(asCurrentRevisionRequest(request, ownMethod, ownMethod === "server/discover" ? {} : params, paramHeaders));
34
34
  if (!response.ok) return response;
35
35
  const reply = parseObject(await response.text()) ?? {};
36
36
  if (!("id" in message)) return new Response(null, { status: 202 });
@@ -68,48 +68,62 @@ async function answer(handler, request, message) {
68
68
  result: Object.fromEntries(Object.entries(result).filter(([member]) => !CURRENT_ONLY_MEMBERS.has(member)))
69
69
  });
70
70
  }
71
+ async function cardWithHandshake(handler, request) {
72
+ const headers = new Headers(request.headers);
73
+ headers.delete("If-None-Match");
74
+ const response = await handler(new Request(request.url, {
75
+ method: "GET",
76
+ headers,
77
+ signal: request.signal
78
+ }));
79
+ if (response.status !== 200 || response.headers.get("Content-Type") !== MCP_SERVER_CARD_MEDIA_TYPE) return response;
80
+ const card = parseObject(await response.text());
81
+ if (card === void 0) return new Response(null, { status: 502 });
82
+ const remotes = Array.isArray(card["remotes"]) ? card["remotes"].map((remote) => isJsonObject(remote) ? withHandshakeVersions(remote) : remote) : card["remotes"];
83
+ return mcpServerCardResponse(request, {
84
+ ...card,
85
+ remotes
86
+ });
87
+ }
88
+ function withHandshakeVersions(remote) {
89
+ const listed = remote["supportedProtocolVersions"];
90
+ const versions = Array.isArray(listed) ? listed : [];
91
+ return {
92
+ ...remote,
93
+ supportedProtocolVersions: [...versions, ...HANDSHAKE_VERSIONS.filter((version) => !versions.includes(version))]
94
+ };
95
+ }
71
96
  function isHandshakeRequest(header, metaVersion) {
72
97
  if (header === null) return metaVersion !== MCP_PROTOCOL_VERSION;
73
98
  return HANDSHAKE_VERSIONS.some((version) => version === header);
74
99
  }
75
- function asCurrentRevisionRequest(request, method, params) {
76
- const headers = new Headers(request.headers);
77
- headers.set("MCP-Protocol-Version", MCP_PROTOCOL_VERSION);
78
- headers.set("Mcp-Method", method);
79
- headers.set("Accept", "application/json, text/event-stream");
80
- if (method === "tools/call" && typeof params["name"] === "string") headers.set("Mcp-Name", params["name"]);
81
- const meta = {
82
- ...metaOf({ params }),
83
- [PROTOCOL_VERSION_KEY]: MCP_PROTOCOL_VERSION,
84
- [CLIENT_CAPABILITIES_KEY]: {}
85
- };
86
- const body = JSON.stringify({
87
- jsonrpc: "2.0",
88
- id: 0,
89
- method,
90
- params: {
91
- ...params,
92
- _meta: meta
93
- }
94
- });
100
+ async function inputSchemaOf(handler, request, name) {
101
+ const response = await handler(asCurrentRevisionRequest(request, "tools/list", {}));
102
+ const result = response.ok ? parseObject(await response.text())?.["result"] : void 0;
103
+ const tool = (isJsonObject(result) && Array.isArray(result["tools"]) ? result["tools"] : []).find((each) => isJsonObject(each) && each["name"] === name);
104
+ return isJsonObject(tool) ? tool["inputSchema"] : void 0;
105
+ }
106
+ function asCurrentRevisionRequest(request, method, params, paramHeaders = {}) {
107
+ const wrapped = mcpWrappedRequest(method, params);
108
+ const headers = new Headers([...request.headers].filter(([name]) => !name.startsWith("mcp-param-")));
109
+ for (const [name, value] of Object.entries({
110
+ ...wrapped.headers,
111
+ ...paramHeaders
112
+ })) headers.set(name, value);
95
113
  return new Request(request.url, {
96
114
  method: "POST",
97
115
  headers,
98
- body,
116
+ body: wrapped.body,
99
117
  signal: request.signal
100
118
  });
101
119
  }
102
120
  /**
103
121
  * The result of `initialize`, from the result of `server/discover`. The wrapper keeps no session, so it cannot send
104
122
  * `notifications/tools/list_changed`. The events extension is only for revision 2026-07-28, so the capabilities do not
105
- * have it (spec/bindings/mcp.md#events).
123
+ * have it (spec/surfaces/mcp.md#events).
106
124
  */
107
125
  function initializeResult(offered, discovered) {
108
- const info = metaOf({ params: discovered })[SERVER_INFO_KEY];
109
- const name = isJsonObject(info) && typeof info["name"] === "string" ? info["name"] : "";
110
- const version = isJsonObject(info) && typeof info["version"] === "string" ? info["version"] : "";
111
- const instructions = typeof discovered["instructions"] === "string" ? discovered["instructions"] : void 0;
112
- const capabilities = isJsonObject(discovered["capabilities"]) ? discovered["capabilities"] : {};
126
+ const { name = "", version = "", instructions, capabilities = {} } = mcpDiscovered(discovered);
113
127
  return {
114
128
  protocolVersion: HANDSHAKE_VERSIONS.find((each) => each === offered) ?? LATEST_HANDSHAKE_VERSION,
115
129
  capabilities: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acid-cat/mcp-handshake",
3
- "version": "0.16.0",
3
+ "version": "0.18.0",
4
4
  "description": "Serve MCP clients of the revisions with an initialize handshake from an AcidCat MCP server through HTTP, with no state. Part of the TypeScript reference implementation.",
5
5
  "keywords": [
6
6
  "acidcat",
@@ -31,7 +31,7 @@
31
31
  "access": "public"
32
32
  },
33
33
  "dependencies": {
34
- "@acid-cat/spec-ts": "0.16.0"
34
+ "@acid-cat/spec-ts": "0.18.0"
35
35
  },
36
36
  "devDependencies": {
37
37
  "@types/node": "26.6.6",