mcp-proxy 6.5.2 → 6.5.4

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/dist/index.d.mts CHANGED
@@ -113,6 +113,7 @@ declare const startHTTPServer: <T extends ServerLike>({
113
113
  enableJsonResponse,
114
114
  eventStore,
115
115
  host,
116
+ keepAliveTimeout,
116
117
  oauth,
117
118
  onClose,
118
119
  onConnect,
@@ -132,6 +133,7 @@ declare const startHTTPServer: <T extends ServerLike>({
132
133
  enableJsonResponse?: boolean;
133
134
  eventStore?: EventStore;
134
135
  host?: string;
136
+ keepAliveTimeout?: number;
135
137
  oauth?: AuthConfig["oauth"];
136
138
  onClose?: (server: T) => Promise<void>;
137
139
  onConnect?: (server: T) => Promise<void>;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { C as NEVER, S as _coercedNumber, T as AuthenticationMiddleware, _ as looseObject, a as startHTTPServer, b as string, c as LATEST_PROTOCOL_VERSION, d as isJSONRPCResultResponse, f as ZodNumber, g as literal, h as boolean, i as Client, l as isInitializedNotification, m as array, n as serializeMessage, o as proxyServer, p as any, r as Server, s as JSONRPCMessageSchema, t as ReadBuffer, u as isJSONRPCRequest, v as number$1, w as InMemoryEventStore, x as url, y as object } from "./stdio-DLwYts_5.mjs";
1
+ import { C as NEVER, S as _coercedNumber, T as AuthenticationMiddleware, _ as looseObject, a as startHTTPServer, b as string, c as LATEST_PROTOCOL_VERSION, d as isJSONRPCResultResponse, f as ZodNumber, g as literal, h as boolean, i as Client, l as isInitializedNotification, m as array, n as serializeMessage, o as proxyServer, p as any, r as Server, s as JSONRPCMessageSchema, t as ReadBuffer, u as isJSONRPCRequest, v as number$1, w as InMemoryEventStore, x as url, y as object } from "./stdio-BQunCFor.mjs";
2
2
  import process from "node:process";
3
3
 
4
4
  //#region node_modules/.pnpm/zod@3.25.76/node_modules/zod/v4/classic/compat.js
@@ -15856,6 +15856,7 @@ var StreamableHTTPServerTransport = class {
15856
15856
 
15857
15857
  //#endregion
15858
15858
  //#region src/startHTTPServer.ts
15859
+ const DEFAULT_KEEP_ALIVE_TIMEOUT = 3e5;
15859
15860
  const getBody = (request) => {
15860
15861
  return new Promise((resolve$2) => {
15861
15862
  const bodyParts = [];
@@ -15883,6 +15884,16 @@ const createJsonRpcErrorResponse = (code, message) => {
15883
15884
  jsonrpc: "2.0"
15884
15885
  });
15885
15886
  };
15887
+ const getRequestId = (body) => {
15888
+ if (typeof body !== "object" || body === null || Array.isArray(body) || !("id" in body)) return null;
15889
+ return body.id;
15890
+ };
15891
+ const isJsonRpcMessage = (message) => {
15892
+ return JSONRPCMessageSchema.safeParse(message).success;
15893
+ };
15894
+ const isJsonRpcBody = (body) => {
15895
+ return Array.isArray(body) ? body.every(isJsonRpcMessage) : isJsonRpcMessage(body);
15896
+ };
15886
15897
  const getWWWAuthenticateHeader = (oauth, options) => {
15887
15898
  if (!oauth) return;
15888
15899
  const params = [];
@@ -15902,6 +15913,23 @@ const getWWWAuthenticateHeader = (oauth, options) => {
15902
15913
  if (params.length === 0) return;
15903
15914
  return `Bearer ${params.join(", ")}`;
15904
15915
  };
15916
+ const sendSessionUnauthorizedResponse = ({ body, oauth, res }) => {
15917
+ const message = "Unauthorized: No valid session ID provided";
15918
+ res.setHeader("Content-Type", "application/json");
15919
+ const wwwAuthHeader = getWWWAuthenticateHeader(oauth, {
15920
+ error: "invalid_token",
15921
+ error_description: message
15922
+ });
15923
+ if (wwwAuthHeader) res.setHeader("WWW-Authenticate", wwwAuthHeader);
15924
+ res.writeHead(401).end(JSON.stringify({
15925
+ error: {
15926
+ code: -32e3,
15927
+ message
15928
+ },
15929
+ id: getRequestId(body),
15930
+ jsonrpc: "2.0"
15931
+ }));
15932
+ };
15905
15933
  const isScopeChallengeError = (error$1) => {
15906
15934
  return typeof error$1 === "object" && error$1 !== null && "name" in error$1 && error$1.name === "InsufficientScopeError" && "data" in error$1 && typeof error$1.data === "object" && error$1.data !== null && "error" in error$1.data && error$1.data.error === "insufficient_scope";
15907
15935
  };
@@ -16022,6 +16050,14 @@ const handleStreamRequest = async ({ activeTransports, authenticate, authMiddlew
16022
16050
  if (sessionId) {
16023
16051
  const activeTransport = activeTransports[sessionId];
16024
16052
  if (!activeTransport) {
16053
+ if (authenticate && isJsonRpcBody(body)) {
16054
+ sendSessionUnauthorizedResponse({
16055
+ body,
16056
+ oauth,
16057
+ res
16058
+ });
16059
+ return true;
16060
+ }
16025
16061
  res.setHeader("Content-Type", "application/json");
16026
16062
  res.writeHead(404).end(createJsonRpcErrorResponse(-32001, "Session not found"));
16027
16063
  return true;
@@ -16117,6 +16153,14 @@ const handleStreamRequest = async ({ activeTransports, authenticate, authMiddlew
16117
16153
  await transport.handleRequest(req, res, body);
16118
16154
  return true;
16119
16155
  } else {
16156
+ if (authenticate && isJsonRpcBody(body)) {
16157
+ sendSessionUnauthorizedResponse({
16158
+ body,
16159
+ oauth,
16160
+ res
16161
+ });
16162
+ return true;
16163
+ }
16120
16164
  res.setHeader("Content-Type", "application/json");
16121
16165
  res.writeHead(400).end(createJsonRpcErrorResponse(-32e3, "Bad Request: No valid session ID provided"));
16122
16166
  return true;
@@ -16144,10 +16188,24 @@ const handleStreamRequest = async ({ activeTransports, authenticate, authMiddlew
16144
16188
  res.writeHead(405, { Allow: "POST" }).end("Method Not Allowed");
16145
16189
  return true;
16146
16190
  }
16191
+ if (authenticate) {
16192
+ sendSessionUnauthorizedResponse({
16193
+ oauth,
16194
+ res
16195
+ });
16196
+ return true;
16197
+ }
16147
16198
  res.writeHead(400).end("No sessionId");
16148
16199
  return true;
16149
16200
  }
16150
16201
  if (!activeTransport) {
16202
+ if (authenticate) {
16203
+ sendSessionUnauthorizedResponse({
16204
+ oauth,
16205
+ res
16206
+ });
16207
+ return true;
16208
+ }
16151
16209
  res.writeHead(400).end("No active transport");
16152
16210
  return true;
16153
16211
  }
@@ -16161,12 +16219,26 @@ const handleStreamRequest = async ({ activeTransports, authenticate, authMiddlew
16161
16219
  console.log("[mcp-proxy] received delete request");
16162
16220
  const sessionId = req.headers["mcp-session-id"];
16163
16221
  if (!sessionId) {
16222
+ if (authenticate) {
16223
+ sendSessionUnauthorizedResponse({
16224
+ oauth,
16225
+ res
16226
+ });
16227
+ return true;
16228
+ }
16164
16229
  res.writeHead(400).end("Invalid or missing sessionId");
16165
16230
  return true;
16166
16231
  }
16167
16232
  console.log("[mcp-proxy] received delete request for session", sessionId);
16168
16233
  const activeTransport = activeTransports[sessionId];
16169
16234
  if (!activeTransport) {
16235
+ if (authenticate) {
16236
+ sendSessionUnauthorizedResponse({
16237
+ oauth,
16238
+ res
16239
+ });
16240
+ return true;
16241
+ }
16170
16242
  res.writeHead(400).end("No active transport");
16171
16243
  return true;
16172
16244
  }
@@ -16236,7 +16308,7 @@ const handleSSERequest = async ({ activeTransports, createServer, endpoint, onCl
16236
16308
  }
16237
16309
  return false;
16238
16310
  };
16239
- const startHTTPServer = async ({ apiKey, authenticate, cors, createServer, enableJsonResponse, eventStore, host = "::", oauth, onClose, onConnect, onUnhandledRequest, port, sseEndpoint = "/sse", sslCa, sslCert, sslKey, stateless, streamEndpoint = "/mcp" }) => {
16311
+ const startHTTPServer = async ({ apiKey, authenticate, cors, createServer, enableJsonResponse, eventStore, host = "::", keepAliveTimeout = DEFAULT_KEEP_ALIVE_TIMEOUT, oauth, onClose, onConnect, onUnhandledRequest, port, sseEndpoint = "/sse", sslCa, sslCert, sslKey, stateless, streamEndpoint = "/mcp" }) => {
16240
16312
  const activeSSETransports = {};
16241
16313
  const activeStreamTransports = {};
16242
16314
  const authMiddleware = new AuthenticationMiddleware({
@@ -16321,6 +16393,8 @@ const startHTTPServer = async ({ apiKey, authenticate, cors, createServer, enabl
16321
16393
  }
16322
16394
  httpServer = https.createServer(options, requestListener);
16323
16395
  } else httpServer = http.createServer(requestListener);
16396
+ httpServer.keepAliveTimeout = keepAliveTimeout;
16397
+ httpServer.headersTimeout = Math.max(httpServer.headersTimeout, keepAliveTimeout + 1e3);
16324
16398
  await new Promise((resolve$2) => {
16325
16399
  httpServer.listen(port, host, () => {
16326
16400
  resolve$2(void 0);
@@ -24821,4 +24895,4 @@ function serializeMessage(message) {
24821
24895
 
24822
24896
  //#endregion
24823
24897
  export { NEVER as C, __toESM as D, __commonJSMin as E, _coercedNumber as S, AuthenticationMiddleware as T, looseObject as _, startHTTPServer as a, string as b, LATEST_PROTOCOL_VERSION as c, isJSONRPCResultResponse as d, ZodNumber as f, literal as g, boolean as h, Client as i, isInitializedNotification as l, array as m, serializeMessage as n, proxyServer as o, any as p, Server as r, JSONRPCMessageSchema as s, ReadBuffer as t, isJSONRPCRequest as u, number as v, InMemoryEventStore as w, url as x, object as y };
24824
- //# sourceMappingURL=stdio-DLwYts_5.mjs.map
24898
+ //# sourceMappingURL=stdio-BQunCFor.mjs.map