fastmcp 4.28.4 → 4.29.1

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 (39) hide show
  1. package/README.md +21 -0
  2. package/dist/FastMCP.cjs +4 -4
  3. package/dist/FastMCP.d.cts +4 -2
  4. package/dist/FastMCP.d.ts +4 -2
  5. package/dist/FastMCP.js +3 -3
  6. package/dist/auth/index.cjs +2 -2
  7. package/dist/auth/index.js +1 -1
  8. package/dist/{chunk-DPJW5QEO.cjs → chunk-5EEESM57.cjs} +64 -92
  9. package/dist/chunk-5EEESM57.cjs.map +1 -0
  10. package/dist/{chunk-OCCYKYIM.cjs → chunk-FBDPX4H2.cjs} +30 -3
  11. package/dist/chunk-FBDPX4H2.cjs.map +1 -0
  12. package/dist/{chunk-B2QUCZGV.js → chunk-JCKJRUCE.js} +30 -3
  13. package/dist/chunk-JCKJRUCE.js.map +1 -0
  14. package/dist/{chunk-UFPH23EN.js → chunk-RWPENEKV.js} +5 -1
  15. package/dist/{chunk-UFPH23EN.js.map → chunk-RWPENEKV.js.map} +1 -1
  16. package/dist/{chunk-7IQ5QHVZ.cjs → chunk-UHJQUSUX.cjs} +5 -1
  17. package/dist/chunk-UHJQUSUX.cjs.map +1 -0
  18. package/dist/{chunk-Q57EM4LH.js → chunk-VN5SZ6IE.js} +31 -59
  19. package/dist/chunk-VN5SZ6IE.js.map +1 -0
  20. package/dist/edge/index.cjs +15 -4
  21. package/dist/edge/index.cjs.map +1 -1
  22. package/dist/edge/index.d.cts +4 -0
  23. package/dist/edge/index.d.ts +4 -0
  24. package/dist/edge/index.js +13 -2
  25. package/dist/edge/index.js.map +1 -1
  26. package/dist/examples/custom-routes.cjs +4 -4
  27. package/dist/examples/custom-routes.js +3 -3
  28. package/dist/openapi/index.cjs +160 -135
  29. package/dist/openapi/index.cjs.map +1 -1
  30. package/dist/openapi/index.d.cts +10 -0
  31. package/dist/openapi/index.d.ts +10 -0
  32. package/dist/openapi/index.js +151 -126
  33. package/dist/openapi/index.js.map +1 -1
  34. package/package.json +3 -3
  35. package/dist/chunk-7IQ5QHVZ.cjs.map +0 -1
  36. package/dist/chunk-B2QUCZGV.js.map +0 -1
  37. package/dist/chunk-DPJW5QEO.cjs.map +0 -1
  38. package/dist/chunk-OCCYKYIM.cjs.map +0 -1
  39. package/dist/chunk-Q57EM4LH.js.map +0 -1
@@ -2,9 +2,9 @@ import {
2
2
  FastMCP,
3
3
  UserError,
4
4
  jsonSchemaAdapter
5
- } from "../chunk-Q57EM4LH.js";
6
- import "../chunk-UFPH23EN.js";
7
- import "../chunk-B2QUCZGV.js";
5
+ } from "../chunk-VN5SZ6IE.js";
6
+ import "../chunk-RWPENEKV.js";
7
+ import "../chunk-JCKJRUCE.js";
8
8
 
9
9
  // src/openapi/loadSpec.ts
10
10
  import SwaggerParser from "@apidevtools/swagger-parser";
@@ -65,11 +65,11 @@ function buildFlatSchema(route, sharedDefs) {
65
65
  for (const param of occurrences) {
66
66
  const base = collides ? `${name}__${param.in}` : name;
67
67
  const key = collides ? allocateAlias(base) : name;
68
- properties[key] = rewriteComponentRefs(
69
- param.schema ?? { type: "string" }
70
- );
68
+ const input = parameterInput(param);
69
+ properties[key] = rewriteComponentRefs(input.schema);
71
70
  parameterMap[key] = {
72
71
  allowReserved: param.allowReserved,
72
+ ...input.encoding ? { encoding: input.encoding } : {},
73
73
  explode: param.explode,
74
74
  in: param.in,
75
75
  name,
@@ -220,7 +220,8 @@ function extractBodyProperties(requestBody, sharedDefs) {
220
220
  }
221
221
  const schema = bodyEncoding === "form" ? resolveComponentRef(declaredSchema, sharedDefs) : declaredSchema;
222
222
  const schemaProperties = schema.properties;
223
- if (schema.type === "object" && schemaProperties && (bodyEncoding === "form" || schema.additionalProperties === false || Object.keys(schemaProperties).length > 0)) {
223
+ const composedJsonBody = bodyEncoding === "json" && ["allOf", "anyOf", "oneOf"].some((key) => Array.isArray(schema[key]));
224
+ if (!composedJsonBody && schema.type === "object" && schemaProperties && (bodyEncoding === "form" || schema.additionalProperties === false || Object.keys(schemaProperties).length > 0)) {
224
225
  const requiredNames = new Set(
225
226
  schema.required ?? []
226
227
  );
@@ -351,6 +352,13 @@ function omitReadOnlyRequired(value, mode) {
351
352
  }
352
353
  return node;
353
354
  }
355
+ function parameterInput(param) {
356
+ const json = param.schema === void 0 ? findMediaType(param.content, "application/json") : void 0;
357
+ if (!json) {
358
+ return { schema: param.schema ?? { type: "string" } };
359
+ }
360
+ return { encoding: "json", schema: json.schema ?? {} };
361
+ }
354
362
  function rewriteNode(value, mode, stripExamples = false) {
355
363
  if (mode === "data") {
356
364
  return value;
@@ -517,109 +525,24 @@ function slugify(value) {
517
525
 
518
526
  // src/openapi/requestBuilder.ts
519
527
  async function executeRequest(options) {
520
- const baseUrl = resolveBaseUrl(
521
- options.servers,
522
- options.origin,
523
- options.baseUrlOverride
524
- );
525
- const pathParams = /* @__PURE__ */ Object.create(null);
526
- const queryParts = [];
527
- const headers = new Headers(
528
- await resolveHeaders(options.headers, options.context)
529
- );
530
- const bodyProps = /* @__PURE__ */ Object.create(null);
531
- for (const [key, value] of Object.entries(options.args)) {
532
- const mapping = options.parameterMap[key];
533
- if (!mapping || value === void 0) {
534
- continue;
535
- }
536
- if (value === null && (mapping.in === "query" || mapping.in === "header" || mapping.in === "cookie")) {
537
- continue;
538
- }
539
- switch (mapping.in) {
540
- case "body":
541
- bodyProps[mapping.name] = value;
542
- break;
543
- case "cookie": {
544
- const pair = `${mapping.name}=${encodeCookieValue(mapping, value)}`;
545
- const existing = headers.get("cookie");
546
- headers.set("cookie", existing ? `${existing}; ${pair}` : pair);
547
- break;
548
- }
549
- case "header":
550
- headers.set(
551
- mapping.name,
552
- serializeHeaderValue(value, mapping.style, mapping.explode)
553
- );
554
- break;
555
- case "path":
556
- pathParams[mapping.name] = encodePathValue(
557
- value,
558
- mapping.style,
559
- mapping.explode
560
- );
561
- break;
562
- case "query": {
563
- const query = new URLSearchParams();
564
- appendQueryValue(
565
- query,
566
- mapping.name,
567
- mapping.style,
568
- value,
569
- mapping.explode
570
- );
571
- if (query.size > 0) {
572
- queryParts.push(serializeQuery(query, mapping.allowReserved));
573
- }
574
- break;
575
- }
576
- }
577
- }
578
- let path = options.route.path;
579
- for (const [name, value] of Object.entries(pathParams)) {
580
- path = path.replaceAll(`{${name}}`, value);
581
- }
582
- assertNoSubstitutedDotSegments(options.route, path);
583
- const url = new URL(baseUrl.replace(/\/$/, "") + path);
584
- url.search = queryParts.join("&");
585
- let body;
586
- const requiredJsonBody = options.bodyEncoding === "json" && options.route.requestBody?.required === true && !options.wholeBodyKey;
587
- if (Object.keys(bodyProps).length > 0 || requiredJsonBody) {
588
- const payload = options.wholeBodyKey ? bodyProps[options.wholeBodyKey] : bodyProps;
589
- if (options.bodyEncoding === "form") {
590
- if (!headers.has("content-type")) {
591
- headers.set("content-type", "application/x-www-form-urlencoded");
592
- }
593
- body = encodeFormBody(payload);
594
- } else {
595
- if (!headers.has("content-type")) {
596
- headers.set("content-type", "application/json");
597
- }
598
- body = JSON.stringify(payload);
599
- }
600
- }
601
- const response = await options.fetchImpl(url.toString(), {
602
- body,
603
- headers,
604
- method: options.route.method.toUpperCase(),
605
- signal: options.signal
606
- });
607
- const text = await response.text();
608
- if (!response.ok) {
609
- throw new UserError(
610
- `${options.route.method.toUpperCase()} ${path} failed with ${response.status}: ${text.slice(0, 2e3)}`
611
- );
528
+ return readTextResponse(await fetchResponse(options));
529
+ }
530
+ async function executeResourceRequest(options) {
531
+ const response = await fetchResponse(options);
532
+ const mimeType = response.headers.get("content-type")?.split(";")[0].trim().toLowerCase();
533
+ if (mimeType && !mimeType.startsWith("text/") && !mimeType.includes("json") && !/(?:\/|\+)(?:x-)?(?:xml|yaml|javascript|ecmascript)$/.test(mimeType) && mimeType !== "application/x-www-form-urlencoded") {
534
+ return {
535
+ blob: Buffer.from(await response.arrayBuffer()).toString("base64"),
536
+ mimeType
537
+ };
612
538
  }
613
- const mediaType = response.headers.get("content-type")?.split(";")[0];
614
- if (mediaType?.toLowerCase().includes("json")) {
615
- try {
616
- const json = JSON.parse(text);
617
- return { json, text: JSON.stringify(json, null, 2) };
618
- } catch {
619
- return { text };
620
- }
539
+ const { text } = await readTextResponse(response);
540
+ try {
541
+ JSON.parse(text);
542
+ return { mimeType: "application/json", text };
543
+ } catch {
544
+ return { mimeType: "text/plain", text };
621
545
  }
622
- return { text };
623
546
  }
624
547
  function resolveBaseUrl(servers, origin, overrideUrl) {
625
548
  if (overrideUrl) {
@@ -765,6 +688,117 @@ function encodePathValue(value, style, explode) {
765
688
  }
766
689
  return encodeURIComponent(String(value));
767
690
  }
691
+ async function fetchResponse(options) {
692
+ const baseUrl = resolveBaseUrl(
693
+ options.servers,
694
+ options.origin,
695
+ options.baseUrlOverride
696
+ );
697
+ const pathParams = /* @__PURE__ */ Object.create(null);
698
+ const queryParts = [];
699
+ const headers = new Headers(
700
+ await resolveHeaders(options.headers, options.context)
701
+ );
702
+ const bodyProps = /* @__PURE__ */ Object.create(null);
703
+ for (const [key, value] of Object.entries(options.args)) {
704
+ const mapping = options.parameterMap[key];
705
+ if (!mapping || value === void 0) {
706
+ continue;
707
+ }
708
+ if (value === null && mapping.encoding !== "json" && (mapping.in === "query" || mapping.in === "header" || mapping.in === "cookie")) {
709
+ continue;
710
+ }
711
+ switch (mapping.in) {
712
+ case "body":
713
+ bodyProps[mapping.name] = value;
714
+ break;
715
+ case "cookie": {
716
+ const encoded = mapping.encoding === "json" ? encodePathComponent(JSON.stringify(value)) : encodeCookieValue(mapping, value);
717
+ const pair = `${mapping.name}=${encoded}`;
718
+ const existing = headers.get("cookie");
719
+ headers.set("cookie", existing ? `${existing}; ${pair}` : pair);
720
+ break;
721
+ }
722
+ case "header":
723
+ headers.set(
724
+ mapping.name,
725
+ mapping.encoding === "json" ? JSON.stringify(value) : serializeHeaderValue(value, mapping.style, mapping.explode)
726
+ );
727
+ break;
728
+ case "path":
729
+ pathParams[mapping.name] = mapping.encoding === "json" ? encodePathComponent(JSON.stringify(value)) : encodePathValue(value, mapping.style, mapping.explode);
730
+ break;
731
+ case "query": {
732
+ const query = new URLSearchParams();
733
+ if (mapping.encoding === "json") {
734
+ query.append(mapping.name, JSON.stringify(value));
735
+ queryParts.push(serializeQuery(query, false));
736
+ break;
737
+ }
738
+ appendQueryValue(
739
+ query,
740
+ mapping.name,
741
+ mapping.style,
742
+ value,
743
+ mapping.explode
744
+ );
745
+ if (query.size > 0) {
746
+ queryParts.push(serializeQuery(query, mapping.allowReserved));
747
+ }
748
+ break;
749
+ }
750
+ }
751
+ }
752
+ let path = options.route.path;
753
+ for (const [name, value] of Object.entries(pathParams)) {
754
+ path = path.replaceAll(`{${name}}`, value);
755
+ }
756
+ assertNoSubstitutedDotSegments(options.route, path);
757
+ const url = new URL(baseUrl.replace(/\/$/, "") + path);
758
+ url.search = queryParts.join("&");
759
+ let body;
760
+ const requiredJsonBody = options.bodyEncoding === "json" && options.route.requestBody?.required === true && !options.wholeBodyKey;
761
+ if (Object.keys(bodyProps).length > 0 || requiredJsonBody) {
762
+ const payload = options.wholeBodyKey ? bodyProps[options.wholeBodyKey] : bodyProps;
763
+ if (options.bodyEncoding === "form") {
764
+ if (!headers.has("content-type")) {
765
+ headers.set("content-type", "application/x-www-form-urlencoded");
766
+ }
767
+ body = encodeFormBody(payload);
768
+ } else {
769
+ if (!headers.has("content-type")) {
770
+ headers.set("content-type", "application/json");
771
+ }
772
+ body = JSON.stringify(payload);
773
+ }
774
+ }
775
+ const response = await options.fetchImpl(url.toString(), {
776
+ body,
777
+ headers,
778
+ method: options.route.method.toUpperCase(),
779
+ signal: options.signal
780
+ });
781
+ if (!response.ok) {
782
+ const text = await response.text();
783
+ throw new UserError(
784
+ `${options.route.method.toUpperCase()} ${path} failed with ${response.status}: ${text.slice(0, 2e3)}`
785
+ );
786
+ }
787
+ return response;
788
+ }
789
+ async function readTextResponse(response) {
790
+ const text = await response.text();
791
+ const mediaType = response.headers.get("content-type")?.split(";")[0];
792
+ if (mediaType?.toLowerCase().includes("json")) {
793
+ try {
794
+ const json = JSON.parse(text);
795
+ return { json, text: JSON.stringify(json, null, 2) };
796
+ } catch {
797
+ return { text };
798
+ }
799
+ }
800
+ return { text };
801
+ }
768
802
  async function resolveHeaders(headers, context) {
769
803
  if (!headers) {
770
804
  return {};
@@ -835,6 +869,9 @@ function isEligibleForResource(route, sharedDefs) {
835
869
  if (param.in === "header" || param.in === "cookie") {
836
870
  return false;
837
871
  }
872
+ if (param.content && param.schema === void 0) {
873
+ return false;
874
+ }
838
875
  return !hasStructuredType(param.schema ?? {}, sharedDefs);
839
876
  });
840
877
  }
@@ -1086,9 +1123,7 @@ function registerResource(server, route, name, parameterMap, requiredKeys, execO
1086
1123
  if (mapping.kind === "resource") {
1087
1124
  server.addResource({
1088
1125
  description,
1089
- load: async (_auth, context) => wrapAsResourceResult(
1090
- await executeRequest({ ...execOptions, args: {}, context })
1091
- ),
1126
+ load: async (_auth, context) => executeResourceRequest({ ...execOptions, args: {}, context }),
1092
1127
  name,
1093
1128
  uri: mapping.uri
1094
1129
  });
@@ -1097,13 +1132,11 @@ function registerResource(server, route, name, parameterMap, requiredKeys, execO
1097
1132
  server.addResourceTemplate({
1098
1133
  arguments: mapping.args,
1099
1134
  description,
1100
- load: async (args, _auth, context) => wrapAsResourceResult(
1101
- await executeRequest({
1102
- ...execOptions,
1103
- args,
1104
- context
1105
- })
1106
- ),
1135
+ load: async (args, _auth, context) => executeResourceRequest({
1136
+ ...execOptions,
1137
+ args,
1138
+ context
1139
+ }),
1107
1140
  name,
1108
1141
  uriTemplate: mapping.uriTemplate
1109
1142
  });
@@ -1121,14 +1154,6 @@ async function resolveToolResult(result, outputSchema) {
1121
1154
  }
1122
1155
  return { content: [{ text, type: "text" }], isError: true };
1123
1156
  }
1124
- function wrapAsResourceResult({ text }) {
1125
- try {
1126
- JSON.parse(text);
1127
- return { mimeType: "application/json", text };
1128
- } catch {
1129
- return { mimeType: "text/plain", text };
1130
- }
1131
- }
1132
1157
  export {
1133
1158
  fromOpenAPI
1134
1159
  };