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.
- package/LICENSE +21 -674
- package/README.md +70 -267
- package/dist/src/server/emulatorStaticServer.d.ts +15 -0
- package/dist/src/server/emulatorStaticServer.js +38 -0
- package/dist/src/server/emulatorStaticServer.js.map +1 -0
- package/dist/src/server/index.d.ts +3 -2
- package/dist/src/server/index.js +1 -0
- package/dist/src/server/index.js.map +1 -1
- package/dist/src/server/inferUtilityTypes.d.ts +46 -15
- package/dist/src/server/server.d.ts +54 -11
- package/dist/src/server/server.js +8 -4
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/server/templateHelper.js +2 -2
- package/dist/src/server/templateHelper.js.map +1 -1
- package/dist/src/server/widgetsDevServer.js +3 -3
- package/dist/src/server/widgetsDevServer.js.map +1 -1
- package/dist/src/test/utils.d.ts +97 -1
- package/dist/src/test/utils.js +181 -2
- package/dist/src/test/utils.js.map +1 -1
- package/dist/src/test/widget.test.js +19 -20
- package/dist/src/test/widget.test.js.map +1 -1
- package/dist/src/web/create-store.d.ts +3 -0
- package/dist/src/web/create-store.js +25 -0
- package/dist/src/web/create-store.js.map +1 -0
- package/dist/src/web/create-store.test.js +70 -0
- package/dist/src/web/create-store.test.js.map +1 -0
- package/dist/src/web/data-llm.d.ts +14 -0
- package/dist/src/web/data-llm.js +68 -0
- package/dist/src/web/data-llm.js.map +1 -0
- package/dist/src/web/data-llm.test.js +76 -0
- package/dist/src/web/data-llm.test.js.map +1 -0
- package/dist/src/web/generate-helpers.d.ts +115 -0
- package/dist/src/web/generate-helpers.js +111 -0
- package/dist/src/web/generate-helpers.js.map +1 -0
- package/dist/src/web/generate-helpers.test-d.d.ts +1 -0
- package/dist/src/web/generate-helpers.test-d.js +180 -0
- package/dist/src/web/generate-helpers.test-d.js.map +1 -0
- package/dist/src/web/generate-helpers.test.d.ts +1 -0
- package/dist/src/web/generate-helpers.test.js +17 -0
- package/dist/src/web/generate-helpers.test.js.map +1 -0
- package/dist/src/web/helpers/state.d.ts +7 -0
- package/dist/src/web/helpers/state.js +40 -0
- package/dist/src/web/helpers/state.js.map +1 -0
- package/dist/src/web/helpers/state.test.d.ts +1 -0
- package/dist/src/web/helpers/state.test.js +53 -0
- package/dist/src/web/helpers/state.test.js.map +1 -0
- package/dist/src/web/hooks/index.d.ts +2 -4
- package/dist/src/web/hooks/index.js +2 -4
- package/dist/src/web/hooks/index.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.d.ts +57 -19
- package/dist/src/web/hooks/use-call-tool.js +7 -15
- package/dist/src/web/hooks/use-call-tool.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.test-d.d.ts +1 -0
- package/dist/src/web/hooks/use-call-tool.test-d.js +104 -0
- package/dist/src/web/hooks/use-call-tool.test-d.js.map +1 -0
- package/dist/src/web/hooks/use-call-tool.test.js +18 -10
- package/dist/src/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/src/web/hooks/use-display-mode.js.map +1 -1
- package/dist/src/web/hooks/use-display-mode.test.js +2 -2
- package/dist/src/web/hooks/use-display-mode.test.js.map +1 -1
- package/dist/src/web/hooks/use-files.test.js +1 -1
- package/dist/src/web/hooks/use-files.test.js.map +1 -1
- package/dist/src/web/hooks/use-locale.test.js +2 -2
- package/dist/src/web/hooks/use-locale.test.js.map +1 -1
- package/dist/src/web/hooks/use-open-external.test.js +2 -2
- package/dist/src/web/hooks/use-open-external.test.js.map +1 -1
- package/dist/src/web/hooks/use-openai-global.d.ts +2 -2
- package/dist/src/web/hooks/use-openai-global.js +1 -1
- package/dist/src/web/hooks/use-openai-global.js.map +1 -1
- package/dist/src/web/hooks/use-request-modal.test.js +2 -2
- package/dist/src/web/hooks/use-request-modal.test.js.map +1 -1
- package/dist/src/web/hooks/use-theme.test.js +2 -2
- package/dist/src/web/hooks/use-theme.test.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.d.ts +1 -1
- package/dist/src/web/hooks/use-tool-info.js +1 -1
- package/dist/src/web/hooks/use-tool-info.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test-d.js +0 -1
- package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test.js +2 -2
- package/dist/src/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/src/web/hooks/use-user-agent.test.js +2 -2
- package/dist/src/web/hooks/use-user-agent.test.js.map +1 -1
- package/dist/src/web/hooks/use-widget-state.js +9 -7
- package/dist/src/web/hooks/use-widget-state.js.map +1 -1
- package/dist/src/web/hooks/use-widget-state.test.js +2 -2
- package/dist/src/web/hooks/use-widget-state.test.js.map +1 -1
- package/dist/src/web/index.d.ts +6 -4
- package/dist/src/web/index.js +6 -4
- package/dist/src/web/index.js.map +1 -1
- package/dist/src/web/plugin/data-llm.test.d.ts +1 -0
- package/dist/src/web/plugin/data-llm.test.js +81 -0
- package/dist/src/web/plugin/data-llm.test.js.map +1 -0
- package/dist/src/web/{plugin.js → plugin/plugin.js} +5 -0
- package/dist/src/web/plugin/plugin.js.map +1 -0
- package/dist/src/web/plugin/transform-data-llm.d.ts +12 -0
- package/dist/src/web/plugin/transform-data-llm.js +93 -0
- package/dist/src/web/plugin/transform-data-llm.js.map +1 -0
- package/dist/src/web/plugin/transform-data-llm.test.d.ts +1 -0
- package/dist/src/web/plugin/transform-data-llm.test.js +81 -0
- package/dist/src/web/plugin/transform-data-llm.test.js.map +1 -0
- package/dist/src/web/proxy.js +1 -1
- package/dist/src/web/proxy.js.map +1 -1
- package/dist/src/web/types.d.ts +18 -4
- package/dist/src/web/types.js +1 -0
- package/dist/src/web/types.js.map +1 -1
- package/package.json +14 -6
- package/dist/src/web/hooks/use-tool-output.d.ts +0 -4
- package/dist/src/web/hooks/use-tool-output.js +0 -9
- package/dist/src/web/hooks/use-tool-output.js.map +0 -1
- package/dist/src/web/hooks/use-tool-response-metadata.d.ts +0 -4
- package/dist/src/web/hooks/use-tool-response-metadata.js +0 -8
- package/dist/src/web/hooks/use-tool-response-metadata.js.map +0 -1
- package/dist/src/web/plugin.js.map +0 -1
- package/dist/src/web/typed-hooks.d.ts +0 -61
- package/dist/src/web/typed-hooks.js +0 -61
- package/dist/src/web/typed-hooks.js.map +0 -1
- package/dist/src/web/typed-hooks.test-d.js +0 -72
- package/dist/src/web/typed-hooks.test-d.js.map +0 -1
- package/dist/src/web/typed-hooks.test.js +0 -10
- package/dist/src/web/typed-hooks.test.js.map +0 -1
- /package/dist/src/web/{typed-hooks.test-d.d.ts → create-store.test.d.ts} +0 -0
- /package/dist/src/web/{typed-hooks.test.d.ts → data-llm.test.d.ts} +0 -0
- /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
|
-
|
|
4
|
-
|
|
5
|
-
**Skybridge is the TypeScript framework for building ChatGPT apps**
|
|
6
|
-
|
|
7
|
-
[](https://alpic.ai)
|
|
3
|
+
<img alt="Skybridge" src="docs/static/img/logo.png" width="220">
|
|
8
4
|
|
|
9
|
-
|
|
10
|
-

|
|
11
|
-

|
|
5
|
+
# Skybridge
|
|
12
6
|
|
|
13
|
-
|
|
7
|
+
**Build ChatGPT Apps. The Modern TypeScript Way.**
|
|
14
8
|
|
|
15
|
-
|
|
9
|
+
The fullstack TypeScript framework for ChatGPT Apps.<br />
|
|
10
|
+
**Type-safe. React-powered. Zero config.**
|
|
16
11
|
|
|
17
|
-
|
|
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
|
-
|
|
14
|
+
[](https://www.npmjs.com/package/skybridge)
|
|
15
|
+
[](https://www.npmjs.com/package/skybridge)
|
|
16
|
+
[](https://github.com/alpic-ai/skybridge/blob/main/LICENSE)
|
|
21
17
|
|
|
22
|
-
|
|
18
|
+
<br />
|
|
23
19
|
|
|
24
|
-
|
|
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
|
-
|
|
27
|
-
pnpm add skybridge
|
|
28
|
-
```
|
|
22
|
+
</div>
|
|
29
23
|
|
|
30
|
-
|
|
24
|
+
<br />
|
|
31
25
|
|
|
32
|
-
|
|
26
|
+
## ✨ Why Skybridge?
|
|
33
27
|
|
|
34
|
-
|
|
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
|
-
|
|
30
|
+
**Skybridge fixes that.**
|
|
37
31
|
|
|
38
|
-
|
|
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
|
-
|
|
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
|
-
|
|
41
|
+
**Create a new ChatGPT app:**
|
|
53
42
|
|
|
54
|
-
```
|
|
55
|
-
|
|
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
|
-
|
|
48
|
+
**Or add to an existing project:**
|
|
66
49
|
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
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
|
-
|
|
60
|
+
**👉 [Read the Docs](https://skybridge.tech) 👈**
|
|
90
61
|
|
|
91
|
-
|
|
92
|
-
import { defineConfig } from "vite";
|
|
93
|
-
import { skybridge } from "skybridge/web";
|
|
62
|
+
</div>
|
|
94
63
|
|
|
95
|
-
|
|
96
|
-
plugins: [skybridge()],
|
|
97
|
-
});
|
|
98
|
-
```
|
|
64
|
+
<br />
|
|
99
65
|
|
|
100
|
-
|
|
66
|
+
## 📦 The Stack
|
|
101
67
|
|
|
102
|
-
|
|
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
|
-
|
|
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
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
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
|
-
|
|
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
|
-
|
|
216
|
-
|
|
217
|
-
output: { results: string[] };
|
|
218
|
-
responseMetadata: { id: number };
|
|
219
|
-
}>();
|
|
89
|
+
function FlightsWidget() {
|
|
90
|
+
const { output } = useToolInfo();
|
|
220
91
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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
|
-
|
|
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
|
-
|
|
248
|
-
return <div>Found {toolInfo.output.totalCount} results</div>;
|
|
249
|
-
}
|
|
100
|
+
## 🎯 Features at a Glance
|
|
250
101
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
109
|
+
<br />
|
|
282
110
|
|
|
283
|
-
|
|
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
|
-
|
|
113
|
+
[](https://github.com/alpic-ai/skybridge/discussions)
|
|
114
|
+
[](https://github.com/alpic-ai/skybridge/issues)
|
|
115
|
+
[](https://discord.com/invite/gNAazGueab)
|
|
306
116
|
|
|
307
|
-
|
|
117
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions
|
|
308
118
|
|
|
309
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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";
|
package/dist/src/server/index.js
CHANGED
|
@@ -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 {
|
|
2
|
-
/** Any widget registry shape */
|
|
3
|
-
export type AnyWidgetRegistry = Record<string, WidgetDef>;
|
|
1
|
+
import type { McpServerTypes, ToolDef } from "./server.js";
|
|
4
2
|
/**
|
|
5
|
-
*
|
|
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
|
|
10
|
-
* // { "search": WidgetDef<...>, "details": WidgetDef<...> }
|
|
41
|
+
* type SearchInput = ToolInput<MyServer, "search">;
|
|
11
42
|
* ```
|
|
12
43
|
*/
|
|
13
|
-
export type
|
|
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
|
|
46
|
+
* Get the output type for a specific tool (widget or regular tool).
|
|
18
47
|
*
|
|
19
48
|
* @example
|
|
20
49
|
* ```ts
|
|
21
|
-
* type
|
|
50
|
+
* type SearchOutput = ToolOutput<MyServer, "search">;
|
|
22
51
|
* ```
|
|
23
52
|
*/
|
|
24
|
-
export type
|
|
53
|
+
export type ToolOutput<ServerType, ToolName extends ToolNames<ServerType>> = ExtractTool<ServerType, ToolName>["output"];
|
|
25
54
|
/**
|
|
26
|
-
* Get the
|
|
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
|
|
60
|
+
* type SearchMeta = ToolResponseMetadata<MyServer, "search">;
|
|
31
61
|
* ```
|
|
32
62
|
*/
|
|
33
|
-
export type
|
|
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
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
|
|
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
|
|
10
|
-
|
|
11
|
-
|
|
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?:
|
|
14
|
-
}, toolCallback:
|
|
15
|
-
|
|
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 {};
|