@agent-native/dispatch 0.42.9-nightly-20261118063257 → 0.42.10

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 (63) hide show
  1. package/CHANGELOG.md +25 -61
  2. package/dist/actions/call_app_action.d.ts +15 -0
  3. package/dist/actions/call_app_action.d.ts.map +1 -0
  4. package/dist/actions/call_app_action.js +48 -0
  5. package/dist/actions/call_app_action.js.map +1 -0
  6. package/dist/actions/create-workspace-resource.d.ts +13 -13
  7. package/dist/actions/delete-workspace-resource.d.ts +13 -13
  8. package/dist/actions/import-agent.d.ts +13 -13
  9. package/dist/actions/index.d.ts +8 -0
  10. package/dist/actions/index.d.ts.map +1 -1
  11. package/dist/actions/index.js +31 -0
  12. package/dist/actions/index.js.map +1 -1
  13. package/dist/actions/list_app_actions.d.ts +14 -0
  14. package/dist/actions/list_app_actions.d.ts.map +1 -0
  15. package/dist/actions/list_app_actions.js +29 -0
  16. package/dist/actions/list_app_actions.js.map +1 -0
  17. package/dist/actions/read_view.d.ts +8 -0
  18. package/dist/actions/read_view.d.ts.map +1 -0
  19. package/dist/actions/read_view.js +38 -0
  20. package/dist/actions/read_view.js.map +1 -0
  21. package/dist/components/usage-alerts-panel.d.ts +6 -0
  22. package/dist/components/usage-alerts-panel.d.ts.map +1 -1
  23. package/dist/components/usage-alerts-panel.js +12 -1
  24. package/dist/components/usage-alerts-panel.js.map +1 -1
  25. package/dist/server/index.d.ts +1 -0
  26. package/dist/server/index.d.ts.map +1 -1
  27. package/dist/server/index.js +1 -0
  28. package/dist/server/index.js.map +1 -1
  29. package/dist/server/lib/app-action-result.d.ts +4 -0
  30. package/dist/server/lib/app-action-result.d.ts.map +1 -0
  31. package/dist/server/lib/app-action-result.js +15 -0
  32. package/dist/server/lib/app-action-result.js.map +1 -0
  33. package/dist/server/lib/app-creation-store.d.ts +9 -1
  34. package/dist/server/lib/app-creation-store.d.ts.map +1 -1
  35. package/dist/server/lib/app-creation-store.js +70 -7
  36. package/dist/server/lib/app-creation-store.js.map +1 -1
  37. package/dist/server/lib/mcp-gateway.d.ts +33 -0
  38. package/dist/server/lib/mcp-gateway.d.ts.map +1 -1
  39. package/dist/server/lib/mcp-gateway.js +204 -3
  40. package/dist/server/lib/mcp-gateway.js.map +1 -1
  41. package/dist/server/lib/usage-metrics-store.d.ts.map +1 -1
  42. package/dist/server/lib/usage-metrics-store.js +2 -2
  43. package/dist/server/lib/usage-metrics-store.js.map +1 -1
  44. package/dist/server/plugins/agent-chat.js +2 -2
  45. package/dist/server/plugins/agent-chat.js.map +1 -1
  46. package/package.json +4 -4
  47. package/src/actions/app_actions.spec.ts +198 -0
  48. package/src/actions/call_app_action.ts +59 -0
  49. package/src/actions/index.spec.ts +50 -2
  50. package/src/actions/index.ts +34 -0
  51. package/src/actions/list_app_actions.ts +33 -0
  52. package/src/actions/read_view.ts +46 -0
  53. package/src/components/usage-alerts-panel.tsx +17 -3
  54. package/src/server/index.ts +5 -0
  55. package/src/server/lib/app-action-result.ts +19 -0
  56. package/src/server/lib/app-creation-store.spec.ts +181 -0
  57. package/src/server/lib/app-creation-store.ts +100 -7
  58. package/src/server/lib/mcp-gateway.spec.ts +254 -0
  59. package/src/server/lib/mcp-gateway.ts +279 -2
  60. package/src/server/lib/usage-metrics-store.spec.ts +1 -0
  61. package/src/server/lib/usage-metrics-store.ts +2 -1
  62. package/src/server/plugins/agent-chat.spec.ts +4 -0
  63. package/src/server/plugins/agent-chat.ts +2 -2
@@ -10,11 +10,13 @@ import {
10
10
  type A2APersistedMutationReceipt,
11
11
  type Task,
12
12
  } from "@agent-native/core/a2a";
13
+ import { fail } from "@agent-native/core/action";
13
14
  import { getAppConfig } from "@agent-native/core/app-config";
14
15
  import { isFeatureFlagEnabled } from "@agent-native/core/feature-flags";
15
16
  import {
16
17
  buildMcpToolName,
17
18
  McpClientManager,
19
+ mcpToolCallWasNotSent,
18
20
  } from "@agent-native/core/mcp-client";
19
21
  import { getOrgA2ASecret, getOrgDomain } from "@agent-native/core/org";
20
22
  import {
@@ -811,7 +813,9 @@ function toAccessibleApp(
811
813
  return {
812
814
  id: agent.id,
813
815
  name: agent.name,
814
- description: agent.description,
816
+ // Deployment manifest and directory entries may omit a description even
817
+ // though the discovered type declares one.
818
+ description: typeof agent.description === "string" ? agent.description : "",
815
819
  url: projectEnvironmentUrl(agent.url),
816
820
  color: agent.color,
817
821
  granted: isAppAllowedByMcpAccess(agent.id, settings),
@@ -1467,6 +1471,277 @@ async function callTargetCreateEmbedSession(input: {
1467
1471
  }
1468
1472
  }
1469
1473
 
1474
+ const TARGET_APP_ACTION_CONNECT_MS = 15_000;
1475
+ // Embed minting is Dispatch's own handshake with the target, not an app action.
1476
+ const TARGET_APP_HIDDEN_ACTIONS = new Set([
1477
+ "create_embed_session",
1478
+ "open_app",
1479
+ "list_apps",
1480
+ ]);
1481
+
1482
+ export type GrantedAppAction = {
1483
+ name: string;
1484
+ description: string;
1485
+ inputSchema?: Record<string, unknown>;
1486
+ readOnly?: boolean;
1487
+ };
1488
+
1489
+ export type GrantedAppActionResult = {
1490
+ isError?: boolean;
1491
+ approvalReview?: { url?: string; expiresAt?: number };
1492
+ structuredContent?: unknown;
1493
+ content: unknown[];
1494
+ };
1495
+
1496
+ export function dispatchDirectAppActionsEnabled(): boolean {
1497
+ return getAppConfig().dispatch.directAppActions;
1498
+ }
1499
+
1500
+ /** One direct app action connection, as a header provider sees it before it opens. */
1501
+ export interface DispatchAppActionRequest {
1502
+ app: string;
1503
+ /** The action about to be called; undefined when only listing actions. */
1504
+ action: string | undefined;
1505
+ ownerEmail: string;
1506
+ orgId?: string;
1507
+ }
1508
+
1509
+ export type DispatchAppActionHeaderProvider = (
1510
+ request: DispatchAppActionRequest,
1511
+ ) =>
1512
+ | Record<string, string>
1513
+ | undefined
1514
+ | Promise<Record<string, string> | undefined>;
1515
+
1516
+ let appActionHeaderProvider: DispatchAppActionHeaderProvider | undefined;
1517
+
1518
+ /**
1519
+ * Let the deployment add headers to direct app action connections, such as a
1520
+ * per-turn grant the target app's MCP turn grant admitter recognizes. Returns
1521
+ * a function that removes it.
1522
+ */
1523
+ export function setDispatchAppActionHeaderProvider(
1524
+ provider: DispatchAppActionHeaderProvider,
1525
+ ): () => void {
1526
+ if (appActionHeaderProvider && appActionHeaderProvider !== provider) {
1527
+ throw new Error(
1528
+ "A Dispatch app action header provider is already registered.",
1529
+ );
1530
+ }
1531
+ appActionHeaderProvider = provider;
1532
+ return () => {
1533
+ if (appActionHeaderProvider === provider)
1534
+ appActionHeaderProvider = undefined;
1535
+ };
1536
+ }
1537
+
1538
+ async function appActionHeaders(
1539
+ request: DispatchAppActionRequest,
1540
+ ): Promise<Record<string, string>> {
1541
+ if (!appActionHeaderProvider) return {};
1542
+ const headers = await appActionHeaderProvider(request);
1543
+ if (headers === undefined) return {};
1544
+ for (const [name, value] of Object.entries(headers)) {
1545
+ if (name.toLowerCase() === "authorization" || typeof value !== "string") {
1546
+ throw new Error(
1547
+ `The app action header provider returned an invalid "${name}" header.`,
1548
+ );
1549
+ }
1550
+ }
1551
+ return headers;
1552
+ }
1553
+
1554
+ // Runs `withClient` against a connected MCP client for the granted app, signed
1555
+ // as the requesting user, the way embed sessions reach the same target. Without a
1556
+ // header provider no per-turn grant is attached, so a target action that
1557
+ // requires a user turn refuses with its own error code.
1558
+ async function withGrantedAppMcp<T>(
1559
+ app: string,
1560
+ action: string | undefined,
1561
+ withClient: (manager: McpClientManager, serverId: string) => Promise<T>,
1562
+ ): Promise<T> {
1563
+ if (!dispatchDirectAppActionsEnabled()) {
1564
+ fail("Direct app actions are not enabled on this Dispatch deployment.", {
1565
+ errorCode: "direct_app_actions_disabled",
1566
+ statusCode: 403,
1567
+ });
1568
+ }
1569
+ const target = await resolveGrantedDispatchMcpApp(app);
1570
+ if (target.id === DISPATCH_APP_ID) {
1571
+ throw new Error(
1572
+ "Dispatch's own actions are tools on this connection; call them directly.",
1573
+ );
1574
+ }
1575
+ const ownerEmail = getRequestUserEmail();
1576
+ if (!ownerEmail) throw new Error("no authenticated user");
1577
+ const orgId = getRequestOrgId();
1578
+ const mcpUrl = appMcpUrl(target);
1579
+ if (!targetEndpointKeepsCredentialsPrivate(mcpUrl)) {
1580
+ throw new Error("App actions need an HTTPS target endpoint.");
1581
+ }
1582
+ const firstPartyMcp = isWorkspaceSsoAppUrl(
1583
+ { id: target.id, url: mcpUrl },
1584
+ { nodeEnv: "production", environmentLane: requestEnvironmentLane() },
1585
+ );
1586
+ const prepared = await createTargetMcpUserCredentials({
1587
+ mcpUrl,
1588
+ ownerEmail,
1589
+ fullCatalog: true,
1590
+ ...(firstPartyMcp ? { firstPartyMcp: true } : {}),
1591
+ ...(orgId !== undefined ? { orgId } : {}),
1592
+ });
1593
+ const credentials = [...prepared.credentials];
1594
+ const extraHeaders = await appActionHeaders({
1595
+ app: target.id,
1596
+ action,
1597
+ ownerEmail,
1598
+ ...(orgId !== undefined ? { orgId } : {}),
1599
+ });
1600
+ const serverId = "target";
1601
+ let orgMemberTried = false;
1602
+ for (const [index, credential] of credentials.entries()) {
1603
+ const manager = new McpClientManager(
1604
+ {
1605
+ servers: {
1606
+ [serverId]: {
1607
+ type: "http",
1608
+ url: mcpUrl,
1609
+ headers: {
1610
+ ...extraHeaders,
1611
+ Authorization: `Bearer ${credential.token}`,
1612
+ ...(credential.firstPartyMcp
1613
+ ? { "x-agent-native-mcp-inline-apps": "1" }
1614
+ : {}),
1615
+ },
1616
+ },
1617
+ },
1618
+ },
1619
+ { connectTimeoutMs: TARGET_APP_ACTION_CONNECT_MS },
1620
+ );
1621
+ try {
1622
+ await manager.start();
1623
+ const status = manager.getConnectErrorStatus(serverId);
1624
+ if (status === 401 || status === 403) {
1625
+ // The org-member fallback is tried once; its own rejection is final.
1626
+ if (index === credentials.length - 1 && !orgMemberTried) {
1627
+ orgMemberTried = true;
1628
+ const orgMember = await prepared.loadOrgMemberCredential();
1629
+ if (orgMember) credentials.push(orgMember);
1630
+ }
1631
+ if (index < credentials.length - 1) continue;
1632
+ throw new Error(`The ${target.id} app rejected this connection.`);
1633
+ }
1634
+ if (!manager.hasServer(serverId)) {
1635
+ throw new Error(`The ${target.id} app's actions are unavailable.`);
1636
+ }
1637
+ return await withClient(manager, serverId);
1638
+ } finally {
1639
+ await manager.stop().catch((stopError: unknown) => {
1640
+ console.warn("[dispatch] Failed to stop target MCP client:", stopError);
1641
+ });
1642
+ }
1643
+ }
1644
+ throw new Error(`The ${target.id} app rejected this connection.`);
1645
+ }
1646
+
1647
+ export async function listGrantedDispatchMcpAppActions(
1648
+ app: string,
1649
+ names?: string[],
1650
+ ): Promise<GrantedAppAction[]> {
1651
+ return withGrantedAppMcp(app, undefined, async (manager, serverId) =>
1652
+ manager
1653
+ .getToolsForServer(serverId)
1654
+ .filter((tool) => !TARGET_APP_HIDDEN_ACTIONS.has(tool.originalName))
1655
+ .map((tool) => {
1656
+ const detailed = names?.includes(tool.originalName) === true;
1657
+ return {
1658
+ name: tool.originalName,
1659
+ description: detailed
1660
+ ? tool.description
1661
+ : (tool.description.split(/(?<=\.)\s/)[0] ?? "").slice(0, 160),
1662
+ ...(detailed ? { inputSchema: tool.inputSchema } : {}),
1663
+ ...(tool.annotations?.readOnlyHint === true
1664
+ ? { readOnly: true }
1665
+ : {}),
1666
+ };
1667
+ }),
1668
+ );
1669
+ }
1670
+
1671
+ function approvalReviewFrom(
1672
+ result: object,
1673
+ ): { url?: string; expiresAt?: number } | undefined {
1674
+ const meta = "_meta" in result ? result._meta : undefined;
1675
+ const review =
1676
+ meta !== null && typeof meta === "object"
1677
+ ? (meta as Record<string, unknown>)["agent-native/approvalReview"]
1678
+ : undefined;
1679
+ if (review === null || typeof review !== "object") return undefined;
1680
+ const { url, expiresAt } = review as Record<string, unknown>;
1681
+ return {
1682
+ ...(typeof url === "string" ? { url } : {}),
1683
+ ...(typeof expiresAt === "number" ? { expiresAt } : {}),
1684
+ };
1685
+ }
1686
+
1687
+ export async function callGrantedDispatchMcpAppAction(
1688
+ app: string,
1689
+ action: string,
1690
+ args: Record<string, unknown>,
1691
+ ): Promise<GrantedAppActionResult> {
1692
+ if (TARGET_APP_HIDDEN_ACTIONS.has(action)) {
1693
+ throw new Error(`"${action}" is not an app action.`);
1694
+ }
1695
+ return withGrantedAppMcp(app, action, async (manager, serverId) => {
1696
+ const toolName = buildMcpToolName(serverId, action);
1697
+ if (!manager.getTool(toolName)) {
1698
+ throw new Error(
1699
+ `The app has no action named "${action}". Call list_app_actions to see them.`,
1700
+ );
1701
+ }
1702
+ let result: unknown;
1703
+ try {
1704
+ result = await manager.callTool(toolName, args);
1705
+ } catch (error) {
1706
+ const reason = error instanceof Error ? error.message : String(error);
1707
+ if (mcpToolCallWasNotSent(error)) {
1708
+ fail(`The app action did not run (${reason}).`, {
1709
+ errorCode: "app_action_not_sent",
1710
+ statusCode:
1711
+ (error as { statusCode?: unknown }).statusCode === 403 ? 403 : 502,
1712
+ });
1713
+ }
1714
+ fail(
1715
+ `The app action failed before returning a result (${reason}). Its write outcome may be unknown; inspect the app before retrying.`,
1716
+ { errorCode: "app_action_outcome_unknown", statusCode: 502 },
1717
+ );
1718
+ }
1719
+ if (
1720
+ result === null ||
1721
+ typeof result !== "object" ||
1722
+ !("content" in result) ||
1723
+ !Array.isArray(result.content)
1724
+ ) {
1725
+ fail(
1726
+ "The app action returned an unreadable result. Its write outcome may be unknown; inspect the app before retrying.",
1727
+ { errorCode: "app_action_result_unreadable", statusCode: 502 },
1728
+ );
1729
+ }
1730
+ const approvalReview = approvalReviewFrom(result);
1731
+ return {
1732
+ ...("isError" in result && result.isError === true
1733
+ ? { isError: true }
1734
+ : {}),
1735
+ ...(approvalReview ? { approvalReview } : {}),
1736
+ ...("structuredContent" in result &&
1737
+ result.structuredContent !== undefined
1738
+ ? { structuredContent: result.structuredContent }
1739
+ : {}),
1740
+ content: result.content as unknown[],
1741
+ };
1742
+ });
1743
+ }
1744
+
1470
1745
  type TargetMcpCredential = {
1471
1746
  strategy: "deployment-user" | "org-member-delegation";
1472
1747
  token: string;
@@ -1478,6 +1753,7 @@ async function createTargetMcpUserCredentials(input: {
1478
1753
  ownerEmail: string;
1479
1754
  orgId?: string;
1480
1755
  firstPartyMcp?: boolean;
1756
+ fullCatalog?: boolean;
1481
1757
  runtime?: "workspace-app-chat";
1482
1758
  }): Promise<{
1483
1759
  credentials: TargetMcpCredential[];
@@ -1518,9 +1794,10 @@ async function createTargetMcpUserCredentials(input: {
1518
1794
  expiresIn: "5m",
1519
1795
  audience,
1520
1796
  preferGlobalSecret: true,
1521
- ...(input.firstPartyMcp || input.runtime
1797
+ ...(input.firstPartyMcp || input.runtime || input.fullCatalog
1522
1798
  ? {
1523
1799
  extraClaims: {
1800
+ ...(input.fullCatalog ? { catalog_scope: "full" } : {}),
1524
1801
  ...(input.firstPartyMcp
1525
1802
  ? {
1526
1803
  jti: globalThis.crypto.randomUUID(),
@@ -72,6 +72,7 @@ vi.mock("@agent-native/core/server", () => ({
72
72
 
73
73
  vi.mock("@agent-native/core/usage", () => ({
74
74
  builderCreditsFromCostCents: (cents: number) => cents / 4,
75
+ deploymentUsageBilling: (billing: unknown) => billing,
75
76
  getUsageSummary: (...args: any[]) => mocks.getUsageSummary(...args),
76
77
  usageBillingForEngine: (engineName?: string | null) =>
77
78
  engineName === "builder"
@@ -14,6 +14,7 @@ import { readOrgMemberRole } from "@agent-native/core/server";
14
14
  import { ForbiddenError } from "@agent-native/core/sharing";
15
15
  import {
16
16
  builderCreditsFromCostCents,
17
+ deploymentUsageBilling,
17
18
  getUsageSummary,
18
19
  isSelfScopedUsageRead,
19
20
  MIXED_USAGE_BILLING,
@@ -1180,7 +1181,7 @@ export async function listDispatchUsageMetrics(input: {
1180
1181
  const billing = unknownCalls
1181
1182
  ? UNKNOWN_USAGE_BILLING
1182
1183
  : builderCalls > 0 && providerCalls > 0
1183
- ? MIXED_USAGE_BILLING
1184
+ ? deploymentUsageBilling(MIXED_USAGE_BILLING)
1184
1185
  : builderCalls > 0
1185
1186
  ? usageBillingForEngine("builder")
1186
1187
  : providerCalls > 0
@@ -32,8 +32,11 @@ vi.mock("../lib/workspace-app-action-auth.js", () => ({
32
32
  WORKSPACE_APPS_ACTION_PATH: "/_agent-native/actions/list-workspace-apps",
33
33
  }));
34
34
 
35
+ const resolveDispatchActions = vi.hoisted(() => () => ({}));
36
+
35
37
  vi.mock("../../actions/index.js", () => ({
36
38
  dispatchActions: {},
39
+ resolveDispatchActions,
37
40
  }));
38
41
 
39
42
  describe("Dispatch agent chat plugin", () => {
@@ -43,6 +46,7 @@ describe("Dispatch agent chat plugin", () => {
43
46
  expect(mocks.createAgentChatPlugin).toHaveBeenCalledWith(
44
47
  expect.objectContaining({
45
48
  appId: "dispatch",
49
+ actions: resolveDispatchActions,
46
50
  durableBackgroundRuns: true,
47
51
  initialToolNames: expect.arrayContaining([
48
52
  "list-workspace-apps",
@@ -5,7 +5,7 @@ import {
5
5
  registerAgentContextAgentFilter,
6
6
  } from "@agent-native/core/server";
7
7
 
8
- import { dispatchActions } from "../../actions/index.js";
8
+ import { resolveDispatchActions } from "../../actions/index.js";
9
9
  import { hideHostedAppsWhenOff } from "../lib/hosted-apps-settings.js";
10
10
  import { listGrantedDispatchMcpApps } from "../lib/mcp-gateway.js";
11
11
  import {
@@ -91,7 +91,7 @@ export default createAgentChatPlugin({
91
91
  const ctx = await getOrgContext(event);
92
92
  return ctx.orgId;
93
93
  },
94
- actions: dispatchActions,
94
+ actions: resolveDispatchActions,
95
95
  actionRouteAuth: workspaceAppActionRouteAuth,
96
96
  actionRoutePublicPaths: [
97
97
  WORKSPACE_APPS_ACTION_PATH,