mcp-use 2.0.0-beta.13 → 2.0.0-beta.15

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 (34) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/{chunk-XKJP5S7C.js → chunk-E7M6OYZ5.js} +5 -2
  3. package/dist/{chunk-XKJP5S7C.js.map → chunk-E7M6OYZ5.js.map} +1 -1
  4. package/dist/{chunk-STCR3P5O.js → chunk-XUI7QE2D.js} +4 -1
  5. package/dist/chunk-XUI7QE2D.js.map +1 -0
  6. package/dist/{chunk-JEVBOMCE.js → chunk-ZJJD33SF.js} +2 -2
  7. package/dist/chunk-ZJJD33SF.js.map +1 -0
  8. package/dist/cli/dev.d.ts.map +1 -1
  9. package/dist/cli/views-plugin.d.ts.map +1 -1
  10. package/dist/commands/build.js +2 -2
  11. package/dist/commands/dev.js +2 -1
  12. package/dist/commands/dev.js.map +1 -1
  13. package/dist/index.d.ts +2 -2
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +212 -41
  16. package/dist/index.js.map +1 -1
  17. package/dist/inspector-shell.d.ts.map +1 -1
  18. package/dist/{landing-handler-Z7WDR4S2.js → landing-handler-WE66NIGB.js} +2 -2
  19. package/dist/landing.js +1 -1
  20. package/dist/middleware/mcp-middleware.d.ts +142 -102
  21. package/dist/middleware/mcp-middleware.d.ts.map +1 -1
  22. package/dist/react/components/theme-provider.d.ts +3 -0
  23. package/dist/react/components/theme-provider.d.ts.map +1 -1
  24. package/dist/react/hooks/use-host-context.d.ts +5 -0
  25. package/dist/react/hooks/use-host-context.d.ts.map +1 -1
  26. package/dist/react/index.js +58 -1
  27. package/dist/react/index.js.map +1 -1
  28. package/dist/server.d.ts +6 -4
  29. package/dist/server.d.ts.map +1 -1
  30. package/dist/views/document.d.ts.map +1 -1
  31. package/package.json +4 -4
  32. package/dist/chunk-JEVBOMCE.js.map +0 -1
  33. package/dist/chunk-STCR3P5O.js.map +0 -1
  34. /package/dist/{landing-handler-Z7WDR4S2.js.map → landing-handler-WE66NIGB.js.map} +0 -0
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  } from "./chunk-KIZ72TIK.js";
12
12
  import {
13
13
  synthesizeViewDocument
14
- } from "./chunk-STCR3P5O.js";
14
+ } from "./chunk-XUI7QE2D.js";
15
15
  import {
16
16
  hasExplicitAssetsBase,
17
17
  originFromAssetsBase,
@@ -51,7 +51,7 @@ import {
51
51
  localhostAllowedOrigins,
52
52
  McpServer as SdkMcpServer,
53
53
  ResourceTemplate,
54
- isInputRequiredResult
54
+ isInputRequiredResult as isInputRequiredResult2
55
55
  } from "@modelcontextprotocol/server";
56
56
 
57
57
  // src/branding.ts
@@ -563,6 +563,72 @@ function isGlobalCorsEnabled(cors) {
563
563
  }
564
564
 
565
565
  // src/middleware/mcp-middleware.ts
566
+ import {
567
+ CallToolResultSchema,
568
+ GetPromptResultSchema,
569
+ ListPromptsResultSchema,
570
+ ListResourcesResultSchema,
571
+ ListToolsResultSchema,
572
+ ReadResourceResultSchema
573
+ } from "@modelcontextprotocol/core";
574
+ import { isInputRequiredResult } from "@modelcontextprotocol/server";
575
+ var MCP_MIDDLEWARE_METHODS = [
576
+ "tools/call",
577
+ "tools/list",
578
+ "resources/read",
579
+ "resources/list",
580
+ "prompts/get",
581
+ "prompts/list"
582
+ ];
583
+ function withMcpMiddlewareParams(request, params) {
584
+ if (typeof request !== "object" || request === null || Array.isArray(request)) {
585
+ throw new TypeError(
586
+ "[mcp-use] MCP middleware received an invalid downstream request"
587
+ );
588
+ }
589
+ return { ...request, params };
590
+ }
591
+ function createMcpMiddlewareEntry(pattern, handler) {
592
+ const normalizedPattern = normalizeMcpMiddlewarePattern(pattern);
593
+ const invoke = handler;
594
+ if (normalizedPattern !== "*" && !isMcpMiddlewareMethod(normalizedPattern)) {
595
+ throw new TypeError(
596
+ `Unsupported MCP middleware pattern "${pattern}". Use an exact MCP method or "mcp:*".`
597
+ );
598
+ }
599
+ if (normalizedPattern === "*") {
600
+ return {
601
+ pattern: normalizedPattern,
602
+ handler: async (ctx, next) => {
603
+ let downstreamCalled = false;
604
+ let downstreamResult;
605
+ await invoke(ctx, async () => {
606
+ downstreamResult = await next();
607
+ downstreamCalled = true;
608
+ });
609
+ if (!downstreamCalled) {
610
+ throw new Error(
611
+ `Wildcard MCP middleware "${normalizedPattern}" must call next()`
612
+ );
613
+ }
614
+ return downstreamResult;
615
+ }
616
+ };
617
+ }
618
+ return {
619
+ pattern: normalizedPattern,
620
+ handler: (ctx, next) => invoke(ctx, next)
621
+ };
622
+ }
623
+ function createMcpEventListenerEntry(pattern, handler) {
624
+ const { pattern: normalizedPattern, phase } = parseMcpPattern(pattern);
625
+ const invoke = handler;
626
+ return {
627
+ pattern: normalizedPattern,
628
+ phase,
629
+ handler: (ctx, result) => invoke(ctx, result)
630
+ };
631
+ }
566
632
  function matchesPattern(pattern, method) {
567
633
  if (pattern === "*") return true;
568
634
  if (pattern.endsWith("/*")) {
@@ -571,8 +637,13 @@ function matchesPattern(pattern, method) {
571
637
  }
572
638
  return pattern === method;
573
639
  }
640
+ function isMcpMiddlewareMethod(value) {
641
+ return MCP_MIDDLEWARE_METHODS.some((method) => method === value);
642
+ }
574
643
  function composeMiddleware(entries, method, innerFn) {
575
- const matching = entries.filter((e) => matchesPattern(e.pattern, method));
644
+ const matching = entries.filter(
645
+ (entry) => matchesPattern(entry.pattern, method)
646
+ );
576
647
  if (matching.length === 0) {
577
648
  return (_ctx) => innerFn();
578
649
  }
@@ -596,17 +667,69 @@ function freezeMiddlewareContext(ctx) {
596
667
  return Object.freeze({
597
668
  method: ctx.method,
598
669
  params: Object.freeze({ ...ctx.params }),
670
+ ...ctx.session !== void 0 && {
671
+ session: Object.freeze({ ...ctx.session })
672
+ },
599
673
  ...ctx.auth !== void 0 && { auth: ctx.auth },
600
- state: ctx.state
674
+ state: new Map(ctx.state)
601
675
  });
602
676
  }
603
677
  async function runMcpOperation(middlewares, events, method, ctx, innerFn) {
604
678
  dispatchMcpEvents(events, method, "before", ctx);
605
679
  const chained = composeMiddleware(middlewares, method, innerFn);
606
680
  const result = await chained(ctx);
681
+ assertValidMiddlewareResult(method, result);
607
682
  dispatchMcpEvents(events, method, "complete", ctx, result);
608
683
  return result;
609
684
  }
685
+ function assertValidMiddlewareResult(method, result) {
686
+ if ((method === "tools/call" || method === "resources/read" || method === "prompts/get") && isInputRequiredResult(result)) {
687
+ return;
688
+ }
689
+ const validation = (() => {
690
+ switch (method) {
691
+ case "tools/call":
692
+ assertArrayProperty(method, result, "content");
693
+ return CallToolResultSchema.safeParse(result);
694
+ case "tools/list":
695
+ assertArrayResult(method, result);
696
+ return ListToolsResultSchema.safeParse({ tools: result });
697
+ case "resources/read":
698
+ assertArrayProperty(method, result, "contents");
699
+ return ReadResourceResultSchema.safeParse(result);
700
+ case "resources/list":
701
+ assertArrayResult(method, result);
702
+ return ListResourcesResultSchema.safeParse({ resources: result });
703
+ case "prompts/get":
704
+ assertArrayProperty(method, result, "messages");
705
+ return GetPromptResultSchema.safeParse(result);
706
+ case "prompts/list":
707
+ assertArrayResult(method, result);
708
+ return ListPromptsResultSchema.safeParse({ prompts: result });
709
+ default:
710
+ throw new TypeError(`Unsupported MCP middleware method "${method}"`);
711
+ }
712
+ })();
713
+ if (!validation.success) {
714
+ throw new TypeError(
715
+ `[mcp-use] ${method} middleware returned an invalid result: ${validation.error.message}`
716
+ );
717
+ }
718
+ }
719
+ function assertArrayResult(method, result) {
720
+ if (!Array.isArray(result)) {
721
+ throw new TypeError(
722
+ `[mcp-use] ${method} middleware returned an invalid result: expected an array`
723
+ );
724
+ }
725
+ }
726
+ function assertArrayProperty(method, result, property) {
727
+ if (typeof result !== "object" || result === null || !Array.isArray(result[property])) {
728
+ throw new TypeError(
729
+ `[mcp-use] ${method} middleware returned an invalid result: expected a ${property} array`
730
+ );
731
+ }
732
+ }
610
733
  function dispatchMcpEvents(events, method, phase, ctx, result) {
611
734
  const frozen = freezeMiddlewareContext(ctx);
612
735
  for (const entry of events) {
@@ -614,11 +737,7 @@ function dispatchMcpEvents(events, method, phase, ctx, result) {
614
737
  continue;
615
738
  }
616
739
  try {
617
- if (phase === "before") {
618
- entry.handler(frozen);
619
- } else {
620
- entry.handler(frozen, result);
621
- }
740
+ entry.handler(frozen, result);
622
741
  } catch (error) {
623
742
  console.error(
624
743
  `[mcp-use] MCP event listener for "${entry.pattern}" (${phase}) threw:`,
@@ -716,6 +835,7 @@ function renderInspectorShell(options) {
716
835
  };
717
836
  // Read by the current inspector bundle to derive its own URLs.
718
837
  window.__MCP_BASE_PATH__ = basePath;
838
+ ${process.env.MCP_USE_DEV_CLI === "1" ? "window.__MCP_DEV_CLI__ = true;" : ""}
719
839
  ${serializedManufactChatUrl ? `window.__MANUFACT_CHAT_URL__ = ${serializedManufactChatUrl};` : ""}
720
840
  // The bundle carries Node-flavored dependencies that touch \`process\`
721
841
  // at module scope; give them the same browser polyfill the v1
@@ -1412,6 +1532,21 @@ function viewResourceConfig(_viewName, _entry, authorFacts, options) {
1412
1532
  }
1413
1533
 
1414
1534
  // src/server.ts
1535
+ function omitRootSchemaDialect(schema) {
1536
+ if (typeof schema !== "object" || schema === null || Array.isArray(schema) || !("$schema" in schema)) {
1537
+ return schema;
1538
+ }
1539
+ const emittedSchema = { ...schema };
1540
+ delete emittedSchema["$schema"];
1541
+ return emittedSchema;
1542
+ }
1543
+ function omitToolSchemaDialects(tools) {
1544
+ return tools.map((tool) => ({
1545
+ ...tool,
1546
+ inputSchema: omitRootSchemaDialect(tool.inputSchema),
1547
+ ...tool.outputSchema === void 0 ? {} : { outputSchema: omitRootSchemaDialect(tool.outputSchema) }
1548
+ }));
1549
+ }
1415
1550
  var MCPServer = class _MCPServer {
1416
1551
  #config;
1417
1552
  #branding;
@@ -1692,7 +1827,9 @@ var MCPServer = class _MCPServer {
1692
1827
  * Register MCP operation middleware that intercepts JSON-RPC dispatch.
1693
1828
  *
1694
1829
  * Patterns use an `mcp:` prefix (for example `mcp:tools/call`, `mcp:*`).
1695
- * Middleware runs in registration order; call `next()` to continue the chain.
1830
+ * Exact methods may transform their typed result. The global `mcp:*` pattern
1831
+ * is pass-through middleware. Middleware runs in registration order; call
1832
+ * `next()` to continue the chain.
1696
1833
  *
1697
1834
  * @example
1698
1835
  * ```ts
@@ -1704,10 +1841,7 @@ var MCPServer = class _MCPServer {
1704
1841
  */
1705
1842
  use(pattern, handler) {
1706
1843
  this.#assertNotStarted("middleware", pattern);
1707
- this.#mcpMiddlewares.push({
1708
- pattern: normalizeMcpMiddlewarePattern(pattern),
1709
- handler
1710
- });
1844
+ this.#mcpMiddlewares.push(createMcpMiddlewareEntry(pattern, handler));
1711
1845
  return this;
1712
1846
  }
1713
1847
  /**
@@ -1727,12 +1861,7 @@ var MCPServer = class _MCPServer {
1727
1861
  */
1728
1862
  on(pattern, handler) {
1729
1863
  this.#assertNotStarted("event listener", pattern);
1730
- const { pattern: stripped, phase } = parseMcpPattern(pattern);
1731
- this.#mcpEventListeners.push({
1732
- pattern: stripped,
1733
- phase,
1734
- handler
1735
- });
1864
+ this.#mcpEventListeners.push(createMcpEventListenerEntry(pattern, handler));
1736
1865
  return this;
1737
1866
  }
1738
1867
  /**
@@ -2076,7 +2205,7 @@ var MCPServer = class _MCPServer {
2076
2205
  return mcpRouteHandler(request);
2077
2206
  }
2078
2207
  const respond = async () => {
2079
- const { createLandingPageResponse } = await import("./landing-handler-Z7WDR4S2.js");
2208
+ const { createLandingPageResponse } = await import("./landing-handler-WE66NIGB.js");
2080
2209
  return createLandingPageResponse(
2081
2210
  request,
2082
2211
  basePath,
@@ -2287,6 +2416,9 @@ var MCPServer = class _MCPServer {
2287
2416
  return {
2288
2417
  method,
2289
2418
  params,
2419
+ ...ctx.sessionId !== void 0 && {
2420
+ session: { sessionId: ctx.sessionId }
2421
+ },
2290
2422
  ...ctx.http?.authInfo !== void 0 && { auth: ctx.http.authInfo },
2291
2423
  state: /* @__PURE__ */ new Map()
2292
2424
  };
@@ -2312,7 +2444,7 @@ var MCPServer = class _MCPServer {
2312
2444
  if (handlers === void 0) {
2313
2445
  return;
2314
2446
  }
2315
- const wrapListMethod = (method, resultKey) => {
2447
+ const wrapListMethod = (method, resultKey, transform) => {
2316
2448
  const original = handlers.get(method);
2317
2449
  if (original === void 0) {
2318
2450
  return;
@@ -2321,27 +2453,50 @@ var MCPServer = class _MCPServer {
2321
2453
  return;
2322
2454
  }
2323
2455
  const wrapped = async (request, extra) => {
2324
- const authInfo = extra?.http?.authInfo;
2325
- const mwCtx = {
2456
+ const serverContext = extra;
2457
+ const params = request.params ?? {};
2458
+ const mwCtx = this.#createMiddlewareContext(
2326
2459
  method,
2327
- params: {},
2328
- ...authInfo !== void 0 && { auth: authInfo },
2329
- state: /* @__PURE__ */ new Map()
2330
- };
2460
+ params,
2461
+ serverContext
2462
+ );
2463
+ let originalEnvelope;
2331
2464
  const innerFn = async () => {
2332
- const result = await original(request, extra);
2333
- return result[resultKey] ?? result;
2465
+ const downstreamRequest = withMcpMiddlewareParams(
2466
+ request,
2467
+ mwCtx.params
2468
+ );
2469
+ const result = await original(downstreamRequest, extra);
2470
+ if (typeof result !== "object" || result === null) {
2471
+ throw new TypeError(
2472
+ `[mcp-use] ${method} handler returned a non-object result`
2473
+ );
2474
+ }
2475
+ originalEnvelope = result;
2476
+ const items = originalEnvelope[resultKey];
2477
+ if (!Array.isArray(items)) {
2478
+ throw new TypeError(
2479
+ `[mcp-use] ${method} handler result is missing a ${resultKey} array`
2480
+ );
2481
+ }
2482
+ return items;
2334
2483
  };
2335
2484
  const filtered = await this.#runMcpHook(method, mwCtx, innerFn);
2336
- if (Array.isArray(filtered)) {
2337
- return { [resultKey]: filtered };
2485
+ if (!Array.isArray(filtered)) {
2486
+ throw new TypeError(
2487
+ `[mcp-use] ${method} middleware must return a ${resultKey} array`
2488
+ );
2338
2489
  }
2339
- return filtered;
2490
+ const emitted = transform?.(filtered) ?? filtered;
2491
+ return {
2492
+ ...originalEnvelope,
2493
+ [resultKey]: emitted
2494
+ };
2340
2495
  };
2341
2496
  wrapped.__mcpListWrapped = true;
2342
2497
  handlers.set(method, wrapped);
2343
2498
  };
2344
- wrapListMethod("tools/list", "tools");
2499
+ wrapListMethod("tools/list", "tools", omitToolSchemaDialects);
2345
2500
  wrapListMethod("resources/list", "resources");
2346
2501
  wrapListMethod("prompts/list", "prompts");
2347
2502
  }
@@ -2370,7 +2525,11 @@ var MCPServer = class _MCPServer {
2370
2525
  const invokeTool = async (args, ctx) => {
2371
2526
  const mwCtx = this.#createMiddlewareContext(
2372
2527
  "tools/call",
2373
- { name: definition.name, arguments: args },
2528
+ {
2529
+ name: definition.name,
2530
+ arguments: args,
2531
+ ...ctx.mcpReq._meta !== void 0 && { _meta: ctx.mcpReq._meta }
2532
+ },
2374
2533
  ctx
2375
2534
  );
2376
2535
  const innerFn = async () => {
@@ -2379,7 +2538,7 @@ var MCPServer = class _MCPServer {
2379
2538
  effectiveArgs,
2380
2539
  this.#toRequestContext(ctx)
2381
2540
  );
2382
- if (isInputRequiredResult(result) || wireResultMeta === void 0 || result.isError === true) {
2541
+ if (isInputRequiredResult2(result) || wireResultMeta === void 0 || result.isError === true) {
2383
2542
  return result;
2384
2543
  }
2385
2544
  return {
@@ -2466,7 +2625,10 @@ var MCPServer = class _MCPServer {
2466
2625
  async (uri, ctx) => {
2467
2626
  const mwCtx = this.#createMiddlewareContext(
2468
2627
  "resources/read",
2469
- { uri: uri.href },
2628
+ {
2629
+ uri: uri.href,
2630
+ ...ctx.mcpReq._meta !== void 0 && { _meta: ctx.mcpReq._meta }
2631
+ },
2470
2632
  ctx
2471
2633
  );
2472
2634
  const innerFn = async () => callback(uri, this.#toRequestContext(ctx));
@@ -2504,7 +2666,10 @@ var MCPServer = class _MCPServer {
2504
2666
  async (uri, variables, ctx) => {
2505
2667
  const mwCtx = this.#createMiddlewareContext(
2506
2668
  "resources/read",
2507
- { uri: uri.href },
2669
+ {
2670
+ uri: uri.href,
2671
+ ...ctx.mcpReq._meta !== void 0 && { _meta: ctx.mcpReq._meta }
2672
+ },
2508
2673
  ctx
2509
2674
  );
2510
2675
  const innerFn = async () => callback(
@@ -2548,7 +2713,11 @@ var MCPServer = class _MCPServer {
2548
2713
  const invokePrompt = async (args, ctx) => {
2549
2714
  const mwCtx = this.#createMiddlewareContext(
2550
2715
  "prompts/get",
2551
- { name: definition.name, arguments: args },
2716
+ {
2717
+ name: definition.name,
2718
+ arguments: args,
2719
+ ...ctx.mcpReq._meta !== void 0 && { _meta: ctx.mcpReq._meta }
2720
+ },
2552
2721
  ctx
2553
2722
  );
2554
2723
  const innerFn = async () => {
@@ -2586,7 +2755,7 @@ import {
2586
2755
  createRequestStateCodec,
2587
2756
  inputRequired as inputRequired2,
2588
2757
  inputResponse as inputResponse2,
2589
- isInputRequiredResult as isInputRequiredResult2
2758
+ isInputRequiredResult as isInputRequiredResult3
2590
2759
  } from "@modelcontextprotocol/server";
2591
2760
  export {
2592
2761
  MCPServer,
@@ -2594,13 +2763,15 @@ export {
2594
2763
  completable,
2595
2764
  composeFetch,
2596
2765
  composeMiddleware,
2766
+ createMcpEventListenerEntry,
2767
+ createMcpMiddlewareEntry,
2597
2768
  createMcpMount,
2598
2769
  createRequestStateCodec,
2599
2770
  getRequestBag,
2600
2771
  hostValidationMiddleware,
2601
2772
  inputRequired2 as inputRequired,
2602
2773
  inputResponse2 as inputResponse,
2603
- isInputRequiredResult2 as isInputRequiredResult,
2774
+ isInputRequiredResult3 as isInputRequiredResult,
2604
2775
  jsonBodyMiddleware,
2605
2776
  matchesPath,
2606
2777
  matchesPathPrefix,