skybridge 0.0.0-dev.66b8f6b → 0.0.0-dev.6b57ab9

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 (149) hide show
  1. package/README.md +321 -1
  2. package/dist/src/server/index.d.ts +4 -0
  3. package/dist/{server/index.d.ts → src/server/index.js} +1 -1
  4. package/dist/src/server/index.js.map +1 -0
  5. package/dist/src/server/inferUtilityTypes.d.ts +46 -0
  6. package/dist/src/server/inferUtilityTypes.js +2 -0
  7. package/dist/src/server/inferUtilityTypes.js.map +1 -0
  8. package/dist/src/server/server.d.ts +37 -0
  9. package/dist/src/server/server.js +62 -0
  10. package/dist/src/server/server.js.map +1 -0
  11. package/dist/src/server/templateHelper.d.ts +15 -0
  12. package/dist/src/server/templateHelper.js +29 -0
  13. package/dist/src/server/templateHelper.js.map +1 -0
  14. package/dist/src/server/templates/development.hbs +12 -0
  15. package/dist/src/server/templates/production.hbs +6 -0
  16. package/dist/{server → src/server}/widgetsDevServer.d.ts +5 -2
  17. package/dist/{server → src/server}/widgetsDevServer.js +5 -2
  18. package/dist/src/server/widgetsDevServer.js.map +1 -0
  19. package/dist/src/test/utils.d.ts +69 -0
  20. package/dist/src/test/utils.js +136 -0
  21. package/dist/src/test/utils.js.map +1 -0
  22. package/dist/src/test/widget.test.d.ts +1 -0
  23. package/dist/src/test/widget.test.js +90 -0
  24. package/dist/src/test/widget.test.js.map +1 -0
  25. package/dist/src/web/hooks/index.d.ts +14 -0
  26. package/dist/src/web/hooks/index.js +15 -0
  27. package/dist/src/web/hooks/index.js.map +1 -0
  28. package/dist/src/web/hooks/use-call-tool.d.ts +62 -0
  29. package/dist/src/web/hooks/use-call-tool.js +68 -0
  30. package/dist/src/web/hooks/use-call-tool.js.map +1 -0
  31. package/dist/src/web/hooks/use-call-tool.test.d.ts +1 -0
  32. package/dist/src/web/hooks/use-call-tool.test.js +163 -0
  33. package/dist/src/web/hooks/use-call-tool.test.js.map +1 -0
  34. package/dist/src/web/hooks/use-display-mode.d.ts +4 -0
  35. package/dist/src/web/hooks/use-display-mode.js +7 -0
  36. package/dist/src/web/hooks/use-display-mode.js.map +1 -0
  37. package/dist/src/web/hooks/use-display-mode.test.d.ts +1 -0
  38. package/dist/src/web/hooks/use-display-mode.test.js +40 -0
  39. package/dist/src/web/hooks/use-display-mode.test.js.map +1 -0
  40. package/dist/src/web/hooks/use-files.d.ts +10 -0
  41. package/dist/src/web/hooks/use-files.js +7 -0
  42. package/dist/src/web/hooks/use-files.js.map +1 -0
  43. package/dist/src/web/hooks/use-files.test.d.ts +1 -0
  44. package/dist/src/web/hooks/use-files.test.js +29 -0
  45. package/dist/src/web/hooks/use-files.test.js.map +1 -0
  46. package/dist/src/web/hooks/use-locale.d.ts +1 -0
  47. package/dist/src/web/hooks/use-locale.js +5 -0
  48. package/dist/src/web/hooks/use-locale.js.map +1 -0
  49. package/dist/src/web/hooks/use-locale.test.d.ts +1 -0
  50. package/dist/src/web/hooks/use-locale.test.js +21 -0
  51. package/dist/src/web/hooks/use-locale.test.js.map +1 -0
  52. package/dist/src/web/hooks/use-open-external.d.ts +1 -0
  53. package/dist/src/web/hooks/use-open-external.js +6 -0
  54. package/dist/src/web/hooks/use-open-external.js.map +1 -0
  55. package/dist/src/web/hooks/use-open-external.test.d.ts +1 -0
  56. package/dist/src/web/hooks/use-open-external.test.js +24 -0
  57. package/dist/src/web/hooks/use-open-external.test.js.map +1 -0
  58. package/dist/{web → src/web/hooks}/use-openai-global.d.ts +1 -1
  59. package/dist/{web → src/web/hooks}/use-openai-global.js +4 -2
  60. package/dist/src/web/hooks/use-openai-global.js.map +1 -0
  61. package/dist/src/web/hooks/use-request-modal.d.ts +6 -0
  62. package/dist/src/web/hooks/use-request-modal.js +9 -0
  63. package/dist/src/web/hooks/use-request-modal.js.map +1 -0
  64. package/dist/src/web/hooks/use-request-modal.test.d.ts +1 -0
  65. package/dist/src/web/hooks/use-request-modal.test.js +24 -0
  66. package/dist/src/web/hooks/use-request-modal.test.js.map +1 -0
  67. package/dist/src/web/hooks/use-send-follow-up-message.d.ts +1 -0
  68. package/dist/src/web/hooks/use-send-follow-up-message.js +11 -0
  69. package/dist/src/web/hooks/use-send-follow-up-message.js.map +1 -0
  70. package/dist/src/web/hooks/use-theme.d.ts +1 -0
  71. package/dist/src/web/hooks/use-theme.js +5 -0
  72. package/dist/src/web/hooks/use-theme.js.map +1 -0
  73. package/dist/src/web/hooks/use-theme.test.d.ts +1 -0
  74. package/dist/src/web/hooks/use-theme.test.js +26 -0
  75. package/dist/src/web/hooks/use-theme.test.js.map +1 -0
  76. package/dist/src/web/hooks/use-tool-info.d.ts +25 -0
  77. package/dist/src/web/hooks/use-tool-info.js +22 -0
  78. package/dist/src/web/hooks/use-tool-info.js.map +1 -0
  79. package/dist/src/web/hooks/use-tool-info.test-d.d.ts +1 -0
  80. package/dist/src/web/hooks/use-tool-info.test-d.js +74 -0
  81. package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -0
  82. package/dist/src/web/hooks/use-tool-info.test.d.ts +1 -0
  83. package/dist/src/web/hooks/use-tool-info.test.js +59 -0
  84. package/dist/src/web/hooks/use-tool-info.test.js.map +1 -0
  85. package/dist/src/web/hooks/use-tool-output.d.ts +4 -0
  86. package/dist/src/web/hooks/use-tool-output.js +9 -0
  87. package/dist/src/web/hooks/use-tool-output.js.map +1 -0
  88. package/dist/src/web/hooks/use-tool-response-metadata.d.ts +4 -0
  89. package/dist/src/web/hooks/use-tool-response-metadata.js +8 -0
  90. package/dist/src/web/hooks/use-tool-response-metadata.js.map +1 -0
  91. package/dist/src/web/hooks/use-user-agent.d.ts +1 -0
  92. package/dist/src/web/hooks/use-user-agent.js +5 -0
  93. package/dist/src/web/hooks/use-user-agent.js.map +1 -0
  94. package/dist/src/web/hooks/use-user-agent.test.d.ts +1 -0
  95. package/dist/src/web/hooks/use-user-agent.test.js +31 -0
  96. package/dist/src/web/hooks/use-user-agent.test.js.map +1 -0
  97. package/dist/src/web/hooks/use-widget-state.d.ts +4 -0
  98. package/dist/src/web/hooks/use-widget-state.js +30 -0
  99. package/dist/src/web/hooks/use-widget-state.js.map +1 -0
  100. package/dist/src/web/hooks/use-widget-state.test.d.ts +1 -0
  101. package/dist/src/web/hooks/use-widget-state.test.js +60 -0
  102. package/dist/src/web/hooks/use-widget-state.test.js.map +1 -0
  103. package/dist/src/web/index.d.ts +5 -0
  104. package/dist/src/web/index.js +6 -0
  105. package/dist/src/web/index.js.map +1 -0
  106. package/dist/src/web/mount-widget.js +19 -0
  107. package/dist/src/web/mount-widget.js.map +1 -0
  108. package/dist/src/web/plugin.d.ts +2 -0
  109. package/dist/src/web/plugin.js +28 -0
  110. package/dist/src/web/plugin.js.map +1 -0
  111. package/dist/src/web/proxy.d.ts +1 -0
  112. package/dist/src/web/proxy.js +48 -0
  113. package/dist/src/web/proxy.js.map +1 -0
  114. package/dist/src/web/typed-hooks.d.ts +107 -0
  115. package/dist/src/web/typed-hooks.js +111 -0
  116. package/dist/src/web/typed-hooks.js.map +1 -0
  117. package/dist/src/web/typed-hooks.test-d.d.ts +1 -0
  118. package/dist/src/web/typed-hooks.test-d.js +87 -0
  119. package/dist/src/web/typed-hooks.test-d.js.map +1 -0
  120. package/dist/src/web/typed-hooks.test.d.ts +1 -0
  121. package/dist/src/web/typed-hooks.test.js +17 -0
  122. package/dist/src/web/typed-hooks.test.js.map +1 -0
  123. package/dist/{web → src/web}/types.d.ts +39 -15
  124. package/dist/src/web/types.js.map +1 -0
  125. package/dist/vitest.config.d.ts +2 -0
  126. package/dist/vitest.config.js +8 -0
  127. package/dist/vitest.config.js.map +1 -0
  128. package/package.json +26 -12
  129. package/dist/server/index.js +0 -4
  130. package/dist/server/index.js.map +0 -1
  131. package/dist/server/middleware.d.ts +0 -3
  132. package/dist/server/middleware.js +0 -47
  133. package/dist/server/middleware.js.map +0 -1
  134. package/dist/server/server.d.ts +0 -12
  135. package/dist/server/server.js +0 -70
  136. package/dist/server/server.js.map +0 -1
  137. package/dist/server/widgetsDevServer.js.map +0 -1
  138. package/dist/web/index.d.ts +0 -4
  139. package/dist/web/index.js +0 -5
  140. package/dist/web/index.js.map +0 -1
  141. package/dist/web/mount-widget.js +0 -10
  142. package/dist/web/mount-widget.js.map +0 -1
  143. package/dist/web/types.js.map +0 -1
  144. package/dist/web/use-openai-global.js.map +0 -1
  145. package/dist/web/use-tool-output.d.ts +0 -3
  146. package/dist/web/use-tool-output.js +0 -5
  147. package/dist/web/use-tool-output.js.map +0 -1
  148. /package/dist/{web → src/web}/mount-widget.d.ts +0 -0
  149. /package/dist/{web → src/web}/types.js +0 -0
package/README.md CHANGED
@@ -1,3 +1,323 @@
1
+ <div align="center">
2
+
1
3
  # Skybridge
2
4
 
3
- Skybridge is a framework for building ChatGPT apps
5
+ **Skybridge is the TypeScript framework for building ChatGPT apps**
6
+
7
+ [![By Alpic](https://img.shields.io/badge/Made%20by%20Alpic-f6ffed?logo=alpic)](https://alpic.ai)
8
+
9
+ ![NPM Downloads](https://img.shields.io/npm/dm/skybridge?color=e90060)
10
+ ![NPM Version](https://img.shields.io/npm/v/skybridge?color=e90060)
11
+ ![GitHub License](https://img.shields.io/github/license/alpic-ai/skybridge?color=e90060)
12
+
13
+ </div>
14
+
15
+ Skybridge comes with 2 packages:
16
+
17
+ - `skybridge/server`: A drop-in replacement of the `@modelcontextprotocol/sdk` official `McpServer` class with extra features for widget development.
18
+ - `skybridge/web`: A react library with hooks and components to build widgets on the underlying _OpenAI iFrame skybridge_ runtime.
19
+
20
+ ## Quick start
21
+
22
+ To get started in less than a minute, you can [create a new repository](https://github.com/new?template_name=apps-sdk-template&template_owner=alpic-ai) using our [ChatGPT SDK template](https://github.com/alpic-ai/apps-sdk-template). This template includes a basic setup for both the server and the widgets.
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ pnpm add skybridge
28
+ ```
29
+
30
+ ## Concepts
31
+
32
+ ### Widgets
33
+
34
+ > A widget is a UI component that turns structured tool results into a human-friendly UI. Those are built using React components. They are rendered inside an iframe inline with the conversation on ChatGPT.
35
+
36
+ Each widget in your app must have a unique name. The name is used to bridge the tool invocation result with the widget React component.
37
+
38
+ For example, in order to register a new widget named `pokemon` on your ChatGPT app. You should have the following file structure and file contents:
39
+
40
+ _Project structure_
41
+
42
+ ```
43
+ server/
44
+ └── src/
45
+ └── index.ts // Register the widget with McpServer.widget()
46
+ web/
47
+ └── src/
48
+ └── widgets/
49
+ └── pokemon.tsx // Use the same widget name as the file name
50
+ ```
51
+
52
+ _server/src/index.ts_
53
+
54
+ ```ts
55
+ import { McpServer } from "skybridge/server";
56
+
57
+ const server = new McpServer();
58
+
59
+ server.widget(
60
+ "pokemon"
61
+ // Remaining arguments...
62
+ );
63
+ ```
64
+
65
+ _web/src/widgets/pokemon.tsx_
66
+
67
+ ```ts
68
+ import { mountWidget } from "skybridge/web";
69
+
70
+ const Pokemon: React.FunctionComponent = () => {
71
+ // Your React component code goes here...
72
+ };
73
+
74
+ mountWidget(<Pokemon />);
75
+ ```
76
+
77
+ ## Packages
78
+
79
+ ### skybridge/server
80
+
81
+ The `skybridge/server` package is a drop-in replacement of the `@modelcontextprotocol/sdk` official `McpServer` class with extra features for widget development. If you're already using the `@modelcontextprotocol/sdk`, you can simply replace your `McpServer` import with `skybridge/server` and you're good to go.
82
+
83
+ ### skybridge/web
84
+
85
+ The `skybridge/web` package is a react library with hooks and components to build widgets on the underlying _OpenAI iFrame skybridge_ runtime.
86
+
87
+ **Vite plugin**
88
+
89
+ The `skybridge/web` package comes with a Vite plugin that allows you to build your widgets as regular Vite apps.
90
+
91
+ ```ts
92
+ import { defineConfig } from "vite";
93
+ import { skybridge } from "skybridge/web";
94
+
95
+ export default defineConfig({
96
+ plugins: [skybridge()],
97
+ });
98
+ ```
99
+
100
+ **Typed Hooks**
101
+
102
+ Skybridge provides fully typed hooks that give you autocomplete for tool names and type inference for inputs/outputs - similar to tRPC. This is opt-in and requires exporting your server type.
103
+
104
+ > **Tip:** For the best TypeScript experience, use typed hooks throughout your application. They provide autocomplete, type safety, and better IDE support.
105
+
106
+ _Server setup (server/src/index.ts)_
107
+
108
+ ```ts
109
+ import { McpServer } from "skybridge/server";
110
+ import { z } from "zod";
111
+
112
+ const server = new McpServer({ name: "my-app", version: "1.0" }, {})
113
+ .widget("search-voyage", {}, {
114
+ description: "Search for trips",
115
+ inputSchema: {
116
+ destination: z.string(),
117
+ departureDate: z.string().optional(),
118
+ },
119
+ outputSchema: {
120
+ results: z.array(z.object({ id: z.string(), name: z.string() })),
121
+ totalCount: z.number(),
122
+ },
123
+ }, async ({ destination }) => {
124
+ // Your tool logic here...
125
+ return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
126
+ })
127
+ .widget("get-details", {}, {
128
+ inputSchema: { tripId: z.string() },
129
+ }, async ({ tripId }) => {
130
+ return { content: [{ type: "text", text: `Details for ${tripId}` }] };
131
+ });
132
+
133
+ // Export the server type for the client
134
+ export type AppType = typeof server;
135
+ ```
136
+
137
+ _One-time setup (web/src/skybridge.ts)_
138
+
139
+ Create typed hooks once and export them for use across your app. This file acts as a bridge between your server types and your widgets:
140
+
141
+ ```ts
142
+ import type { AppType } from "../server"; // type-only import
143
+ import { createTypedHooks } from "skybridge/web";
144
+
145
+ export const { useCallTool, useToolInfo } = createTypedHooks<AppType>();
146
+ ```
147
+
148
+ _Usage in widgets (web/src/widgets/search.tsx)_
149
+
150
+ ```tsx
151
+ import { useCallTool, useToolInfo } from "../skybridge"; // import typed hooks
152
+
153
+ export function SearchWidget() {
154
+ const { callTool, data, isPending } = useCallTool("search-voyage");
155
+ // ^ autocomplete for tool names
156
+ const toolInfo = useToolInfo<"search-voyage">();
157
+ // ^ autocomplete for widget names
158
+
159
+ const handleSearch = () => {
160
+ callTool({ destination: "Spain" });
161
+ // ^ autocomplete for input fields
162
+ };
163
+
164
+ return (
165
+ <div>
166
+ <button onClick={handleSearch} disabled={isPending}>
167
+ Search
168
+ </button>
169
+ {toolInfo.isSuccess && (
170
+ <div>Found {toolInfo.output.totalCount} results</div>
171
+ // ^ typed output
172
+ )}
173
+ </div>
174
+ );
175
+ }
176
+ ```
177
+
178
+ **Hooks**
179
+
180
+ The `skybridge/web` package comes with a set of hooks to help you build your widgets :
181
+
182
+ - `useOpenAiGlobal`: A generic hook to get any global data from the OpenAI iFrame skybridge runtime (in `window.openai`).
183
+ - `useToolOutput`: A hook to get the initial tool `structuredContent` returned when rendering the widget for the first time. The data inside this hook is not updated when the tool is called again.
184
+ - `useToolResponseMetadata`: A hook to get the initial tool `meta` returned when rendering the widget for the first time. The data inside this hook is not updated when the tool is called again.
185
+ - `useToolInfo`: A hook to get the tool input, output, and response metadata with type inference. Provides a discriminated union based on status (pending/success).
186
+ - `useCallTool`: A @tanstack/react-query inspired hook to send make additional tool calls inside a widget.
187
+ - `createTypedHooks`: A factory that creates typed versions of `useCallTool` and `useToolInfo` with full type inference from your server type.
188
+
189
+ _useOpenAiGlobal_
190
+
191
+ ```ts
192
+ import { useOpenAiGlobal } from "skybridge/web";
193
+
194
+ const theme = useOpenAiGlobal("theme");
195
+ ```
196
+
197
+ _useToolOutput_
198
+
199
+ ```ts
200
+ import { useToolOutput } from "skybridge/web";
201
+
202
+ const toolOutput = useToolOutput();
203
+ ```
204
+
205
+ _useToolResponseMetadata_
206
+
207
+ ```ts
208
+ import { useToolResponseMetadata } from "skybridge/web";
209
+
210
+ const toolResponseMetadata = useToolResponseMetadata();
211
+ ```
212
+
213
+ _useToolInfo_
214
+
215
+ ```ts
216
+ import { useToolInfo } from "skybridge/web";
217
+
218
+ const toolInfo = useToolInfo<{
219
+ input: { query: string };
220
+ output: { results: string[] };
221
+ responseMetadata: { id: number };
222
+ }>();
223
+
224
+ // toolInfo.input is typed based on the input type
225
+ // toolInfo.output is typed based on the output type (undefined when pending)
226
+ // toolInfo.status narrows correctly: "pending" | "success"
227
+
228
+ if (toolInfo.isPending) {
229
+ // toolInfo.output is undefined here
230
+ console.log(toolInfo.input.query);
231
+ }
232
+
233
+ if (toolInfo.isSuccess) {
234
+ // toolInfo.output is typed here
235
+ console.log(toolInfo.output.results);
236
+ }
237
+ ```
238
+
239
+ _useToolInfo_ with typed hooks (recommended)
240
+
241
+ ```tsx
242
+ import { useToolInfo } from "../skybridge"; // import typed hooks
243
+
244
+ export function SearchWidget() {
245
+ const toolInfo = useToolInfo<"search-voyage">();
246
+ // ^ autocomplete for widget names
247
+ // toolInfo.input is typed as { destination: string; departureDate?: string; ... }
248
+ // toolInfo.output is typed as { results: Array<...>; totalCount: number; } | undefined
249
+
250
+ if (toolInfo.isSuccess) {
251
+ return <div>Found {toolInfo.output.totalCount} results</div>;
252
+ }
253
+
254
+ return <div>Searching for {toolInfo.input.destination}...</div>;
255
+ }
256
+ ```
257
+
258
+ _useCallTool_ in synchronous mode
259
+
260
+ ```ts
261
+ import { useCallTool } from "skybridge/web";
262
+
263
+ export const TestTool: React.FunctionComponent = () => {
264
+ const { callTool, isPending } = useCallTool("myToolName");
265
+
266
+ return (
267
+ <div>
268
+ <button
269
+ disabled={isPending}
270
+ onClick={() => {
271
+ callTool({ input: "test input" }, {
272
+ onSuccess: (data) => {
273
+ alert("Tool returned: " + data);
274
+ },
275
+ });
276
+ >
277
+ Call Tool inside a widget
278
+ </button>
279
+ </div>
280
+ );
281
+ };
282
+ ```
283
+
284
+ _useCallTool_ in asynchronous mode
285
+
286
+ ```ts
287
+ import { useCallTool } from "skybridge/web";
288
+
289
+ export const TestTool: React.FunctionComponent = () => {
290
+ const { callToolAsync, isPending } = useCallTool("myToolName");
291
+
292
+ return (
293
+ <div>
294
+ <button
295
+ disabled={isPending}
296
+ onClick={async () => {
297
+ const data = await callToolAsync({ input: "test input" });
298
+ alert("Tool returned: " + data);
299
+ }}
300
+ >
301
+ Call Tool inside a widget
302
+ </button>
303
+ </div>
304
+ );
305
+ };
306
+ ```
307
+
308
+ ## Migrate your existing MCP server to a ChatGPT app
309
+
310
+ If you're already using the `@modelcontextprotocol/sdk` to build a MCP server, you can migrate to a ChatGPT app by following these steps:
311
+
312
+ 1. Replace your `McpServer` import from `@modelcontextprotocol/sdk` with the same import from `skybridge/server`
313
+ 2. Create a new vite project in a folder named `web` and install the `skybridge` package
314
+ 3. Replace the `vite.config.ts` file with the following:
315
+
316
+ ```ts
317
+ import { defineConfig } from "vite";
318
+ import { skybridge } from "skybridge/web";
319
+
320
+ export default defineConfig({
321
+ plugins: [skybridge()],
322
+ });
323
+ ```
@@ -0,0 +1,4 @@
1
+ export { McpServer } from "./server.js";
2
+ export { widgetsDevServer } from "./widgetsDevServer.js";
3
+ export type { ToolDef } from "./server.js";
4
+ export type { InferTools, AnyToolRegistry, ToolNames, ToolInput, ToolOutput, } from "./inferUtilityTypes.js";
@@ -1,3 +1,3 @@
1
1
  export { McpServer } from "./server.js";
2
2
  export { widgetsDevServer } from "./widgetsDevServer.js";
3
- export { mcp } from "./middleware.js";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,46 @@
1
+ import type { McpServer, ToolDef } from "./server.js";
2
+ /**
3
+ * Any tool registry shape (includes both widgets and regular tools).
4
+ * Used as a constraint for type parameters that accept tool registries.
5
+ */
6
+ export type AnyToolRegistry = Record<string, ToolDef>;
7
+ /**
8
+ * Extract the tool registry type from an McpServer instance.
9
+ * This includes both widgets (registered via widget()) and regular tools (registered via registerTool()).
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * type MyTools = InferTools<MyServer>;
14
+ * // { "search": ToolDef<...>, "calculate": ToolDef<...> }
15
+ * ```
16
+ */
17
+ export type InferTools<T> = T extends McpServer<infer W extends AnyToolRegistry> ? W : T extends McpServer<any> ? never : never;
18
+ /**
19
+ * Get a union of all tool names from an McpServer instance.
20
+ * This includes both widgets and regular tools.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * type Names = ToolNames<MyServer>;
25
+ * // "search" | "calculate" | "details"
26
+ * ```
27
+ */
28
+ export type ToolNames<T> = keyof InferTools<T> & string;
29
+ /**
30
+ * Get the input type for a specific tool (widget or regular tool).
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * type SearchInput = ToolInput<MyServer, "search">;
35
+ * ```
36
+ */
37
+ export type ToolInput<T, K extends ToolNames<T>> = InferTools<T>[K]["input"];
38
+ /**
39
+ * Get the output type for a specific tool (widget or regular tool).
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * type SearchOutput = ToolOutput<MyServer, "search">;
44
+ * ```
45
+ */
46
+ export type ToolOutput<T, K extends ToolNames<T>> = InferTools<T>[K]["output"];
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=inferUtilityTypes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"inferUtilityTypes.js","sourceRoot":"","sources":["../../../src/server/inferUtilityTypes.ts"],"names":[],"mappings":""}
@@ -0,0 +1,37 @@
1
+ import { McpServer as McpServerBase, type ToolCallback, type RegisteredTool } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { Resource, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
3
+ import type { ZodRawShape, ZodObject, infer as Infer } from "zod";
4
+ export type ToolDef<TInput = unknown, TOutput = unknown> = {
5
+ input: TInput;
6
+ output: TOutput;
7
+ };
8
+ type McpServerOriginalResourceConfig = Omit<Resource, "uri" | "name" | "mimeType">;
9
+ type McpServerOriginalToolConfig = Omit<Parameters<McpServerBase["registerTool"]>[1], "inputSchema" | "outputSchema">;
10
+ export declare class McpServer<TTools extends Record<string, ToolDef> = {}> extends McpServerBase {
11
+ widget<TName extends string, TInput extends ZodRawShape, TOutput extends ZodRawShape = {}>(name: TName, resourceConfig: McpServerOriginalResourceConfig, toolConfig: McpServerOriginalToolConfig & {
12
+ inputSchema?: TInput;
13
+ outputSchema?: TOutput;
14
+ }, toolCallback: ToolCallback<TInput>): McpServer<TTools & {
15
+ [K in TName]: ToolDef<Infer<ZodObject<TInput>>, Infer<ZodObject<TOutput>>>;
16
+ }>;
17
+ registerTool<TName extends string, InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape = {}>(name: TName, config: {
18
+ title?: string;
19
+ description?: string;
20
+ inputSchema?: InputArgs;
21
+ outputSchema?: OutputArgs;
22
+ annotations?: ToolAnnotations;
23
+ _meta?: Record<string, unknown>;
24
+ }, cb: ToolCallback<InputArgs>): McpServer<TTools & {
25
+ [K in TName]: ToolDef<InputArgs extends ZodRawShape ? Infer<ZodObject<InputArgs>> : unknown, OutputArgs extends ZodRawShape ? Infer<ZodObject<OutputArgs>> : unknown>;
26
+ }>;
27
+ registerTool<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape = {}>(name: string, config: {
28
+ title?: string;
29
+ description?: string;
30
+ inputSchema?: InputArgs;
31
+ outputSchema?: OutputArgs;
32
+ annotations?: ToolAnnotations;
33
+ _meta?: Record<string, unknown>;
34
+ }, cb: ToolCallback<InputArgs>): RegisteredTool;
35
+ private lookupDistFile;
36
+ }
37
+ export {};
@@ -0,0 +1,62 @@
1
+ import { McpServer as McpServerBase, } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import { templateHelper } from "./templateHelper.js";
3
+ import { readFileSync } from "node:fs";
4
+ import path from "node:path";
5
+ export class McpServer extends McpServerBase {
6
+ widget(name, resourceConfig, toolConfig, toolCallback) {
7
+ const uri = `ui://widgets/${name}.html`;
8
+ const resourceMetadata = {
9
+ ...(resourceConfig._meta ?? {}),
10
+ };
11
+ if (toolConfig.description !== undefined) {
12
+ resourceMetadata["openai/widgetDescription"] = toolConfig.description;
13
+ }
14
+ this.resource(name, uri, {
15
+ ...resourceConfig,
16
+ _meta: resourceMetadata,
17
+ }, async (_uri, extra) => {
18
+ const serverUrl = process.env.NODE_ENV === "production"
19
+ ? `https://${extra?.requestInfo?.headers?.["x-forwarded-host"] ??
20
+ extra?.requestInfo?.headers?.host}`
21
+ : `http://localhost:3000`;
22
+ const html = process.env.NODE_ENV === "production"
23
+ ? templateHelper.renderProduction({
24
+ serverUrl,
25
+ widgetFile: this.lookupDistFile(`src/widgets/${name}.tsx`),
26
+ styleFile: this.lookupDistFile("style.css"),
27
+ })
28
+ : templateHelper.renderDevelopment({
29
+ serverUrl,
30
+ widgetName: name,
31
+ });
32
+ return {
33
+ contents: [
34
+ {
35
+ uri,
36
+ mimeType: "text/html+skybridge",
37
+ text: html,
38
+ },
39
+ ],
40
+ };
41
+ });
42
+ const toolMeta = {
43
+ ...toolConfig._meta,
44
+ "openai/outputTemplate": uri,
45
+ "ui/resourceUri": uri,
46
+ };
47
+ this.registerTool(name, {
48
+ ...toolConfig,
49
+ _meta: toolMeta,
50
+ }, toolCallback);
51
+ return this;
52
+ }
53
+ registerTool(name, config, cb) {
54
+ super.registerTool(name, config, cb);
55
+ return this;
56
+ }
57
+ lookupDistFile(key) {
58
+ const manifest = JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
59
+ return manifest[key]?.file;
60
+ }
61
+ }
62
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,aAAa,GAG3B,MAAM,yCAAyC,CAAC;AAMjD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAuD7B,MAAM,OAAO,SAEX,SAAQ,aAAa;IACrB,MAAM,CAKJ,IAAW,EACX,cAA+C,EAC/C,UAGC,EACD,YAAkC;QASlC,MAAM,GAAG,GAAG,gBAAgB,IAAI,OAAO,CAAC;QACxC,MAAM,gBAAgB,GAAiB;YACrC,GAAG,CAAC,cAAc,CAAC,KAAK,IAAI,EAAE,CAAC;SAChC,CAAC;QACF,IAAI,UAAU,CAAC,WAAW,KAAK,SAAS,EAAE,CAAC;YACzC,gBAAgB,CAAC,0BAA0B,CAAC,GAAG,UAAU,CAAC,WAAW,CAAC;QACxE,CAAC;QAED,IAAI,CAAC,QAAQ,CACX,IAAI,EACJ,GAAG,EACH;YACE,GAAG,cAAc;YACjB,KAAK,EAAE,gBAAgB;SACxB,EACD,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,EAAE;YACpB,MAAM,SAAS,GACb,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;gBACnC,CAAC,CAAC,WACE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,CAAC,kBAAkB,CAAC;oBACjD,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,IAC/B,EAAE;gBACJ,CAAC,CAAC,uBAAuB,CAAC;YAE9B,MAAM,IAAI,GACR,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY;gBACnC,CAAC,CAAC,cAAc,CAAC,gBAAgB,CAAC;oBAC9B,SAAS;oBACT,UAAU,EAAE,IAAI,CAAC,cAAc,CAAC,eAAe,IAAI,MAAM,CAAC;oBAC1D,SAAS,EAAE,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC;iBAC5C,CAAC;gBACJ,CAAC,CAAC,cAAc,CAAC,iBAAiB,CAAC;oBAC/B,SAAS;oBACT,UAAU,EAAE,IAAI;iBACjB,CAAC,CAAC;YAET,OAAO;gBACL,QAAQ,EAAE;oBACR;wBACE,GAAG;wBACH,QAAQ,EAAE,qBAAqB;wBAC/B,IAAI,EAAE,IAAI;qBACX;iBACF;aACF,CAAC;QACJ,CAAC,CACF,CAAC;QAEF,MAAM,QAAQ,GAAa;YACzB,GAAG,UAAU,CAAC,KAAK;YACnB,uBAAuB,EAAE,GAAG;YAC5B,gBAAgB,EAAE,GAAG;SACtB,CAAC;QAEF,IAAI,CAAC,YAAY,CACf,IAAI,EACJ;YACE,GAAG,UAAU;YACb,KAAK,EAAE,QAAQ;SAChB,EACD,YAAY,CACb,CAAC;QAEF,OAAO,IAAI,CAAC;IACd,CAAC;IA8CQ,YAAY,CAInB,IAAY,EACZ,MAOC,EACD,EAA2B;QAE3B,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QAErC,OAAO,IAAI,CAAC;IACd,CAAC;IAEO,cAAc,CAAC,GAAW;QAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CACzB,YAAY,CACV,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,eAAe,CAAC,EACpE,OAAO,CACR,CACF,CAAC;QAEF,OAAO,QAAQ,CAAC,GAAG,CAAC,EAAE,IAAI,CAAC;IAC7B,CAAC;CACF"}
@@ -0,0 +1,15 @@
1
+ declare class TemplateHelper {
2
+ private templateCache;
3
+ private loadTemplate;
4
+ renderProduction(data: {
5
+ serverUrl: string;
6
+ widgetFile: string;
7
+ styleFile: string;
8
+ }): string;
9
+ renderDevelopment(data: {
10
+ serverUrl: string;
11
+ widgetName: string;
12
+ }): string;
13
+ }
14
+ export declare const templateHelper: TemplateHelper;
15
+ export {};
@@ -0,0 +1,29 @@
1
+ import Handlebars from "handlebars";
2
+ import { readFileSync } from "node:fs";
3
+ import { join, dirname } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ const __filename = fileURLToPath(import.meta.url);
6
+ const __dirname = dirname(__filename);
7
+ class TemplateHelper {
8
+ templateCache = new Map();
9
+ loadTemplate(templateName) {
10
+ if (this.templateCache.has(templateName)) {
11
+ return this.templateCache.get(templateName);
12
+ }
13
+ const templatePath = join(__dirname, "templates", `${templateName}.hbs`);
14
+ const templateSource = readFileSync(templatePath, "utf-8");
15
+ const template = Handlebars.compile(templateSource);
16
+ this.templateCache.set(templateName, template);
17
+ return template;
18
+ }
19
+ renderProduction(data) {
20
+ const template = this.loadTemplate("production");
21
+ return template(data);
22
+ }
23
+ renderDevelopment(data) {
24
+ const template = this.loadTemplate("development");
25
+ return template(data);
26
+ }
27
+ }
28
+ export const templateHelper = new TemplateHelper();
29
+ //# sourceMappingURL=templateHelper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"templateHelper.js","sourceRoot":"","sources":["../../../src/server/templateHelper.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAClD,MAAM,SAAS,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;AAOtC,MAAM,cAAc;IACV,aAAa,GAAG,IAAI,GAAG,EAAsC,CAAC;IAE9D,YAAY,CAAC,YAAoB;QACvC,IAAI,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,YAAY,CAAC,EAAE,CAAC;YACzC,OAAO,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,YAAY,CAAE,CAAC;QAC/C,CAAC;QAED,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,EAAE,WAAW,EAAE,GAAG,YAAY,MAAM,CAAC,CAAC;QACzE,MAAM,cAAc,GAAG,YAAY,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;QAC3D,MAAM,QAAQ,GAAG,UAAU,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;QAEpD,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,YAAY,EAAE,QAAQ,CAAC,CAAC;QAC/C,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,gBAAgB,CAAC,IAIhB;QACC,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,CAAC;QACjD,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;IAED,iBAAiB,CAAC,IAA+C;QAC/D,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,aAAa,CAAC,CAAC;QAClD,OAAO,QAAQ,CAAC,IAAI,CAAC,CAAC;IACxB,CAAC;CACF;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,cAAc,EAAE,CAAC"}
@@ -0,0 +1,12 @@
1
+ <base href="{{serverUrl}}" />
2
+ <script type="module">
3
+ import { injectIntoGlobalHook } from "{{serverUrl}}/@react-refresh";
4
+ injectIntoGlobalHook(window); window.$RefreshReg$ = () => {};
5
+ window.$RefreshSig$ = () => (type) => type;
6
+ window.__vite_plugin_react_preamble_installed__ = true;
7
+ </script>
8
+ <script type="module" src="{{serverUrl}}/@vite/client"></script>
9
+ <div id="root"></div>
10
+ <script type="module">
11
+ import('{{serverUrl}}/src/widgets/{{widgetName}}.tsx');
12
+ </script>
@@ -0,0 +1,6 @@
1
+ <base href="{{serverUrl}}" />
2
+ <div id="root"></div>
3
+ <script type="module">
4
+ import('{{serverUrl}}/assets/{{widgetFile}}');
5
+ </script>
6
+ <link rel="stylesheet" crossorigin href="{{serverUrl}}/assets/{{styleFile}}" />
@@ -1,9 +1,12 @@
1
1
  import { type RequestHandler } from "express";
2
2
  /**
3
- * Install Vite dev server when env is not production
3
+ * Install Vite dev server
4
4
  * This router MUST be installed at the application root, like so:
5
5
  *
6
6
  * const app = express();
7
- * app.use(await widgetsRouter());
7
+ *
8
+ * if (env.NODE_ENV !== "production") {
9
+ * app.use(await widgetsRouter());
10
+ * }
8
11
  */
9
12
  export declare const widgetsDevServer: () => Promise<RequestHandler>;
@@ -2,11 +2,14 @@ import express, {} from "express";
2
2
  import cors from "cors";
3
3
  import path from "node:path";
4
4
  /**
5
- * Install Vite dev server when env is not production
5
+ * Install Vite dev server
6
6
  * This router MUST be installed at the application root, like so:
7
7
  *
8
8
  * const app = express();
9
- * app.use(await widgetsRouter());
9
+ *
10
+ * if (env.NODE_ENV !== "production") {
11
+ * app.use(await widgetsRouter());
12
+ * }
10
13
  */
11
14
  export const widgetsDevServer = async () => {
12
15
  const router = express.Router();
@@ -0,0 +1 @@
1
+ {"version":3,"file":"widgetsDevServer.js","sourceRoot":"","sources":["../../../src/server/widgetsDevServer.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,EAAE,EAAuB,MAAM,SAAS,CAAC;AACvD,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,IAA6B,EAAE;IAClE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAEhC,MAAM,EAAE,YAAY,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,GAChE,MAAM,MAAM,CAAC,MAAM,CAAC,CAAC;IACvB,MAAM,aAAa,GAAG,sBAAsB,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;IAC5D,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,KAAK,CAAC,CAAC;IAEnD,MAAM,YAAY,GAAG,MAAM,kBAAkB,CAC3C,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,EACzC,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,gBAAgB,CAAC,EACvC,UAAU,CACX,CAAC;IAEF,+DAA+D;IAC/D,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,GAAG,SAAS,EAAE,GAAG,YAAY,EAAE,MAAM,IAAI,EAAE,CAAC;IAEpE,MAAM,IAAI,GAAG,MAAM,YAAY,CAAC;QAC9B,GAAG,SAAS;QACZ,UAAU,EAAE,KAAK,EAAE,kFAAkF;QACrG,OAAO,EAAE,QAAQ;QACjB,MAAM,EAAE;YACN,YAAY,EAAE,IAAI;YAClB,cAAc,EAAE,IAAI;SACrB;QACD,IAAI,EAAE,UAAU;QAChB,YAAY,EAAE;YACZ,OAAO,EAAE,CAAC,OAAO,EAAE,kBAAkB,CAAC;SACvC;KACF,CAAC,CAAC;IAEH,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IACnB,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;IAElC,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}