@acid-cat/mcp-handshake 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Hebilicious
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # @acid-cat/mcp-handshake
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The [`initialize` handshake of earlier MCP revisions](../../spec/bindings/mcp.md#earlier-revisions-through-http) in front of an MCP server through HTTP. npm package: `@acid-cat/mcp-handshake`.
4
+
5
+ An MCP server of the [MCP binding](../../spec/bindings/mcp.md#http) through HTTP serves revision `2026-07-28`. Many MCP clients open the session with the `initialize` handshake of revision `2025-11-25` or `2025-06-18`. `mcpHandshake(handler)` gives a web handler that serves these clients too. It keeps no state.
6
+
7
+ This package is part of the TypeScript reference implementation. [`@acid-cat/core-ts`](../core-ts) gives it as `mcpHandshake`, and [`@acid-cat/core-effect`](../core-effect) gives it as `Mcp.withHandshake`. It works in front of any web handler that serves MCP revision `2026-07-28`.
8
+
9
+ ```sh
10
+ pnpm add @acid-cat/mcp-handshake
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```ts
16
+ import { mcpHandler, mcpHandshake } from "@acid-cat/core-ts";
17
+
18
+ const mcp = mcpHandshake(mcpHandler(app, options));
19
+ ```
20
+
21
+ ```ts
22
+ import { Mcp } from "@acid-cat/core-effect";
23
+
24
+ const mcp = Mcp.withHandshake(Mcp.toWebHandler(app, layer, options));
25
+ ```
26
+
27
+ | Option | Default | Use |
28
+ | --- | --- | --- |
29
+ | `maximumBytes` | 1 MiB | The longest body that the wrapper reads. The wrapper sends a longer body to the handler unchanged, and the handler rejects it |
30
+
31
+ ## How it works
32
+
33
+ - A request of revision `2026-07-28`, and a request that is not `POST`, go to the handler unchanged.
34
+ - The wrapper sends each request of a handshake to the handler as a request of revision `2026-07-28`: `tools/list` and `tools/call` as they are, and each other method as `server/discover`. So the handler screens the origin, the media type and the size of each request.
35
+ - A response that is not 2xx comes back unchanged.
36
+ - `initialize` gives the result of `server/discover` in the form of the handshake. `ping` gives an empty result. A notification gets 202. Another method gets the JSON-RPC error `-32601`.
37
+ - A result has no `_meta`, `resultType`, `ttlMs` or `cacheScope` member. A JSON-RPC error has the HTTP status 200.
38
+
39
+ The wrapper keeps no session, so it does not send `notifications/tools/list_changed`. Its capabilities are `"tools": { "listChanged": false }`. A client that must see the changes of the tools uses revision `2026-07-28`, or MCP through stdio.
@@ -0,0 +1,11 @@
1
+ /** A web request handler. */
2
+ export type WebHandler = (request: Request) => Promise<Response>;
3
+ export interface McpHandshakeOptions {
4
+ /** The maximum size of a request body, in bytes. Give the limit of the wrapped handler. The default is 1 MiB. */
5
+ readonly maximumBytes?: number | undefined;
6
+ }
7
+ /**
8
+ * A web handler that serves the `initialize` handshake of revisions 2025-11-25 and 2025-06-18, and gives all other
9
+ * requests to `handler` unchanged.
10
+ */
11
+ export declare function mcpHandshake(handler: WebHandler, options?: McpHandshakeOptions): WebHandler;
package/dist/index.js ADDED
@@ -0,0 +1,175 @@
1
+ import { MCP_EVENTS_EXTENSION, MCP_PROTOCOL_VERSION, isJsonObject, readBounded } from "@acid-cat/spec-ts";
2
+ //#region src/index.ts
3
+ /**
4
+ * The `initialize` handshake of earlier MCP revisions through HTTP (spec/bindings/mcp.md#earlier-revisions-through-http).
5
+ * `mcpHandshake` wraps the web handler of an MCP server that serves revision 2026-07-28. Each request of a revision with
6
+ * a handshake goes to that handler as a request of revision 2026-07-28, so the handler screens it. The wrapper keeps no
7
+ * state.
8
+ */
9
+ const PROTOCOL_VERSION_KEY = "io.modelcontextprotocol/protocolVersion";
10
+ const CLIENT_CAPABILITIES_KEY = "io.modelcontextprotocol/clientCapabilities";
11
+ const SERVER_INFO_KEY = "io.modelcontextprotocol/serverInfo";
12
+ /** The revisions with an `initialize` handshake that the wrapper serves, the latest first. */
13
+ const HANDSHAKE_VERSIONS = ["2025-11-25", "2025-06-18"];
14
+ /** The members of a result that only revision 2026-07-28 has. */
15
+ const CURRENT_ONLY_MEMBERS = /* @__PURE__ */ new Set([
16
+ "_meta",
17
+ "resultType",
18
+ "ttlMs",
19
+ "cacheScope"
20
+ ]);
21
+ /**
22
+ * A web handler that serves the `initialize` handshake of revisions 2025-11-25 and 2025-06-18, and gives all other
23
+ * requests to `handler` unchanged.
24
+ */
25
+ function mcpHandshake(handler, options = {}) {
26
+ const maximumBytes = options.maximumBytes ?? 1048576;
27
+ return async (request) => {
28
+ const header = request.headers.get("MCP-Protocol-Version");
29
+ if (request.method !== "POST" || header === MCP_PROTOCOL_VERSION) return handler(request);
30
+ const message = await readMessage(request.clone(), maximumBytes);
31
+ if (message === void 0 || !isHandshakeRequest(header, metaOf(message)[PROTOCOL_VERSION_KEY])) return handler(request);
32
+ return answer(handler, request, message);
33
+ };
34
+ }
35
+ /**
36
+ * Answer a request of a revision with a handshake. `initialize`, `ping` and notifications go to the handler as
37
+ * `server/discover`, and the other methods go as they are.
38
+ */
39
+ async function answer(handler, request, message) {
40
+ const method = typeof message["method"] === "string" ? message["method"] : "";
41
+ const params = isJsonObject(message["params"]) ? message["params"] : {};
42
+ const ownMethod = method === "tools/list" || method === "tools/call" ? method : "server/discover";
43
+ const response = await handler(current(request, ownMethod, ownMethod === "server/discover" ? {} : params));
44
+ if (!response.ok) return response;
45
+ const reply = parseObject(await response.text()) ?? {};
46
+ if (!("id" in message)) return new Response(null, { status: 202 });
47
+ const id = message["id"];
48
+ const result = reply["result"];
49
+ if (!isJsonObject(result)) return json({
50
+ jsonrpc: "2.0",
51
+ id,
52
+ error: reply["error"] ?? {
53
+ code: -32603,
54
+ message: "The server gave no result."
55
+ }
56
+ });
57
+ if (method === "initialize") return json({
58
+ jsonrpc: "2.0",
59
+ id,
60
+ result: initializeResult(params["protocolVersion"], result)
61
+ });
62
+ if (method === "ping") return json({
63
+ jsonrpc: "2.0",
64
+ id,
65
+ result: {}
66
+ });
67
+ if (ownMethod === "server/discover") return json({
68
+ jsonrpc: "2.0",
69
+ id,
70
+ error: {
71
+ code: -32601,
72
+ message: `Method not found: ${method}`
73
+ }
74
+ });
75
+ return json({
76
+ jsonrpc: "2.0",
77
+ id,
78
+ result: Object.fromEntries(Object.entries(result).filter(([member]) => !CURRENT_ONLY_MEMBERS.has(member)))
79
+ });
80
+ }
81
+ /**
82
+ * True if a request is of a revision with a handshake: its header names one, or it has no header and no revision
83
+ * 2026-07-28 in its `params._meta`.
84
+ */
85
+ function isHandshakeRequest(header, metaVersion) {
86
+ if (header === null) return metaVersion !== MCP_PROTOCOL_VERSION;
87
+ return HANDSHAKE_VERSIONS.some((version) => version === header);
88
+ }
89
+ /** The request of revision 2026-07-28 for `method`, with the headers and the metadata that the revision requires. */
90
+ function current(request, method, params) {
91
+ const headers = new Headers(request.headers);
92
+ headers.set("MCP-Protocol-Version", MCP_PROTOCOL_VERSION);
93
+ headers.set("Mcp-Method", method);
94
+ headers.set("Accept", "application/json, text/event-stream");
95
+ if (method === "tools/call" && typeof params["name"] === "string") headers.set("Mcp-Name", params["name"]);
96
+ const meta = {
97
+ ...metaOf({ params }),
98
+ [PROTOCOL_VERSION_KEY]: MCP_PROTOCOL_VERSION,
99
+ [CLIENT_CAPABILITIES_KEY]: {}
100
+ };
101
+ const body = JSON.stringify({
102
+ jsonrpc: "2.0",
103
+ id: 0,
104
+ method,
105
+ params: {
106
+ ...params,
107
+ _meta: meta
108
+ }
109
+ });
110
+ return new Request(request.url, {
111
+ method: "POST",
112
+ headers,
113
+ body,
114
+ signal: request.signal
115
+ });
116
+ }
117
+ /**
118
+ * The result of `initialize`, from the result of `server/discover`. The wrapper keeps no session, so it cannot send
119
+ * `notifications/tools/list_changed`. The events extension is only for revision 2026-07-28, so the capabilities do not
120
+ * have it (spec/bindings/mcp.md#events).
121
+ */
122
+ function initializeResult(offered, discovered) {
123
+ const info = metaOf({ params: discovered })[SERVER_INFO_KEY];
124
+ const name = isJsonObject(info) && typeof info["name"] === "string" ? info["name"] : "";
125
+ const version = isJsonObject(info) && typeof info["version"] === "string" ? info["version"] : "";
126
+ const instructions = typeof discovered["instructions"] === "string" ? discovered["instructions"] : void 0;
127
+ const capabilities = isJsonObject(discovered["capabilities"]) ? discovered["capabilities"] : {};
128
+ return {
129
+ protocolVersion: HANDSHAKE_VERSIONS.find((each) => each === offered) ?? HANDSHAKE_VERSIONS[0],
130
+ capabilities: {
131
+ ...withoutEvents(capabilities),
132
+ tools: { listChanged: false }
133
+ },
134
+ serverInfo: {
135
+ name,
136
+ version
137
+ },
138
+ ...instructions === void 0 ? {} : { instructions }
139
+ };
140
+ }
141
+ /** Capabilities without the events extension. A capability object with no other extension has no `extensions` member. */
142
+ function withoutEvents(capabilities) {
143
+ const { extensions, ...rest } = capabilities;
144
+ if (!isJsonObject(extensions)) return capabilities;
145
+ const { [MCP_EVENTS_EXTENSION]: _, ...others } = extensions;
146
+ return Object.keys(others).length === 0 ? rest : {
147
+ ...rest,
148
+ extensions: others
149
+ };
150
+ }
151
+ /** The `params._meta` of a message, or an empty object. */
152
+ function metaOf(message) {
153
+ const params = message["params"];
154
+ const meta = isJsonObject(params) ? params["_meta"] : void 0;
155
+ return isJsonObject(meta) ? meta : {};
156
+ }
157
+ /** The JSON object in the body of a request, or `undefined` when the body is too large or not a JSON object. */
158
+ async function readMessage(request, maximumBytes) {
159
+ const body = await readBounded(request, maximumBytes);
160
+ return "text" in body ? parseObject(body.text) : void 0;
161
+ }
162
+ function parseObject(text) {
163
+ try {
164
+ const value = JSON.parse(text);
165
+ return isJsonObject(value) ? value : void 0;
166
+ } catch {
167
+ return;
168
+ }
169
+ }
170
+ const json = (body) => new Response(JSON.stringify(body), {
171
+ status: 200,
172
+ headers: { "Content-Type": "application/json" }
173
+ });
174
+ //#endregion
175
+ export { mcpHandshake };
package/package.json CHANGED
@@ -1,6 +1,40 @@
1
1
  {
2
2
  "name": "@acid-cat/mcp-handshake",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.10.0",
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
+ "keywords": [
6
+ "acidcat",
7
+ "mcp"
8
+ ],
9
+ "license": "MIT",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/Hebilicious/acidcat.git",
13
+ "directory": "packages/mcp-handshake"
14
+ },
15
+ "homepage": "https://github.com/Hebilicious/acidcat/tree/main/packages/mcp-handshake#readme",
16
+ "bugs": "https://github.com/Hebilicious/acidcat/issues",
17
+ "type": "module",
18
+ "engines": {
19
+ "node": ">=24"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ }
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "dependencies": {
34
+ "@acid-cat/spec-ts": "0.10.0"
35
+ },
36
+ "devDependencies": {
37
+ "@types/node": "26.6.5",
38
+ "typescript": "7.0.2"
39
+ }
6
40
  }