fastmcp 4.21.1 → 4.21.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.
@@ -1,6 +1,9 @@
1
1
  import {
2
2
  cancelResponseBody
3
3
  } from "./chunk-AR3LVWUF.js";
4
+ import {
5
+ strictInputSchema
6
+ } from "./chunk-UMDUIBFW.js";
4
7
 
5
8
  // src/FastMCP.ts
6
9
  import { Server } from "@modelcontextprotocol/sdk/server/index.js";
@@ -376,21 +379,6 @@ function assertToolSchemas(tool) {
376
379
  assertStandardSchema(tool.name, "outputSchema", tool.outputSchema);
377
380
  }
378
381
  }
379
- function strictInputSchema(schema) {
380
- const isDictionary = typeof schema.additionalProperties === "object" && Object.keys(schema.properties ?? {}).length === 0;
381
- return {
382
- ...schema,
383
- additionalProperties: isDictionary ? schema.additionalProperties : false,
384
- ...schema.properties && {
385
- properties: Object.fromEntries(
386
- Object.entries(schema.properties).map(([key, value]) => [
387
- key,
388
- typeof value === "object" && value.type === "object" ? strictInputSchema(value) : value
389
- ])
390
- )
391
- }
392
- };
393
- }
394
382
  var STREAM_KEEPALIVE_LOGGER = "fastmcp-keepalive";
395
383
  var STREAM_KEEPALIVE_DEFAULT_INTERVAL_MS = 2e4;
396
384
  var TextContentZodSchema = z.object({
@@ -537,6 +525,7 @@ var FastMCPSession = class extends FastMCPSessionEventEmitter {
537
525
  */
538
526
  #abortController = new AbortController();
539
527
  #auth;
528
+ #authRequired;
540
529
  #capabilities = {};
541
530
  #clientCapabilities;
542
531
  #connectionState = "connecting";
@@ -575,6 +564,7 @@ var FastMCPSession = class extends FastMCPSessionEventEmitter {
575
564
  #utils;
576
565
  constructor({
577
566
  auth,
567
+ authRequired = false,
578
568
  hasTools,
579
569
  icons,
580
570
  instructions,
@@ -598,6 +588,7 @@ var FastMCPSession = class extends FastMCPSessionEventEmitter {
598
588
  }) {
599
589
  super();
600
590
  this.#auth = auth;
591
+ this.#authRequired = authRequired;
601
592
  this.#logger = logger;
602
593
  this.#onToolCall = onToolCall;
603
594
  this.#pingConfig = ping;
@@ -804,7 +795,9 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
804
795
  }
805
796
  }
806
797
  toolsListChanged(tools) {
807
- this.setupToolHandlers(toolsVisibleTo(tools, this.#auth));
798
+ this.setupToolHandlers(
799
+ toolsVisibleTo(tools, this.#auth, this.#authRequired)
800
+ );
808
801
  this.triggerListChangedNotification("notifications/tools/list_changed");
809
802
  }
810
803
  async triggerListChangedNotification(method) {
@@ -1067,9 +1060,10 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1067
1060
  const ref = request.params.ref;
1068
1061
  const prompt = "name" in ref && this.#prompts.get(ref.name);
1069
1062
  if (!prompt) {
1070
- throw new UnexpectedStateError("Unknown prompt", {
1071
- request
1072
- });
1063
+ throw new McpError(
1064
+ ErrorCode.InvalidParams,
1065
+ `Unknown prompt: ${ref.name}`
1066
+ );
1073
1067
  }
1074
1068
  if (!prompt.complete) {
1075
1069
  throw new UnexpectedStateError("Prompt does not support completion", {
@@ -1095,9 +1089,10 @@ ${error instanceof Error ? error.stack : JSON.stringify(error)}`
1095
1089
  (resource2) => resource2.uriTemplate === ref.uri
1096
1090
  );
1097
1091
  if (!resource) {
1098
- throw new UnexpectedStateError("Unknown resource", {
1099
- request
1100
- });
1092
+ throw new McpError(
1093
+ ErrorCode.InvalidParams,
1094
+ `Unknown resource template: ${ref.uri}`
1095
+ );
1101
1096
  }
1102
1097
  if (!("uriTemplate" in resource)) {
1103
1098
  throw new UnexpectedStateError("Unexpected resource");
@@ -1645,8 +1640,13 @@ function stripBasePath(path, basePath) {
1645
1640
  }
1646
1641
  return null;
1647
1642
  }
1648
- function toolsVisibleTo(tools, auth) {
1649
- return auth ? tools.filter((tool) => tool.canAccess ? tool.canAccess(auth) : true) : tools;
1643
+ function toolsVisibleTo(tools, auth, authRequired) {
1644
+ if (auth) {
1645
+ return tools.filter(
1646
+ (tool) => tool.canAccess ? tool.canAccess(auth) : true
1647
+ );
1648
+ }
1649
+ return authRequired ? tools.filter((tool) => !tool.canAccess) : tools;
1650
1650
  }
1651
1651
  var FastMCPEventEmitterBase = EventEmitter;
1652
1652
  var FastMCPEventEmitter = class extends FastMCPEventEmitterBase {
@@ -2027,6 +2027,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2027
2027
  if (config.transportType === "stdio") {
2028
2028
  const transport = new StdioServerTransport();
2029
2029
  let auth;
2030
+ const authRequired = this.#authenticate !== void 0;
2030
2031
  if (this.#authenticate) {
2031
2032
  try {
2032
2033
  auth = await this.#authenticate(
@@ -2038,9 +2039,15 @@ var FastMCP = class extends FastMCPEventEmitter {
2038
2039
  error instanceof Error ? error.message : String(error)
2039
2040
  );
2040
2041
  }
2042
+ if (!auth && this.#tools.some((tool) => tool.canAccess)) {
2043
+ this.#logger.warn(
2044
+ "[FastMCP warning] The stdio session has no auth, so tools gated by canAccess are hidden from it."
2045
+ );
2046
+ }
2041
2047
  }
2042
2048
  const session = new FastMCPSession({
2043
2049
  auth,
2050
+ authRequired,
2044
2051
  hasTools: this.#tools.length > 0,
2045
2052
  icons: this.#options.icons,
2046
2053
  instructions: this.#options.instructions,
@@ -2054,7 +2061,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2054
2061
  roots: this.#options.roots,
2055
2062
  streamKeepalive: this.#options.streamKeepalive,
2056
2063
  title: this.#options.title,
2057
- tools: toolsVisibleTo(this.#tools, auth),
2064
+ tools: toolsVisibleTo(this.#tools, auth, authRequired),
2058
2065
  transportType: "stdio",
2059
2066
  utils: this.#options.utils,
2060
2067
  version: this.#options.version,
@@ -2224,8 +2231,10 @@ var FastMCP = class extends FastMCPEventEmitter {
2224
2231
  const errorMessage = "error" in auth && typeof auth.error === "string" ? auth.error : "Authentication failed";
2225
2232
  throw this.#createUnauthorizedResponse(errorMessage);
2226
2233
  }
2234
+ const authRequired = this.#authenticate !== void 0;
2227
2235
  return new FastMCPSession({
2228
2236
  auth,
2237
+ authRequired,
2229
2238
  hasTools: this.#tools.length > 0,
2230
2239
  icons: this.#options.icons,
2231
2240
  instructions: this.#options.instructions,
@@ -2241,7 +2250,7 @@ var FastMCP = class extends FastMCPEventEmitter {
2241
2250
  stateless,
2242
2251
  streamKeepalive: this.#options.streamKeepalive,
2243
2252
  title: this.#options.title,
2244
- tools: toolsVisibleTo(this.#tools, auth),
2253
+ tools: toolsVisibleTo(this.#tools, auth, authRequired),
2245
2254
  transportType: "httpStream",
2246
2255
  utils: this.#options.utils,
2247
2256
  version: this.#options.version,
@@ -2937,4 +2946,4 @@ export {
2937
2946
  FastMCPSession,
2938
2947
  FastMCP
2939
2948
  };
2940
- //# sourceMappingURL=chunk-JNP6BG5L.js.map
2949
+ //# sourceMappingURL=chunk-QMRS4ZLD.js.map