@forklaunch/express 1.2.53 → 2.0.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/lib/index.js CHANGED
@@ -2061,6 +2061,67 @@ function contentParse(options2) {
2061
2061
  };
2062
2062
  }
2063
2063
 
2064
+ // src/mcpBridge.ts
2065
+ var import_stream2 = require("stream");
2066
+ async function toFetchRequest(req, protocol) {
2067
+ const headers = new Headers();
2068
+ for (const [key, value] of Object.entries(req.headers)) {
2069
+ if (Array.isArray(value)) {
2070
+ value.forEach((v) => headers.append(key, v));
2071
+ } else if (value != null) {
2072
+ headers.set(key, value);
2073
+ }
2074
+ }
2075
+ let body;
2076
+ if (req.method !== "GET" && req.method !== "HEAD") {
2077
+ const rawBody = req._rawBody;
2078
+ if (Buffer.isBuffer(rawBody)) {
2079
+ body = rawBody;
2080
+ } else if (!req.readableEnded) {
2081
+ const chunks = [];
2082
+ for await (const chunk of req) {
2083
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
2084
+ }
2085
+ body = Buffer.concat(chunks);
2086
+ } else if (req.body !== void 0) {
2087
+ body = Buffer.from(JSON.stringify(req.body));
2088
+ }
2089
+ }
2090
+ return new globalThis.Request(
2091
+ `${protocol}://${req.headers.host ?? "localhost"}${req.originalUrl}`,
2092
+ {
2093
+ method: req.method,
2094
+ headers,
2095
+ body: body ? new Uint8Array(body) : void 0
2096
+ }
2097
+ );
2098
+ }
2099
+ async function writeFetchResponse(response, res) {
2100
+ const headers = {};
2101
+ response.headers.forEach((value, key) => {
2102
+ headers[key] = value;
2103
+ });
2104
+ res.writeHead(response.status, headers);
2105
+ if (!response.body) {
2106
+ res.end();
2107
+ return;
2108
+ }
2109
+ res.flushHeaders();
2110
+ const body = import_stream2.Readable.fromWeb(
2111
+ response.body
2112
+ );
2113
+ res.on("close", () => body.destroy());
2114
+ await new Promise((resolve) => {
2115
+ body.on("end", resolve);
2116
+ body.on("error", () => {
2117
+ res.destroy();
2118
+ resolve();
2119
+ });
2120
+ body.on("close", resolve);
2121
+ body.pipe(res);
2122
+ });
2123
+ }
2124
+
2064
2125
  // src/middleware/enrichResponseTransmission.middleware.ts
2065
2126
  var import_common3 = require("@forklaunch/common");
2066
2127
  var import_http2 = require("@forklaunch/core/http");
@@ -2177,10 +2238,12 @@ var Application = class extends import_http3.ForklaunchExpressLikeApplication {
2177
2238
  version,
2178
2239
  additionalTools,
2179
2240
  contentTypeMapping,
2180
- authenticate
2241
+ authenticate,
2242
+ sessions,
2243
+ sessionIdleTimeoutMs
2181
2244
  } = this.mcpConfiguration ?? {};
2182
2245
  const zodSchemaValidator = this.schemaValidator;
2183
- const finalMcpPort = mcpPort ?? port2 + 2e3;
2246
+ const finalMcpPort = mcpPort ?? (Number(process.env.MCP_PORT) || port2 + 2e3);
2184
2247
  const mcpServer = (0, import_http3.generateMcpServer)(
2185
2248
  zodSchemaValidator,
2186
2249
  protocol,
@@ -2196,7 +2259,25 @@ var Application = class extends import_http3.ForklaunchExpressLikeApplication {
2196
2259
  if (additionalTools) {
2197
2260
  additionalTools(mcpServer);
2198
2261
  }
2199
- if (this.hostingConfiguration?.workerCount && this.hostingConfiguration.workerCount > 1) {
2262
+ if (!(0, import_http3.isSeparateMcpListener)(mcpPort)) {
2263
+ const mcpHandler = (0, import_http3.createMcpHttpHandler)(mcpServer, {
2264
+ authenticate,
2265
+ sessions,
2266
+ sessionIdleTimeoutMs,
2267
+ workerCount: this.hostingConfiguration?.workerCount
2268
+ });
2269
+ this.internal.all(mcpPath ?? "/mcp", async (req, res, next) => {
2270
+ try {
2271
+ const response = await mcpHandler.handle(
2272
+ await toFetchRequest(req, protocol),
2273
+ req
2274
+ );
2275
+ await writeFetchResponse(response, res);
2276
+ } catch (error) {
2277
+ next(error);
2278
+ }
2279
+ });
2280
+ } else if (this.hostingConfiguration?.workerCount && this.hostingConfiguration.workerCount > 1) {
2200
2281
  (0, import_http3.isPortBound)(finalMcpPort, host).then((isBound) => {
2201
2282
  if (!isBound) {
2202
2283
  mcpServer.start({
package/lib/index.mjs CHANGED
@@ -7,11 +7,13 @@ import {
7
7
  import { getEnvVar, safeStringify as safeStringify2 } from "@forklaunch/common";
8
8
  import {
9
9
  ATTR_HTTP_RESPONSE_STATUS_CODE,
10
+ createMcpHttpHandler,
10
11
  ForklaunchExpressLikeApplication,
11
12
  generateMcpServer,
12
13
  generateOpenApiSpecs,
13
14
  isForklaunchRequest,
14
15
  isPortBound,
16
+ isSeparateMcpListener,
15
17
  meta,
16
18
  OPENAPI_DEFAULT_VERSION
17
19
  } from "@forklaunch/core/http";
@@ -2035,6 +2037,67 @@ function contentParse(options2) {
2035
2037
  };
2036
2038
  }
2037
2039
 
2040
+ // src/mcpBridge.ts
2041
+ import { Readable as Readable2 } from "stream";
2042
+ async function toFetchRequest(req, protocol) {
2043
+ const headers = new Headers();
2044
+ for (const [key, value] of Object.entries(req.headers)) {
2045
+ if (Array.isArray(value)) {
2046
+ value.forEach((v) => headers.append(key, v));
2047
+ } else if (value != null) {
2048
+ headers.set(key, value);
2049
+ }
2050
+ }
2051
+ let body;
2052
+ if (req.method !== "GET" && req.method !== "HEAD") {
2053
+ const rawBody = req._rawBody;
2054
+ if (Buffer.isBuffer(rawBody)) {
2055
+ body = rawBody;
2056
+ } else if (!req.readableEnded) {
2057
+ const chunks = [];
2058
+ for await (const chunk of req) {
2059
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
2060
+ }
2061
+ body = Buffer.concat(chunks);
2062
+ } else if (req.body !== void 0) {
2063
+ body = Buffer.from(JSON.stringify(req.body));
2064
+ }
2065
+ }
2066
+ return new globalThis.Request(
2067
+ `${protocol}://${req.headers.host ?? "localhost"}${req.originalUrl}`,
2068
+ {
2069
+ method: req.method,
2070
+ headers,
2071
+ body: body ? new Uint8Array(body) : void 0
2072
+ }
2073
+ );
2074
+ }
2075
+ async function writeFetchResponse(response, res) {
2076
+ const headers = {};
2077
+ response.headers.forEach((value, key) => {
2078
+ headers[key] = value;
2079
+ });
2080
+ res.writeHead(response.status, headers);
2081
+ if (!response.body) {
2082
+ res.end();
2083
+ return;
2084
+ }
2085
+ res.flushHeaders();
2086
+ const body = Readable2.fromWeb(
2087
+ response.body
2088
+ );
2089
+ res.on("close", () => body.destroy());
2090
+ await new Promise((resolve) => {
2091
+ body.on("end", resolve);
2092
+ body.on("error", () => {
2093
+ res.destroy();
2094
+ resolve();
2095
+ });
2096
+ body.on("close", resolve);
2097
+ body.pipe(res);
2098
+ });
2099
+ }
2100
+
2038
2101
  // src/middleware/enrichResponseTransmission.middleware.ts
2039
2102
  import { safeStringify } from "@forklaunch/common";
2040
2103
  import {
@@ -2153,10 +2216,12 @@ var Application = class extends ForklaunchExpressLikeApplication {
2153
2216
  version,
2154
2217
  additionalTools,
2155
2218
  contentTypeMapping,
2156
- authenticate
2219
+ authenticate,
2220
+ sessions,
2221
+ sessionIdleTimeoutMs
2157
2222
  } = this.mcpConfiguration ?? {};
2158
2223
  const zodSchemaValidator = this.schemaValidator;
2159
- const finalMcpPort = mcpPort ?? port2 + 2e3;
2224
+ const finalMcpPort = mcpPort ?? (Number(process.env.MCP_PORT) || port2 + 2e3);
2160
2225
  const mcpServer = generateMcpServer(
2161
2226
  zodSchemaValidator,
2162
2227
  protocol,
@@ -2172,7 +2237,25 @@ var Application = class extends ForklaunchExpressLikeApplication {
2172
2237
  if (additionalTools) {
2173
2238
  additionalTools(mcpServer);
2174
2239
  }
2175
- if (this.hostingConfiguration?.workerCount && this.hostingConfiguration.workerCount > 1) {
2240
+ if (!isSeparateMcpListener(mcpPort)) {
2241
+ const mcpHandler = createMcpHttpHandler(mcpServer, {
2242
+ authenticate,
2243
+ sessions,
2244
+ sessionIdleTimeoutMs,
2245
+ workerCount: this.hostingConfiguration?.workerCount
2246
+ });
2247
+ this.internal.all(mcpPath ?? "/mcp", async (req, res, next) => {
2248
+ try {
2249
+ const response = await mcpHandler.handle(
2250
+ await toFetchRequest(req, protocol),
2251
+ req
2252
+ );
2253
+ await writeFetchResponse(response, res);
2254
+ } catch (error) {
2255
+ next(error);
2256
+ }
2257
+ });
2258
+ } else if (this.hostingConfiguration?.workerCount && this.hostingConfiguration.workerCount > 1) {
2176
2259
  isPortBound(finalMcpPort, host).then((isBound) => {
2177
2260
  if (!isBound) {
2178
2261
  mcpServer.start({
@@ -1 +1 @@
1
- {"version":3,"file":"expressApplication.d.ts","sourceRoot":"","sources":["../../src/expressApplication.ts"],"names":[],"mappings":"AACA,OAAO,EAIL,gCAAgC,EAOhC,iBAAiB,EAEjB,sBAAsB,EACtB,aAAa,EACd,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAI3D,OAAgB,EAEd,OAAO,EACP,YAAY,EACZ,OAAO,EACP,cAAc,EACd,QAAQ,EACT,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAO9B,OAAO,EAAE,yBAAyB,EAAE,MAAM,8BAA8B,CAAC;AAEzE;;;;;;;;;;GAUG;AACH,qBAAa,WAAW,CACtB,EAAE,SAAS,kBAAkB,EAC7B,aAAa,SAAS,aAAa,CAAC,EAAE,CAAC,CACvC,SAAQ,gCAAgC,CACxC,EAAE,EACF,OAAO,EACP,cAAc,EACd,OAAO,EACP,QAAQ,EACR,YAAY,EACZ,aAAa,CACd;IACC,OAAO,CAAC,iBAAiB,CAAgC;IACzD,OAAO,CAAC,gBAAgB,CAC8C;IACtE,OAAO,CAAC,oBAAoB,CAC8C;IAC1E,OAAO,CAAC,oBAAoB,CAC8C;IAC1E;;;;;;OAMG;IACH,YACE,eAAe,EAAE,EAAE,EACnB,sBAAsB,EAAE,sBAAsB,CAAC,iBAAiB,CAAC,EACjE,OAAO,CAAC,EAAE,yBAAyB,CAAC,EAAE,EAAE,aAAa,CAAC,EA0BvD;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,MAAM,CACJ,IAAI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OAAO,CAAC,iBAAiB,CAAgC;IACzD,OAAO,CAAC,gBAAgB,CAC8C;IACtE,OAAO,CAAC,oBAAoB,CAC8C;IAC1E,OAAO,CAAC,oBAAoB,CAC8C;IAC1E;;;;;;OAMG;IACH,YACE,eAAe,EAAE,EAAE,EACnB,sBAAsB,EAAE,sBAAsB,CAAC,iBAAiB,CAAC,EACjE,OAAO,CAAC,EAAE,yBAAyB,CAAC,EAAE,EAAE,aAAa,CAAC,EA0BvD;IAED;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,MAAM,CACJ,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,MAAM,EACf,QAAQ,CAAC,EAAE,MAAM,IAAI,GACpB,MAAM,CAAC;IACV,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,MAAM,CAAC;IACtE,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,MAAM,CAAC;IACpD,MAAM,CAAC,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,MAAM,CAAC;IACtC,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,IAAI,GAAG,MAAM,CAAC;IACpD,MAAM,CAAC,MAAM,EAAE,OAAO,EAAE,iBAAiB,CAAC,EAAE,MAAM,IAAI,GAAG,MAAM,CAAC;CAiUjE"}
@@ -0,0 +1,13 @@
1
+ import { Request, Response } from 'express';
2
+ /**
3
+ * Builds a fetch `Request` from an Express request so the shared MCP handler
4
+ * can serve it. Uses the bytes captured by the content parser when a body was
5
+ * already consumed, and reads the stream otherwise.
6
+ */
7
+ export declare function toFetchRequest(req: Request, protocol: 'http' | 'https'): Promise<globalThis.Request>;
8
+ /**
9
+ * Writes a fetch `Response` to an Express response, streaming the body so
10
+ * Server-Sent Events reach the client as they are produced.
11
+ */
12
+ export declare function writeFetchResponse(response: globalThis.Response, res: Response): Promise<void>;
13
+ //# sourceMappingURL=mcpBridge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mcpBridge.d.ts","sourceRoot":"","sources":["../../src/mcpBridge.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAI5C;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,GAAG,EAAE,OAAO,EACZ,QAAQ,EAAE,MAAM,GAAG,OAAO,GACzB,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAkC7B;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,CACtC,QAAQ,EAAE,UAAU,CAAC,QAAQ,EAC7B,GAAG,EAAE,QAAQ,GACZ,OAAO,CAAC,IAAI,CAAC,CA0Bf"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@forklaunch/express",
3
- "version": "1.2.53",
3
+ "version": "2.0.0",
4
4
  "description": "Forklaunch framework for express.",
5
5
  "homepage": "https://github.com/forklaunch/forklaunch-js#readme",
6
6
  "bugs": {
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/forklaunch/forklaunch-js.git"
11
+ "url": "git+https://github.com/forklaunch/forklaunch.git"
12
12
  },
13
13
  "license": "MIT",
14
14
  "author": "Rohin Bhargava",
@@ -29,48 +29,43 @@
29
29
  ],
30
30
  "dependencies": {
31
31
  "@forklaunch/common": "^1.2.32",
32
- "@forklaunch/core": "^3.3.0",
32
+ "@forklaunch/core": "^4.0.0",
33
33
  "@forklaunch/validator": "^1.3.0",
34
- "@modelcontextprotocol/sdk": "^1.32.1",
35
- "@scalar/express-api-reference": "^0.10.30",
34
+ "@modelcontextprotocol/sdk": "^1.32.2",
35
+ "@scalar/express-api-reference": "^0.10.39",
36
36
  "@types/multer": "^2.3.0",
37
- "body-parser": "^2.3.0",
37
+ "body-parser": "^2.4.0",
38
38
  "busboy": "^1.6.0",
39
39
  "cors": "^2.8.6",
40
40
  "express": "^5.3.0",
41
- "fastmcp": "^4.23.2",
42
- "multer": "2.4.0",
43
- "qs": "^6.16.1",
41
+ "fastmcp": "^4.28.2",
42
+ "multer": "2.5.0",
43
+ "qs": "^6.17.1",
44
44
  "range-parser": "^1.3.0",
45
45
  "swagger-ui-express": "^5.0.1"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@eslint/js": "^10.0.1",
49
49
  "@types/body-parser": "^1.19.6",
50
- "@types/bun": "^1.4.3",
50
+ "@types/bun": "^1.4.4",
51
51
  "@types/busboy": "^1.5.5",
52
52
  "@types/cors": "^2.8.19",
53
53
  "@types/express": "^5.0.6",
54
54
  "@types/express-serve-static-core": "^5.1.3",
55
- "@types/jest": "^30.0.0",
56
- "@types/node": "^26.6.5",
55
+ "@types/node": "^26.6.6",
57
56
  "@types/qs": "^6.15.1",
58
57
  "@types/range-parser": "^1.2.7",
59
58
  "@types/swagger-ui-express": "^4.1.8",
60
- "jest": "^30.5.2",
61
59
  "kill-port-process": "^5.0.0",
62
- "prettier": "^3.9.10",
63
- "ts-jest": "^29.4.15",
60
+ "prettier": "^3.10.1",
64
61
  "ts-node": "^10.9.2",
65
62
  "tsup": "^9.0.0",
66
- "typedoc": "^0.28.20",
67
63
  "typescript": "^7.0.2",
68
- "typescript-eslint": "^8.72.0"
64
+ "typescript-eslint": "^8.73.0"
69
65
  },
70
66
  "scripts": {
71
67
  "build": "tsc --noEmit && tsup index.ts --format cjs,esm --no-splitting --tsconfig tsconfig.json --outDir lib --clean && tsc -p tsconfig.build.json --emitDeclarationOnly",
72
68
  "clean": "rm -rf lib pnpm.lock.yaml node_modules",
73
- "docs": "typedoc --out docs *",
74
69
  "format": "prettier --ignore-path=.prettierignore --config .prettierrc '**/*.{ts,tsx,json}' --write",
75
70
  "lint": "eslint . -c eslint.config.mjs",
76
71
  "lint:fix": "eslint . -c eslint.config.mjs --fix",