@mastra/mcp-apps-components 0.1.0-alpha.0 → 0.1.0-alpha.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 (40) hide show
  1. package/README.md +103 -91
  2. package/dist/apps/observability-widget/tool.d.ts +20 -0
  3. package/dist/apps/observability-widget/tool.d.ts.map +1 -0
  4. package/dist/host.cjs +53 -0
  5. package/dist/host.cjs.map +1 -0
  6. package/dist/host.js +50 -0
  7. package/dist/host.js.map +1 -0
  8. package/dist/index.cjs +32 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.ts +20 -0
  11. package/dist/index.d.ts.map +1 -0
  12. package/dist/index.js +29 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/tool-BD-7EJf2.js +13 -0
  15. package/dist/tool-BD-7EJf2.js.map +1 -0
  16. package/dist/tool-C6_-nIOM.cjs +30 -0
  17. package/dist/tool-C6_-nIOM.cjs.map +1 -0
  18. package/package.json +18 -28
  19. package/dist/apps/dashboard-renderer/input.d.ts +0 -1406
  20. package/dist/apps/dashboard-renderer/input.d.ts.map +0 -1
  21. package/dist/apps/dashboard-renderer/pin.d.ts +0 -1016
  22. package/dist/apps/dashboard-renderer/pin.d.ts.map +0 -1
  23. package/dist/dashboard-renderer.cjs +0 -76
  24. package/dist/dashboard-renderer.cjs.map +0 -1
  25. package/dist/dashboard-renderer.d.ts +0 -15
  26. package/dist/dashboard-renderer.d.ts.map +0 -1
  27. package/dist/dashboard-renderer.js +0 -71
  28. package/dist/dashboard-renderer.js.map +0 -1
  29. package/dist/define-mcp-app-resource.d.ts +0 -16
  30. package/dist/define-mcp-app-resource.d.ts.map +0 -1
  31. package/dist/observability-protocol.d.ts +0 -62
  32. package/dist/observability-protocol.d.ts.map +0 -1
  33. package/dist/observability-tools.cjs +0 -118
  34. package/dist/observability-tools.cjs.map +0 -1
  35. package/dist/observability-tools.d.ts +0 -18
  36. package/dist/observability-tools.d.ts.map +0 -1
  37. package/dist/observability-tools.js +0 -115
  38. package/dist/observability-tools.js.map +0 -1
  39. package/dist/types.d.ts +0 -9
  40. package/dist/types.d.ts.map +0 -1
package/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # @mastra/mcp-apps-components
2
2
 
3
- Prebuilt [MCP Apps](https://mastra.ai/docs/connections/mcp) that render Mastra UI components inside any MCP App host, such as Mastra Studio. Each app ships as a self-contained HTML resource you register on an `MCPServer`, plus a server-side helper to link it to your tools.
4
-
5
- The apps never reach the network from their iframe. They read data by calling app-only observability tools on the same server, which agents don't see.
3
+ Prebuilt [MCP Apps](https://mastra.ai/docs/connections/mcp) that you register as app resources on an `MCPServer`. Each app is a single self-contained HTML page that follows the [MCP Apps spec](https://github.com/modelcontextprotocol/ext-apps), so it runs in any MCP Apps host, such as Mastra Studio.
6
4
 
7
5
  ## Installation
8
6
 
@@ -12,116 +10,130 @@ npm install @mastra/mcp-apps-components
12
10
 
13
11
  ## Usage
14
12
 
15
- Register the dashboard renderer and `observabilityAppTools` on an `MCPServer`, then return a dashboard definition from a tool linked to the app's `ui://` URI.
13
+ `greetingApp` greets someone by name. It talks to its host with standard MCP Apps messages only:
14
+
15
+ 1. The model calls the tool that opens the app. The host sends the app that tool's arguments (`ui/notifications/tool-input`); the app reads `name`.
16
+ 2. The app calls the app-only `getGreeting` tool on the same server (`tools/call`) and shows the `greeting` string from its structured content, or the tool's error.
17
+ 3. A **Say hi back** button posts a user message to the conversation (`ui/message`).
18
+
19
+ You write both tools. `getGreeting` runs on your server, so it can call any API with your server's credentials; the app's iframe has no network access.
16
20
 
17
21
  ```typescript
18
- import type { DashboardDefinition } from '@mastra/core/storage/dashboards';
19
22
  import { createTool } from '@mastra/core/tools';
20
23
  import { MCPServer } from '@mastra/mcp';
21
- import { dashboardRendererApp, dashboardRendererInputSchema } from '@mastra/mcp-apps-components/dashboard-renderer';
22
- import { observabilityAppTools } from '@mastra/mcp-apps-components/observability-tools';
24
+ import { greetingApp } from '@mastra/mcp-apps-components';
23
25
  import { z } from 'zod';
24
26
 
25
- const DASHBOARD_APP_URI = 'ui://mastra/dashboard';
26
-
27
- const agentRuns: DashboardDefinition = {
28
- schemaVersion: 1,
29
- defaults: { datePreset: '7d' },
30
- rows: [
31
- {
32
- widgets: [
33
- {
34
- id: 'runs',
35
- span: 12,
36
- kind: 'kpis',
37
- views: [
38
- {
39
- queries: [
40
- {
41
- id: 'runs',
42
- source: 'metrics',
43
- request: 'aggregate',
44
- name: ['mastra_agent_duration_ms'],
45
- aggregation: 'count',
46
- },
47
- ],
48
- options: { items: [{ label: 'Agent runs', format: 'count', value: { query: 'runs' } }] },
49
- },
50
- ],
51
- },
52
- ],
53
- },
54
- ],
55
- };
56
-
57
- const showAgentRuns = createTool({
58
- id: 'showAgentRuns',
59
- description: 'Shows how many agent runs happened in the last 7 days',
60
- inputSchema: z.object({}),
61
- outputSchema: dashboardRendererInputSchema,
62
- mcp: { _meta: { ui: { resourceUri: DASHBOARD_APP_URI } } },
63
- execute: async () => ({ definition: agentRuns, scope: 'project' as const }),
64
- });
27
+ const uri = 'ui://mastra/greeting';
65
28
 
66
- export const dashboardServer = new MCPServer({
67
- id: 'dashboard-server',
68
- name: 'Dashboard Server',
29
+ export const greetingServer = new MCPServer({
30
+ id: 'greeting',
31
+ name: 'Greeting',
69
32
  version: '1.0.0',
70
- appResources: { [DASHBOARD_APP_URI]: dashboardRendererApp() },
71
- tools: { showAgentRuns, ...observabilityAppTools },
33
+ appResources: { [uri]: greetingApp },
34
+ tools: {
35
+ // Visible to the model: opens the app.
36
+ showGreeting: createTool({
37
+ id: 'showGreeting',
38
+ description: 'Shows a greeting card for someone',
39
+ inputSchema: z.object({ name: z.string() }),
40
+ mcp: { _meta: { ui: { resourceUri: uri } } },
41
+ execute: async ({ name }) => ({ name }),
42
+ }),
43
+ // Visible to the app only: returns its data.
44
+ getGreeting: createTool({
45
+ id: 'getGreeting',
46
+ description: 'Returns the greeting for a name',
47
+ inputSchema: z.object({ name: z.string() }),
48
+ outputSchema: z.object({ greeting: z.string() }),
49
+ mcp: { _meta: { ui: { resourceUri: uri, visibility: ['app'] } } },
50
+ execute: async ({ name }) => ({ greeting: `Hello, ${name}!` }),
51
+ }),
52
+ },
72
53
  });
73
54
  ```
74
55
 
75
- Each tool runs its read-only observability route in-process as the caller, with the route's own validation, authentication, permission and FGA checks, so the app sees the same data the signed-in user can.
76
-
77
- ### Pinning widgets
56
+ ### Observability widget
78
57
 
79
- Each widget shows a pin button when the host says it can pin. The host owns the pinned widgets; the app only asks it to change them.
58
+ `observabilityWidgetApp` draws one Studio dashboard widget (KPIs, time series, table or scores) with the same components Studio uses.
80
59
 
81
- 1. The host sets `mastra.ai/dashboard-pinned-widgets` in its host context to the ids of the widgets it has pinned (`[]` for none). Without it, no pin buttons show.
82
- 2. When the user clicks a widget's button, the app sends a `mastra.ai/dashboard-widget-pin` notification with `{ widget, scope, defaults, isPinned }`: the widget definition, the dashboard's scope, the date range and filters the user had on, and the state the user wants. Together, `widget`, `scope`, and `defaults` render the pinned widget on its own as the user saw it.
83
- 3. The host stores the change and sends the new list of ids back in its host context. Each button follows the host, not the click.
60
+ 1. The model calls the tool that opens the app. That tool must declare an `outputSchema` so its result reaches the app as structured content: `{ widget, datePreset? }`, where `widget` is a stored dashboard widget and `datePreset` is one of `24h` (default), `3d`, `7d`, `14d` or `30d`. The window ends when the app opens.
61
+ 2. The app reads its data through the app-only `queryObservability` tool (exported as `QUERY_OBSERVABILITY_TOOL`). It calls it once per read with `{ action, args }`, where `action` is a `MastraClient` observability method (`getMetricAggregate`, `getMetricTimeSeries`, `listScores`, `aggregateTraces`, …) and `args` are that method's arguments, with dates as ISO strings. The tool returns `{ data }`, the method's result; if it throws, the widget shows as failed.
84
62
 
85
- The params come from the app's iframe, so parse them with `dashboardWidgetPinNotificationParamsSchema` before storing or rendering anything.
63
+ The tool runs on your server, so you decide which Mastra server the widget reads from and how it authenticates. Only allow read actions:
86
64
 
87
65
  ```typescript
88
- import { AppBridge } from '@modelcontextprotocol/ext-apps/app-bridge';
89
- import {
90
- DASHBOARD_PINNED_WIDGETS_CONTEXT_KEY,
91
- DASHBOARD_WIDGET_PIN_NOTIFICATION,
92
- dashboardWidgetPinNotificationParamsSchema,
93
- } from '@mastra/mcp-apps-components/dashboard-renderer';
94
-
95
- let hostContext = { theme: 'dark' as const, [DASHBOARD_PINNED_WIDGETS_CONTEXT_KEY]: [] as string[] };
96
- const bridge = new AppBridge(client, hostInfo, capabilities, { hostContext });
97
-
98
- bridge.fallbackNotificationHandler = async ({ method, params }) => {
99
- if (method !== DASHBOARD_WIDGET_PIN_NOTIFICATION) return;
100
- const parsed = dashboardWidgetPinNotificationParamsSchema.safeParse(params);
101
- if (!parsed.success) return;
102
- const { widget, scope, defaults, isPinned } = parsed.data;
103
-
104
- const pinned = hostContext[DASHBOARD_PINNED_WIDGETS_CONTEXT_KEY];
105
- if (isPinned === pinned.includes(widget.id)) return;
106
- await (isPinned ? savePinnedWidget({ widget, scope, defaults }) : deletePinnedWidget(widget.id));
107
-
108
- const ids = isPinned ? [...pinned, widget.id] : pinned.filter(id => id !== widget.id);
109
- // setHostContext replaces the whole context, so keep the other fields
110
- hostContext = { ...hostContext, [DASHBOARD_PINNED_WIDGETS_CONTEXT_KEY]: ids };
111
- bridge.setHostContext(hostContext);
112
- };
66
+ import { MastraClient } from '@mastra/client-js';
67
+ import { createTool } from '@mastra/core/tools';
68
+ import { MCPServer } from '@mastra/mcp';
69
+ import { QUERY_OBSERVABILITY_TOOL, observabilityWidgetApp } from '@mastra/mcp-apps-components';
70
+ import { z } from 'zod';
71
+
72
+ const uri = 'ui://mastra/observability-widget';
73
+ const client = new MastraClient({ baseUrl: process.env.MASTRA_URL! });
74
+ const ACTIONS = ['getMetricAggregate', 'getMetricTimeSeries'] as const;
75
+
76
+ export const observabilityServer = new MCPServer({
77
+ id: 'observability',
78
+ name: 'Observability',
79
+ version: '1.0.0',
80
+ appResources: { [uri]: observabilityWidgetApp },
81
+ tools: {
82
+ // Visible to the model: opens the app with a widget.
83
+ showObservabilityWidget: createTool({
84
+ id: 'showObservabilityWidget',
85
+ description: 'Shows agent runs over time',
86
+ inputSchema: z.object({ datePreset: z.enum(['24h', '3d', '7d', '14d', '30d']).default('24h') }),
87
+ outputSchema: z.object({ widget: z.unknown(), datePreset: z.string() }),
88
+ mcp: { _meta: { ui: { resourceUri: uri } } },
89
+ execute: async ({ datePreset }) => ({ widget: agentRunsWidget, datePreset }),
90
+ }),
91
+ // Visible to the app only: runs one read on the server's client.
92
+ [QUERY_OBSERVABILITY_TOOL]: createTool({
93
+ id: QUERY_OBSERVABILITY_TOOL,
94
+ description: 'Runs one observability read',
95
+ inputSchema: z.object({ action: z.enum(ACTIONS), args: z.record(z.string(), z.unknown()) }),
96
+ outputSchema: z.object({ data: z.unknown() }),
97
+ mcp: { _meta: { ui: { resourceUri: uri, visibility: ['app'] } } },
98
+ execute: async ({ action, args }) => {
99
+ const read = client[action].bind(client) as (args: unknown) => Promise<unknown>;
100
+ return { data: await read(args) };
101
+ },
102
+ }),
103
+ },
104
+ });
113
105
  ```
114
106
 
115
- Keep in mind:
107
+ `examples/agent` has a complete version with every read action, a set of preset widgets and an agent that opens them.
108
+
109
+ #### Pinning widgets
110
+
111
+ When the chat host supports it, the widget shows a pin button. The host owns the pinned widgets: it sends their ids to the app, and the app reports each pin and unpin through `ui/update-model-context`. In a host built on `@mastra/playground-ui`, pass `observabilityWidgetPins` from `@mastra/mcp-apps-components/host` to `McpAppHostProvider`:
112
+
113
+ ```tsx
114
+ import { McpAppHostProvider } from '@mastra/playground-ui/domains/mcps';
115
+ import { observabilityWidgetPins } from '@mastra/mcp-apps-components/host';
116
+
117
+ const host = useMemo(
118
+ () =>
119
+ observabilityWidgetPins({
120
+ resourceUri: 'ui://mastra/observability-widget',
121
+ pinnedIds,
122
+ onPin: ({ widget, datePreset }) => savePin(widget, datePreset),
123
+ onUnpin: widgetId => removePin(widgetId),
124
+ }),
125
+ [pinnedIds],
126
+ );
127
+
128
+ <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>;
129
+ ```
116
130
 
117
- - To turn pinning off, set the key to a non-array value such as `null`. Deleting the key does nothing, because `AppBridge` only sends the fields that changed, so the app keeps the last ids it got.
118
- - Widget ids are only unique within one dashboard definition. Keep pinned ids per bridge (one tool call), not across dashboards.
119
- - A widget that no longer parses shows no pin button, so the host owns unpinning it.
131
+ The button only shows when the host sends pinned ids and accepts structured model context. A click shows right away and holds until the host sends new ids.
120
132
 
121
133
  ## Documentation
122
134
 
123
- - [Build MCP Apps](https://mastra.ai/docs/connections/mcp)
124
- - [`MCPServer` reference](https://mastra.ai/reference/tools/mcp-server)
135
+ - [MCP Apps in Mastra](https://mastra.ai/docs/connections/mcp)
136
+ - [MCP Apps spec](https://github.com/modelcontextprotocol/ext-apps)
125
137
 
126
138
  ## Changelog
127
139
 
@@ -0,0 +1,20 @@
1
+ /** The app-only tool the observability widget calls, on the server that opened it, for every read. */
2
+ export declare const QUERY_OBSERVABILITY_TOOL = "queryObservability";
3
+ /**
4
+ * Host context key holding the ids of the widgets the host has pinned (`string[]`). The pin button
5
+ * only shows when the host sends it and supports `updateModelContext.structuredContent`.
6
+ */
7
+ export declare const OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = "mastra.ai/observability-widget-pinned";
8
+ /** `structuredContent` key of the model context update the app sends when the user (un)pins. */
9
+ export declare const OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY = "mastra.ai/observability-widget-pin";
10
+ /** What the app sends under `OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY`: the state the user asked for. */
11
+ export type ObservabilityWidgetPin = {
12
+ pinned: boolean;
13
+ /** The stored dashboard widget, as the opening tool returned it. */
14
+ widget: {
15
+ id: string;
16
+ [key: string]: unknown;
17
+ };
18
+ datePreset: string;
19
+ };
20
+ //# sourceMappingURL=tool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../../src/apps/observability-widget/tool.ts"],"names":[],"mappings":"AAAA,sGAAsG;AACtG,eAAO,MAAM,wBAAwB,uBAAuB,CAAC;AAE7D;;;GAGG;AACH,eAAO,MAAM,uCAAuC,0CAA0C,CAAC;AAE/F,gGAAgG;AAChG,eAAO,MAAM,oCAAoC,uCAAuC,CAAC;AAEzF,qGAAqG;AACrG,MAAM,MAAM,sBAAsB,GAAG;IACnC,MAAM,EAAE,OAAO,CAAC;IAChB,oEAAoE;IACpE,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;IAC/C,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC"}
package/dist/host.cjs ADDED
@@ -0,0 +1,53 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_tool = require("./tool-C6_-nIOM.cjs");
3
+ // src/host.ts
4
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
5
+ /** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
6
+ function readPin(structuredContent) {
7
+ const pin = structuredContent?.[require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
8
+ if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
9
+ if (!isRecord(pin.widget) || typeof pin.widget.id !== "string") return void 0;
10
+ return {
11
+ pinned: pin.pinned,
12
+ widget: {
13
+ ...pin.widget,
14
+ id: pin.widget.id
15
+ },
16
+ datePreset: pin.datePreset
17
+ };
18
+ }
19
+ /**
20
+ * Lets the user pin observability widgets from the chat. The host owns the pinned widgets: it
21
+ * sends their ids to each widget app, and hears each pin and unpin.
22
+ *
23
+ * ```tsx
24
+ * const host = useMemo(
25
+ * () => observabilityWidgetPins({ resourceUri: 'ui://mastra/observability-widget', pinnedIds, onPin, onUnpin }),
26
+ * [pinnedIds, onPin, onUnpin],
27
+ * );
28
+ * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>
29
+ * ```
30
+ *
31
+ * A click shows on the button right away and holds until the host's ids next change, so a host
32
+ * that turns a click down leaves it showing until then.
33
+ */
34
+ function observabilityWidgetPins({ resourceUri, pinnedIds, onPin, onUnpin }) {
35
+ return {
36
+ hostContext: (app) => app.resourceUri === resourceUri ? { [require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pinnedIds } : void 0,
37
+ onModelContext: (app, params) => {
38
+ if (app.resourceUri !== resourceUri) return;
39
+ const pin = readPin(params.structuredContent);
40
+ if (!pin) return;
41
+ if (pin.pinned) onPin({
42
+ widget: pin.widget,
43
+ datePreset: pin.datePreset
44
+ }, app);
45
+ else onUnpin(pin.widget.id, app);
46
+ }
47
+ };
48
+ }
49
+ exports.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY;
50
+ exports.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY = require_tool.OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY;
51
+ exports.observabilityWidgetPins = observabilityWidgetPins;
52
+
53
+ //# sourceMappingURL=host.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.cjs","names":["OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY","OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY"],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPinsOptions = {\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /** The user pinned `pin.widget` over `pin.datePreset`. */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean' || typeof pin.datePreset !== 'string') return undefined;\n if (!isRecord(pin.widget) || typeof pin.widget.id !== 'string') return undefined;\n return { pinned: pin.pinned, widget: { ...pin.widget, id: pin.widget.id }, datePreset: pin.datePreset };\n}\n\n/**\n * Lets the user pin observability widgets from the chat. The host owns the pinned widgets: it\n * sends their ids to each widget app, and hears each pin and unpin.\n *\n * ```tsx\n * const host = useMemo(\n * () => observabilityWidgetPins({ resourceUri: 'ui://mastra/observability-widget', pinnedIds, onPin, onUnpin }),\n * [pinnedIds, onPin, onUnpin],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * A click shows on the button right away and holds until the host's ids next change, so a host\n * that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetPins({\n resourceUri,\n pinnedIds,\n onPin,\n onUnpin,\n}: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return {\n hostContext: app =>\n app.resourceUri === resourceUri ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pinnedIds } : undefined,\n onModelContext: (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const pin = readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) onPin({ widget: pin.widget, datePreset: pin.datePreset }, app);\n else onUnpin(pin.widget.id, app);\n },\n };\n}\n"],"mappings":";;;AAsCA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,QAAQ,mBAA4F;CAC3G,MAAM,MAAM,oBAAoBA,aAAAA;CAChC,IAAI,CAAC,SAAS,GAAG,KAAK,OAAO,IAAI,WAAW,aAAa,OAAO,IAAI,eAAe,UAAU,OAAO,KAAA;CACpG,IAAI,CAAC,SAAS,IAAI,MAAM,KAAK,OAAO,IAAI,OAAO,OAAO,UAAU,OAAO,KAAA;CACvE,OAAO;EAAE,QAAQ,IAAI;EAAQ,QAAQ;GAAE,GAAG,IAAI;GAAQ,IAAI,IAAI,OAAO;EAAG;EAAG,YAAY,IAAI;CAAW;AACxG;;;;;;;;;;;;;;;;AAiBA,SAAgB,wBAAwB,EACtC,aACA,WACA,OACA,WACkD;CAClD,OAAO;EACL,cAAa,QACX,IAAI,gBAAgB,cAAc,GAAGC,aAAAA,0CAA0C,UAAU,IAAI,KAAA;EAC/F,iBAAiB,KAAK,WAAW;GAC/B,IAAI,IAAI,gBAAgB,aAAa;GACrC,MAAM,MAAM,QAAQ,OAAO,iBAAiB;GAC5C,IAAI,CAAC,KAAK;GACV,IAAI,IAAI,QAAQ,MAAM;IAAE,QAAQ,IAAI;IAAQ,YAAY,IAAI;GAAW,GAAG,GAAG;QACxE,QAAQ,IAAI,OAAO,IAAI,GAAG;EACjC;CACF;AACF"}
package/dist/host.js ADDED
@@ -0,0 +1,50 @@
1
+ import { n as OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, t as OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY } from "./tool-BD-7EJf2.js";
2
+ // src/host.ts
3
+ const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
+ /** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */
5
+ function readPin(structuredContent) {
6
+ const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];
7
+ if (!isRecord(pin) || typeof pin.pinned !== "boolean" || typeof pin.datePreset !== "string") return void 0;
8
+ if (!isRecord(pin.widget) || typeof pin.widget.id !== "string") return void 0;
9
+ return {
10
+ pinned: pin.pinned,
11
+ widget: {
12
+ ...pin.widget,
13
+ id: pin.widget.id
14
+ },
15
+ datePreset: pin.datePreset
16
+ };
17
+ }
18
+ /**
19
+ * Lets the user pin observability widgets from the chat. The host owns the pinned widgets: it
20
+ * sends their ids to each widget app, and hears each pin and unpin.
21
+ *
22
+ * ```tsx
23
+ * const host = useMemo(
24
+ * () => observabilityWidgetPins({ resourceUri: 'ui://mastra/observability-widget', pinnedIds, onPin, onUnpin }),
25
+ * [pinnedIds, onPin, onUnpin],
26
+ * );
27
+ * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>
28
+ * ```
29
+ *
30
+ * A click shows on the button right away and holds until the host's ids next change, so a host
31
+ * that turns a click down leaves it showing until then.
32
+ */
33
+ function observabilityWidgetPins({ resourceUri, pinnedIds, onPin, onUnpin }) {
34
+ return {
35
+ hostContext: (app) => app.resourceUri === resourceUri ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pinnedIds } : void 0,
36
+ onModelContext: (app, params) => {
37
+ if (app.resourceUri !== resourceUri) return;
38
+ const pin = readPin(params.structuredContent);
39
+ if (!pin) return;
40
+ if (pin.pinned) onPin({
41
+ widget: pin.widget,
42
+ datePreset: pin.datePreset
43
+ }, app);
44
+ else onUnpin(pin.widget.id, app);
45
+ }
46
+ };
47
+ }
48
+ export { OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY, OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY, observabilityWidgetPins };
49
+
50
+ //# sourceMappingURL=host.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"host.js","names":[],"sources":["../src/host.ts"],"sourcesContent":["import {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nimport type { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\nexport {\n OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY,\n OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY,\n} from './apps/observability-widget/tool';\nexport type { ObservabilityWidgetPin } from './apps/observability-widget/tool';\n\n/** The MCP App instance a host callback is about: one tool call's app. */\nexport type McpAppRef = { toolName: string; toolCallId: string; resourceUri: string };\n\n/**\n * What a chat host passes to `McpAppHostProvider` from `@mastra/playground-ui/domains/mcps`. Typed\n * here by shape, so this package doesn't depend on the host's UI package.\n */\nexport type McpAppHostValue = {\n hostContext?: (app: McpAppRef) => Record<string, unknown> | undefined;\n onModelContext?: (\n app: McpAppRef,\n params: { content?: unknown[]; structuredContent?: Record<string, unknown> },\n ) => void;\n};\n\nexport type ObservabilityWidgetPinsOptions = {\n /** The `ui://` URI the observability widget app is registered under; other apps are left alone. */\n resourceUri: string;\n /** The ids of the widgets the host has pinned. Pass new ids after `onPin`/`onUnpin` to answer. */\n pinnedIds: readonly string[];\n /** The user pinned `pin.widget` over `pin.datePreset`. */\n onPin: (pin: Omit<ObservabilityWidgetPin, 'pinned'>, app: McpAppRef) => void;\n /** The user unpinned the widget with this id. */\n onUnpin: (widgetId: string, app: McpAppRef) => void;\n};\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value);\n\n/** The app's pin update, checked by shape only: the widget itself is validated when it's drawn. */\nfunction readPin(structuredContent: Record<string, unknown> | undefined): ObservabilityWidgetPin | undefined {\n const pin = structuredContent?.[OBSERVABILITY_WIDGET_PIN_CONTEXT_KEY];\n if (!isRecord(pin) || typeof pin.pinned !== 'boolean' || typeof pin.datePreset !== 'string') return undefined;\n if (!isRecord(pin.widget) || typeof pin.widget.id !== 'string') return undefined;\n return { pinned: pin.pinned, widget: { ...pin.widget, id: pin.widget.id }, datePreset: pin.datePreset };\n}\n\n/**\n * Lets the user pin observability widgets from the chat. The host owns the pinned widgets: it\n * sends their ids to each widget app, and hears each pin and unpin.\n *\n * ```tsx\n * const host = useMemo(\n * () => observabilityWidgetPins({ resourceUri: 'ui://mastra/observability-widget', pinnedIds, onPin, onUnpin }),\n * [pinnedIds, onPin, onUnpin],\n * );\n * <McpAppHostProvider value={host}>{chat}</McpAppHostProvider>\n * ```\n *\n * A click shows on the button right away and holds until the host's ids next change, so a host\n * that turns a click down leaves it showing until then.\n */\nexport function observabilityWidgetPins({\n resourceUri,\n pinnedIds,\n onPin,\n onUnpin,\n}: ObservabilityWidgetPinsOptions): McpAppHostValue {\n return {\n hostContext: app =>\n app.resourceUri === resourceUri ? { [OBSERVABILITY_WIDGET_PINNED_CONTEXT_KEY]: pinnedIds } : undefined,\n onModelContext: (app, params) => {\n if (app.resourceUri !== resourceUri) return;\n const pin = readPin(params.structuredContent);\n if (!pin) return;\n if (pin.pinned) onPin({ widget: pin.widget, datePreset: pin.datePreset }, app);\n else onUnpin(pin.widget.id, app);\n },\n };\n}\n"],"mappings":";;AAsCA,MAAM,YAAY,UAChB,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;;AAGrE,SAAS,QAAQ,mBAA4F;CAC3G,MAAM,MAAM,oBAAoB;CAChC,IAAI,CAAC,SAAS,GAAG,KAAK,OAAO,IAAI,WAAW,aAAa,OAAO,IAAI,eAAe,UAAU,OAAO,KAAA;CACpG,IAAI,CAAC,SAAS,IAAI,MAAM,KAAK,OAAO,IAAI,OAAO,OAAO,UAAU,OAAO,KAAA;CACvE,OAAO;EAAE,QAAQ,IAAI;EAAQ,QAAQ;GAAE,GAAG,IAAI;GAAQ,IAAI,IAAI,OAAO;EAAG;EAAG,YAAY,IAAI;CAAW;AACxG;;;;;;;;;;;;;;;;AAiBA,SAAgB,wBAAwB,EACtC,aACA,WACA,OACA,WACkD;CAClD,OAAO;EACL,cAAa,QACX,IAAI,gBAAgB,cAAc,GAAG,0CAA0C,UAAU,IAAI,KAAA;EAC/F,iBAAiB,KAAK,WAAW;GAC/B,IAAI,IAAI,gBAAgB,aAAa;GACrC,MAAM,MAAM,QAAQ,OAAO,iBAAiB;GAC5C,IAAI,CAAC,KAAK;GACV,IAAI,IAAI,QAAQ,MAAM;IAAE,QAAQ,IAAI;IAAQ,YAAY,IAAI;GAAW,GAAG,GAAG;QACxE,QAAQ,IAAI,OAAO,IAAI,GAAG;EACjC;CACF;AACF"}