mcp-proxy 5.10.0 → 5.11.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.
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { Client, InMemoryEventStore, ReadBuffer, Server, __commonJS, __toESM, proxyServer, serializeMessage, startHTTPServer } from "../stdio-DF5lH8jj.js";
2
+ import { Client, InMemoryEventStore, ReadBuffer, Server, __commonJS, __toESM, proxyServer, serializeMessage, startHTTPServer } from "../stdio-BEX6di72.js";
3
3
  import { createRequire } from "node:module";
4
4
  import { basename, dirname, extname, join, normalize, relative, resolve } from "path";
5
5
  import { format, inspect } from "util";
package/dist/index.d.ts CHANGED
@@ -11,15 +11,25 @@ import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
11
11
  interface AuthConfig {
12
12
  apiKey?: string;
13
13
  oauth?: {
14
+ error?: string;
15
+ error_description?: string;
16
+ error_uri?: string;
14
17
  protectedResource?: {
15
18
  resource?: string;
16
19
  };
20
+ realm?: string;
21
+ scope?: string;
17
22
  };
18
23
  }
19
24
  declare class AuthenticationMiddleware {
20
25
  private config;
21
26
  constructor(config?: AuthConfig);
22
- getUnauthorizedResponse(): {
27
+ getUnauthorizedResponse(options?: {
28
+ error?: string;
29
+ error_description?: string;
30
+ error_uri?: string;
31
+ scope?: string;
32
+ }): {
23
33
  body: string;
24
34
  headers: Record<string, string>;
25
35
  };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { AuthenticationMiddleware, Client, InMemoryEventStore, JSONRPCMessageSchema, LATEST_PROTOCOL_VERSION, NEVER, ReadBuffer, Server, ZodIssueCode, anyType, arrayType, booleanType, isInitializedNotification, isJSONRPCRequest, isJSONRPCResponse, numberType, objectType, proxyServer, serializeMessage, startHTTPServer, stringType } from "./stdio-DF5lH8jj.js";
1
+ import { AuthenticationMiddleware, Client, InMemoryEventStore, JSONRPCMessageSchema, LATEST_PROTOCOL_VERSION, NEVER, ReadBuffer, Server, ZodIssueCode, anyType, arrayType, booleanType, isInitializedNotification, isJSONRPCRequest, isJSONRPCResponse, numberType, objectType, proxyServer, serializeMessage, startHTTPServer, stringType } from "./stdio-BEX6di72.js";
2
2
  import process from "node:process";
3
3
 
4
4
  //#region node_modules/.pnpm/eventsource-parser@3.0.6/node_modules/eventsource-parser/dist/index.js
@@ -35,14 +35,27 @@ var AuthenticationMiddleware = class {
35
35
  constructor(config = {}) {
36
36
  this.config = config;
37
37
  }
38
- getUnauthorizedResponse() {
38
+ getUnauthorizedResponse(options) {
39
39
  const headers = { "Content-Type": "application/json" };
40
- if (this.config.oauth?.protectedResource?.resource) headers["WWW-Authenticate"] = `Bearer resource_metadata="${this.config.oauth.protectedResource.resource}/.well-known/oauth-protected-resource"`;
40
+ if (this.config.oauth) {
41
+ const params = [];
42
+ if (this.config.oauth.realm) params.push(`realm="${this.config.oauth.realm}"`);
43
+ if (this.config.oauth.protectedResource?.resource) params.push(`resource_metadata="${this.config.oauth.protectedResource.resource}/.well-known/oauth-protected-resource"`);
44
+ const error = options?.error || this.config.oauth.error || "invalid_token";
45
+ params.push(`error="${error}"`);
46
+ const escaped = (options?.error_description || this.config.oauth.error_description || "Unauthorized: Invalid or missing API key").replace(/"/g, "\\\"");
47
+ params.push(`error_description="${escaped}"`);
48
+ const error_uri = options?.error_uri || this.config.oauth.error_uri;
49
+ if (error_uri) params.push(`error_uri="${error_uri}"`);
50
+ const scope = options?.scope || this.config.oauth.scope;
51
+ if (scope) params.push(`scope="${scope}"`);
52
+ if (params.length > 0) headers["WWW-Authenticate"] = `Bearer ${params.join(", ")}`;
53
+ }
41
54
  return {
42
55
  body: JSON.stringify({
43
56
  error: {
44
57
  code: 401,
45
- message: "Unauthorized: Invalid or missing API key"
58
+ message: options?.error_description || "Unauthorized: Invalid or missing API key"
46
59
  },
47
60
  id: null,
48
61
  jsonrpc: "2.0"
@@ -15026,19 +15039,37 @@ const createJsonRpcErrorResponse = (code, message) => {
15026
15039
  jsonrpc: "2.0"
15027
15040
  });
15028
15041
  };
15029
- const getWWWAuthenticateHeader = (oauth) => {
15030
- if (!oauth?.protectedResource?.resource) return;
15031
- return `Bearer resource_metadata="${oauth.protectedResource.resource}/.well-known/oauth-protected-resource"`;
15042
+ const getWWWAuthenticateHeader = (oauth, options) => {
15043
+ if (!oauth) return;
15044
+ const params = [];
15045
+ if (oauth.realm) params.push(`realm="${oauth.realm}"`);
15046
+ if (oauth.protectedResource?.resource) params.push(`resource_metadata="${oauth.protectedResource.resource}/.well-known/oauth-protected-resource"`);
15047
+ const error = options?.error || oauth.error;
15048
+ if (error) params.push(`error="${error}"`);
15049
+ const error_description = options?.error_description || oauth.error_description;
15050
+ if (error_description) {
15051
+ const escaped = error_description.replace(/"/g, "\\\"");
15052
+ params.push(`error_description="${escaped}"`);
15053
+ }
15054
+ const error_uri = options?.error_uri || oauth.error_uri;
15055
+ if (error_uri) params.push(`error_uri="${error_uri}"`);
15056
+ const scope = options?.scope || oauth.scope;
15057
+ if (scope) params.push(`scope="${scope}"`);
15058
+ if (params.length === 0) return;
15059
+ return `Bearer ${params.join(", ")}`;
15032
15060
  };
15033
- const handleResponseError = (error, res) => {
15034
- if (error instanceof Response) {
15061
+ const handleResponseError = async (error, res) => {
15062
+ if (error && typeof error === "object" && "status" in error && "headers" in error && "statusText" in error || error instanceof Response) {
15063
+ const responseError = error;
15035
15064
  const fixedHeaders = {};
15036
- error.headers.forEach((value, key$1) => {
15065
+ responseError.headers.forEach((value, key$1) => {
15037
15066
  if (fixedHeaders[key$1]) if (Array.isArray(fixedHeaders[key$1])) fixedHeaders[key$1].push(value);
15038
15067
  else fixedHeaders[key$1] = [fixedHeaders[key$1], value];
15039
15068
  else fixedHeaders[key$1] = value;
15040
15069
  });
15041
- res.writeHead(error.status, error.statusText, fixedHeaders).end(error.statusText);
15070
+ const body = await responseError.text();
15071
+ res.writeHead(responseError.status, responseError.statusText, fixedHeaders);
15072
+ res.end(body);
15042
15073
  return true;
15043
15074
  }
15044
15075
  return false;
@@ -15104,7 +15135,10 @@ const handleStreamRequest = async ({ activeTransports, authenticate, createServe
15104
15135
  if (!authResult || typeof authResult === "object" && "authenticated" in authResult && !authResult.authenticated) {
15105
15136
  const errorMessage = authResult && typeof authResult === "object" && "error" in authResult && typeof authResult.error === "string" ? authResult.error : "Unauthorized: Authentication failed";
15106
15137
  res.setHeader("Content-Type", "application/json");
15107
- const wwwAuthHeader = getWWWAuthenticateHeader(oauth);
15138
+ const wwwAuthHeader = getWWWAuthenticateHeader(oauth, {
15139
+ error: "invalid_token",
15140
+ error_description: errorMessage
15141
+ });
15108
15142
  if (wwwAuthHeader) res.setHeader("WWW-Authenticate", wwwAuthHeader);
15109
15143
  res.writeHead(401).end(JSON.stringify({
15110
15144
  error: {
@@ -15117,10 +15151,14 @@ const handleStreamRequest = async ({ activeTransports, authenticate, createServe
15117
15151
  return true;
15118
15152
  }
15119
15153
  } catch (error) {
15154
+ if (await handleResponseError(error, res)) return true;
15120
15155
  const errorMessage = error instanceof Error ? error.message : "Unauthorized: Authentication error";
15121
15156
  console.error("Authentication error:", error);
15122
15157
  res.setHeader("Content-Type", "application/json");
15123
- const wwwAuthHeader = getWWWAuthenticateHeader(oauth);
15158
+ const wwwAuthHeader = getWWWAuthenticateHeader(oauth, {
15159
+ error: "invalid_token",
15160
+ error_description: errorMessage
15161
+ });
15124
15162
  if (wwwAuthHeader) res.setHeader("WWW-Authenticate", wwwAuthHeader);
15125
15163
  res.writeHead(401).end(JSON.stringify({
15126
15164
  error: {
@@ -15166,10 +15204,14 @@ const handleStreamRequest = async ({ activeTransports, authenticate, createServe
15166
15204
  try {
15167
15205
  server = await createServer(req);
15168
15206
  } catch (error) {
15207
+ if (await handleResponseError(error, res)) return true;
15169
15208
  const errorMessage = error instanceof Error ? error.message : String(error);
15170
15209
  if (errorMessage.includes("Authentication") || errorMessage.includes("Invalid JWT") || errorMessage.includes("Token") || errorMessage.includes("Unauthorized")) {
15171
15210
  res.setHeader("Content-Type", "application/json");
15172
- const wwwAuthHeader = getWWWAuthenticateHeader(oauth);
15211
+ const wwwAuthHeader = getWWWAuthenticateHeader(oauth, {
15212
+ error: "invalid_token",
15213
+ error_description: errorMessage
15214
+ });
15173
15215
  if (wwwAuthHeader) res.setHeader("WWW-Authenticate", wwwAuthHeader);
15174
15216
  res.writeHead(401).end(JSON.stringify({
15175
15217
  error: {
@@ -15181,7 +15223,6 @@ const handleStreamRequest = async ({ activeTransports, authenticate, createServe
15181
15223
  }));
15182
15224
  return true;
15183
15225
  }
15184
- if (handleResponseError(error, res)) return true;
15185
15226
  res.writeHead(500).end("Error creating server");
15186
15227
  return true;
15187
15228
  }
@@ -15199,10 +15240,14 @@ const handleStreamRequest = async ({ activeTransports, authenticate, createServe
15199
15240
  try {
15200
15241
  server = await createServer(req);
15201
15242
  } catch (error) {
15243
+ if (await handleResponseError(error, res)) return true;
15202
15244
  const errorMessage = error instanceof Error ? error.message : String(error);
15203
15245
  if (errorMessage.includes("Authentication") || errorMessage.includes("Invalid JWT") || errorMessage.includes("Token") || errorMessage.includes("Unauthorized")) {
15204
15246
  res.setHeader("Content-Type", "application/json");
15205
- const wwwAuthHeader = getWWWAuthenticateHeader(oauth);
15247
+ const wwwAuthHeader = getWWWAuthenticateHeader(oauth, {
15248
+ error: "invalid_token",
15249
+ error_description: errorMessage
15250
+ });
15206
15251
  if (wwwAuthHeader) res.setHeader("WWW-Authenticate", wwwAuthHeader);
15207
15252
  res.writeHead(401).end(JSON.stringify({
15208
15253
  error: {
@@ -15214,7 +15259,6 @@ const handleStreamRequest = async ({ activeTransports, authenticate, createServe
15214
15259
  }));
15215
15260
  return true;
15216
15261
  }
15217
- if (handleResponseError(error, res)) return true;
15218
15262
  res.writeHead(500).end("Error creating server");
15219
15263
  return true;
15220
15264
  }
@@ -15284,7 +15328,7 @@ const handleSSERequest = async ({ activeTransports, createServer, endpoint, onCl
15284
15328
  try {
15285
15329
  server = await createServer(req);
15286
15330
  } catch (error) {
15287
- if (handleResponseError(error, res)) return true;
15331
+ if (await handleResponseError(error, res)) return true;
15288
15332
  res.writeHead(500).end("Error creating server");
15289
15333
  return true;
15290
15334
  }
@@ -21589,4 +21633,4 @@ function serializeMessage(message) {
21589
21633
 
21590
21634
  //#endregion
21591
21635
  export { AuthenticationMiddleware, Client, InMemoryEventStore, JSONRPCMessageSchema, LATEST_PROTOCOL_VERSION, NEVER, ReadBuffer, Server, ZodIssueCode, __commonJS, __toESM, anyType, arrayType, booleanType, isInitializedNotification, isJSONRPCRequest, isJSONRPCResponse, numberType, objectType, proxyServer, serializeMessage, startHTTPServer, stringType };
21592
- //# sourceMappingURL=stdio-DF5lH8jj.js.map
21636
+ //# sourceMappingURL=stdio-BEX6di72.js.map