skybridge 0.0.0-dev.e3e0986 → 0.0.0-dev.e41a369

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 (123) hide show
  1. package/LICENSE +21 -674
  2. package/README.md +70 -267
  3. package/dist/src/server/emulatorStaticServer.d.ts +15 -0
  4. package/dist/src/server/emulatorStaticServer.js +38 -0
  5. package/dist/src/server/emulatorStaticServer.js.map +1 -0
  6. package/dist/src/server/index.d.ts +3 -2
  7. package/dist/src/server/index.js +1 -0
  8. package/dist/src/server/index.js.map +1 -1
  9. package/dist/src/server/inferUtilityTypes.d.ts +46 -15
  10. package/dist/src/server/server.d.ts +54 -11
  11. package/dist/src/server/server.js +8 -4
  12. package/dist/src/server/server.js.map +1 -1
  13. package/dist/src/server/templateHelper.js +2 -2
  14. package/dist/src/server/templateHelper.js.map +1 -1
  15. package/dist/src/server/widgetsDevServer.js +3 -3
  16. package/dist/src/server/widgetsDevServer.js.map +1 -1
  17. package/dist/src/test/utils.d.ts +97 -1
  18. package/dist/src/test/utils.js +181 -2
  19. package/dist/src/test/utils.js.map +1 -1
  20. package/dist/src/test/widget.test.js +19 -20
  21. package/dist/src/test/widget.test.js.map +1 -1
  22. package/dist/src/web/create-store.d.ts +3 -0
  23. package/dist/src/web/create-store.js +25 -0
  24. package/dist/src/web/create-store.js.map +1 -0
  25. package/dist/src/web/create-store.test.js +70 -0
  26. package/dist/src/web/create-store.test.js.map +1 -0
  27. package/dist/src/web/data-llm.d.ts +14 -0
  28. package/dist/src/web/data-llm.js +68 -0
  29. package/dist/src/web/data-llm.js.map +1 -0
  30. package/dist/src/web/data-llm.test.js +76 -0
  31. package/dist/src/web/data-llm.test.js.map +1 -0
  32. package/dist/src/web/generate-helpers.d.ts +115 -0
  33. package/dist/src/web/generate-helpers.js +111 -0
  34. package/dist/src/web/generate-helpers.js.map +1 -0
  35. package/dist/src/web/generate-helpers.test-d.d.ts +1 -0
  36. package/dist/src/web/generate-helpers.test-d.js +180 -0
  37. package/dist/src/web/generate-helpers.test-d.js.map +1 -0
  38. package/dist/src/web/generate-helpers.test.d.ts +1 -0
  39. package/dist/src/web/generate-helpers.test.js +17 -0
  40. package/dist/src/web/generate-helpers.test.js.map +1 -0
  41. package/dist/src/web/helpers/state.d.ts +7 -0
  42. package/dist/src/web/helpers/state.js +40 -0
  43. package/dist/src/web/helpers/state.js.map +1 -0
  44. package/dist/src/web/helpers/state.test.d.ts +1 -0
  45. package/dist/src/web/helpers/state.test.js +53 -0
  46. package/dist/src/web/helpers/state.test.js.map +1 -0
  47. package/dist/src/web/hooks/index.d.ts +2 -4
  48. package/dist/src/web/hooks/index.js +2 -4
  49. package/dist/src/web/hooks/index.js.map +1 -1
  50. package/dist/src/web/hooks/use-call-tool.d.ts +57 -19
  51. package/dist/src/web/hooks/use-call-tool.js +7 -15
  52. package/dist/src/web/hooks/use-call-tool.js.map +1 -1
  53. package/dist/src/web/hooks/use-call-tool.test-d.d.ts +1 -0
  54. package/dist/src/web/hooks/use-call-tool.test-d.js +104 -0
  55. package/dist/src/web/hooks/use-call-tool.test-d.js.map +1 -0
  56. package/dist/src/web/hooks/use-call-tool.test.js +18 -10
  57. package/dist/src/web/hooks/use-call-tool.test.js.map +1 -1
  58. package/dist/src/web/hooks/use-display-mode.js.map +1 -1
  59. package/dist/src/web/hooks/use-display-mode.test.js +2 -2
  60. package/dist/src/web/hooks/use-display-mode.test.js.map +1 -1
  61. package/dist/src/web/hooks/use-files.test.js +1 -1
  62. package/dist/src/web/hooks/use-files.test.js.map +1 -1
  63. package/dist/src/web/hooks/use-locale.test.js +2 -2
  64. package/dist/src/web/hooks/use-locale.test.js.map +1 -1
  65. package/dist/src/web/hooks/use-open-external.test.js +2 -2
  66. package/dist/src/web/hooks/use-open-external.test.js.map +1 -1
  67. package/dist/src/web/hooks/use-openai-global.d.ts +2 -2
  68. package/dist/src/web/hooks/use-openai-global.js +1 -1
  69. package/dist/src/web/hooks/use-openai-global.js.map +1 -1
  70. package/dist/src/web/hooks/use-request-modal.test.js +2 -2
  71. package/dist/src/web/hooks/use-request-modal.test.js.map +1 -1
  72. package/dist/src/web/hooks/use-theme.test.js +2 -2
  73. package/dist/src/web/hooks/use-theme.test.js.map +1 -1
  74. package/dist/src/web/hooks/use-tool-info.d.ts +1 -1
  75. package/dist/src/web/hooks/use-tool-info.js +1 -1
  76. package/dist/src/web/hooks/use-tool-info.js.map +1 -1
  77. package/dist/src/web/hooks/use-tool-info.test-d.js +0 -1
  78. package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -1
  79. package/dist/src/web/hooks/use-tool-info.test.js +2 -2
  80. package/dist/src/web/hooks/use-tool-info.test.js.map +1 -1
  81. package/dist/src/web/hooks/use-user-agent.test.js +2 -2
  82. package/dist/src/web/hooks/use-user-agent.test.js.map +1 -1
  83. package/dist/src/web/hooks/use-widget-state.js +9 -7
  84. package/dist/src/web/hooks/use-widget-state.js.map +1 -1
  85. package/dist/src/web/hooks/use-widget-state.test.js +2 -2
  86. package/dist/src/web/hooks/use-widget-state.test.js.map +1 -1
  87. package/dist/src/web/index.d.ts +6 -4
  88. package/dist/src/web/index.js +6 -4
  89. package/dist/src/web/index.js.map +1 -1
  90. package/dist/src/web/plugin/data-llm.test.d.ts +1 -0
  91. package/dist/src/web/plugin/data-llm.test.js +81 -0
  92. package/dist/src/web/plugin/data-llm.test.js.map +1 -0
  93. package/dist/src/web/{plugin.js → plugin/plugin.js} +5 -0
  94. package/dist/src/web/plugin/plugin.js.map +1 -0
  95. package/dist/src/web/plugin/transform-data-llm.d.ts +12 -0
  96. package/dist/src/web/plugin/transform-data-llm.js +93 -0
  97. package/dist/src/web/plugin/transform-data-llm.js.map +1 -0
  98. package/dist/src/web/plugin/transform-data-llm.test.d.ts +1 -0
  99. package/dist/src/web/plugin/transform-data-llm.test.js +81 -0
  100. package/dist/src/web/plugin/transform-data-llm.test.js.map +1 -0
  101. package/dist/src/web/proxy.js +1 -1
  102. package/dist/src/web/proxy.js.map +1 -1
  103. package/dist/src/web/types.d.ts +18 -4
  104. package/dist/src/web/types.js +1 -0
  105. package/dist/src/web/types.js.map +1 -1
  106. package/package.json +14 -6
  107. package/dist/src/web/hooks/use-tool-output.d.ts +0 -4
  108. package/dist/src/web/hooks/use-tool-output.js +0 -9
  109. package/dist/src/web/hooks/use-tool-output.js.map +0 -1
  110. package/dist/src/web/hooks/use-tool-response-metadata.d.ts +0 -4
  111. package/dist/src/web/hooks/use-tool-response-metadata.js +0 -8
  112. package/dist/src/web/hooks/use-tool-response-metadata.js.map +0 -1
  113. package/dist/src/web/plugin.js.map +0 -1
  114. package/dist/src/web/typed-hooks.d.ts +0 -61
  115. package/dist/src/web/typed-hooks.js +0 -61
  116. package/dist/src/web/typed-hooks.js.map +0 -1
  117. package/dist/src/web/typed-hooks.test-d.js +0 -72
  118. package/dist/src/web/typed-hooks.test-d.js.map +0 -1
  119. package/dist/src/web/typed-hooks.test.js +0 -10
  120. package/dist/src/web/typed-hooks.test.js.map +0 -1
  121. /package/dist/src/web/{typed-hooks.test-d.d.ts → create-store.test.d.ts} +0 -0
  122. /package/dist/src/web/{typed-hooks.test.d.ts → data-llm.test.d.ts} +0 -0
  123. /package/dist/src/web/{plugin.d.ts → plugin/plugin.d.ts} +0 -0
package/README.md CHANGED
@@ -1,320 +1,123 @@
1
1
  <div align="center">
2
2
 
3
- # Skybridge
4
-
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)
3
+ <img alt="Skybridge" src="docs/static/img/logo.png" width="220">
8
4
 
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)
5
+ # Skybridge
12
6
 
13
- </div>
7
+ **Build ChatGPT Apps. The Modern TypeScript Way.**
14
8
 
15
- Skybridge comes with 2 packages:
9
+ The fullstack TypeScript framework for ChatGPT Apps.<br />
10
+ **Type-safe. React-powered. Zero config.**
16
11
 
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.
12
+ <br />
19
13
 
20
- ## Quick start
14
+ [![NPM Version](https://img.shields.io/npm/v/skybridge?color=e90060&style=for-the-badge)](https://www.npmjs.com/package/skybridge)
15
+ [![NPM Downloads](https://img.shields.io/npm/dm/skybridge?color=e90060&style=for-the-badge)](https://www.npmjs.com/package/skybridge)
16
+ [![GitHub License](https://img.shields.io/github/license/alpic-ai/skybridge?color=e90060&style=for-the-badge)](https://github.com/alpic-ai/skybridge/blob/main/LICENSE)
21
17
 
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.
18
+ <br />
23
19
 
24
- ## Installation
20
+ [Documentation](https://skybridge.tech) · [Quick Start](https://github.com/new?template_name=apps-sdk-template&template_owner=alpic-ai) · [Examples](https://github.com/alpic-ai/apps-sdk-template)
25
21
 
26
- ```bash
27
- pnpm add skybridge
28
- ```
22
+ </div>
29
23
 
30
- ## Concepts
24
+ <br />
31
25
 
32
- ### Widgets
26
+ ## ✨ Why Skybridge?
33
27
 
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.
28
+ ChatGPT Apps let you embed **rich, interactive UIs** directly in conversations. But the raw SDK is low-level—no hooks, no type safety, no dev tools, and no HMR.
35
29
 
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.
30
+ **Skybridge fixes that.**
37
31
 
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:
32
+ | | |
33
+ |:--|:--|
34
+ | 👨‍💻 **Full Dev Environment** — HMR, debug traces, and local emulator. No more refresh loops. | ✅ **End-to-End Type Safety** — tRPC-style inference from server to widget. Autocomplete everywhere. |
35
+ | 🔄 **Widget-to-Model Sync** — Keep the model aware of UI state with `data-llm`. Dual surfaces, one source of truth. | ⚒️ **React Query-style Hooks** — `isPending`, `isError`, callbacks. State management you already know. |
39
36
 
40
- _Project structure_
37
+ <br />
41
38
 
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
- ```
39
+ ## 🚀 Get Started
51
40
 
52
- _server/src/index.ts_
41
+ **Create a new ChatGPT app:**
53
42
 
54
- ```ts
55
- import { McpServer } from "skybridge/server";
56
-
57
- const server = new McpServer();
58
-
59
- server.widget(
60
- "pokemon"
61
- // Remaining arguments...
62
- );
43
+ ```bash
44
+ gh repo create my-app --template alpic-ai/apps-sdk-template --clone
45
+ cd my-app && pnpm install
63
46
  ```
64
47
 
65
- _web/src/widgets/pokemon.tsx_
48
+ **Or add to an existing project:**
66
49
 
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 />);
50
+ ```bash
51
+ npm i skybridge
52
+ yarn add skybridge
53
+ pnpm add skybridge
54
+ bun add skybridge
55
+ deno add skybridge
75
56
  ```
76
57
 
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**
58
+ <div align="center">
88
59
 
89
- The `skybridge/web` package comes with a Vite plugin that allows you to build your widgets as regular Vite apps.
60
+ **👉 [Read the Docs](https://skybridge.tech) 👈**
90
61
 
91
- ```ts
92
- import { defineConfig } from "vite";
93
- import { skybridge } from "skybridge/web";
62
+ </div>
94
63
 
95
- export default defineConfig({
96
- plugins: [skybridge()],
97
- });
98
- ```
64
+ <br />
99
65
 
100
- **Typed Hooks**
66
+ ## 📦 The Stack
101
67
 
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.
68
+ - **`skybridge/server`** Drop-in MCP SDK replacement with widget registration and type inference.
69
+ - **`skybridge/web`** — React hooks and components for ChatGPT's runtime.
103
70
 
104
- _Server setup (server/src/index.ts)_
71
+ ### Server
105
72
 
106
73
  ```ts
107
74
  import { McpServer } from "skybridge/server";
108
- import { z } from "zod";
109
-
110
- const server = new McpServer({ name: "my-app", version: "1.0" }, {})
111
- .widget("search-voyage", {}, {
112
- description: "Search for trips",
113
- inputSchema: {
114
- destination: z.string(),
115
- departureDate: z.string().optional(),
116
- },
117
- outputSchema: {
118
- results: z.array(z.object({ id: z.string(), name: z.string() })),
119
- },
120
- }, async ({ destination }) => {
121
- // Your tool logic here...
122
- return { content: [{ type: "text", text: `Found trips to ${destination}` }] };
123
- })
124
- .widget("get-details", {}, {
125
- inputSchema: { tripId: z.string() },
126
- }, async ({ tripId }) => {
127
- return { content: [{ type: "text", text: `Details for ${tripId}` }] };
128
- });
129
-
130
- // Export the server type for the client
131
- export type AppType = typeof server;
132
- ```
133
75
 
134
- _One-time setup (web/src/skybridge.ts)_
135
-
136
- Create typed hooks once and export them for use across your app:
137
-
138
- ```ts
139
- import type { AppType } from "../server"; // type-only import
140
- import { createTypedHooks } from "skybridge/web";
141
-
142
- export const { useCallTool, useToolInfo } = createTypedHooks<AppType>();
76
+ server.registerWidget("flights", {}, {
77
+ inputSchema: { destination: z.string() },
78
+ }, async ({ destination }) => {
79
+ const flights = await searchFlights(destination);
80
+ return { structuredContent: { flights } };
81
+ });
143
82
  ```
144
83
 
145
- _Usage in widgets (web/src/widgets/search.tsx)_
84
+ ### Widget
146
85
 
147
86
  ```tsx
148
- import { useCallTool, useToolInfo } from "../skybridge"; // import typed hooks
149
-
150
- export function SearchWidget() {
151
- const { callTool, data, isPending } = useCallTool("search-voyage");
152
- // ^ autocomplete for tool names
153
- const toolInfo = useToolInfo<"search-voyage">();
154
- // ^ autocomplete for widget names
155
-
156
- const handleSearch = () => {
157
- callTool({ destination: "Spain" });
158
- // ^ autocomplete for input fields
159
- };
160
-
161
- return (
162
- <div>
163
- <button onClick={handleSearch} disabled={isPending}>
164
- Search
165
- </button>
166
- {toolInfo.isSuccess && (
167
- <div>Found {toolInfo.output.totalCount} results</div>
168
- // ^ typed output
169
- )}
170
- </div>
171
- );
172
- }
173
- ```
174
-
175
- **Hooks**
176
-
177
- The `skybridge/web` package comes with a set of hooks to help you build your widgets :
178
-
179
- - `useOpenAiGlobal`: A generic hook to get any global data from the OpenAI iFrame skybridge runtime (in `window.openai`).
180
- - `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.
181
- - `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.
182
- - `useToolInfo`: A hook to get the tool input, output, and response metadata with type inference. Provides a discriminated union based on status (pending/success).
183
- - `useCallTool`: A @tanstack/react-query inspired hook to send make additional tool calls inside a widget.
184
- - `createTypedHooks`: A factory that creates typed versions of `useCallTool` and `useToolInfo` with full type inference from your server type.
185
-
186
- _useOpenAiGlobal_
187
-
188
- ```ts
189
- import { useOpenAiGlobal } from "skybridge/web";
190
-
191
- const theme = useOpenAiGlobal("theme");
192
- ```
193
-
194
- _useToolOutput_
195
-
196
- ```ts
197
- import { useToolOutput } from "skybridge/web";
198
-
199
- const toolOutput = useToolOutput();
200
- ```
201
-
202
- _useToolResponseMetadata_
203
-
204
- ```ts
205
- import { useToolResponseMetadata } from "skybridge/web";
206
-
207
- const toolResponseMetadata = useToolResponseMetadata();
208
- ```
209
-
210
- _useToolInfo_
211
-
212
- ```ts
213
87
  import { useToolInfo } from "skybridge/web";
214
88
 
215
- const toolInfo = useToolInfo<{
216
- input: { query: string };
217
- output: { results: string[] };
218
- responseMetadata: { id: number };
219
- }>();
89
+ function FlightsWidget() {
90
+ const { output } = useToolInfo();
220
91
 
221
- // toolInfo.input is typed based on the input type
222
- // toolInfo.output is typed based on the output type (undefined when pending)
223
- // toolInfo.status narrows correctly: "pending" | "success"
224
-
225
- if (toolInfo.isPending) {
226
- // toolInfo.output is undefined here
227
- console.log(toolInfo.input.query);
228
- }
229
-
230
- if (toolInfo.isSuccess) {
231
- // toolInfo.output is typed here
232
- console.log(toolInfo.output.results);
92
+ return output.structuredContent.flights.map(f =>
93
+ <FlightCard key={f.id} flight={f} />
94
+ );
233
95
  }
234
96
  ```
235
97
 
236
- _useToolInfo_ with typed hooks (recommended)
237
-
238
- ```tsx
239
- import { useToolInfo } from "../skybridge"; // import typed hooks
240
-
241
- export function SearchWidget() {
242
- const toolInfo = useToolInfo<"search-voyage">();
243
- // ^ autocomplete for widget names
244
- // toolInfo.input is typed as { destination: string; departureDate?: string; ... }
245
- // toolInfo.output is typed as { results: Array<...>; totalCount: number; } | undefined
98
+ <br />
246
99
 
247
- if (toolInfo.isSuccess) {
248
- return <div>Found {toolInfo.output.totalCount} results</div>;
249
- }
100
+ ## 🎯 Features at a Glance
250
101
 
251
- return <div>Searching for {toolInfo.input.destination}...</div>;
252
- }
253
- ```
254
-
255
- _useCallTool_ in synchronous mode
256
-
257
- ```ts
258
- import { useCallTool } from "skybridge/web";
259
-
260
- export const TestTool: React.FunctionComponent = () => {
261
- const { callTool, isPending } = useCallTool("myToolName");
262
-
263
- return (
264
- <div>
265
- <button
266
- disabled={isPending}
267
- onClick={() => {
268
- callTool({ input: "test input" }, {
269
- onSuccess: (data) => {
270
- alert("Tool returned: " + data);
271
- },
272
- });
273
- >
274
- Call Tool inside a widget
275
- </button>
276
- </div>
277
- );
278
- };
279
- ```
102
+ - **Live Reload** — Vite HMR. See changes instantly without reinstalling.
103
+ - **Typed Hooks** — Full autocomplete for tools, inputs, outputs.
104
+ - **Widget → Tool Calls** — Trigger server actions from UI.
105
+ - **Dual Surface Sync** — Keep model aware of what users see with `data-llm`.
106
+ - **React Query-style API** — `isPending`, `isError`, callbacks.
107
+ - **MCP Compatible** — Extends the official SDK. Works with any MCP client.
280
108
 
281
- _useCallTool_ in asynchronous mode
109
+ <br />
282
110
 
283
- ```ts
284
- import { useCallTool } from "skybridge/web";
285
-
286
- export const TestTool: React.FunctionComponent = () => {
287
- const { callToolAsync, isPending } = useCallTool("myToolName");
288
-
289
- return (
290
- <div>
291
- <button
292
- disabled={isPending}
293
- onClick={async () => {
294
- const data = await callToolAsync({ input: "test input" });
295
- alert("Tool returned: " + data);
296
- }}
297
- >
298
- Call Tool inside a widget
299
- </button>
300
- </div>
301
- );
302
- };
303
- ```
111
+ <div align="center">
304
112
 
305
- ## Migrate your existing MCP server to a ChatGPT app
113
+ [![GitHub Discussions](https://img.shields.io/badge/Discussions-Ask%20Questions-blue?style=flat-square&logo=github)](https://github.com/alpic-ai/skybridge/discussions)
114
+ [![GitHub Issues](https://img.shields.io/badge/Issues-Report%20Bugs-red?style=flat-square&logo=github)](https://github.com/alpic-ai/skybridge/issues)
115
+ [![Discord](https://img.shields.io/badge/Discord-Chat-5865F2?style=flat-square&logo=discord&logoColor=white)](https://discord.com/invite/gNAazGueab)
306
116
 
307
- If you're already using the `@modelcontextprotocol/sdk` to build a MCP server, you can migrate to a ChatGPT app by following these steps:
117
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions
308
118
 
309
- 1. Replace your `McpServer` import from `@modelcontextprotocol/sdk` with the same import from `skybridge/server`
310
- 2. Create a new vite project in a folder named `web` and install the `skybridge` package
311
- 3. Replace the `vite.config.ts` file with the following:
119
+ <br />
312
120
 
313
- ```ts
314
- import { defineConfig } from "vite";
315
- import { skybridge } from "skybridge/web";
121
+ **[MIT License](LICENSE)** · Made with ❤️ by **[Alpic](https://alpic.ai)**
316
122
 
317
- export default defineConfig({
318
- plugins: [skybridge()],
319
- });
320
- ```
123
+ </div>
@@ -0,0 +1,15 @@
1
+ import { type RequestHandler } from "express";
2
+ /**
3
+ * Serve the built emulator React app
4
+ * This router serves static files from the emulator's dist directory.
5
+ * It should be installed at the application root, like so:
6
+ *
7
+ * const app = express();
8
+ *
9
+ * if (env.NODE_ENV !== "production") {
10
+ * app.use(await emulatorStaticServer(server));
11
+ * app.use(await widgetsDevServer());
12
+ * ^^^^^^^^ Make sure to install the emulatorStaticServer before the widgetsDevServer
13
+ * }
14
+ */
15
+ export declare const emulatorStaticServer: () => Promise<RequestHandler>;
@@ -0,0 +1,38 @@
1
+ import { readFileSync } from "node:fs";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ import cors from "cors";
5
+ import express, {} from "express";
6
+ /**
7
+ * Serve the built emulator React app
8
+ * This router serves static files from the emulator's dist directory.
9
+ * It should be installed at the application root, like so:
10
+ *
11
+ * const app = express();
12
+ *
13
+ * if (env.NODE_ENV !== "production") {
14
+ * app.use(await emulatorStaticServer(server));
15
+ * app.use(await widgetsDevServer());
16
+ * ^^^^^^^^ Make sure to install the emulatorStaticServer before the widgetsDevServer
17
+ * }
18
+ */
19
+ export const emulatorStaticServer = async () => {
20
+ const router = express.Router();
21
+ const currentDir = path.dirname(fileURLToPath(import.meta.url));
22
+ const emulatorPath = path.join(currentDir, "..", "emulator");
23
+ router.use(cors());
24
+ router.use(express.static(emulatorPath));
25
+ router.get("/", (_req, res, next) => {
26
+ const indexHtmlPath = path.join(emulatorPath, "index.html");
27
+ try {
28
+ const indexHtml = readFileSync(indexHtmlPath, "utf-8");
29
+ res.setHeader("Content-Type", "text/html");
30
+ res.send(indexHtml);
31
+ }
32
+ catch (error) {
33
+ next(error);
34
+ }
35
+ });
36
+ return router;
37
+ };
38
+ //# sourceMappingURL=emulatorStaticServer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"emulatorStaticServer.js","sourceRoot":"","sources":["../../../src/server/emulatorStaticServer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,OAAO,EAAE,EAAuB,MAAM,SAAS,CAAC;AAEvD;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,KAAK,IAA6B,EAAE;IACtE,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAChC,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;IAChE,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,UAAU,CAAC,CAAC;IAE7D,MAAM,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IAEnB,MAAM,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC;IAEzC,MAAM,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAClC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;QAC5D,IAAI,CAAC;YACH,MAAM,SAAS,GAAG,YAAY,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;YACvD,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,WAAW,CAAC,CAAC;YAC3C,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACtB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,KAAK,CAAC,CAAC;QACd,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
@@ -1,4 +1,5 @@
1
+ export { emulatorStaticServer } from "./emulatorStaticServer.js";
2
+ export type { AnyToolRegistry, InferTools, ToolInput, ToolNames, ToolOutput, ToolResponseMetadata, } from "./inferUtilityTypes.js";
3
+ export type { McpServerTypes, ToolDef } from "./server.js";
1
4
  export { McpServer } from "./server.js";
2
5
  export { widgetsDevServer } from "./widgetsDevServer.js";
3
- export type { WidgetDef } from "./server.js";
4
- export type { InferWidgets, AnyWidgetRegistry, WidgetNames, WidgetInput, WidgetOutput, } from "./inferUtilityTypes.js";
@@ -1,3 +1,4 @@
1
+ export { emulatorStaticServer } from "./emulatorStaticServer.js";
1
2
  export { McpServer } from "./server.js";
2
3
  export { widgetsDevServer } from "./widgetsDevServer.js";
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +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"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAUjE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC"}
@@ -1,33 +1,64 @@
1
- import type { McpServer, WidgetDef } from "./server.js";
2
- /** Any widget registry shape */
3
- export type AnyWidgetRegistry = Record<string, WidgetDef>;
1
+ import type { McpServerTypes, ToolDef } from "./server.js";
4
2
  /**
5
- * Extract the widget registry type from an McpServer instance.
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
+ * Uses the `$types` property pattern for cross-package type inference.
12
+ * This works across package boundaries because TypeScript uses structural typing
13
+ * on the shape of `$types`, rather than nominal typing on the McpServer class itself.
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * type MyTools = InferTools<MyServer>;
18
+ * // { "search": ToolDef<...>, "calculate": ToolDef<...> }
19
+ * ```
20
+ */
21
+ export type InferTools<ServerType> = ServerType extends {
22
+ $types: McpServerTypes<infer W>;
23
+ } ? W : never;
24
+ type ExtractTool<ServerType, K extends ToolNames<ServerType>> = InferTools<ServerType>[K];
25
+ /**
26
+ * Get a union of all tool names from an McpServer instance.
27
+ * This includes both widgets and regular tools.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * type Names = ToolNames<MyServer>;
32
+ * // "search" | "calculate" | "details"
33
+ * ```
34
+ */
35
+ export type ToolNames<ServerType> = keyof InferTools<ServerType> & string;
36
+ /**
37
+ * Get the input type for a specific tool (widget or regular tool).
6
38
  *
7
39
  * @example
8
40
  * ```ts
9
- * type MyWidgets = InferWidgets<MyServer>;
10
- * // { "search": WidgetDef<...>, "details": WidgetDef<...> }
41
+ * type SearchInput = ToolInput<MyServer, "search">;
11
42
  * ```
12
43
  */
13
- export type InferWidgets<T> = T extends McpServer<infer W extends AnyWidgetRegistry> ? W : T extends McpServer<any> ? never : "Error: T must be an McpServer instance";
14
- /** Get a union of all widget names from an McpServer instance */
15
- export type WidgetNames<T> = keyof InferWidgets<T> & string;
44
+ export type ToolInput<ServerType, ToolName extends ToolNames<ServerType>> = ExtractTool<ServerType, ToolName>["input"];
16
45
  /**
17
- * Get the input type for a specific widget.
46
+ * Get the output type for a specific tool (widget or regular tool).
18
47
  *
19
48
  * @example
20
49
  * ```ts
21
- * type SearchInput = WidgetInput<MyServer, "search">;
50
+ * type SearchOutput = ToolOutput<MyServer, "search">;
22
51
  * ```
23
52
  */
24
- export type WidgetInput<T, K extends WidgetNames<T>> = InferWidgets<T> extends Record<string, WidgetDef> ? K extends keyof InferWidgets<T> ? InferWidgets<T>[K]["input"] : never : never;
53
+ export type ToolOutput<ServerType, ToolName extends ToolNames<ServerType>> = ExtractTool<ServerType, ToolName>["output"];
25
54
  /**
26
- * Get the output type for a specific widget.
55
+ * Get the responseMetadata type for a specific tool (widget or regular tool).
56
+ * This is inferred from the `_meta` property of the tool callback's return value.
27
57
  *
28
58
  * @example
29
59
  * ```ts
30
- * type SearchOutput = WidgetOutput<MyServer, "search">;
60
+ * type SearchMeta = ToolResponseMetadata<MyServer, "search">;
31
61
  * ```
32
62
  */
33
- export type WidgetOutput<T, K extends WidgetNames<T>> = InferWidgets<T> extends Record<string, WidgetDef> ? K extends keyof InferWidgets<T> ? InferWidgets<T>[K]["output"] : never : never;
63
+ export type ToolResponseMetadata<ServerType, ToolName extends ToolNames<ServerType>> = ExtractTool<ServerType, ToolName>["responseMetadata"];
64
+ export {};
@@ -1,19 +1,62 @@
1
- import { McpServer as McpServerBase, type ToolCallback } from "@modelcontextprotocol/sdk/server/mcp.js";
2
- import type { Resource } from "@modelcontextprotocol/sdk/types.js";
3
- import type { ZodRawShape, ZodObject, infer as Infer } from "zod";
4
- export type WidgetDef<TInput = unknown, TOutput = unknown> = {
1
+ import { McpServer as McpServerBase, type RegisteredTool } from "@modelcontextprotocol/sdk/server/mcp.js";
2
+ import type { AnySchema, SchemaOutput, ZodRawShapeCompat } from "@modelcontextprotocol/sdk/server/zod-compat.js";
3
+ import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
4
+ import type { CallToolResult, Resource, ServerNotification, ServerRequest, ToolAnnotations } from "@modelcontextprotocol/sdk/types.js";
5
+ export type ToolDef<TInput = unknown, TOutput = unknown, TResponseMetadata = unknown> = {
5
6
  input: TInput;
6
7
  output: TOutput;
8
+ responseMetadata: TResponseMetadata;
7
9
  };
8
10
  type McpServerOriginalResourceConfig = Omit<Resource, "uri" | "name" | "mimeType">;
9
- type McpServerOriginalToolConfig = Omit<Parameters<McpServerBase["registerTool"]>[1], "inputSchema" | "outputSchema">;
10
- export declare class McpServer<TWidgets extends Record<string, WidgetDef> = {}> extends McpServerBase {
11
- widget<TName extends string, TInput extends ZodRawShape, TOutput extends ZodRawShape = {}>(name: TName, resourceConfig: McpServerOriginalResourceConfig, toolConfig: McpServerOriginalToolConfig & {
11
+ type McpServerOriginalToolConfig = Omit<Parameters<typeof McpServerBase.prototype.registerTool<ZodRawShapeCompat, ZodRawShapeCompat>>[1], "inputSchema" | "outputSchema">;
12
+ type ExtractStructuredContent<T> = T extends {
13
+ structuredContent: infer SC;
14
+ } ? SC : never;
15
+ type ExtractMeta<T> = Extract<T, {
16
+ _meta: unknown;
17
+ }> extends {
18
+ _meta: infer M;
19
+ } ? M : unknown;
20
+ /**
21
+ * Type-level marker interface for cross-package type inference.
22
+ * This enables TypeScript to infer tool types across package boundaries
23
+ * using structural typing on the $types property, rather than relying on
24
+ * class generic inference which fails when McpServer comes from different
25
+ * package installations.
26
+ *
27
+ * Inspired by tRPC's _def pattern and Hono's type markers.
28
+ */
29
+ export interface McpServerTypes<TTools extends Record<string, ToolDef> = {}> {
30
+ readonly tools: TTools;
31
+ }
32
+ type Simplify<T> = {
33
+ [K in keyof T]: T[K];
34
+ };
35
+ type ShapeOutput<Shape extends ZodRawShapeCompat> = Simplify<{
36
+ [K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? never : K]: SchemaOutput<Shape[K]>;
37
+ } & {
38
+ [K in keyof Shape as undefined extends SchemaOutput<Shape[K]> ? K : never]?: SchemaOutput<Shape[K]>;
39
+ }>;
40
+ type AddTool<TTools, TName extends string, TInput extends ZodRawShapeCompat, TOutput, TResponseMetadata = unknown> = McpServer<TTools & {
41
+ [K in TName]: ToolDef<ShapeOutput<TInput>, TOutput, TResponseMetadata>;
42
+ }>;
43
+ type ToolConfig<TInput extends ZodRawShapeCompat | AnySchema> = {
44
+ title?: string;
45
+ description?: string;
46
+ inputSchema?: TInput;
47
+ outputSchema?: ZodRawShapeCompat | AnySchema;
48
+ annotations?: ToolAnnotations;
49
+ _meta?: Record<string, unknown>;
50
+ };
51
+ type ToolHandler<TInput extends ZodRawShapeCompat, TReturn extends CallToolResult = CallToolResult> = (args: ShapeOutput<TInput>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => TReturn | Promise<TReturn>;
52
+ export declare class McpServer<TTools extends Record<string, ToolDef> = {}> extends McpServerBase {
53
+ readonly $types: McpServerTypes<TTools>;
54
+ registerWidget<TName extends string, TInput extends ZodRawShapeCompat, TReturn extends CallToolResult>(name: TName, resourceConfig: McpServerOriginalResourceConfig, toolConfig: McpServerOriginalToolConfig & {
12
55
  inputSchema?: TInput;
13
- outputSchema?: TOutput;
14
- }, toolCallback: ToolCallback<TInput>): McpServer<TWidgets & {
15
- [K in TName]: WidgetDef<Infer<ZodObject<TInput>>, Infer<ZodObject<TOutput>>>;
16
- }>;
56
+ outputSchema?: ZodRawShapeCompat | AnySchema;
57
+ }, toolCallback: ToolHandler<TInput, TReturn>): AddTool<TTools, TName, TInput, ExtractStructuredContent<TReturn>, ExtractMeta<TReturn>>;
58
+ registerTool<TName extends string, InputArgs extends ZodRawShapeCompat, TReturn extends CallToolResult>(name: TName, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs, TReturn>): AddTool<TTools, TName, InputArgs, ExtractStructuredContent<TReturn>, ExtractMeta<TReturn>>;
59
+ registerTool<InputArgs extends ZodRawShapeCompat>(name: string, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs>): RegisteredTool;
17
60
  private lookupDistFile;
18
61
  }
19
62
  export {};