@wrongstack/acp 0.287.0 → 0.289.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.
@@ -833,7 +833,21 @@ var WrongStackACPServer = class {
833
833
  async startHttp(port) {
834
834
  const host = this.options.host ?? "127.0.0.1";
835
835
  const handler = this.handler;
836
+ const authToken = this.options.authToken;
837
+ let httpChain = Promise.resolve();
836
838
  this.httpServer = createServer(async (req, res) => {
839
+ if (authToken) {
840
+ const url = new URL(req.url ?? "/", `http://${host}:${port}`);
841
+ const queryToken = url.searchParams.get("token");
842
+ const authHeader = req.headers["authorization"];
843
+ const bearerToken = Array.isArray(authHeader) ? authHeader[0]?.replace(/^Bearer\s+/i, "") : authHeader?.replace(/^Bearer\s+/i, "");
844
+ const supplied = queryToken ?? bearerToken ?? "";
845
+ if (supplied !== authToken) {
846
+ res.writeHead(401, { "Content-Type": "application/json" });
847
+ res.end(JSON.stringify({ error: { code: -32001, message: "Unauthorized" } }));
848
+ return;
849
+ }
850
+ }
837
851
  const selfOrigin = `http://${host}:${port}`;
838
852
  const reqOrigin = Array.isArray(req.headers.origin) ? req.headers.origin[0] : req.headers.origin;
839
853
  if (reqOrigin && reqOrigin !== selfOrigin) {
@@ -843,7 +857,7 @@ var WrongStackACPServer = class {
843
857
  }
844
858
  if (reqOrigin) res.setHeader("Access-Control-Allow-Origin", reqOrigin);
845
859
  res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
846
- res.setHeader("Access-Control-Allow-Headers", "Content-Type, Mcp-Session-Id");
860
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type, Mcp-Session-Id, Authorization");
847
861
  if (req.method === "OPTIONS") {
848
862
  res.writeHead(204);
849
863
  res.end();
@@ -854,10 +868,23 @@ var WrongStackACPServer = class {
854
868
  res.end(JSON.stringify({ error: "method not allowed" }));
855
869
  return;
856
870
  }
871
+ const MAX_HTTP_BODY = 10 * 1024 * 1024;
857
872
  let body = "";
873
+ let bodyBytes = 0;
874
+ let tooLarge = false;
858
875
  for await (const chunk of req) {
876
+ bodyBytes += chunk.length;
877
+ if (bodyBytes > MAX_HTTP_BODY) {
878
+ tooLarge = true;
879
+ break;
880
+ }
859
881
  body += chunk;
860
882
  }
883
+ if (tooLarge) {
884
+ res.writeHead(413, { "Content-Type": "application/json" });
885
+ res.end(JSON.stringify({ error: { code: -32700, message: "Request body too large" } }));
886
+ return;
887
+ }
861
888
  let msg;
862
889
  try {
863
890
  msg = JSON.parse(body);
@@ -866,26 +893,43 @@ var WrongStackACPServer = class {
866
893
  res.end(JSON.stringify({ error: { code: -32700, message: "Parse error" } }));
867
894
  return;
868
895
  }
869
- const notifications = [];
870
- let response = null;
871
- const originalSend = this.transport.send.bind(this.transport);
872
- this.transport.send = async (m) => {
873
- if (m.id !== void 0 && (m.result !== void 0 || m.error !== void 0)) {
874
- response = m;
875
- } else if (m.method === "session/update") {
876
- notifications.push(m.params);
877
- } else {
878
- notifications.push(m);
896
+ const isNotification = typeof msg === "object" && msg !== null && msg.id === void 0 && typeof msg.method === "string";
897
+ if (isNotification) {
898
+ try {
899
+ await handler.handleMessage(msg);
900
+ } catch {
879
901
  }
880
- };
902
+ res.writeHead(200, { "Content-Type": "application/json" });
903
+ res.end(JSON.stringify({ notifications: [] }));
904
+ return;
905
+ }
906
+ const requestPromise = httpChain.then(async () => {
907
+ const notifications = [];
908
+ let response = null;
909
+ const originalSend = this.transport.send.bind(this.transport);
910
+ this.transport.send = async (m) => {
911
+ if (m.id !== void 0 && (m.result !== void 0 || m.error !== void 0)) {
912
+ response = m;
913
+ } else if (m.method === "session/update") {
914
+ notifications.push(m.params);
915
+ } else {
916
+ notifications.push(m);
917
+ }
918
+ };
919
+ try {
920
+ await handler.handleMessage(msg);
921
+ } finally {
922
+ this.transport.send = originalSend;
923
+ }
924
+ res.writeHead(200, { "Content-Type": "application/json" });
925
+ const responseBody = response !== null ? { ...response, notifications } : { notifications };
926
+ res.end(JSON.stringify(responseBody));
927
+ });
928
+ httpChain = requestPromise.catch(() => void 0);
881
929
  try {
882
- await handler.handleMessage(msg);
883
- } finally {
884
- this.transport.send = originalSend;
930
+ await requestPromise;
931
+ } catch {
885
932
  }
886
- res.writeHead(200, { "Content-Type": "application/json" });
887
- const responseBody = response !== null ? { ...response, notifications } : { notifications };
888
- res.end(JSON.stringify(responseBody));
889
933
  });
890
934
  return new Promise((resolve) => {
891
935
  this.httpServer.listen(port, host, () => {