@forklaunch/core 3.4.0 → 3.5.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/http/index.d.ts +1 -0
- package/lib/http/index.d.ts.map +1 -1
- package/lib/http/index.js +227 -13
- package/lib/http/index.js.map +1 -1
- package/lib/http/index.mjs +223 -13
- package/lib/http/index.mjs.map +1 -1
- package/lib/http/mcpGenerator/mcpGenerator.d.ts.map +1 -1
- package/lib/http/mcpGenerator/mcpHttpHandler.d.ts +57 -0
- package/lib/http/mcpGenerator/mcpHttpHandler.d.ts.map +1 -0
- package/lib/http/telemetry/openTelemetryCollector.d.ts.map +1 -1
- package/lib/http/types/expressLikeOptions.d.ts +12 -0
- package/lib/http/types/expressLikeOptions.d.ts.map +1 -1
- package/package.json +26 -26
package/lib/http/index.mjs
CHANGED
|
@@ -446,13 +446,15 @@ new NodeSDK({
|
|
|
446
446
|
url: `${getEnvVar("OTEL_EXPORTER_OTLP_ENDPOINT") ?? "http://localhost:4318"}/v1/traces`,
|
|
447
447
|
headers: otelHeaders
|
|
448
448
|
}),
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
449
|
+
metricReaders: [
|
|
450
|
+
new PeriodicExportingMetricReader({
|
|
451
|
+
exporter: new OTLPMetricExporter({
|
|
452
|
+
url: `${getEnvVar("OTEL_EXPORTER_OTLP_ENDPOINT") ?? "http://localhost:4318"}/v1/metrics`,
|
|
453
|
+
headers: otelHeaders
|
|
454
|
+
}),
|
|
455
|
+
exportIntervalMillis: 5e3
|
|
456
|
+
})
|
|
457
|
+
],
|
|
456
458
|
logRecordProcessors: [
|
|
457
459
|
new BatchLogRecordProcessor({
|
|
458
460
|
exporter: new OTLPLogExporter({
|
|
@@ -4780,6 +4782,182 @@ function unpackRouters(routers, recursiveBasePath = []) {
|
|
|
4780
4782
|
}, []);
|
|
4781
4783
|
}
|
|
4782
4784
|
|
|
4785
|
+
// src/http/mcpGenerator/mcpHttpHandler.ts
|
|
4786
|
+
import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
|
|
4787
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
4788
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
4789
|
+
var DEFAULT_MCP_SESSION_IDLE_TIMEOUT_MS = 15 * 60 * 1e3;
|
|
4790
|
+
var mcpTransportHeaders = new AsyncLocalStorage2();
|
|
4791
|
+
function getMcpTransportHeaders() {
|
|
4792
|
+
return mcpTransportHeaders.getStore();
|
|
4793
|
+
}
|
|
4794
|
+
function createMcpHttpHandler(mcpServer, options2) {
|
|
4795
|
+
const mode = options2?.sessions ?? "stateless";
|
|
4796
|
+
if (mode === "stateful" && (options2?.workerCount ?? 1) > 1) {
|
|
4797
|
+
throw new Error(
|
|
4798
|
+
"Stateful MCP sessions live in one process's memory and cannot be shared across workers. Use mcp.sessions: 'stateless' or a single worker."
|
|
4799
|
+
);
|
|
4800
|
+
}
|
|
4801
|
+
const idleTimeoutMs = options2?.sessionIdleTimeoutMs ?? DEFAULT_MCP_SESSION_IDLE_TIMEOUT_MS;
|
|
4802
|
+
const sessions = /* @__PURE__ */ new Map();
|
|
4803
|
+
const jsonRpcError = (status, code, message, headers) => new Response(
|
|
4804
|
+
JSON.stringify({ jsonrpc: "2.0", error: { code, message }, id: null }),
|
|
4805
|
+
{
|
|
4806
|
+
status,
|
|
4807
|
+
headers: { "Content-Type": "application/json", ...headers }
|
|
4808
|
+
}
|
|
4809
|
+
);
|
|
4810
|
+
let sweepTimer;
|
|
4811
|
+
if (mode === "stateful") {
|
|
4812
|
+
sweepTimer = setInterval(
|
|
4813
|
+
() => {
|
|
4814
|
+
const cutoff = Date.now() - idleTimeoutMs;
|
|
4815
|
+
for (const [id, entry] of sessions) {
|
|
4816
|
+
if (entry.lastSeen < cutoff) {
|
|
4817
|
+
sessions.delete(id);
|
|
4818
|
+
entry.session.close().catch(() => void 0);
|
|
4819
|
+
}
|
|
4820
|
+
}
|
|
4821
|
+
},
|
|
4822
|
+
Math.min(idleTimeoutMs, 6e4)
|
|
4823
|
+
);
|
|
4824
|
+
sweepTimer.unref();
|
|
4825
|
+
}
|
|
4826
|
+
async function authenticateRequest(authRequest) {
|
|
4827
|
+
if (!options2?.authenticate) {
|
|
4828
|
+
return { ok: true, auth: void 0 };
|
|
4829
|
+
}
|
|
4830
|
+
try {
|
|
4831
|
+
const auth = await options2.authenticate(authRequest);
|
|
4832
|
+
return auth ? { ok: true, auth } : { ok: false };
|
|
4833
|
+
} catch {
|
|
4834
|
+
return { ok: false };
|
|
4835
|
+
}
|
|
4836
|
+
}
|
|
4837
|
+
async function handleStateless(request, auth) {
|
|
4838
|
+
if (request.method !== "POST") {
|
|
4839
|
+
return jsonRpcError(405, -32e3, "Method not allowed", {
|
|
4840
|
+
Allow: "POST"
|
|
4841
|
+
});
|
|
4842
|
+
}
|
|
4843
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
4844
|
+
sessionIdGenerator: void 0
|
|
4845
|
+
});
|
|
4846
|
+
const session = await mcpServer.connect(transport, auth);
|
|
4847
|
+
let closed = false;
|
|
4848
|
+
const closeSession = () => {
|
|
4849
|
+
if (!closed) {
|
|
4850
|
+
closed = true;
|
|
4851
|
+
session.close().catch(() => void 0);
|
|
4852
|
+
}
|
|
4853
|
+
};
|
|
4854
|
+
try {
|
|
4855
|
+
const response = await mcpTransportHeaders.run(
|
|
4856
|
+
request.headers,
|
|
4857
|
+
() => transport.handleRequest(request)
|
|
4858
|
+
);
|
|
4859
|
+
return closeWhenBodyEnds(response, closeSession);
|
|
4860
|
+
} catch (error) {
|
|
4861
|
+
closeSession();
|
|
4862
|
+
throw error;
|
|
4863
|
+
}
|
|
4864
|
+
}
|
|
4865
|
+
async function handleStateful(request, auth) {
|
|
4866
|
+
const sessionId = request.headers.get("mcp-session-id");
|
|
4867
|
+
if (sessionId) {
|
|
4868
|
+
const entry = sessions.get(sessionId);
|
|
4869
|
+
if (!entry) {
|
|
4870
|
+
return jsonRpcError(404, -32001, "Session not found");
|
|
4871
|
+
}
|
|
4872
|
+
entry.lastSeen = Date.now();
|
|
4873
|
+
if (auth) {
|
|
4874
|
+
entry.session.updateAuth(auth);
|
|
4875
|
+
}
|
|
4876
|
+
return mcpTransportHeaders.run(
|
|
4877
|
+
request.headers,
|
|
4878
|
+
() => entry.transport.handleRequest(request)
|
|
4879
|
+
);
|
|
4880
|
+
}
|
|
4881
|
+
const transport = new WebStandardStreamableHTTPServerTransport({
|
|
4882
|
+
sessionIdGenerator: () => randomUUID2(),
|
|
4883
|
+
onsessioninitialized: (id) => {
|
|
4884
|
+
sessions.set(id, { transport, session, lastSeen: Date.now() });
|
|
4885
|
+
},
|
|
4886
|
+
onsessionclosed: (id) => {
|
|
4887
|
+
sessions.delete(id);
|
|
4888
|
+
}
|
|
4889
|
+
});
|
|
4890
|
+
const session = await mcpServer.connect(transport, auth);
|
|
4891
|
+
transport.onclose = /* @__PURE__ */ ((onclose) => () => {
|
|
4892
|
+
if (transport.sessionId) {
|
|
4893
|
+
sessions.delete(transport.sessionId);
|
|
4894
|
+
}
|
|
4895
|
+
onclose?.();
|
|
4896
|
+
})(transport.onclose);
|
|
4897
|
+
const response = await mcpTransportHeaders.run(
|
|
4898
|
+
request.headers,
|
|
4899
|
+
() => transport.handleRequest(request)
|
|
4900
|
+
);
|
|
4901
|
+
if (!transport.sessionId) {
|
|
4902
|
+
await session.close();
|
|
4903
|
+
}
|
|
4904
|
+
return response;
|
|
4905
|
+
}
|
|
4906
|
+
async function handle(request, authRequest) {
|
|
4907
|
+
const authResult = await authenticateRequest(authRequest);
|
|
4908
|
+
if (!authResult.ok) {
|
|
4909
|
+
return jsonRpcError(401, -32001, "Unauthorized", {
|
|
4910
|
+
"WWW-Authenticate": "Bearer"
|
|
4911
|
+
});
|
|
4912
|
+
}
|
|
4913
|
+
return mode === "stateless" ? handleStateless(request, authResult.auth) : handleStateful(request, authResult.auth);
|
|
4914
|
+
}
|
|
4915
|
+
async function close() {
|
|
4916
|
+
if (sweepTimer) {
|
|
4917
|
+
clearInterval(sweepTimer);
|
|
4918
|
+
}
|
|
4919
|
+
const entries = [...sessions.values()];
|
|
4920
|
+
sessions.clear();
|
|
4921
|
+
await Promise.all(entries.map(({ session }) => session.close()));
|
|
4922
|
+
}
|
|
4923
|
+
return { handle, close, sessions };
|
|
4924
|
+
}
|
|
4925
|
+
function closeWhenBodyEnds(response, done) {
|
|
4926
|
+
if (!response.body) {
|
|
4927
|
+
done();
|
|
4928
|
+
return response;
|
|
4929
|
+
}
|
|
4930
|
+
const reader = response.body.getReader();
|
|
4931
|
+
const body = new ReadableStream({
|
|
4932
|
+
async pull(controller) {
|
|
4933
|
+
try {
|
|
4934
|
+
const { done: finished, value } = await reader.read();
|
|
4935
|
+
if (finished) {
|
|
4936
|
+
controller.close();
|
|
4937
|
+
done();
|
|
4938
|
+
} else {
|
|
4939
|
+
controller.enqueue(value);
|
|
4940
|
+
}
|
|
4941
|
+
} catch (error) {
|
|
4942
|
+
controller.error(error);
|
|
4943
|
+
done();
|
|
4944
|
+
}
|
|
4945
|
+
},
|
|
4946
|
+
cancel(reason) {
|
|
4947
|
+
done();
|
|
4948
|
+
return reader.cancel(reason);
|
|
4949
|
+
}
|
|
4950
|
+
});
|
|
4951
|
+
return new Response(body, {
|
|
4952
|
+
status: response.status,
|
|
4953
|
+
statusText: response.statusText,
|
|
4954
|
+
headers: response.headers
|
|
4955
|
+
});
|
|
4956
|
+
}
|
|
4957
|
+
function isSeparateMcpListener(configuredPort) {
|
|
4958
|
+
return process.env.MCP_LISTENER === "separate" || configuredPort != null || Boolean(Number(process.env.MCP_PORT));
|
|
4959
|
+
}
|
|
4960
|
+
|
|
4783
4961
|
// src/http/mcpGenerator/mcpGenerator.ts
|
|
4784
4962
|
function generateInputSchema(schemaValidator, body, params, query, requestHeaders, auth) {
|
|
4785
4963
|
let discriminatedBody;
|
|
@@ -4809,6 +4987,36 @@ function generateInputSchema(schemaValidator, body, params, query, requestHeader
|
|
|
4809
4987
|
} : {}
|
|
4810
4988
|
});
|
|
4811
4989
|
}
|
|
4990
|
+
function buildRouteHeaders(toolHeaders, contentType) {
|
|
4991
|
+
const transportHeaders = getMcpTransportHeaders();
|
|
4992
|
+
const routeHeaders = new Headers();
|
|
4993
|
+
const authorization = transportHeaders?.get("authorization");
|
|
4994
|
+
if (authorization) {
|
|
4995
|
+
routeHeaders.set("authorization", authorization);
|
|
4996
|
+
}
|
|
4997
|
+
for (const [name, value] of Object.entries(toolHeaders ?? {})) {
|
|
4998
|
+
if (value != null) {
|
|
4999
|
+
routeHeaders.set(name, String(value));
|
|
5000
|
+
}
|
|
5001
|
+
}
|
|
5002
|
+
const correlationId = transportHeaders?.get("x-correlation-id");
|
|
5003
|
+
if (correlationId) {
|
|
5004
|
+
routeHeaders.set("x-correlation-id", correlationId);
|
|
5005
|
+
}
|
|
5006
|
+
const traceparent = transportHeaders?.get("traceparent");
|
|
5007
|
+
if (traceparent) {
|
|
5008
|
+
routeHeaders.set("traceparent", traceparent);
|
|
5009
|
+
routeHeaders.delete("tracestate");
|
|
5010
|
+
const tracestate = transportHeaders?.get("tracestate");
|
|
5011
|
+
if (tracestate) {
|
|
5012
|
+
routeHeaders.set("tracestate", tracestate);
|
|
5013
|
+
}
|
|
5014
|
+
}
|
|
5015
|
+
if (contentType !== void 0) {
|
|
5016
|
+
routeHeaders.set("content-type", contentType);
|
|
5017
|
+
}
|
|
5018
|
+
return routeHeaders;
|
|
5019
|
+
}
|
|
4812
5020
|
function generateMcpServer(schemaValidator, protocol, host, port, version, application, appOptions, options2, contentTypeMap, authenticate) {
|
|
4813
5021
|
if (!(schemaValidator instanceof ZodSchemaValidator)) {
|
|
4814
5022
|
throw new Error(
|
|
@@ -4961,12 +5169,10 @@ function generateMcpServer(schemaValidator, protocol, host, port, version, appli
|
|
|
4961
5169
|
}
|
|
4962
5170
|
const response = await fetch(url, {
|
|
4963
5171
|
method: route.method.toUpperCase(),
|
|
4964
|
-
headers:
|
|
4965
|
-
|
|
4966
|
-
|
|
4967
|
-
|
|
4968
|
-
} : {}
|
|
4969
|
-
},
|
|
5172
|
+
headers: buildRouteHeaders(
|
|
5173
|
+
headers,
|
|
5174
|
+
discriminatedBody?.contentType != "multipart/form-data" ? contentType ?? discriminatedBody?.contentType : void 0
|
|
5175
|
+
),
|
|
4970
5176
|
body: parsedBody
|
|
4971
5177
|
});
|
|
4972
5178
|
if (response.status >= 300) {
|
|
@@ -5939,6 +6145,7 @@ export {
|
|
|
5939
6145
|
ATTR_HTTP_ROUTE,
|
|
5940
6146
|
ATTR_SERVICE_NAME,
|
|
5941
6147
|
AuditLogger,
|
|
6148
|
+
DEFAULT_MCP_SESSION_IDLE_TIMEOUT_MS,
|
|
5942
6149
|
EMAIL_LIMITS,
|
|
5943
6150
|
EmailRequestError,
|
|
5944
6151
|
ForklaunchExpressLikeApplication,
|
|
@@ -5973,6 +6180,7 @@ export {
|
|
|
5973
6180
|
createHmacToken,
|
|
5974
6181
|
createInstanceGatewayTransport,
|
|
5975
6182
|
createLocalModelGatewayClient,
|
|
6183
|
+
createMcpHttpHandler,
|
|
5976
6184
|
createModelGatewayClient,
|
|
5977
6185
|
createPaymentsClient,
|
|
5978
6186
|
createPreviewModelGatewayClient,
|
|
@@ -5993,6 +6201,7 @@ export {
|
|
|
5993
6201
|
get,
|
|
5994
6202
|
getCachedJwks,
|
|
5995
6203
|
getCodeForStatus,
|
|
6204
|
+
getMcpTransportHeaders,
|
|
5996
6205
|
head,
|
|
5997
6206
|
httpErrorsTotalCounter,
|
|
5998
6207
|
httpRequestDurationMsHistogram,
|
|
@@ -6009,6 +6218,7 @@ export {
|
|
|
6009
6218
|
isManagedInstance,
|
|
6010
6219
|
isPortBound,
|
|
6011
6220
|
isRedirection,
|
|
6221
|
+
isSeparateMcpListener,
|
|
6012
6222
|
isServerError,
|
|
6013
6223
|
isSuccessful,
|
|
6014
6224
|
isValidStatusCode,
|