fastmcp 4.28.4 → 4.29.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/README.md +21 -0
- package/dist/FastMCP.cjs +4 -4
- package/dist/FastMCP.d.cts +4 -2
- package/dist/FastMCP.d.ts +4 -2
- package/dist/FastMCP.js +3 -3
- package/dist/auth/index.cjs +2 -2
- package/dist/auth/index.js +1 -1
- package/dist/{chunk-DPJW5QEO.cjs → chunk-5EEESM57.cjs} +64 -92
- package/dist/chunk-5EEESM57.cjs.map +1 -0
- package/dist/{chunk-OCCYKYIM.cjs → chunk-FBDPX4H2.cjs} +30 -3
- package/dist/chunk-FBDPX4H2.cjs.map +1 -0
- package/dist/{chunk-B2QUCZGV.js → chunk-JCKJRUCE.js} +30 -3
- package/dist/chunk-JCKJRUCE.js.map +1 -0
- package/dist/{chunk-UFPH23EN.js → chunk-RWPENEKV.js} +5 -1
- package/dist/{chunk-UFPH23EN.js.map → chunk-RWPENEKV.js.map} +1 -1
- package/dist/{chunk-7IQ5QHVZ.cjs → chunk-UHJQUSUX.cjs} +5 -1
- package/dist/chunk-UHJQUSUX.cjs.map +1 -0
- package/dist/{chunk-Q57EM4LH.js → chunk-VN5SZ6IE.js} +31 -59
- package/dist/chunk-VN5SZ6IE.js.map +1 -0
- package/dist/edge/index.cjs +15 -4
- package/dist/edge/index.cjs.map +1 -1
- package/dist/edge/index.d.cts +4 -0
- package/dist/edge/index.d.ts +4 -0
- package/dist/edge/index.js +13 -2
- package/dist/edge/index.js.map +1 -1
- package/dist/examples/custom-routes.cjs +4 -4
- package/dist/examples/custom-routes.js +3 -3
- package/dist/openapi/index.cjs +145 -132
- package/dist/openapi/index.cjs.map +1 -1
- package/dist/openapi/index.js +136 -123
- package/dist/openapi/index.js.map +1 -1
- package/package.json +3 -3
- package/dist/chunk-7IQ5QHVZ.cjs.map +0 -1
- package/dist/chunk-B2QUCZGV.js.map +0 -1
- package/dist/chunk-DPJW5QEO.cjs.map +0 -1
- package/dist/chunk-OCCYKYIM.cjs.map +0 -1
- package/dist/chunk-Q57EM4LH.js.map +0 -1
package/dist/openapi/index.js
CHANGED
|
@@ -2,9 +2,9 @@ import {
|
|
|
2
2
|
FastMCP,
|
|
3
3
|
UserError,
|
|
4
4
|
jsonSchemaAdapter
|
|
5
|
-
} from "../chunk-
|
|
6
|
-
import "../chunk-
|
|
7
|
-
import "../chunk-
|
|
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";
|
|
@@ -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
|
-
|
|
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
|
);
|
|
@@ -517,109 +518,24 @@ function slugify(value) {
|
|
|
517
518
|
|
|
518
519
|
// src/openapi/requestBuilder.ts
|
|
519
520
|
async function executeRequest(options) {
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
);
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
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
|
-
);
|
|
521
|
+
return readTextResponse(await fetchResponse(options));
|
|
522
|
+
}
|
|
523
|
+
async function executeResourceRequest(options) {
|
|
524
|
+
const response = await fetchResponse(options);
|
|
525
|
+
const mimeType = response.headers.get("content-type")?.split(";")[0].trim().toLowerCase();
|
|
526
|
+
if (mimeType && !mimeType.startsWith("text/") && !mimeType.includes("json") && !/(?:\/|\+)(?:x-)?(?:xml|yaml|javascript|ecmascript)$/.test(mimeType) && mimeType !== "application/x-www-form-urlencoded") {
|
|
527
|
+
return {
|
|
528
|
+
blob: Buffer.from(await response.arrayBuffer()).toString("base64"),
|
|
529
|
+
mimeType
|
|
530
|
+
};
|
|
612
531
|
}
|
|
613
|
-
const
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
return { text };
|
|
620
|
-
}
|
|
532
|
+
const { text } = await readTextResponse(response);
|
|
533
|
+
try {
|
|
534
|
+
JSON.parse(text);
|
|
535
|
+
return { mimeType: "application/json", text };
|
|
536
|
+
} catch {
|
|
537
|
+
return { mimeType: "text/plain", text };
|
|
621
538
|
}
|
|
622
|
-
return { text };
|
|
623
539
|
}
|
|
624
540
|
function resolveBaseUrl(servers, origin, overrideUrl) {
|
|
625
541
|
if (overrideUrl) {
|
|
@@ -765,6 +681,115 @@ function encodePathValue(value, style, explode) {
|
|
|
765
681
|
}
|
|
766
682
|
return encodeURIComponent(String(value));
|
|
767
683
|
}
|
|
684
|
+
async function fetchResponse(options) {
|
|
685
|
+
const baseUrl = resolveBaseUrl(
|
|
686
|
+
options.servers,
|
|
687
|
+
options.origin,
|
|
688
|
+
options.baseUrlOverride
|
|
689
|
+
);
|
|
690
|
+
const pathParams = /* @__PURE__ */ Object.create(null);
|
|
691
|
+
const queryParts = [];
|
|
692
|
+
const headers = new Headers(
|
|
693
|
+
await resolveHeaders(options.headers, options.context)
|
|
694
|
+
);
|
|
695
|
+
const bodyProps = /* @__PURE__ */ Object.create(null);
|
|
696
|
+
for (const [key, value] of Object.entries(options.args)) {
|
|
697
|
+
const mapping = options.parameterMap[key];
|
|
698
|
+
if (!mapping || value === void 0) {
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
if (value === null && (mapping.in === "query" || mapping.in === "header" || mapping.in === "cookie")) {
|
|
702
|
+
continue;
|
|
703
|
+
}
|
|
704
|
+
switch (mapping.in) {
|
|
705
|
+
case "body":
|
|
706
|
+
bodyProps[mapping.name] = value;
|
|
707
|
+
break;
|
|
708
|
+
case "cookie": {
|
|
709
|
+
const pair = `${mapping.name}=${encodeCookieValue(mapping, value)}`;
|
|
710
|
+
const existing = headers.get("cookie");
|
|
711
|
+
headers.set("cookie", existing ? `${existing}; ${pair}` : pair);
|
|
712
|
+
break;
|
|
713
|
+
}
|
|
714
|
+
case "header":
|
|
715
|
+
headers.set(
|
|
716
|
+
mapping.name,
|
|
717
|
+
serializeHeaderValue(value, mapping.style, mapping.explode)
|
|
718
|
+
);
|
|
719
|
+
break;
|
|
720
|
+
case "path":
|
|
721
|
+
pathParams[mapping.name] = encodePathValue(
|
|
722
|
+
value,
|
|
723
|
+
mapping.style,
|
|
724
|
+
mapping.explode
|
|
725
|
+
);
|
|
726
|
+
break;
|
|
727
|
+
case "query": {
|
|
728
|
+
const query = new URLSearchParams();
|
|
729
|
+
appendQueryValue(
|
|
730
|
+
query,
|
|
731
|
+
mapping.name,
|
|
732
|
+
mapping.style,
|
|
733
|
+
value,
|
|
734
|
+
mapping.explode
|
|
735
|
+
);
|
|
736
|
+
if (query.size > 0) {
|
|
737
|
+
queryParts.push(serializeQuery(query, mapping.allowReserved));
|
|
738
|
+
}
|
|
739
|
+
break;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
let path = options.route.path;
|
|
744
|
+
for (const [name, value] of Object.entries(pathParams)) {
|
|
745
|
+
path = path.replaceAll(`{${name}}`, value);
|
|
746
|
+
}
|
|
747
|
+
assertNoSubstitutedDotSegments(options.route, path);
|
|
748
|
+
const url = new URL(baseUrl.replace(/\/$/, "") + path);
|
|
749
|
+
url.search = queryParts.join("&");
|
|
750
|
+
let body;
|
|
751
|
+
const requiredJsonBody = options.bodyEncoding === "json" && options.route.requestBody?.required === true && !options.wholeBodyKey;
|
|
752
|
+
if (Object.keys(bodyProps).length > 0 || requiredJsonBody) {
|
|
753
|
+
const payload = options.wholeBodyKey ? bodyProps[options.wholeBodyKey] : bodyProps;
|
|
754
|
+
if (options.bodyEncoding === "form") {
|
|
755
|
+
if (!headers.has("content-type")) {
|
|
756
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
757
|
+
}
|
|
758
|
+
body = encodeFormBody(payload);
|
|
759
|
+
} else {
|
|
760
|
+
if (!headers.has("content-type")) {
|
|
761
|
+
headers.set("content-type", "application/json");
|
|
762
|
+
}
|
|
763
|
+
body = JSON.stringify(payload);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
const response = await options.fetchImpl(url.toString(), {
|
|
767
|
+
body,
|
|
768
|
+
headers,
|
|
769
|
+
method: options.route.method.toUpperCase(),
|
|
770
|
+
signal: options.signal
|
|
771
|
+
});
|
|
772
|
+
if (!response.ok) {
|
|
773
|
+
const text = await response.text();
|
|
774
|
+
throw new UserError(
|
|
775
|
+
`${options.route.method.toUpperCase()} ${path} failed with ${response.status}: ${text.slice(0, 2e3)}`
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
return response;
|
|
779
|
+
}
|
|
780
|
+
async function readTextResponse(response) {
|
|
781
|
+
const text = await response.text();
|
|
782
|
+
const mediaType = response.headers.get("content-type")?.split(";")[0];
|
|
783
|
+
if (mediaType?.toLowerCase().includes("json")) {
|
|
784
|
+
try {
|
|
785
|
+
const json = JSON.parse(text);
|
|
786
|
+
return { json, text: JSON.stringify(json, null, 2) };
|
|
787
|
+
} catch {
|
|
788
|
+
return { text };
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
return { text };
|
|
792
|
+
}
|
|
768
793
|
async function resolveHeaders(headers, context) {
|
|
769
794
|
if (!headers) {
|
|
770
795
|
return {};
|
|
@@ -1086,9 +1111,7 @@ function registerResource(server, route, name, parameterMap, requiredKeys, execO
|
|
|
1086
1111
|
if (mapping.kind === "resource") {
|
|
1087
1112
|
server.addResource({
|
|
1088
1113
|
description,
|
|
1089
|
-
load: async (_auth, context) =>
|
|
1090
|
-
await executeRequest({ ...execOptions, args: {}, context })
|
|
1091
|
-
),
|
|
1114
|
+
load: async (_auth, context) => executeResourceRequest({ ...execOptions, args: {}, context }),
|
|
1092
1115
|
name,
|
|
1093
1116
|
uri: mapping.uri
|
|
1094
1117
|
});
|
|
@@ -1097,13 +1120,11 @@ function registerResource(server, route, name, parameterMap, requiredKeys, execO
|
|
|
1097
1120
|
server.addResourceTemplate({
|
|
1098
1121
|
arguments: mapping.args,
|
|
1099
1122
|
description,
|
|
1100
|
-
load: async (args, _auth, context) =>
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
})
|
|
1106
|
-
),
|
|
1123
|
+
load: async (args, _auth, context) => executeResourceRequest({
|
|
1124
|
+
...execOptions,
|
|
1125
|
+
args,
|
|
1126
|
+
context
|
|
1127
|
+
}),
|
|
1107
1128
|
name,
|
|
1108
1129
|
uriTemplate: mapping.uriTemplate
|
|
1109
1130
|
});
|
|
@@ -1121,14 +1142,6 @@ async function resolveToolResult(result, outputSchema) {
|
|
|
1121
1142
|
}
|
|
1122
1143
|
return { content: [{ text, type: "text" }], isError: true };
|
|
1123
1144
|
}
|
|
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
1145
|
export {
|
|
1133
1146
|
fromOpenAPI
|
|
1134
1147
|
};
|