sunpeak 0.16.1 → 0.16.2

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.
Files changed (47) hide show
  1. package/README.md +2 -1
  2. package/bin/commands/build.mjs +106 -2
  3. package/bin/commands/dev.mjs +104 -8
  4. package/bin/commands/start.mjs +215 -0
  5. package/bin/sunpeak.js +11 -1
  6. package/dist/chatgpt/index.cjs +2 -2
  7. package/dist/chatgpt/index.js +2 -2
  8. package/dist/claude/index.cjs +1 -1
  9. package/dist/claude/index.js +1 -1
  10. package/dist/{index-BvQ_ZuOO.cjs → index-Bll1bszc.cjs} +2 -2
  11. package/dist/{index-BvQ_ZuOO.cjs.map → index-Bll1bszc.cjs.map} +1 -1
  12. package/dist/{index-CTGEqlgk.js → index-CACtnwu2.js} +2 -2
  13. package/dist/{index-CTGEqlgk.js.map → index-CACtnwu2.js.map} +1 -1
  14. package/dist/{index-BjnAsaqp.js → index-CLcr8IyR.js} +2 -2
  15. package/dist/index-CLcr8IyR.js.map +1 -0
  16. package/dist/{index-C9CVbGFt.cjs → index-CaQmwZJc.cjs} +2 -2
  17. package/dist/index-CaQmwZJc.cjs.map +1 -0
  18. package/dist/index.cjs +5 -5
  19. package/dist/index.js +6 -6
  20. package/dist/mcp/index.cjs +1579 -3
  21. package/dist/mcp/index.cjs.map +1 -1
  22. package/dist/mcp/index.d.ts +2 -0
  23. package/dist/mcp/index.js +1580 -4
  24. package/dist/mcp/index.js.map +1 -1
  25. package/dist/mcp/production-server.d.ts +156 -0
  26. package/dist/platform/chatgpt/index.cjs +1 -1
  27. package/dist/platform/chatgpt/index.js +1 -1
  28. package/dist/{protocol-DFbsCx7E.js → protocol-BD5jDQEx.js} +8 -1
  29. package/dist/{protocol-DFbsCx7E.js.map → protocol-BD5jDQEx.js.map} +1 -1
  30. package/dist/{protocol-CL4_Npj5.cjs → protocol-BOjXuK6l.cjs} +8 -1
  31. package/dist/{protocol-CL4_Npj5.cjs.map → protocol-BOjXuK6l.cjs.map} +1 -1
  32. package/dist/simulator/index.cjs +1 -1
  33. package/dist/simulator/index.js +1 -1
  34. package/dist/{simulator-C0H_k092.js → simulator-B7rw83zP.js} +2 -2
  35. package/dist/{simulator-C0H_k092.js.map → simulator-B7rw83zP.js.map} +1 -1
  36. package/dist/{simulator-B56j5P8W.cjs → simulator-DjZNa1MI.cjs} +2 -2
  37. package/dist/{simulator-B56j5P8W.cjs.map → simulator-DjZNa1MI.cjs.map} +1 -1
  38. package/dist/{use-app-BuufpXTQ.cjs → use-app-BpAJqzdE.cjs} +2 -2
  39. package/dist/{use-app-BuufpXTQ.cjs.map → use-app-BpAJqzdE.cjs.map} +1 -1
  40. package/dist/{use-app-BThbgFFT.js → use-app-WOUdh1PR.js} +2 -2
  41. package/dist/{use-app-BThbgFFT.js.map → use-app-WOUdh1PR.js.map} +1 -1
  42. package/package.json +1 -1
  43. package/template/README.md +13 -12
  44. package/template/package.json +1 -0
  45. package/template/src/server.ts +7 -5
  46. package/dist/index-BjnAsaqp.js.map +0 -1
  47. package/dist/index-C9CVbGFt.cjs.map +0 -1
@@ -4,9 +4,14 @@ const node_http = require("node:http");
4
4
  const node_url = require("node:url");
5
5
  const fs = require("node:fs");
6
6
  const path = require("node:path");
7
- const protocol = require("../protocol-CL4_Npj5.cjs");
7
+ const protocol = require("../protocol-BOjXuK6l.cjs");
8
8
  const zod = require("zod");
9
9
  const sse_js = require("@modelcontextprotocol/sdk/server/sse.js");
10
+ const node_crypto = require("node:crypto");
11
+ require("http");
12
+ const http2 = require("http2");
13
+ const stream = require("stream");
14
+ const crypto$1 = require("crypto");
10
15
  const FAVICON_BASE64 = "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";
11
16
  const FAVICON_BUFFER = Buffer.from(FAVICON_BASE64, "base64");
12
17
  function getDefaultExportFromCjs(x2) {
@@ -4135,8 +4140,8 @@ function requireCore$1() {
4135
4140
  return this;
4136
4141
  }
4137
4142
  case "object": {
4138
- const cacheKey = schemaKeyRef;
4139
- this._cache.delete(cacheKey);
4143
+ const cacheKey2 = schemaKeyRef;
4144
+ this._cache.delete(cacheKey2);
4140
4145
  let id2 = schemaKeyRef[this.opts.schemaId];
4141
4146
  if (id2) {
4142
4147
  id2 = (0, resolve_1.normalizeId)(id2);
@@ -14582,13 +14587,1584 @@ function runMCPServer(config) {
14582
14587
  }
14583
14588
  };
14584
14589
  }
14590
+ var RequestError = class extends Error {
14591
+ constructor(message, options) {
14592
+ super(message, options);
14593
+ this.name = "RequestError";
14594
+ }
14595
+ };
14596
+ var toRequestError = (e) => {
14597
+ if (e instanceof RequestError) {
14598
+ return e;
14599
+ }
14600
+ return new RequestError(e.message, { cause: e });
14601
+ };
14602
+ var GlobalRequest = global.Request;
14603
+ var Request = class extends GlobalRequest {
14604
+ constructor(input, options) {
14605
+ if (typeof input === "object" && getRequestCache in input) {
14606
+ input = input[getRequestCache]();
14607
+ }
14608
+ if (typeof options?.body?.getReader !== "undefined") {
14609
+ options.duplex ??= "half";
14610
+ }
14611
+ super(input, options);
14612
+ }
14613
+ };
14614
+ var newHeadersFromIncoming = (incoming) => {
14615
+ const headerRecord = [];
14616
+ const rawHeaders = incoming.rawHeaders;
14617
+ for (let i = 0; i < rawHeaders.length; i += 2) {
14618
+ const { [i]: key, [i + 1]: value } = rawHeaders;
14619
+ if (key.charCodeAt(0) !== /*:*/
14620
+ 58) {
14621
+ headerRecord.push([key, value]);
14622
+ }
14623
+ }
14624
+ return new Headers(headerRecord);
14625
+ };
14626
+ var wrapBodyStream = /* @__PURE__ */ Symbol("wrapBodyStream");
14627
+ var newRequestFromIncoming = (method, url, headers, incoming, abortController) => {
14628
+ const init = {
14629
+ method,
14630
+ headers,
14631
+ signal: abortController.signal
14632
+ };
14633
+ if (method === "TRACE") {
14634
+ init.method = "GET";
14635
+ const req = new Request(url, init);
14636
+ Object.defineProperty(req, "method", {
14637
+ get() {
14638
+ return "TRACE";
14639
+ }
14640
+ });
14641
+ return req;
14642
+ }
14643
+ if (!(method === "GET" || method === "HEAD")) {
14644
+ if ("rawBody" in incoming && incoming.rawBody instanceof Buffer) {
14645
+ init.body = new ReadableStream({
14646
+ start(controller) {
14647
+ controller.enqueue(incoming.rawBody);
14648
+ controller.close();
14649
+ }
14650
+ });
14651
+ } else if (incoming[wrapBodyStream]) {
14652
+ let reader;
14653
+ init.body = new ReadableStream({
14654
+ async pull(controller) {
14655
+ try {
14656
+ reader ||= stream.Readable.toWeb(incoming).getReader();
14657
+ const { done, value } = await reader.read();
14658
+ if (done) {
14659
+ controller.close();
14660
+ } else {
14661
+ controller.enqueue(value);
14662
+ }
14663
+ } catch (error) {
14664
+ controller.error(error);
14665
+ }
14666
+ }
14667
+ });
14668
+ } else {
14669
+ init.body = stream.Readable.toWeb(incoming);
14670
+ }
14671
+ }
14672
+ return new Request(url, init);
14673
+ };
14674
+ var getRequestCache = /* @__PURE__ */ Symbol("getRequestCache");
14675
+ var requestCache = /* @__PURE__ */ Symbol("requestCache");
14676
+ var incomingKey = /* @__PURE__ */ Symbol("incomingKey");
14677
+ var urlKey = /* @__PURE__ */ Symbol("urlKey");
14678
+ var headersKey = /* @__PURE__ */ Symbol("headersKey");
14679
+ var abortControllerKey = /* @__PURE__ */ Symbol("abortControllerKey");
14680
+ var getAbortController = /* @__PURE__ */ Symbol("getAbortController");
14681
+ var requestPrototype = {
14682
+ get method() {
14683
+ return this[incomingKey].method || "GET";
14684
+ },
14685
+ get url() {
14686
+ return this[urlKey];
14687
+ },
14688
+ get headers() {
14689
+ return this[headersKey] ||= newHeadersFromIncoming(this[incomingKey]);
14690
+ },
14691
+ [getAbortController]() {
14692
+ this[getRequestCache]();
14693
+ return this[abortControllerKey];
14694
+ },
14695
+ [getRequestCache]() {
14696
+ this[abortControllerKey] ||= new AbortController();
14697
+ return this[requestCache] ||= newRequestFromIncoming(
14698
+ this.method,
14699
+ this[urlKey],
14700
+ this.headers,
14701
+ this[incomingKey],
14702
+ this[abortControllerKey]
14703
+ );
14704
+ }
14705
+ };
14706
+ [
14707
+ "body",
14708
+ "bodyUsed",
14709
+ "cache",
14710
+ "credentials",
14711
+ "destination",
14712
+ "integrity",
14713
+ "mode",
14714
+ "redirect",
14715
+ "referrer",
14716
+ "referrerPolicy",
14717
+ "signal",
14718
+ "keepalive"
14719
+ ].forEach((k2) => {
14720
+ Object.defineProperty(requestPrototype, k2, {
14721
+ get() {
14722
+ return this[getRequestCache]()[k2];
14723
+ }
14724
+ });
14725
+ });
14726
+ ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k2) => {
14727
+ Object.defineProperty(requestPrototype, k2, {
14728
+ value: function() {
14729
+ return this[getRequestCache]()[k2]();
14730
+ }
14731
+ });
14732
+ });
14733
+ Object.setPrototypeOf(requestPrototype, Request.prototype);
14734
+ var newRequest = (incoming, defaultHostname) => {
14735
+ const req = Object.create(requestPrototype);
14736
+ req[incomingKey] = incoming;
14737
+ const incomingUrl = incoming.url || "";
14738
+ if (incomingUrl[0] !== "/" && // short-circuit for performance. most requests are relative URL.
14739
+ (incomingUrl.startsWith("http://") || incomingUrl.startsWith("https://"))) {
14740
+ if (incoming instanceof http2.Http2ServerRequest) {
14741
+ throw new RequestError("Absolute URL for :path is not allowed in HTTP/2");
14742
+ }
14743
+ try {
14744
+ const url2 = new URL(incomingUrl);
14745
+ req[urlKey] = url2.href;
14746
+ } catch (e) {
14747
+ throw new RequestError("Invalid absolute URL", { cause: e });
14748
+ }
14749
+ return req;
14750
+ }
14751
+ const host = (incoming instanceof http2.Http2ServerRequest ? incoming.authority : incoming.headers.host) || defaultHostname;
14752
+ if (!host) {
14753
+ throw new RequestError("Missing host header");
14754
+ }
14755
+ let scheme;
14756
+ if (incoming instanceof http2.Http2ServerRequest) {
14757
+ scheme = incoming.scheme;
14758
+ if (!(scheme === "http" || scheme === "https")) {
14759
+ throw new RequestError("Unsupported scheme");
14760
+ }
14761
+ } else {
14762
+ scheme = incoming.socket && incoming.socket.encrypted ? "https" : "http";
14763
+ }
14764
+ const url = new URL(`${scheme}://${host}${incomingUrl}`);
14765
+ if (url.hostname.length !== host.length && url.hostname !== host.replace(/:\d+$/, "")) {
14766
+ throw new RequestError("Invalid host header");
14767
+ }
14768
+ req[urlKey] = url.href;
14769
+ return req;
14770
+ };
14771
+ var responseCache = /* @__PURE__ */ Symbol("responseCache");
14772
+ var getResponseCache = /* @__PURE__ */ Symbol("getResponseCache");
14773
+ var cacheKey = /* @__PURE__ */ Symbol("cache");
14774
+ var GlobalResponse = global.Response;
14775
+ var Response2 = class _Response {
14776
+ #body;
14777
+ #init;
14778
+ [getResponseCache]() {
14779
+ delete this[cacheKey];
14780
+ return this[responseCache] ||= new GlobalResponse(this.#body, this.#init);
14781
+ }
14782
+ constructor(body, init) {
14783
+ let headers;
14784
+ this.#body = body;
14785
+ if (init instanceof _Response) {
14786
+ const cachedGlobalResponse = init[responseCache];
14787
+ if (cachedGlobalResponse) {
14788
+ this.#init = cachedGlobalResponse;
14789
+ this[getResponseCache]();
14790
+ return;
14791
+ } else {
14792
+ this.#init = init.#init;
14793
+ headers = new Headers(init.#init.headers);
14794
+ }
14795
+ } else {
14796
+ this.#init = init;
14797
+ }
14798
+ if (typeof body === "string" || typeof body?.getReader !== "undefined" || body instanceof Blob || body instanceof Uint8Array) {
14799
+ headers ||= init?.headers || { "content-type": "text/plain; charset=UTF-8" };
14800
+ this[cacheKey] = [init?.status || 200, body, headers];
14801
+ }
14802
+ }
14803
+ get headers() {
14804
+ const cache = this[cacheKey];
14805
+ if (cache) {
14806
+ if (!(cache[2] instanceof Headers)) {
14807
+ cache[2] = new Headers(cache[2]);
14808
+ }
14809
+ return cache[2];
14810
+ }
14811
+ return this[getResponseCache]().headers;
14812
+ }
14813
+ get status() {
14814
+ return this[cacheKey]?.[0] ?? this[getResponseCache]().status;
14815
+ }
14816
+ get ok() {
14817
+ const status = this.status;
14818
+ return status >= 200 && status < 300;
14819
+ }
14820
+ };
14821
+ ["body", "bodyUsed", "redirected", "statusText", "trailers", "type", "url"].forEach((k2) => {
14822
+ Object.defineProperty(Response2.prototype, k2, {
14823
+ get() {
14824
+ return this[getResponseCache]()[k2];
14825
+ }
14826
+ });
14827
+ });
14828
+ ["arrayBuffer", "blob", "clone", "formData", "json", "text"].forEach((k2) => {
14829
+ Object.defineProperty(Response2.prototype, k2, {
14830
+ value: function() {
14831
+ return this[getResponseCache]()[k2]();
14832
+ }
14833
+ });
14834
+ });
14835
+ Object.setPrototypeOf(Response2, GlobalResponse);
14836
+ Object.setPrototypeOf(Response2.prototype, GlobalResponse.prototype);
14837
+ async function readWithoutBlocking(readPromise) {
14838
+ return Promise.race([readPromise, Promise.resolve().then(() => Promise.resolve(void 0))]);
14839
+ }
14840
+ function writeFromReadableStreamDefaultReader(reader, writable, currentReadPromise) {
14841
+ const cancel = (error) => {
14842
+ reader.cancel(error).catch(() => {
14843
+ });
14844
+ };
14845
+ writable.on("close", cancel);
14846
+ writable.on("error", cancel);
14847
+ (currentReadPromise ?? reader.read()).then(flow, handleStreamError);
14848
+ return reader.closed.finally(() => {
14849
+ writable.off("close", cancel);
14850
+ writable.off("error", cancel);
14851
+ });
14852
+ function handleStreamError(error) {
14853
+ if (error) {
14854
+ writable.destroy(error);
14855
+ }
14856
+ }
14857
+ function onDrain() {
14858
+ reader.read().then(flow, handleStreamError);
14859
+ }
14860
+ function flow({ done, value }) {
14861
+ try {
14862
+ if (done) {
14863
+ writable.end();
14864
+ } else if (!writable.write(value)) {
14865
+ writable.once("drain", onDrain);
14866
+ } else {
14867
+ return reader.read().then(flow, handleStreamError);
14868
+ }
14869
+ } catch (e) {
14870
+ handleStreamError(e);
14871
+ }
14872
+ }
14873
+ }
14874
+ function writeFromReadableStream(stream2, writable) {
14875
+ if (stream2.locked) {
14876
+ throw new TypeError("ReadableStream is locked.");
14877
+ } else if (writable.destroyed) {
14878
+ return;
14879
+ }
14880
+ return writeFromReadableStreamDefaultReader(stream2.getReader(), writable);
14881
+ }
14882
+ var buildOutgoingHttpHeaders = (headers) => {
14883
+ const res = {};
14884
+ if (!(headers instanceof Headers)) {
14885
+ headers = new Headers(headers ?? void 0);
14886
+ }
14887
+ const cookies = [];
14888
+ for (const [k2, v] of headers) {
14889
+ if (k2 === "set-cookie") {
14890
+ cookies.push(v);
14891
+ } else {
14892
+ res[k2] = v;
14893
+ }
14894
+ }
14895
+ if (cookies.length > 0) {
14896
+ res["set-cookie"] = cookies;
14897
+ }
14898
+ res["content-type"] ??= "text/plain; charset=UTF-8";
14899
+ return res;
14900
+ };
14901
+ var X_ALREADY_SENT = "x-hono-already-sent";
14902
+ var webFetch = global.fetch;
14903
+ if (typeof global.crypto === "undefined") {
14904
+ global.crypto = crypto$1;
14905
+ }
14906
+ global.fetch = (info, init) => {
14907
+ init = {
14908
+ // Disable compression handling so people can return the result of a fetch
14909
+ // directly in the loader without messing with the Content-Encoding header.
14910
+ compress: false,
14911
+ ...init
14912
+ };
14913
+ return webFetch(info, init);
14914
+ };
14915
+ var outgoingEnded = /* @__PURE__ */ Symbol("outgoingEnded");
14916
+ var handleRequestError = () => new Response(null, {
14917
+ status: 400
14918
+ });
14919
+ var handleFetchError = (e) => new Response(null, {
14920
+ status: e instanceof Error && (e.name === "TimeoutError" || e.constructor.name === "TimeoutError") ? 504 : 500
14921
+ });
14922
+ var handleResponseError = (e, outgoing) => {
14923
+ const err = e instanceof Error ? e : new Error("unknown error", { cause: e });
14924
+ if (err.code === "ERR_STREAM_PREMATURE_CLOSE") {
14925
+ console.info("The user aborted a request.");
14926
+ } else {
14927
+ console.error(e);
14928
+ if (!outgoing.headersSent) {
14929
+ outgoing.writeHead(500, { "Content-Type": "text/plain" });
14930
+ }
14931
+ outgoing.end(`Error: ${err.message}`);
14932
+ outgoing.destroy(err);
14933
+ }
14934
+ };
14935
+ var flushHeaders = (outgoing) => {
14936
+ if ("flushHeaders" in outgoing && outgoing.writable) {
14937
+ outgoing.flushHeaders();
14938
+ }
14939
+ };
14940
+ var responseViaCache = async (res, outgoing) => {
14941
+ let [status, body, header] = res[cacheKey];
14942
+ if (header instanceof Headers) {
14943
+ header = buildOutgoingHttpHeaders(header);
14944
+ }
14945
+ if (typeof body === "string") {
14946
+ header["Content-Length"] = Buffer.byteLength(body);
14947
+ } else if (body instanceof Uint8Array) {
14948
+ header["Content-Length"] = body.byteLength;
14949
+ } else if (body instanceof Blob) {
14950
+ header["Content-Length"] = body.size;
14951
+ }
14952
+ outgoing.writeHead(status, header);
14953
+ if (typeof body === "string" || body instanceof Uint8Array) {
14954
+ outgoing.end(body);
14955
+ } else if (body instanceof Blob) {
14956
+ outgoing.end(new Uint8Array(await body.arrayBuffer()));
14957
+ } else {
14958
+ flushHeaders(outgoing);
14959
+ await writeFromReadableStream(body, outgoing)?.catch(
14960
+ (e) => handleResponseError(e, outgoing)
14961
+ );
14962
+ }
14963
+ outgoing[outgoingEnded]?.();
14964
+ };
14965
+ var isPromise = (res) => typeof res.then === "function";
14966
+ var responseViaResponseObject = async (res, outgoing, options = {}) => {
14967
+ if (isPromise(res)) {
14968
+ if (options.errorHandler) {
14969
+ try {
14970
+ res = await res;
14971
+ } catch (err) {
14972
+ const errRes = await options.errorHandler(err);
14973
+ if (!errRes) {
14974
+ return;
14975
+ }
14976
+ res = errRes;
14977
+ }
14978
+ } else {
14979
+ res = await res.catch(handleFetchError);
14980
+ }
14981
+ }
14982
+ if (cacheKey in res) {
14983
+ return responseViaCache(res, outgoing);
14984
+ }
14985
+ const resHeaderRecord = buildOutgoingHttpHeaders(res.headers);
14986
+ if (res.body) {
14987
+ const reader = res.body.getReader();
14988
+ const values = [];
14989
+ let done = false;
14990
+ let currentReadPromise = void 0;
14991
+ if (resHeaderRecord["transfer-encoding"] !== "chunked") {
14992
+ let maxReadCount = 2;
14993
+ for (let i = 0; i < maxReadCount; i++) {
14994
+ currentReadPromise ||= reader.read();
14995
+ const chunk = await readWithoutBlocking(currentReadPromise).catch((e) => {
14996
+ console.error(e);
14997
+ done = true;
14998
+ });
14999
+ if (!chunk) {
15000
+ if (i === 1) {
15001
+ await new Promise((resolve2) => setTimeout(resolve2));
15002
+ maxReadCount = 3;
15003
+ continue;
15004
+ }
15005
+ break;
15006
+ }
15007
+ currentReadPromise = void 0;
15008
+ if (chunk.value) {
15009
+ values.push(chunk.value);
15010
+ }
15011
+ if (chunk.done) {
15012
+ done = true;
15013
+ break;
15014
+ }
15015
+ }
15016
+ if (done && !("content-length" in resHeaderRecord)) {
15017
+ resHeaderRecord["content-length"] = values.reduce((acc, value) => acc + value.length, 0);
15018
+ }
15019
+ }
15020
+ outgoing.writeHead(res.status, resHeaderRecord);
15021
+ values.forEach((value) => {
15022
+ outgoing.write(value);
15023
+ });
15024
+ if (done) {
15025
+ outgoing.end();
15026
+ } else {
15027
+ if (values.length === 0) {
15028
+ flushHeaders(outgoing);
15029
+ }
15030
+ await writeFromReadableStreamDefaultReader(reader, outgoing, currentReadPromise);
15031
+ }
15032
+ } else if (resHeaderRecord[X_ALREADY_SENT]) ;
15033
+ else {
15034
+ outgoing.writeHead(res.status, resHeaderRecord);
15035
+ outgoing.end();
15036
+ }
15037
+ outgoing[outgoingEnded]?.();
15038
+ };
15039
+ var getRequestListener = (fetchCallback, options = {}) => {
15040
+ const autoCleanupIncoming = options.autoCleanupIncoming ?? true;
15041
+ if (options.overrideGlobalObjects !== false && global.Request !== Request) {
15042
+ Object.defineProperty(global, "Request", {
15043
+ value: Request
15044
+ });
15045
+ Object.defineProperty(global, "Response", {
15046
+ value: Response2
15047
+ });
15048
+ }
15049
+ return async (incoming, outgoing) => {
15050
+ let res, req;
15051
+ try {
15052
+ req = newRequest(incoming, options.hostname);
15053
+ let incomingEnded = !autoCleanupIncoming || incoming.method === "GET" || incoming.method === "HEAD";
15054
+ if (!incomingEnded) {
15055
+ ;
15056
+ incoming[wrapBodyStream] = true;
15057
+ incoming.on("end", () => {
15058
+ incomingEnded = true;
15059
+ });
15060
+ if (incoming instanceof http2.Http2ServerRequest) {
15061
+ ;
15062
+ outgoing[outgoingEnded] = () => {
15063
+ if (!incomingEnded) {
15064
+ setTimeout(() => {
15065
+ if (!incomingEnded) {
15066
+ setTimeout(() => {
15067
+ incoming.destroy();
15068
+ outgoing.destroy();
15069
+ });
15070
+ }
15071
+ });
15072
+ }
15073
+ };
15074
+ }
15075
+ }
15076
+ outgoing.on("close", () => {
15077
+ const abortController = req[abortControllerKey];
15078
+ if (abortController) {
15079
+ if (incoming.errored) {
15080
+ req[abortControllerKey].abort(incoming.errored.toString());
15081
+ } else if (!outgoing.writableFinished) {
15082
+ req[abortControllerKey].abort("Client connection prematurely closed.");
15083
+ }
15084
+ }
15085
+ if (!incomingEnded) {
15086
+ setTimeout(() => {
15087
+ if (!incomingEnded) {
15088
+ setTimeout(() => {
15089
+ incoming.destroy();
15090
+ });
15091
+ }
15092
+ });
15093
+ }
15094
+ });
15095
+ res = fetchCallback(req, { incoming, outgoing });
15096
+ if (cacheKey in res) {
15097
+ return responseViaCache(res, outgoing);
15098
+ }
15099
+ } catch (e) {
15100
+ if (!res) {
15101
+ if (options.errorHandler) {
15102
+ res = await options.errorHandler(req ? e : toRequestError(e));
15103
+ if (!res) {
15104
+ return;
15105
+ }
15106
+ } else if (!req) {
15107
+ res = handleRequestError();
15108
+ } else {
15109
+ res = handleFetchError(e);
15110
+ }
15111
+ } else {
15112
+ return handleResponseError(e, outgoing);
15113
+ }
15114
+ }
15115
+ try {
15116
+ return await responseViaResponseObject(res, outgoing, options);
15117
+ } catch (e) {
15118
+ return handleResponseError(e, outgoing);
15119
+ }
15120
+ };
15121
+ };
15122
+ class WebStandardStreamableHTTPServerTransport {
15123
+ constructor(options = {}) {
15124
+ this._started = false;
15125
+ this._streamMapping = /* @__PURE__ */ new Map();
15126
+ this._requestToStreamMapping = /* @__PURE__ */ new Map();
15127
+ this._requestResponseMap = /* @__PURE__ */ new Map();
15128
+ this._initialized = false;
15129
+ this._enableJsonResponse = false;
15130
+ this._standaloneSseStreamId = "_GET_stream";
15131
+ this.sessionIdGenerator = options.sessionIdGenerator;
15132
+ this._enableJsonResponse = options.enableJsonResponse ?? false;
15133
+ this._eventStore = options.eventStore;
15134
+ this._onsessioninitialized = options.onsessioninitialized;
15135
+ this._onsessionclosed = options.onsessionclosed;
15136
+ this._allowedHosts = options.allowedHosts;
15137
+ this._allowedOrigins = options.allowedOrigins;
15138
+ this._enableDnsRebindingProtection = options.enableDnsRebindingProtection ?? false;
15139
+ this._retryInterval = options.retryInterval;
15140
+ }
15141
+ /**
15142
+ * Starts the transport. This is required by the Transport interface but is a no-op
15143
+ * for the Streamable HTTP transport as connections are managed per-request.
15144
+ */
15145
+ async start() {
15146
+ if (this._started) {
15147
+ throw new Error("Transport already started");
15148
+ }
15149
+ this._started = true;
15150
+ }
15151
+ /**
15152
+ * Helper to create a JSON error response
15153
+ */
15154
+ createJsonErrorResponse(status, code2, message, options) {
15155
+ const error = { code: code2, message };
15156
+ if (options?.data !== void 0) {
15157
+ error.data = options.data;
15158
+ }
15159
+ return new Response(JSON.stringify({
15160
+ jsonrpc: "2.0",
15161
+ error,
15162
+ id: null
15163
+ }), {
15164
+ status,
15165
+ headers: {
15166
+ "Content-Type": "application/json",
15167
+ ...options?.headers
15168
+ }
15169
+ });
15170
+ }
15171
+ /**
15172
+ * Validates request headers for DNS rebinding protection.
15173
+ * @returns Error response if validation fails, undefined if validation passes.
15174
+ */
15175
+ validateRequestHeaders(req) {
15176
+ if (!this._enableDnsRebindingProtection) {
15177
+ return void 0;
15178
+ }
15179
+ if (this._allowedHosts && this._allowedHosts.length > 0) {
15180
+ const hostHeader = req.headers.get("host");
15181
+ if (!hostHeader || !this._allowedHosts.includes(hostHeader)) {
15182
+ const error = `Invalid Host header: ${hostHeader}`;
15183
+ this.onerror?.(new Error(error));
15184
+ return this.createJsonErrorResponse(403, -32e3, error);
15185
+ }
15186
+ }
15187
+ if (this._allowedOrigins && this._allowedOrigins.length > 0) {
15188
+ const originHeader = req.headers.get("origin");
15189
+ if (originHeader && !this._allowedOrigins.includes(originHeader)) {
15190
+ const error = `Invalid Origin header: ${originHeader}`;
15191
+ this.onerror?.(new Error(error));
15192
+ return this.createJsonErrorResponse(403, -32e3, error);
15193
+ }
15194
+ }
15195
+ return void 0;
15196
+ }
15197
+ /**
15198
+ * Handles an incoming HTTP request, whether GET, POST, or DELETE
15199
+ * Returns a Response object (Web Standard)
15200
+ */
15201
+ async handleRequest(req, options) {
15202
+ const validationError = this.validateRequestHeaders(req);
15203
+ if (validationError) {
15204
+ return validationError;
15205
+ }
15206
+ switch (req.method) {
15207
+ case "POST":
15208
+ return this.handlePostRequest(req, options);
15209
+ case "GET":
15210
+ return this.handleGetRequest(req);
15211
+ case "DELETE":
15212
+ return this.handleDeleteRequest(req);
15213
+ default:
15214
+ return this.handleUnsupportedRequest();
15215
+ }
15216
+ }
15217
+ /**
15218
+ * Writes a priming event to establish resumption capability.
15219
+ * Only sends if eventStore is configured (opt-in for resumability) and
15220
+ * the client's protocol version supports empty SSE data (>= 2025-11-25).
15221
+ */
15222
+ async writePrimingEvent(controller, encoder, streamId, protocolVersion) {
15223
+ if (!this._eventStore) {
15224
+ return;
15225
+ }
15226
+ if (protocolVersion < "2025-11-25") {
15227
+ return;
15228
+ }
15229
+ const primingEventId = await this._eventStore.storeEvent(streamId, {});
15230
+ let primingEvent = `id: ${primingEventId}
15231
+ data:
15232
+
15233
+ `;
15234
+ if (this._retryInterval !== void 0) {
15235
+ primingEvent = `id: ${primingEventId}
15236
+ retry: ${this._retryInterval}
15237
+ data:
15238
+
15239
+ `;
15240
+ }
15241
+ controller.enqueue(encoder.encode(primingEvent));
15242
+ }
15243
+ /**
15244
+ * Handles GET requests for SSE stream
15245
+ */
15246
+ async handleGetRequest(req) {
15247
+ const acceptHeader = req.headers.get("accept");
15248
+ if (!acceptHeader?.includes("text/event-stream")) {
15249
+ return this.createJsonErrorResponse(406, -32e3, "Not Acceptable: Client must accept text/event-stream");
15250
+ }
15251
+ const sessionError = this.validateSession(req);
15252
+ if (sessionError) {
15253
+ return sessionError;
15254
+ }
15255
+ const protocolError = this.validateProtocolVersion(req);
15256
+ if (protocolError) {
15257
+ return protocolError;
15258
+ }
15259
+ if (this._eventStore) {
15260
+ const lastEventId = req.headers.get("last-event-id");
15261
+ if (lastEventId) {
15262
+ return this.replayEvents(lastEventId);
15263
+ }
15264
+ }
15265
+ if (this._streamMapping.get(this._standaloneSseStreamId) !== void 0) {
15266
+ return this.createJsonErrorResponse(409, -32e3, "Conflict: Only one SSE stream is allowed per session");
15267
+ }
15268
+ const encoder = new TextEncoder();
15269
+ let streamController;
15270
+ const readable = new ReadableStream({
15271
+ start: (controller) => {
15272
+ streamController = controller;
15273
+ },
15274
+ cancel: () => {
15275
+ this._streamMapping.delete(this._standaloneSseStreamId);
15276
+ }
15277
+ });
15278
+ const headers = {
15279
+ "Content-Type": "text/event-stream",
15280
+ "Cache-Control": "no-cache, no-transform",
15281
+ Connection: "keep-alive"
15282
+ };
15283
+ if (this.sessionId !== void 0) {
15284
+ headers["mcp-session-id"] = this.sessionId;
15285
+ }
15286
+ this._streamMapping.set(this._standaloneSseStreamId, {
15287
+ controller: streamController,
15288
+ encoder,
15289
+ cleanup: () => {
15290
+ this._streamMapping.delete(this._standaloneSseStreamId);
15291
+ try {
15292
+ streamController.close();
15293
+ } catch {
15294
+ }
15295
+ }
15296
+ });
15297
+ return new Response(readable, { headers });
15298
+ }
15299
+ /**
15300
+ * Replays events that would have been sent after the specified event ID
15301
+ * Only used when resumability is enabled
15302
+ */
15303
+ async replayEvents(lastEventId) {
15304
+ if (!this._eventStore) {
15305
+ return this.createJsonErrorResponse(400, -32e3, "Event store not configured");
15306
+ }
15307
+ try {
15308
+ let streamId;
15309
+ if (this._eventStore.getStreamIdForEventId) {
15310
+ streamId = await this._eventStore.getStreamIdForEventId(lastEventId);
15311
+ if (!streamId) {
15312
+ return this.createJsonErrorResponse(400, -32e3, "Invalid event ID format");
15313
+ }
15314
+ if (this._streamMapping.get(streamId) !== void 0) {
15315
+ return this.createJsonErrorResponse(409, -32e3, "Conflict: Stream already has an active connection");
15316
+ }
15317
+ }
15318
+ const headers = {
15319
+ "Content-Type": "text/event-stream",
15320
+ "Cache-Control": "no-cache, no-transform",
15321
+ Connection: "keep-alive"
15322
+ };
15323
+ if (this.sessionId !== void 0) {
15324
+ headers["mcp-session-id"] = this.sessionId;
15325
+ }
15326
+ const encoder = new TextEncoder();
15327
+ let streamController;
15328
+ const readable = new ReadableStream({
15329
+ start: (controller) => {
15330
+ streamController = controller;
15331
+ },
15332
+ cancel: () => {
15333
+ }
15334
+ });
15335
+ const replayedStreamId = await this._eventStore.replayEventsAfter(lastEventId, {
15336
+ send: async (eventId, message) => {
15337
+ const success = this.writeSSEEvent(streamController, encoder, message, eventId);
15338
+ if (!success) {
15339
+ this.onerror?.(new Error("Failed replay events"));
15340
+ try {
15341
+ streamController.close();
15342
+ } catch {
15343
+ }
15344
+ }
15345
+ }
15346
+ });
15347
+ this._streamMapping.set(replayedStreamId, {
15348
+ controller: streamController,
15349
+ encoder,
15350
+ cleanup: () => {
15351
+ this._streamMapping.delete(replayedStreamId);
15352
+ try {
15353
+ streamController.close();
15354
+ } catch {
15355
+ }
15356
+ }
15357
+ });
15358
+ return new Response(readable, { headers });
15359
+ } catch (error) {
15360
+ this.onerror?.(error);
15361
+ return this.createJsonErrorResponse(500, -32e3, "Error replaying events");
15362
+ }
15363
+ }
15364
+ /**
15365
+ * Writes an event to an SSE stream via controller with proper formatting
15366
+ */
15367
+ writeSSEEvent(controller, encoder, message, eventId) {
15368
+ try {
15369
+ let eventData = `event: message
15370
+ `;
15371
+ if (eventId) {
15372
+ eventData += `id: ${eventId}
15373
+ `;
15374
+ }
15375
+ eventData += `data: ${JSON.stringify(message)}
15376
+
15377
+ `;
15378
+ controller.enqueue(encoder.encode(eventData));
15379
+ return true;
15380
+ } catch {
15381
+ return false;
15382
+ }
15383
+ }
15384
+ /**
15385
+ * Handles unsupported requests (PUT, PATCH, etc.)
15386
+ */
15387
+ handleUnsupportedRequest() {
15388
+ return new Response(JSON.stringify({
15389
+ jsonrpc: "2.0",
15390
+ error: {
15391
+ code: -32e3,
15392
+ message: "Method not allowed."
15393
+ },
15394
+ id: null
15395
+ }), {
15396
+ status: 405,
15397
+ headers: {
15398
+ Allow: "GET, POST, DELETE",
15399
+ "Content-Type": "application/json"
15400
+ }
15401
+ });
15402
+ }
15403
+ /**
15404
+ * Handles POST requests containing JSON-RPC messages
15405
+ */
15406
+ async handlePostRequest(req, options) {
15407
+ try {
15408
+ const acceptHeader = req.headers.get("accept");
15409
+ if (!acceptHeader?.includes("application/json") || !acceptHeader.includes("text/event-stream")) {
15410
+ return this.createJsonErrorResponse(406, -32e3, "Not Acceptable: Client must accept both application/json and text/event-stream");
15411
+ }
15412
+ const ct2 = req.headers.get("content-type");
15413
+ if (!ct2 || !ct2.includes("application/json")) {
15414
+ return this.createJsonErrorResponse(415, -32e3, "Unsupported Media Type: Content-Type must be application/json");
15415
+ }
15416
+ const requestInfo = {
15417
+ headers: Object.fromEntries(req.headers.entries())
15418
+ };
15419
+ let rawMessage;
15420
+ if (options?.parsedBody !== void 0) {
15421
+ rawMessage = options.parsedBody;
15422
+ } else {
15423
+ try {
15424
+ rawMessage = await req.json();
15425
+ } catch {
15426
+ return this.createJsonErrorResponse(400, -32700, "Parse error: Invalid JSON");
15427
+ }
15428
+ }
15429
+ let messages;
15430
+ try {
15431
+ if (Array.isArray(rawMessage)) {
15432
+ messages = rawMessage.map((msg) => protocol.JSONRPCMessageSchema.parse(msg));
15433
+ } else {
15434
+ messages = [protocol.JSONRPCMessageSchema.parse(rawMessage)];
15435
+ }
15436
+ } catch {
15437
+ return this.createJsonErrorResponse(400, -32700, "Parse error: Invalid JSON-RPC message");
15438
+ }
15439
+ const isInitializationRequest = messages.some(protocol.isInitializeRequest);
15440
+ if (isInitializationRequest) {
15441
+ if (this._initialized && this.sessionId !== void 0) {
15442
+ return this.createJsonErrorResponse(400, -32600, "Invalid Request: Server already initialized");
15443
+ }
15444
+ if (messages.length > 1) {
15445
+ return this.createJsonErrorResponse(400, -32600, "Invalid Request: Only one initialization request is allowed");
15446
+ }
15447
+ this.sessionId = this.sessionIdGenerator?.();
15448
+ this._initialized = true;
15449
+ if (this.sessionId && this._onsessioninitialized) {
15450
+ await Promise.resolve(this._onsessioninitialized(this.sessionId));
15451
+ }
15452
+ }
15453
+ if (!isInitializationRequest) {
15454
+ const sessionError = this.validateSession(req);
15455
+ if (sessionError) {
15456
+ return sessionError;
15457
+ }
15458
+ const protocolError = this.validateProtocolVersion(req);
15459
+ if (protocolError) {
15460
+ return protocolError;
15461
+ }
15462
+ }
15463
+ const hasRequests = messages.some(protocol.isJSONRPCRequest);
15464
+ if (!hasRequests) {
15465
+ for (const message of messages) {
15466
+ this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
15467
+ }
15468
+ return new Response(null, { status: 202 });
15469
+ }
15470
+ const streamId = crypto.randomUUID();
15471
+ const initRequest = messages.find((m2) => protocol.isInitializeRequest(m2));
15472
+ const clientProtocolVersion = initRequest ? initRequest.params.protocolVersion : req.headers.get("mcp-protocol-version") ?? protocol.DEFAULT_NEGOTIATED_PROTOCOL_VERSION;
15473
+ if (this._enableJsonResponse) {
15474
+ return new Promise((resolve2) => {
15475
+ this._streamMapping.set(streamId, {
15476
+ resolveJson: resolve2,
15477
+ cleanup: () => {
15478
+ this._streamMapping.delete(streamId);
15479
+ }
15480
+ });
15481
+ for (const message of messages) {
15482
+ if (protocol.isJSONRPCRequest(message)) {
15483
+ this._requestToStreamMapping.set(message.id, streamId);
15484
+ }
15485
+ }
15486
+ for (const message of messages) {
15487
+ this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo });
15488
+ }
15489
+ });
15490
+ }
15491
+ const encoder = new TextEncoder();
15492
+ let streamController;
15493
+ const readable = new ReadableStream({
15494
+ start: (controller) => {
15495
+ streamController = controller;
15496
+ },
15497
+ cancel: () => {
15498
+ this._streamMapping.delete(streamId);
15499
+ }
15500
+ });
15501
+ const headers = {
15502
+ "Content-Type": "text/event-stream",
15503
+ "Cache-Control": "no-cache",
15504
+ Connection: "keep-alive"
15505
+ };
15506
+ if (this.sessionId !== void 0) {
15507
+ headers["mcp-session-id"] = this.sessionId;
15508
+ }
15509
+ for (const message of messages) {
15510
+ if (protocol.isJSONRPCRequest(message)) {
15511
+ this._streamMapping.set(streamId, {
15512
+ controller: streamController,
15513
+ encoder,
15514
+ cleanup: () => {
15515
+ this._streamMapping.delete(streamId);
15516
+ try {
15517
+ streamController.close();
15518
+ } catch {
15519
+ }
15520
+ }
15521
+ });
15522
+ this._requestToStreamMapping.set(message.id, streamId);
15523
+ }
15524
+ }
15525
+ await this.writePrimingEvent(streamController, encoder, streamId, clientProtocolVersion);
15526
+ for (const message of messages) {
15527
+ let closeSSEStream;
15528
+ let closeStandaloneSSEStream;
15529
+ if (protocol.isJSONRPCRequest(message) && this._eventStore && clientProtocolVersion >= "2025-11-25") {
15530
+ closeSSEStream = () => {
15531
+ this.closeSSEStream(message.id);
15532
+ };
15533
+ closeStandaloneSSEStream = () => {
15534
+ this.closeStandaloneSSEStream();
15535
+ };
15536
+ }
15537
+ this.onmessage?.(message, { authInfo: options?.authInfo, requestInfo, closeSSEStream, closeStandaloneSSEStream });
15538
+ }
15539
+ return new Response(readable, { status: 200, headers });
15540
+ } catch (error) {
15541
+ this.onerror?.(error);
15542
+ return this.createJsonErrorResponse(400, -32700, "Parse error", { data: String(error) });
15543
+ }
15544
+ }
15545
+ /**
15546
+ * Handles DELETE requests to terminate sessions
15547
+ */
15548
+ async handleDeleteRequest(req) {
15549
+ const sessionError = this.validateSession(req);
15550
+ if (sessionError) {
15551
+ return sessionError;
15552
+ }
15553
+ const protocolError = this.validateProtocolVersion(req);
15554
+ if (protocolError) {
15555
+ return protocolError;
15556
+ }
15557
+ await Promise.resolve(this._onsessionclosed?.(this.sessionId));
15558
+ await this.close();
15559
+ return new Response(null, { status: 200 });
15560
+ }
15561
+ /**
15562
+ * Validates session ID for non-initialization requests.
15563
+ * Returns Response error if invalid, undefined otherwise
15564
+ */
15565
+ validateSession(req) {
15566
+ if (this.sessionIdGenerator === void 0) {
15567
+ return void 0;
15568
+ }
15569
+ if (!this._initialized) {
15570
+ return this.createJsonErrorResponse(400, -32e3, "Bad Request: Server not initialized");
15571
+ }
15572
+ const sessionId = req.headers.get("mcp-session-id");
15573
+ if (!sessionId) {
15574
+ return this.createJsonErrorResponse(400, -32e3, "Bad Request: Mcp-Session-Id header is required");
15575
+ }
15576
+ if (sessionId !== this.sessionId) {
15577
+ return this.createJsonErrorResponse(404, -32001, "Session not found");
15578
+ }
15579
+ return void 0;
15580
+ }
15581
+ /**
15582
+ * Validates the MCP-Protocol-Version header on incoming requests.
15583
+ *
15584
+ * For initialization: Version negotiation handles unknown versions gracefully
15585
+ * (server responds with its supported version).
15586
+ *
15587
+ * For subsequent requests with MCP-Protocol-Version header:
15588
+ * - Accept if in supported list
15589
+ * - 400 if unsupported
15590
+ *
15591
+ * For HTTP requests without the MCP-Protocol-Version header:
15592
+ * - Accept and default to the version negotiated at initialization
15593
+ */
15594
+ validateProtocolVersion(req) {
15595
+ const protocolVersion = req.headers.get("mcp-protocol-version");
15596
+ if (protocolVersion !== null && !protocol.SUPPORTED_PROTOCOL_VERSIONS.includes(protocolVersion)) {
15597
+ return this.createJsonErrorResponse(400, -32e3, `Bad Request: Unsupported protocol version: ${protocolVersion} (supported versions: ${protocol.SUPPORTED_PROTOCOL_VERSIONS.join(", ")})`);
15598
+ }
15599
+ return void 0;
15600
+ }
15601
+ async close() {
15602
+ this._streamMapping.forEach(({ cleanup }) => {
15603
+ cleanup();
15604
+ });
15605
+ this._streamMapping.clear();
15606
+ this._requestResponseMap.clear();
15607
+ this.onclose?.();
15608
+ }
15609
+ /**
15610
+ * Close an SSE stream for a specific request, triggering client reconnection.
15611
+ * Use this to implement polling behavior during long-running operations -
15612
+ * client will reconnect after the retry interval specified in the priming event.
15613
+ */
15614
+ closeSSEStream(requestId) {
15615
+ const streamId = this._requestToStreamMapping.get(requestId);
15616
+ if (!streamId)
15617
+ return;
15618
+ const stream2 = this._streamMapping.get(streamId);
15619
+ if (stream2) {
15620
+ stream2.cleanup();
15621
+ }
15622
+ }
15623
+ /**
15624
+ * Close the standalone GET SSE stream, triggering client reconnection.
15625
+ * Use this to implement polling behavior for server-initiated notifications.
15626
+ */
15627
+ closeStandaloneSSEStream() {
15628
+ const stream2 = this._streamMapping.get(this._standaloneSseStreamId);
15629
+ if (stream2) {
15630
+ stream2.cleanup();
15631
+ }
15632
+ }
15633
+ async send(message, options) {
15634
+ let requestId = options?.relatedRequestId;
15635
+ if (protocol.isJSONRPCResultResponse(message) || protocol.isJSONRPCErrorResponse(message)) {
15636
+ requestId = message.id;
15637
+ }
15638
+ if (requestId === void 0) {
15639
+ if (protocol.isJSONRPCResultResponse(message) || protocol.isJSONRPCErrorResponse(message)) {
15640
+ throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
15641
+ }
15642
+ let eventId;
15643
+ if (this._eventStore) {
15644
+ eventId = await this._eventStore.storeEvent(this._standaloneSseStreamId, message);
15645
+ }
15646
+ const standaloneSse = this._streamMapping.get(this._standaloneSseStreamId);
15647
+ if (standaloneSse === void 0) {
15648
+ return;
15649
+ }
15650
+ if (standaloneSse.controller && standaloneSse.encoder) {
15651
+ this.writeSSEEvent(standaloneSse.controller, standaloneSse.encoder, message, eventId);
15652
+ }
15653
+ return;
15654
+ }
15655
+ const streamId = this._requestToStreamMapping.get(requestId);
15656
+ if (!streamId) {
15657
+ throw new Error(`No connection established for request ID: ${String(requestId)}`);
15658
+ }
15659
+ const stream2 = this._streamMapping.get(streamId);
15660
+ if (!this._enableJsonResponse && stream2?.controller && stream2?.encoder) {
15661
+ let eventId;
15662
+ if (this._eventStore) {
15663
+ eventId = await this._eventStore.storeEvent(streamId, message);
15664
+ }
15665
+ this.writeSSEEvent(stream2.controller, stream2.encoder, message, eventId);
15666
+ }
15667
+ if (protocol.isJSONRPCResultResponse(message) || protocol.isJSONRPCErrorResponse(message)) {
15668
+ this._requestResponseMap.set(requestId, message);
15669
+ const relatedIds = Array.from(this._requestToStreamMapping.entries()).filter(([_, sid]) => sid === streamId).map(([id2]) => id2);
15670
+ const allResponsesReady = relatedIds.every((id2) => this._requestResponseMap.has(id2));
15671
+ if (allResponsesReady) {
15672
+ if (!stream2) {
15673
+ throw new Error(`No connection established for request ID: ${String(requestId)}`);
15674
+ }
15675
+ if (this._enableJsonResponse && stream2.resolveJson) {
15676
+ const headers = {
15677
+ "Content-Type": "application/json"
15678
+ };
15679
+ if (this.sessionId !== void 0) {
15680
+ headers["mcp-session-id"] = this.sessionId;
15681
+ }
15682
+ const responses = relatedIds.map((id2) => this._requestResponseMap.get(id2));
15683
+ if (responses.length === 1) {
15684
+ stream2.resolveJson(new Response(JSON.stringify(responses[0]), { status: 200, headers }));
15685
+ } else {
15686
+ stream2.resolveJson(new Response(JSON.stringify(responses), { status: 200, headers }));
15687
+ }
15688
+ } else {
15689
+ stream2.cleanup();
15690
+ }
15691
+ for (const id2 of relatedIds) {
15692
+ this._requestResponseMap.delete(id2);
15693
+ this._requestToStreamMapping.delete(id2);
15694
+ }
15695
+ }
15696
+ }
15697
+ }
15698
+ }
15699
+ class StreamableHTTPServerTransport {
15700
+ constructor(options = {}) {
15701
+ this._requestContext = /* @__PURE__ */ new WeakMap();
15702
+ this._webStandardTransport = new WebStandardStreamableHTTPServerTransport(options);
15703
+ this._requestListener = getRequestListener(async (webRequest) => {
15704
+ const context = this._requestContext.get(webRequest);
15705
+ return this._webStandardTransport.handleRequest(webRequest, {
15706
+ authInfo: context?.authInfo,
15707
+ parsedBody: context?.parsedBody
15708
+ });
15709
+ });
15710
+ }
15711
+ /**
15712
+ * Gets the session ID for this transport instance.
15713
+ */
15714
+ get sessionId() {
15715
+ return this._webStandardTransport.sessionId;
15716
+ }
15717
+ /**
15718
+ * Sets callback for when the transport is closed.
15719
+ */
15720
+ set onclose(handler) {
15721
+ this._webStandardTransport.onclose = handler;
15722
+ }
15723
+ get onclose() {
15724
+ return this._webStandardTransport.onclose;
15725
+ }
15726
+ /**
15727
+ * Sets callback for transport errors.
15728
+ */
15729
+ set onerror(handler) {
15730
+ this._webStandardTransport.onerror = handler;
15731
+ }
15732
+ get onerror() {
15733
+ return this._webStandardTransport.onerror;
15734
+ }
15735
+ /**
15736
+ * Sets callback for incoming messages.
15737
+ */
15738
+ set onmessage(handler) {
15739
+ this._webStandardTransport.onmessage = handler;
15740
+ }
15741
+ get onmessage() {
15742
+ return this._webStandardTransport.onmessage;
15743
+ }
15744
+ /**
15745
+ * Starts the transport. This is required by the Transport interface but is a no-op
15746
+ * for the Streamable HTTP transport as connections are managed per-request.
15747
+ */
15748
+ async start() {
15749
+ return this._webStandardTransport.start();
15750
+ }
15751
+ /**
15752
+ * Closes the transport and all active connections.
15753
+ */
15754
+ async close() {
15755
+ return this._webStandardTransport.close();
15756
+ }
15757
+ /**
15758
+ * Sends a JSON-RPC message through the transport.
15759
+ */
15760
+ async send(message, options) {
15761
+ return this._webStandardTransport.send(message, options);
15762
+ }
15763
+ /**
15764
+ * Handles an incoming HTTP request, whether GET or POST.
15765
+ *
15766
+ * This method converts Node.js HTTP objects to Web Standard Request/Response
15767
+ * and delegates to the underlying WebStandardStreamableHTTPServerTransport.
15768
+ *
15769
+ * @param req - Node.js IncomingMessage, optionally with auth property from middleware
15770
+ * @param res - Node.js ServerResponse
15771
+ * @param parsedBody - Optional pre-parsed body from body-parser middleware
15772
+ */
15773
+ async handleRequest(req, res, parsedBody) {
15774
+ const authInfo = req.auth;
15775
+ const handler = getRequestListener(async (webRequest) => {
15776
+ return this._webStandardTransport.handleRequest(webRequest, {
15777
+ authInfo,
15778
+ parsedBody
15779
+ });
15780
+ });
15781
+ await handler(req, res);
15782
+ }
15783
+ /**
15784
+ * Close an SSE stream for a specific request, triggering client reconnection.
15785
+ * Use this to implement polling behavior during long-running operations -
15786
+ * client will reconnect after the retry interval specified in the priming event.
15787
+ */
15788
+ closeSSEStream(requestId) {
15789
+ this._webStandardTransport.closeSSEStream(requestId);
15790
+ }
15791
+ /**
15792
+ * Close the standalone GET SSE stream, triggering client reconnection.
15793
+ * Use this to implement polling behavior for server-initiated notifications.
15794
+ */
15795
+ closeStandaloneSSEStream() {
15796
+ this._webStandardTransport.closeStandaloneSSEStream();
15797
+ }
15798
+ }
15799
+ function createProductionMcpServer(config) {
15800
+ const { name = "sunpeak-app", version = "0.1.0", tools, resources } = config;
15801
+ const mcpServer = new McpServer(
15802
+ {
15803
+ name,
15804
+ version,
15805
+ icons: [
15806
+ {
15807
+ src: `data:image/png;base64,${FAVICON_BASE64}`,
15808
+ mimeType: "image/png",
15809
+ sizes: ["128x128"]
15810
+ }
15811
+ ]
15812
+ },
15813
+ { capabilities: { resources: {}, tools: {} } }
15814
+ );
15815
+ const resourceByName = /* @__PURE__ */ new Map();
15816
+ for (const res of resources) {
15817
+ resourceByName.set(res.name, res);
15818
+ }
15819
+ const registeredResources = /* @__PURE__ */ new Set();
15820
+ for (const tool of tools) {
15821
+ const res = resourceByName.get(tool.tool.resource);
15822
+ if (!res) {
15823
+ console.warn(
15824
+ `[MCP] Warning: Resource "${tool.tool.resource}" not found for tool "${tool.name}". Skipping.`
15825
+ );
15826
+ continue;
15827
+ }
15828
+ if (!registeredResources.has(res.uri)) {
15829
+ registeredResources.add(res.uri);
15830
+ ak(
15831
+ mcpServer,
15832
+ res.name,
15833
+ res.uri,
15834
+ {
15835
+ description: res.description,
15836
+ _meta: res._meta
15837
+ },
15838
+ async () => ({
15839
+ contents: [
15840
+ {
15841
+ uri: res.uri,
15842
+ mimeType: EI,
15843
+ text: res.html,
15844
+ _meta: res._meta
15845
+ }
15846
+ ]
15847
+ })
15848
+ );
15849
+ }
15850
+ const toolConfig = {
15851
+ title: tool.tool.title,
15852
+ description: tool.tool.description,
15853
+ annotations: tool.tool.annotations,
15854
+ ...tool.schema ? { inputSchema: tool.schema } : {},
15855
+ _meta: {
15856
+ ...tool.tool._meta,
15857
+ ui: {
15858
+ resourceUri: res.uri,
15859
+ ...tool.tool._meta?.ui ?? {}
15860
+ }
15861
+ }
15862
+ };
15863
+ const callback = async (...cbArgs) => {
15864
+ const hasSchema = !!tool.schema;
15865
+ const args = hasSchema ? cbArgs[0] : {};
15866
+ const extra = hasSchema ? cbArgs[1] : cbArgs[0];
15867
+ const argKeys = Object.keys(args);
15868
+ const argsStr = argKeys.length > 0 ? `{${argKeys.join(", ")}}` : "{}";
15869
+ console.log(`[MCP] CallTool: ${tool.name}${argsStr}`);
15870
+ const result = await tool.handler(args, extra);
15871
+ if (typeof result === "string") {
15872
+ return { content: [{ type: "text", text: result }] };
15873
+ }
15874
+ return result;
15875
+ };
15876
+ hk(
15877
+ mcpServer,
15878
+ tool.name,
15879
+ toolConfig,
15880
+ callback
15881
+ );
15882
+ }
15883
+ const toolCount = tools.filter((t) => resourceByName.has(t.tool.resource)).length;
15884
+ const resourceCount = registeredResources.size;
15885
+ console.log(`[MCP] Registered ${toolCount} tool(s) and ${resourceCount} resource(s)`);
15886
+ return mcpServer;
15887
+ }
15888
+ const MCP_PATH = "/mcp";
15889
+ const SESSION_IDLE_TIMEOUT_MS = 5 * 60 * 1e3;
15890
+ function isJsonRpcMessage(value) {
15891
+ return typeof value === "object" && value !== null && typeof value.method === "string";
15892
+ }
15893
+ const CORS_HEADERS = {
15894
+ "Access-Control-Allow-Origin": "*",
15895
+ "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
15896
+ "Access-Control-Allow-Headers": "content-type, accept, authorization, mcp-session-id, ngrok-skip-browser-warning",
15897
+ "Access-Control-Expose-Headers": "mcp-session-id"
15898
+ };
15899
+ function createMcpHandler(config) {
15900
+ const sessions2 = /* @__PURE__ */ new Map();
15901
+ const authFn = config.auth;
15902
+ const cleanupInterval = setInterval(() => {
15903
+ const now = Date.now();
15904
+ for (const [id2, session] of sessions2) {
15905
+ if (now - session.lastActivity > SESSION_IDLE_TIMEOUT_MS) {
15906
+ sessions2.delete(id2);
15907
+ session.transport.close?.();
15908
+ session.server.close();
15909
+ console.log(`[MCP] Session expired: ${id2.substring(0, 8)}... (${sessions2.size} active)`);
15910
+ }
15911
+ }
15912
+ }, 6e4);
15913
+ cleanupInterval.unref();
15914
+ return async (req, res) => {
15915
+ if (!req.url) return;
15916
+ const url = new node_url.URL(req.url, `http://${req.headers.host ?? "localhost"}`);
15917
+ if (url.pathname !== MCP_PATH) return;
15918
+ if (req.method === "OPTIONS") {
15919
+ res.writeHead(204, CORS_HEADERS);
15920
+ res.end();
15921
+ return;
15922
+ }
15923
+ for (const [key, value] of Object.entries(CORS_HEADERS)) {
15924
+ res.setHeader(key, value);
15925
+ }
15926
+ if (authFn) {
15927
+ const authInfo = await authFn(req);
15928
+ if (!authInfo) {
15929
+ res.writeHead(401, { "WWW-Authenticate": "Bearer" });
15930
+ res.end("Unauthorized");
15931
+ return;
15932
+ }
15933
+ req.auth = authInfo;
15934
+ }
15935
+ let parsedBody;
15936
+ if (req.method === "POST") {
15937
+ const chunks = [];
15938
+ await new Promise((resolve2, reject) => {
15939
+ req.on("data", (chunk) => {
15940
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
15941
+ });
15942
+ req.on("end", resolve2);
15943
+ req.on("error", reject);
15944
+ });
15945
+ const rawBody = Buffer.concat(chunks).toString("utf8");
15946
+ try {
15947
+ parsedBody = JSON.parse(rawBody);
15948
+ if (isJsonRpcMessage(parsedBody)) {
15949
+ const sid = req.headers["mcp-session-id"];
15950
+ const sidStr = sid ? ` (${sid.substring(0, 8)}...)` : "";
15951
+ const extra = parsedBody.method === "resources/read" ? ` uri=${JSON.stringify(parsedBody.params?.uri)}` : "";
15952
+ console.log(`[MCP] ← ${parsedBody.method}${extra}${sidStr}`);
15953
+ }
15954
+ } catch {
15955
+ res.writeHead(400).end("Invalid JSON");
15956
+ return;
15957
+ }
15958
+ }
15959
+ const sessionId = req.headers["mcp-session-id"];
15960
+ if (sessionId) {
15961
+ const session = sessions2.get(sessionId);
15962
+ if (!session) {
15963
+ res.writeHead(404).end("Unknown session");
15964
+ return;
15965
+ }
15966
+ session.lastActivity = Date.now();
15967
+ await session.transport.handleRequest(req, res, parsedBody);
15968
+ return;
15969
+ }
15970
+ if (req.method === "POST") {
15971
+ const server = createProductionMcpServer(config);
15972
+ const transport = new StreamableHTTPServerTransport({
15973
+ sessionIdGenerator: () => node_crypto.randomUUID(),
15974
+ onsessioninitialized: (id2) => {
15975
+ sessions2.set(id2, { server, transport, lastActivity: Date.now() });
15976
+ console.log(`[MCP] Session started: ${id2.substring(0, 8)}... (${sessions2.size} active)`);
15977
+ },
15978
+ onsessionclosed: (id2) => {
15979
+ sessions2.delete(id2);
15980
+ console.log(`[MCP] Session closed: ${id2.substring(0, 8)}... (${sessions2.size} active)`);
15981
+ }
15982
+ });
15983
+ transport.onerror = (error) => {
15984
+ const id2 = transport.sessionId;
15985
+ console.error(`[MCP] Transport error${id2 ? ` (${id2.substring(0, 8)}...)` : ""}:`, error);
15986
+ };
15987
+ transport.onclose = async () => {
15988
+ const id2 = transport.sessionId;
15989
+ if (id2 && sessions2.has(id2)) {
15990
+ sessions2.delete(id2);
15991
+ console.log(`[MCP] Session closed: ${id2.substring(0, 8)}... (${sessions2.size} active)`);
15992
+ }
15993
+ await server.close();
15994
+ };
15995
+ await server.connect(transport);
15996
+ await transport.handleRequest(req, res, parsedBody);
15997
+ return;
15998
+ }
15999
+ res.writeHead(400).end("Bad Request: session ID required");
16000
+ };
16001
+ }
16002
+ function createHandler(config) {
16003
+ const sessions2 = /* @__PURE__ */ new Map();
16004
+ const authFn = config.auth;
16005
+ const cleanupInterval = setInterval(() => {
16006
+ const now = Date.now();
16007
+ for (const [id2, session] of sessions2) {
16008
+ if (now - session.lastActivity > SESSION_IDLE_TIMEOUT_MS) {
16009
+ sessions2.delete(id2);
16010
+ session.transport.close?.();
16011
+ session.server.close();
16012
+ console.log(`[MCP] Session expired: ${id2.substring(0, 8)}... (${sessions2.size} active)`);
16013
+ }
16014
+ }
16015
+ }, 6e4);
16016
+ cleanupInterval.unref();
16017
+ return async (req) => {
16018
+ if (req.method === "OPTIONS") {
16019
+ return new Response(null, { status: 204, headers: CORS_HEADERS });
16020
+ }
16021
+ let authInfo;
16022
+ if (authFn) {
16023
+ const result = await authFn(req);
16024
+ if (!result) {
16025
+ return new Response("Unauthorized", {
16026
+ status: 401,
16027
+ headers: { "WWW-Authenticate": "Bearer", "Access-Control-Allow-Origin": "*" }
16028
+ });
16029
+ }
16030
+ authInfo = result;
16031
+ }
16032
+ let parsedBody;
16033
+ if (req.method === "POST") {
16034
+ try {
16035
+ parsedBody = await req.json();
16036
+ } catch {
16037
+ return new Response("Invalid JSON", { status: 400 });
16038
+ }
16039
+ }
16040
+ const sessionId = req.headers.get("mcp-session-id");
16041
+ if (sessionId) {
16042
+ const session = sessions2.get(sessionId);
16043
+ if (!session) {
16044
+ return new Response("Unknown session", { status: 404 });
16045
+ }
16046
+ session.lastActivity = Date.now();
16047
+ const response = await session.transport.handleRequest(req, { parsedBody, authInfo });
16048
+ return addCorsHeaders(response);
16049
+ }
16050
+ if (req.method === "POST") {
16051
+ const { name, version, tools, resources } = config;
16052
+ const server = createProductionMcpServer({ name, version, tools, resources });
16053
+ const transport = new WebStandardStreamableHTTPServerTransport({
16054
+ sessionIdGenerator: () => node_crypto.randomUUID(),
16055
+ onsessioninitialized: (id2) => {
16056
+ sessions2.set(id2, { server, transport, lastActivity: Date.now() });
16057
+ console.log(`[MCP] Session started: ${id2.substring(0, 8)}... (${sessions2.size} active)`);
16058
+ },
16059
+ onsessionclosed: (id2) => {
16060
+ sessions2.delete(id2);
16061
+ console.log(`[MCP] Session closed: ${id2.substring(0, 8)}... (${sessions2.size} active)`);
16062
+ }
16063
+ });
16064
+ transport.onerror = (error) => {
16065
+ console.error("[MCP] Transport error:", error);
16066
+ };
16067
+ transport.onclose = async () => {
16068
+ const id2 = transport.sessionId;
16069
+ if (id2 && sessions2.has(id2)) {
16070
+ sessions2.delete(id2);
16071
+ }
16072
+ await server.close();
16073
+ };
16074
+ await server.connect(transport);
16075
+ const response = await transport.handleRequest(req, { parsedBody, authInfo });
16076
+ return addCorsHeaders(response);
16077
+ }
16078
+ return new Response("Bad Request: session ID required", { status: 400 });
16079
+ };
16080
+ }
16081
+ function addCorsHeaders(response) {
16082
+ const headers = new Headers(response.headers);
16083
+ headers.set("Access-Control-Allow-Origin", "*");
16084
+ headers.set("Access-Control-Expose-Headers", "mcp-session-id");
16085
+ return new Response(response.body, {
16086
+ status: response.status,
16087
+ statusText: response.statusText,
16088
+ headers
16089
+ });
16090
+ }
16091
+ function startProductionHttpServer(config, port) {
16092
+ const mcpHandler = createMcpHandler(config);
16093
+ const httpServer = node_http.createServer(async (req, res) => {
16094
+ if (!req.url) {
16095
+ res.writeHead(400).end("Missing URL");
16096
+ return;
16097
+ }
16098
+ const url = new node_url.URL(req.url, `http://${req.headers.host ?? "localhost"}`);
16099
+ if (req.method === "OPTIONS") {
16100
+ res.writeHead(204, CORS_HEADERS);
16101
+ res.end();
16102
+ return;
16103
+ }
16104
+ if (req.method === "GET" && url.pathname === "/") {
16105
+ res.writeHead(200, {
16106
+ "Content-Type": "text/html",
16107
+ "Access-Control-Allow-Origin": "*"
16108
+ });
16109
+ res.end(`<!DOCTYPE html>
16110
+ <html>
16111
+ <head>
16112
+ <meta charset="UTF-8" />
16113
+ <link rel="icon" type="image/png" href="/favicon.ico" />
16114
+ <title>Sunpeak MCP Server</title>
16115
+ </head>
16116
+ <body><h1>Sunpeak MCP Server</h1><p>Connect via <a href="/mcp">/mcp</a></p></body>
16117
+ </html>`);
16118
+ return;
16119
+ }
16120
+ if (req.method === "GET" && url.pathname === "/favicon.ico") {
16121
+ res.writeHead(200, {
16122
+ "Content-Type": "image/png",
16123
+ "Content-Length": FAVICON_BUFFER.length,
16124
+ "Cache-Control": "public, max-age=86400",
16125
+ "Access-Control-Allow-Origin": "*"
16126
+ });
16127
+ res.end(FAVICON_BUFFER);
16128
+ return;
16129
+ }
16130
+ await mcpHandler(req, res);
16131
+ if (!res.headersSent) {
16132
+ res.writeHead(404).end("Not Found");
16133
+ }
16134
+ });
16135
+ httpServer.on("clientError", (err, socket) => {
16136
+ console.error("HTTP client error", err);
16137
+ socket.end("HTTP/1.1 400 Bad Request\r\n\r\n");
16138
+ });
16139
+ httpServer.listen(port, () => {
16140
+ console.log(`Sunpeak MCP server listening on http://localhost:${port}`);
16141
+ console.log(` MCP endpoint: http://localhost:${port}${MCP_PATH}`);
16142
+ });
16143
+ const shutdown = async () => {
16144
+ console.log("\nShutting down MCP server...");
16145
+ httpServer.close(() => {
16146
+ console.log("MCP server closed");
16147
+ process.exit(0);
16148
+ });
16149
+ setTimeout(() => {
16150
+ console.error("Force closing MCP server");
16151
+ process.exit(1);
16152
+ }, 5e3);
16153
+ };
16154
+ process.on("SIGTERM", () => void shutdown());
16155
+ process.on("SIGINT", () => void shutdown());
16156
+ }
14585
16157
  exports.EXTENSION_ID = C6;
14586
16158
  exports.FAVICON_BASE64 = FAVICON_BASE64;
14587
16159
  exports.FAVICON_BUFFER = FAVICON_BUFFER;
14588
16160
  exports.RESOURCE_MIME_TYPE = EI;
14589
16161
  exports.RESOURCE_URI_META_KEY = je;
16162
+ exports.createHandler = createHandler;
16163
+ exports.createMcpHandler = createMcpHandler;
16164
+ exports.createProductionMcpServer = createProductionMcpServer;
14590
16165
  exports.getUiCapability = pk;
14591
16166
  exports.registerAppResource = ak;
14592
16167
  exports.registerAppTool = hk;
14593
16168
  exports.runMCPServer = runMCPServer;
16169
+ exports.startProductionHttpServer = startProductionHttpServer;
14594
16170
  //# sourceMappingURL=index.cjs.map