@vitemcp/server 1.9.0 → 1.9.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 (41) hide show
  1. package/README.md +7 -2
  2. package/dist/{OAuthProvider-DvW1SyKx.d.cts → OAuthProvider-BaPwgWny.d.cts} +12 -2
  3. package/dist/{OAuthProvider-DvW1SyKx.d.ts → OAuthProvider-BaPwgWny.d.ts} +12 -2
  4. package/dist/ViteMCP.cjs +4 -3
  5. package/dist/ViteMCP.cjs.map +1 -1
  6. package/dist/ViteMCP.d.cts +2 -2
  7. package/dist/ViteMCP.d.ts +2 -2
  8. package/dist/ViteMCP.js +3 -2
  9. package/dist/auth/index.cjs +2 -2
  10. package/dist/auth/index.d.cts +2 -2
  11. package/dist/auth/index.d.ts +2 -2
  12. package/dist/auth/index.js +1 -1
  13. package/dist/chunk-4FH73ERY.js +64 -0
  14. package/dist/chunk-4FH73ERY.js.map +1 -0
  15. package/dist/{chunk-UFLW3ODH.js → chunk-DSDLJS6O.js} +107 -130
  16. package/dist/chunk-DSDLJS6O.js.map +1 -0
  17. package/dist/chunk-FEM6LNS4.cjs +64 -0
  18. package/dist/chunk-FEM6LNS4.cjs.map +1 -0
  19. package/dist/{chunk-HWSTK2ZH.js → chunk-KKSDTVEW.js} +79 -18
  20. package/dist/chunk-KKSDTVEW.js.map +1 -0
  21. package/dist/{chunk-UPZH53RX.cjs → chunk-RQEMPJ2L.cjs} +109 -132
  22. package/dist/chunk-RQEMPJ2L.cjs.map +1 -0
  23. package/dist/{chunk-KFOTRFZS.cjs → chunk-SHPKRGNY.cjs} +93 -32
  24. package/dist/chunk-SHPKRGNY.cjs.map +1 -0
  25. package/dist/edge/index.cjs +128 -21
  26. package/dist/edge/index.cjs.map +1 -1
  27. package/dist/edge/index.d.cts +6 -0
  28. package/dist/edge/index.d.ts +6 -0
  29. package/dist/edge/index.js +127 -20
  30. package/dist/edge/index.js.map +1 -1
  31. package/dist/openapi/index.cjs +69 -29
  32. package/dist/openapi/index.cjs.map +1 -1
  33. package/dist/openapi/index.d.cts +1 -1
  34. package/dist/openapi/index.d.ts +1 -1
  35. package/dist/openapi/index.js +61 -21
  36. package/dist/openapi/index.js.map +1 -1
  37. package/package.json +1 -1
  38. package/dist/chunk-HWSTK2ZH.js.map +0 -1
  39. package/dist/chunk-KFOTRFZS.cjs.map +0 -1
  40. package/dist/chunk-UFLW3ODH.js.map +0 -1
  41. package/dist/chunk-UPZH53RX.cjs.map +0 -1
@@ -1,3 +1,12 @@
1
+ import {
2
+ withoutTrailingSlash
3
+ } from "./chunk-KKSDTVEW.js";
4
+ import {
5
+ formatBytes,
6
+ toSdkSchema,
7
+ valibotToJsonSchema
8
+ } from "./chunk-4FH73ERY.js";
9
+
1
10
  // src/ViteMCP.ts
2
11
  import {
3
12
  acceptedContent,
@@ -371,7 +380,7 @@ async function compileSchema(schema) {
371
380
  return ajv.compile(schema);
372
381
  }
373
382
  function toIssue(error) {
374
- const path = error.instancePath.split("/").filter(Boolean).map((segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~")).map((segment) => {
383
+ const path = error.instancePath.split("/").slice(1).map((segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~")).map((segment) => {
375
384
  const index = Number(segment);
376
385
  return /^(?:0|[1-9]\d*)$/.test(segment) && Number.isSafeInteger(index) ? index : segment;
377
386
  });
@@ -531,16 +540,16 @@ var ViteMCP = class {
531
540
  #logger;
532
541
  #options;
533
542
  #prompts = [];
534
- #resources = [];
535
- #resourceTemplates = [];
536
- #serverState = "stopped" /* Stopped */;
537
543
  /**
538
- * Whether this server publishes an RFC 9728 protected-resource document, and
539
- * so whether a 401 can point a client at it. Latched when the routes are
544
+ * The RFC 9728 protected-resource document this server publishes, if it
545
+ * publishes one — what a 401 points a client at. Latched when the routes are
540
546
  * mounted rather than read back from `auth` per rejection: the provider's
541
547
  * config getter builds its proxy on demand and refuses once destroyed.
542
548
  */
543
- #servesProtectedResourceMetadata = false;
549
+ #protectedResource;
550
+ #resources = [];
551
+ #resourceTemplates = [];
552
+ #serverState = "stopped" /* Stopped */;
544
553
  #stdioHandle = null;
545
554
  #tools = [];
546
555
  /** Each tool's schemas, as `#sdkSchemasFor` converted them. */
@@ -623,7 +632,11 @@ var ViteMCP = class {
623
632
  uri
624
633
  });
625
634
  }
626
- return { ...first, mimeType: first.mimeType ?? direct.mimeType, uri };
635
+ return {
636
+ ...first,
637
+ mimeType: first.mimeType ?? direct.mimeType,
638
+ uri: first.uri ?? uri
639
+ };
627
640
  }
628
641
  for (const template of this.#resourceTemplates) {
629
642
  const params = parseURITemplate(template.uriTemplate).fromUri(uri);
@@ -641,7 +654,11 @@ var ViteMCP = class {
641
654
  { uri }
642
655
  );
643
656
  }
644
- return { ...first, mimeType: first.mimeType ?? template.mimeType, uri };
657
+ return {
658
+ ...first,
659
+ mimeType: first.mimeType ?? template.mimeType,
660
+ uri: first.uri ?? uri
661
+ };
645
662
  }
646
663
  throw new UnexpectedStateError(`Resource not found: ${uri}`, { uri });
647
664
  }
@@ -803,6 +820,29 @@ var ViteMCP = class {
803
820
  const resourceTemplates = visible(this.#resourceTemplates);
804
821
  const prompts = visible(this.#prompts);
805
822
  for (const { schemas, tool } of tools) {
823
+ const call = async (args, ctx) => {
824
+ const timeout = tool.timeoutMs ? { controller: new AbortController(), ms: tool.timeoutMs } : void 0;
825
+ const { context, release } = this.#makeContext(
826
+ auth,
827
+ ctx,
828
+ timeout?.controller.signal
829
+ );
830
+ try {
831
+ const result = await (timeout ? withTimeout(tool.execute(args, context), timeout, tool.name) : tool.execute(args, context));
832
+ return normalizeToolResult(result, tool.outputSchema !== void 0);
833
+ } catch (error) {
834
+ if (error instanceof UserError) {
835
+ return {
836
+ content: [{ text: error.message, type: "text" }],
837
+ isError: true,
838
+ ...error.extras ? { structuredContent: error.extras } : {}
839
+ };
840
+ }
841
+ throw error;
842
+ } finally {
843
+ release();
844
+ }
845
+ };
806
846
  server.registerTool(
807
847
  tool.name,
808
848
  {
@@ -812,29 +852,12 @@ var ViteMCP = class {
812
852
  inputSchema: schemas.inputSchema,
813
853
  outputSchema: schemas.outputSchema
814
854
  },
815
- (async (args, ctx) => {
816
- const timeout = tool.timeoutMs ? { controller: new AbortController(), ms: tool.timeoutMs } : void 0;
817
- const { context, release } = this.#makeContext(
818
- auth,
819
- ctx,
820
- timeout?.controller.signal
821
- );
822
- try {
823
- const result = await (timeout ? withTimeout(tool.execute(args, context), timeout, tool.name) : tool.execute(args, context));
824
- return normalizeToolResult(result, tool.outputSchema !== void 0);
825
- } catch (error) {
826
- if (error instanceof UserError) {
827
- return {
828
- content: [{ text: error.message, type: "text" }],
829
- isError: true,
830
- ...error.extras ? { structuredContent: error.extras } : {}
831
- };
832
- }
833
- throw error;
834
- } finally {
835
- release();
836
- }
837
- })
855
+ // The SDK passes arguments only to a tool that declares an input
856
+ // schema; one that does not is called with the request context alone.
857
+ // Registering `call` as it stands would take that context for `args`
858
+ // and leave the tool without one: no progress, no logs, no request id
859
+ // and a `signal` that never fires.
860
+ schemas.inputSchema ? call : (ctx) => call(void 0, ctx)
838
861
  );
839
862
  }
840
863
  for (const resource of resources) {
@@ -902,31 +925,32 @@ var ViteMCP = class {
902
925
  );
903
926
  }
904
927
  for (const prompt of prompts) {
928
+ const argsSchema = promptArgsSchema(prompt);
929
+ const load = async (args, ctx) => {
930
+ const { context, release } = this.#makeContext(auth, ctx);
931
+ try {
932
+ const loaded = await prompt.load(args ?? {}, context);
933
+ if (typeof loaded !== "string") {
934
+ return loaded;
935
+ }
936
+ return {
937
+ messages: [
938
+ {
939
+ content: { text: loaded, type: "text" },
940
+ role: "user"
941
+ }
942
+ ]
943
+ };
944
+ } finally {
945
+ release();
946
+ }
947
+ };
905
948
  server.registerPrompt(
906
949
  prompt.name,
907
- {
908
- argsSchema: promptArgsSchema(prompt),
909
- description: prompt.description
910
- },
911
- (async (args, ctx) => {
912
- const { context, release } = this.#makeContext(auth, ctx);
913
- try {
914
- const loaded = await prompt.load(args ?? {}, context);
915
- if (typeof loaded !== "string") {
916
- return loaded;
917
- }
918
- return {
919
- messages: [
920
- {
921
- content: { text: loaded, type: "text" },
922
- role: "user"
923
- }
924
- ]
925
- };
926
- } finally {
927
- release();
928
- }
929
- })
950
+ { argsSchema, description: prompt.description },
951
+ // As for tools: without `argsSchema` the SDK passes the request
952
+ // context alone.
953
+ argsSchema ? load : (ctx) => load(void 0, ctx)
930
954
  );
931
955
  }
932
956
  if (this.#tools.length > 0 && tools.length === 0) {
@@ -1143,7 +1167,7 @@ var ViteMCP = class {
1143
1167
  proxy: oauth.proxy
1144
1168
  })
1145
1169
  );
1146
- this.#servesProtectedResourceMetadata = Boolean(oauth.protectedResource);
1170
+ this.#protectedResource = oauth.protectedResource;
1147
1171
  }
1148
1172
  const health = this.#options.health;
1149
1173
  if (health?.enabled !== false) {
@@ -1202,7 +1226,7 @@ var ViteMCP = class {
1202
1226
  outgoing: res
1203
1227
  }
1204
1228
  );
1205
- res.writeHead(response.status, Object.fromEntries(response.headers));
1229
+ res.writeHead(response.status, toNodeHeaders(response.headers));
1206
1230
  if (!response.body) {
1207
1231
  res.end();
1208
1232
  return;
@@ -1260,43 +1284,18 @@ var ViteMCP = class {
1260
1284
  );
1261
1285
  }
1262
1286
  /**
1263
- * Gives the SDK a JSON Schema for a schema that has none of its own. Zod v4
1264
- * and ArkType emit JSON Schema natively and pass through; anything else
1265
- * (Valibot, say) is converted, and the conversion is only what `tools/list`
1266
- * advertises. Validation stays with the library the schema was written in:
1267
- * the conversion drops what JSON Schema cannot say — a transform, a
1268
- * `check()` — so validating against it instead would pass `execute`
1269
- * arguments the schema never produced.
1270
- *
1271
- * Advertised inputs are closed with `strictInputSchema`. Outputs are left as
1272
- * converted, since closing them would misdescribe results the schema allows.
1287
+ * A tool schema in the form the SDK registers it — see `toSdkSchema` — with
1288
+ * a failed conversion reported under the tool's own name.
1273
1289
  */
1274
1290
  async #toSdkSchema(schema, label, io) {
1275
- if (!schema) {
1276
- return void 0;
1277
- }
1278
- const standard = schema["~standard"];
1279
- if (standard.jsonSchema) {
1280
- return schema;
1281
- }
1282
- let json;
1283
1291
  try {
1284
- json = await convertToJsonSchema(schema, io);
1292
+ return await toSdkSchema(schema, io, convertToJsonSchema);
1285
1293
  } catch (error) {
1286
1294
  throw new UnexpectedStateError(
1287
1295
  `${label} is not a supported Standard Schema: ${error instanceof Error ? error.message : String(error)}`,
1288
1296
  { label }
1289
1297
  );
1290
1298
  }
1291
- const advertised = io === "input" ? strictInputSchema(json) : json;
1292
- return {
1293
- "~standard": {
1294
- jsonSchema: { input: () => advertised, output: () => advertised },
1295
- validate: (value) => standard.validate(value),
1296
- vendor: standard.vendor,
1297
- version: standard.version
1298
- }
1299
- };
1300
1299
  }
1301
1300
  /**
1302
1301
  * The RFC 6750 §3 challenge sent when `authenticate` refuses a request.
@@ -1304,26 +1303,34 @@ var ViteMCP = class {
1304
1303
  * `resource_metadata` (RFC 9728 §5.1) is the parameter that turns a bare
1305
1304
  * rejection into a login: it points the client at this server's
1306
1305
  * protected-resource document, from which it discovers the authorization
1307
- * server and starts the flow. It is derived from the URL the request
1308
- * arrived on, which is exactly where `createOAuthRouter` mounts that
1309
- * document, and is omitted when this server publishes no such document.
1306
+ * server and starts the flow. It is omitted when this server publishes no
1307
+ * such document.
1308
+ *
1309
+ * The URL is formed from the resource that document names: the address the
1310
+ * server was configured as, which is the one a client can reach. The
1311
+ * request's own URL is not that address behind a proxy that terminates TLS —
1312
+ * it carries the listener's scheme and whatever Host was forwarded, so a
1313
+ * challenge built from it sent clients to `http://`, or to an internal host.
1314
+ * It is the fallback only, for a document that names no resource.
1310
1315
  */
1311
1316
  #unauthorized(request) {
1317
+ const published = this.#protectedResource;
1312
1318
  return bearerAuthChallengeResponse(
1313
1319
  new OAuthError(OAuthErrorCode.InvalidToken, "Authentication required"),
1314
- this.#servesProtectedResourceMetadata ? {
1320
+ published ? {
1315
1321
  resourceMetadataUrl: getOAuthProtectedResourceMetadataUrl(
1316
- new URL(request.url)
1322
+ toUrl(published.resource) ?? new URL(request.url)
1317
1323
  )
1318
1324
  } : {}
1319
1325
  );
1320
1326
  }
1321
1327
  };
1328
+ var toUrl = (value) => typeof value === "string" && URL.canParse(value) ? new URL(value) : void 0;
1322
1329
  var withEndpointAsResource = (config, endpoint) => ({
1323
1330
  ...config,
1324
1331
  protectedResource: {
1325
1332
  ...config.protectedResource,
1326
- resource: `${config.protectedResource.resource}${endpoint}`
1333
+ resource: `${withoutTrailingSlash(config.protectedResource.resource)}${endpoint}`
1327
1334
  }
1328
1335
  });
1329
1336
  var nodeToWebRequest = (req, body, secure, signal) => {
@@ -1351,6 +1358,13 @@ var nodeToWebRequest = (req, body, secure, signal) => {
1351
1358
  ...hasBody ? { body } : {}
1352
1359
  });
1353
1360
  };
1361
+ var toNodeHeaders = (headers) => {
1362
+ const cookies = headers.getSetCookie();
1363
+ return {
1364
+ ...Object.fromEntries(headers),
1365
+ ...cookies.length > 0 ? { "set-cookie": cookies } : {}
1366
+ };
1367
+ };
1354
1368
  var clientDisconnectSignal = (res) => {
1355
1369
  const controller = new AbortController();
1356
1370
  const abortIfPremature = () => {
@@ -1394,15 +1408,6 @@ var requestScope = (...sources) => {
1394
1408
  }
1395
1409
  return { release: () => detach.abort(), signal: controller.signal };
1396
1410
  };
1397
- var formatBytes = (bytes) => {
1398
- if (bytes >= 1024 * 1024 && bytes % (1024 * 1024) === 0) {
1399
- return `${bytes / (1024 * 1024)} MiB`;
1400
- }
1401
- if (bytes >= 1024 && bytes % 1024 === 0) {
1402
- return `${bytes / 1024} KiB`;
1403
- }
1404
- return `${bytes} bytes`;
1405
- };
1406
1411
  var OVERSIZE = /* @__PURE__ */ Symbol("oversize");
1407
1412
  var readCappedNodeBody = async (req, maxBytes) => {
1408
1413
  const declared = Number(req.headers["content-length"] ?? 0);
@@ -1421,35 +1426,7 @@ var readCappedNodeBody = async (req, maxBytes) => {
1421
1426
  }
1422
1427
  return Buffer.concat(chunks);
1423
1428
  };
1424
- var convertToJsonSchema = async (schema, io) => {
1425
- if (schema["~standard"].vendor !== "valibot") {
1426
- return toJsonSchema(schema);
1427
- }
1428
- let converter;
1429
- try {
1430
- converter = await import("@valibot/to-json-schema");
1431
- } catch {
1432
- throw new Error(
1433
- 'The "@valibot/to-json-schema" package is required to describe Valibot schemas. Install it with: npm install @valibot/to-json-schema'
1434
- );
1435
- }
1436
- return converter.toJsonSchema(schema, {
1437
- errorMode: "ignore",
1438
- typeMode: io
1439
- });
1440
- };
1441
- var strictInputSchema = (schema) => ({
1442
- ...schema,
1443
- additionalProperties: typeof schema.additionalProperties === "object" ? schema.additionalProperties : false,
1444
- ...schema.properties && {
1445
- properties: Object.fromEntries(
1446
- Object.entries(schema.properties).map(([key, value]) => [
1447
- key,
1448
- typeof value === "object" && value.type === "object" ? strictInputSchema(value) : value
1449
- ])
1450
- )
1451
- }
1452
- });
1429
+ var convertToJsonSchema = async (schema, io) => schema["~standard"].vendor === "valibot" ? valibotToJsonSchema(schema, io) : toJsonSchema(schema);
1453
1430
  var assertStandardSchema = (schema, toolName, field) => {
1454
1431
  if (schema === void 0) {
1455
1432
  return;
@@ -1555,4 +1532,4 @@ export {
1555
1532
  ServerState,
1556
1533
  ViteMCP
1557
1534
  };
1558
- //# sourceMappingURL=chunk-UFLW3ODH.js.map
1535
+ //# sourceMappingURL=chunk-DSDLJS6O.js.map