@vitemcp/server 1.4.0 → 1.6.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/dist/ViteMCP.js CHANGED
@@ -573,7 +573,9 @@ var ViteMCP = class {
573
573
  async embedded(uri) {
574
574
  const direct = this.#resources.find((resource) => resource.uri === uri);
575
575
  if (direct) {
576
- const loaded = await direct.load(this.#makeContext(void 0, void 0));
576
+ const loaded = await direct.load(
577
+ this.#makeContext(void 0, void 0).context
578
+ );
577
579
  const first = Array.isArray(loaded) ? loaded[0] : loaded;
578
580
  if (!first) {
579
581
  throw new UnexpectedStateError(`Resource loaded no content: ${uri}`, {
@@ -589,7 +591,7 @@ var ViteMCP = class {
589
591
  }
590
592
  const loaded = await template.load(
591
593
  params,
592
- this.#makeContext(void 0, void 0)
594
+ this.#makeContext(void 0, void 0).context
593
595
  );
594
596
  const first = Array.isArray(loaded) ? loaded[0] : loaded;
595
597
  if (!first) {
@@ -718,7 +720,14 @@ var ViteMCP = class {
718
720
  }
719
721
  async #buildServer(auth) {
720
722
  const server = new McpServer(
721
- { name: this.#options.name, version: this.#options.version },
723
+ {
724
+ description: this.#options.description,
725
+ icons: this.#options.icons,
726
+ name: this.#options.name,
727
+ title: this.#options.title,
728
+ version: this.#options.version,
729
+ websiteUrl: this.#options.websiteUrl
730
+ },
722
731
  { instructions: this.#options.instructions }
723
732
  );
724
733
  for (const tool of this.#tools) {
@@ -741,13 +750,14 @@ var ViteMCP = class {
741
750
  )
742
751
  },
743
752
  (async (args, ctx) => {
744
- const context = this.#makeContext(auth, ctx);
753
+ const timeout = tool.timeoutMs ? { controller: new AbortController(), ms: tool.timeoutMs } : void 0;
754
+ const { context, release } = this.#makeContext(
755
+ auth,
756
+ ctx,
757
+ timeout?.controller.signal
758
+ );
745
759
  try {
746
- const result = await (tool.timeoutMs ? withTimeout(
747
- tool.execute(args, context),
748
- tool.timeoutMs,
749
- tool.name
750
- ) : tool.execute(args, context));
760
+ const result = await (timeout ? withTimeout(tool.execute(args, context), timeout, tool.name) : tool.execute(args, context));
751
761
  return normalizeToolResult(result, tool.outputSchema !== void 0);
752
762
  } catch (error) {
753
763
  if (error instanceof UserError) {
@@ -758,6 +768,8 @@ var ViteMCP = class {
758
768
  };
759
769
  }
760
770
  throw error;
771
+ } finally {
772
+ release();
761
773
  }
762
774
  })
763
775
  );
@@ -775,16 +787,20 @@ var ViteMCP = class {
775
787
  mimeType: resource.mimeType
776
788
  },
777
789
  (async (uri, ctx) => {
778
- const context = this.#makeContext(auth, ctx);
779
- const loaded = await resource.load(context);
780
- const bodies = Array.isArray(loaded) ? loaded : [loaded];
781
- return {
782
- contents: bodies.map((body) => ({
783
- ...body,
784
- mimeType: body.mimeType ?? resource.mimeType,
785
- uri: body.uri ?? uri.toString()
786
- }))
787
- };
790
+ const { context, release } = this.#makeContext(auth, ctx);
791
+ try {
792
+ const loaded = await resource.load(context);
793
+ const bodies = Array.isArray(loaded) ? loaded : [loaded];
794
+ return {
795
+ contents: bodies.map((body) => ({
796
+ ...body,
797
+ mimeType: body.mimeType ?? resource.mimeType,
798
+ uri: body.uri ?? uri.toString()
799
+ }))
800
+ };
801
+ } finally {
802
+ release();
803
+ }
788
804
  })
789
805
  );
790
806
  }
@@ -811,16 +827,20 @@ var ViteMCP = class {
811
827
  mimeType: template.mimeType
812
828
  },
813
829
  (async (uri, args, ctx) => {
814
- const context = this.#makeContext(auth, ctx);
815
- const loaded = await template.load(args ?? {}, context);
816
- const bodies = Array.isArray(loaded) ? loaded : [loaded];
817
- return {
818
- contents: bodies.map((body) => ({
819
- ...body,
820
- mimeType: body.mimeType ?? template.mimeType,
821
- uri: body.uri ?? uri.toString()
822
- }))
823
- };
830
+ const { context, release } = this.#makeContext(auth, ctx);
831
+ try {
832
+ const loaded = await template.load(args ?? {}, context);
833
+ const bodies = Array.isArray(loaded) ? loaded : [loaded];
834
+ return {
835
+ contents: bodies.map((body) => ({
836
+ ...body,
837
+ mimeType: body.mimeType ?? template.mimeType,
838
+ uri: body.uri ?? uri.toString()
839
+ }))
840
+ };
841
+ } finally {
842
+ release();
843
+ }
824
844
  })
825
845
  );
826
846
  }
@@ -835,19 +855,23 @@ var ViteMCP = class {
835
855
  description: prompt.description
836
856
  },
837
857
  (async (args, ctx) => {
838
- const context = this.#makeContext(auth, ctx);
839
- const loaded = await prompt.load(args ?? {}, context);
840
- if (typeof loaded !== "string") {
841
- return loaded;
858
+ const { context, release } = this.#makeContext(auth, ctx);
859
+ try {
860
+ const loaded = await prompt.load(args ?? {}, context);
861
+ if (typeof loaded !== "string") {
862
+ return loaded;
863
+ }
864
+ return {
865
+ messages: [
866
+ {
867
+ content: { text: loaded, type: "text" },
868
+ role: "user"
869
+ }
870
+ ]
871
+ };
872
+ } finally {
873
+ release();
842
874
  }
843
- return {
844
- messages: [
845
- {
846
- content: { text: loaded, type: "text" },
847
- role: "user"
848
- }
849
- ]
850
- };
851
875
  })
852
876
  );
853
877
  }
@@ -862,8 +886,14 @@ var ViteMCP = class {
862
886
  );
863
887
  return app;
864
888
  }
865
- /** Bridges the v2 per-request context onto ViteMCP's `Context`. */
866
- #makeContext(auth, rawCtx) {
889
+ /**
890
+ * Bridges the v2 per-request context onto ViteMCP's `Context`.
891
+ *
892
+ * `timeoutSignal` is folded into `context.signal` for a tool that declares
893
+ * `timeoutMs`; everything else mirrors the SDK's request signal alone. Call
894
+ * `release()` once the handler has settled — see {@link requestScope}.
895
+ */
896
+ #makeContext(auth, rawCtx, timeoutSignal) {
867
897
  const ctx = rawCtx;
868
898
  const mcpReq = ctx?.mcpReq;
869
899
  const requestedLevel = mcpReq?._meta?.["io.modelcontextprotocol/logLevel"];
@@ -877,39 +907,49 @@ var ViteMCP = class {
877
907
  })?.catch(() => {
878
908
  });
879
909
  };
910
+ const scope = requestScope(mcpReq?.signal, timeoutSignal);
880
911
  return {
881
- auth,
882
- elicit: (params) => inputRequired.elicit({
883
- message: params.message,
884
- requestedSchema: params.requestedSchema
885
- }),
886
- input: (key, schema) => acceptedContent(mcpReq?.inputResponses, key, schema),
887
- inputRequired: (inputRequests, requestState) => inputRequired({ inputRequests, requestState }),
888
- inputResponses: mcpReq?.inputResponses,
889
- log: {
890
- debug: (m, d) => emit("debug", m, d),
891
- error: (m, d) => emit("error", m, d),
892
- info: (m, d) => emit("info", m, d),
893
- warn: (m, d) => emit("warning", m, d)
894
- },
895
- reportProgress: async (progress) => {
896
- const progressToken = mcpReq?._meta?.progressToken;
897
- if (progressToken === void 0) {
898
- return;
899
- }
900
- await mcpReq?.notify?.({
901
- method: "notifications/progress",
902
- params: {
903
- message: progress.message,
904
- progress: progress.progress,
905
- progressToken,
906
- total: progress.total
912
+ context: {
913
+ auth,
914
+ elicit: (params) => inputRequired.elicit({
915
+ message: params.message,
916
+ requestedSchema: params.requestedSchema
917
+ }),
918
+ input: (key, schema) => acceptedContent(
919
+ mcpReq?.inputResponses,
920
+ key,
921
+ schema
922
+ ),
923
+ inputRequired: (inputRequests, requestState) => inputRequired({ inputRequests, requestState }),
924
+ inputResponses: mcpReq?.inputResponses,
925
+ log: {
926
+ debug: (m, d) => emit("debug", m, d),
927
+ error: (m, d) => emit("error", m, d),
928
+ info: (m, d) => emit("info", m, d),
929
+ warn: (m, d) => emit("warning", m, d)
930
+ },
931
+ reportProgress: async (progress) => {
932
+ const progressToken = mcpReq?._meta?.progressToken;
933
+ if (progressToken === void 0) {
934
+ return;
907
935
  }
908
- });
936
+ await mcpReq?.notify?.({
937
+ method: "notifications/progress",
938
+ params: {
939
+ message: progress.message,
940
+ progress: progress.progress,
941
+ progressToken,
942
+ total: progress.total
943
+ }
944
+ });
945
+ },
946
+ requestId: mcpReq?.id,
947
+ // The SDK exposes this as an accessor; call it so callers get the
948
+ // value.
949
+ requestState: mcpReq?.requestState?.(),
950
+ signal: scope.signal
909
951
  },
910
- requestId: mcpReq?.id,
911
- // The SDK exposes this as an accessor; call it so callers get the value.
912
- requestState: mcpReq?.requestState?.()
952
+ release: scope.release
913
953
  };
914
954
  }
915
955
  /**
@@ -1006,7 +1046,7 @@ var ViteMCP = class {
1006
1046
  return;
1007
1047
  }
1008
1048
  const response = await app.fetch(
1009
- nodeToWebRequest(req, body, useTls),
1049
+ nodeToWebRequest(req, body, useTls, clientDisconnectSignal(res)),
1010
1050
  {
1011
1051
  incoming: req,
1012
1052
  outgoing: res
@@ -1093,7 +1133,7 @@ var ViteMCP = class {
1093
1133
  );
1094
1134
  }
1095
1135
  };
1096
- var nodeToWebRequest = (req, body, secure) => {
1136
+ var nodeToWebRequest = (req, body, secure, signal) => {
1097
1137
  const host = req.headers.host ?? "localhost";
1098
1138
  const url = `${secure ? "https" : "http"}://${host}${req.url ?? "/"}`;
1099
1139
  const headers = new Headers();
@@ -1114,20 +1154,53 @@ var nodeToWebRequest = (req, body, secure) => {
1114
1154
  return new Request(url, {
1115
1155
  headers,
1116
1156
  method,
1157
+ signal,
1117
1158
  ...hasBody ? { body } : {}
1118
1159
  });
1119
1160
  };
1120
- var withTimeout = (work, timeoutMs, toolName) => new Promise((resolve, reject) => {
1161
+ var clientDisconnectSignal = (res) => {
1162
+ const controller = new AbortController();
1163
+ const abortIfPremature = () => {
1164
+ if (!res.writableEnded) {
1165
+ controller.abort(new Error("Client disconnected"));
1166
+ }
1167
+ };
1168
+ if (res.closed) {
1169
+ abortIfPremature();
1170
+ } else {
1171
+ res.once("close", abortIfPremature);
1172
+ }
1173
+ return controller.signal;
1174
+ };
1175
+ var withTimeout = (work, timeout, toolName) => new Promise((resolve, reject) => {
1121
1176
  const timer = setTimeout(() => {
1122
- reject(
1123
- new UserError(`Tool '${toolName}' timed out after ${timeoutMs}ms`, {
1124
- timeoutMs,
1125
- toolName
1126
- })
1177
+ const error = new UserError(
1178
+ `Tool '${toolName}' timed out after ${timeout.ms}ms`,
1179
+ { timeoutMs: timeout.ms, toolName }
1127
1180
  );
1128
- }, timeoutMs);
1181
+ timeout.controller.abort(error);
1182
+ reject(error);
1183
+ }, timeout.ms);
1129
1184
  work.then(resolve, reject).finally(() => clearTimeout(timer));
1130
1185
  });
1186
+ var requestScope = (...sources) => {
1187
+ const controller = new AbortController();
1188
+ const detach = new AbortController();
1189
+ for (const source of sources) {
1190
+ if (source === void 0) {
1191
+ continue;
1192
+ }
1193
+ if (source.aborted) {
1194
+ controller.abort(source.reason);
1195
+ break;
1196
+ }
1197
+ source.addEventListener("abort", () => controller.abort(source.reason), {
1198
+ once: true,
1199
+ signal: detach.signal
1200
+ });
1201
+ }
1202
+ return { release: () => detach.abort(), signal: controller.signal };
1203
+ };
1131
1204
  var formatBytes = (bytes) => {
1132
1205
  if (bytes >= 1024 * 1024 && bytes % (1024 * 1024) === 0) {
1133
1206
  return `${bytes / (1024 * 1024)} MiB`;