skybridge 0.0.0-dev.e9c6623 → 0.0.0-dev.ea7f5b5
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/README.md +264 -5
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/inferUtilityTypes.d.ts +54 -0
- package/dist/src/server/inferUtilityTypes.js +2 -0
- package/dist/src/server/inferUtilityTypes.js.map +1 -0
- package/dist/src/server/server.d.ts +44 -9
- package/dist/src/server/server.js +10 -2
- package/dist/src/server/server.js.map +1 -1
- package/dist/src/test/utils.d.ts +61 -0
- package/dist/src/test/utils.js +122 -1
- package/dist/src/test/utils.js.map +1 -1
- package/dist/src/test/widget.test.js +3 -3
- 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.d.ts +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 +113 -0
- package/dist/src/web/generate-helpers.js +109 -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 +153 -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 +3 -4
- package/dist/src/web/hooks/index.js +3 -4
- package/dist/src/web/hooks/index.js.map +1 -1
- package/dist/src/web/hooks/use-call-tool.d.ts +69 -23
- package/dist/src/web/hooks/use-call-tool.js +26 -10
- 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 +108 -3
- package/dist/src/web/hooks/use-call-tool.test.js.map +1 -1
- package/dist/src/web/hooks/use-files.d.ts +10 -0
- package/dist/src/web/hooks/use-files.js +7 -0
- package/dist/src/web/hooks/use-files.js.map +1 -0
- package/dist/src/web/hooks/use-files.test.d.ts +1 -0
- package/dist/src/web/hooks/use-files.test.js +29 -0
- package/dist/src/web/hooks/use-files.test.js.map +1 -0
- package/dist/src/web/hooks/use-request-modal.d.ts +3 -2
- package/dist/src/web/hooks/use-request-modal.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.d.ts +22 -11
- package/dist/src/web/hooks/use-tool-info.js +3 -2
- package/dist/src/web/hooks/use-tool-info.js.map +1 -1
- package/dist/src/web/hooks/use-tool-info.test-d.d.ts +1 -0
- package/dist/src/web/hooks/use-tool-info.test-d.js +74 -0
- package/dist/src/web/hooks/use-tool-info.test-d.js.map +1 -0
- package/dist/src/web/hooks/use-tool-info.test.js +30 -29
- package/dist/src/web/hooks/use-tool-info.test.js.map +1 -1
- package/dist/src/web/hooks/use-widget-state.js +8 -6
- package/dist/src/web/hooks/use-widget-state.js.map +1 -1
- package/dist/src/web/hooks/use-widget-state.test.js +0 -1
- package/dist/src/web/hooks/use-widget-state.test.js.map +1 -1
- package/dist/src/web/index.d.ts +4 -1
- package/dist/src/web/index.js +4 -1
- package/dist/src/web/index.js.map +1 -1
- package/dist/src/web/mount-widget.js +5 -0
- package/dist/src/web/mount-widget.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.d.ts +1 -0
- package/dist/src/web/proxy.js +48 -0
- package/dist/src/web/proxy.js.map +1 -0
- package/dist/src/web/types.d.ts +38 -16
- package/dist/src/web/types.js +1 -0
- package/dist/src/web/types.js.map +1 -1
- package/dist/vitest.config.js +0 -1
- package/dist/vitest.config.js.map +1 -1
- package/package.json +7 -3
- package/dist/src/test/setup.js +0 -9
- package/dist/src/test/setup.js.map +0 -1
- 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/{test/setup.d.ts → web/create-store.test.d.ts} +0 -0
- /package/dist/src/web/{plugin.d.ts → plugin/plugin.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -12,14 +12,20 @@
|
|
|
12
12
|
|
|
13
13
|
</div>
|
|
14
14
|
|
|
15
|
-
Skybridge comes with
|
|
15
|
+
Skybridge is a fullstack library. It comes with modular packages aiming at simplifying the development of ChatGPT apps:
|
|
16
16
|
|
|
17
|
-
-
|
|
18
|
-
-
|
|
17
|
+
- HMR server for development and build pipeline for production including end-to-end (tools-to-widget) type safety
|
|
18
|
+
- collection of React hooks for state management, async data fetching
|
|
19
|
+
- attribute for widget-to-context synchronization
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
### <a href="https://skybridge.tech">Read the docs →</b></a>
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
## Get Involved
|
|
24
|
+
|
|
25
|
+
We welcome issues and pull requests!</br>
|
|
26
|
+
Participate in [GitHub discussions](https://github.com/alpic-ai/skybridge/discussions)</br>
|
|
27
|
+
Chat with the community on [Discord](https://discord.com/invite/gNAazGueab)</br>
|
|
28
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for setup instructions
|
|
23
29
|
|
|
24
30
|
## Installation
|
|
25
31
|
|
|
@@ -97,6 +103,166 @@ export default defineConfig({
|
|
|
97
103
|
});
|
|
98
104
|
```
|
|
99
105
|
|
|
106
|
+
**Typed Hooks**
|
|
107
|
+
|
|
108
|
+
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.
|
|
109
|
+
|
|
110
|
+
> **Tip:** For the best TypeScript experience, use typed hooks throughout your application. They provide autocomplete, type safety, and better IDE support.
|
|
111
|
+
|
|
112
|
+
> **Important:** For `generateHelpers` to work correctly, your MCP server must be defined using method chaining (e.g., `server.widget(...).widget(...).registerTool(...)`). This ensures TypeScript can properly infer the tool registry type from the chained calls.
|
|
113
|
+
|
|
114
|
+
**Examples:**
|
|
115
|
+
|
|
116
|
+
✅ **Works** - Using method chaining:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { McpServer } from "skybridge/server";
|
|
120
|
+
import { z } from "zod";
|
|
121
|
+
|
|
122
|
+
const server = new McpServer({ name: "my-app", version: "1.0" }, {})
|
|
123
|
+
.widget(
|
|
124
|
+
"search-voyage",
|
|
125
|
+
{},
|
|
126
|
+
{
|
|
127
|
+
inputSchema: { destination: z.string() },
|
|
128
|
+
},
|
|
129
|
+
async ({ destination }) => {
|
|
130
|
+
return {
|
|
131
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
)
|
|
135
|
+
.registerTool(
|
|
136
|
+
"calculate-price",
|
|
137
|
+
{
|
|
138
|
+
inputSchema: { tripId: z.string() },
|
|
139
|
+
},
|
|
140
|
+
async ({ tripId }) => {
|
|
141
|
+
return { content: [{ type: "text", text: `Price for ${tripId}` }] };
|
|
142
|
+
}
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
export type AppType = typeof server; // ✅ Type inference works correctly
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
❌ **Doesn't work** - Without method chaining:
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
import { McpServer } from "skybridge/server";
|
|
152
|
+
import { z } from "zod";
|
|
153
|
+
|
|
154
|
+
const server = new McpServer({ name: "my-app", version: "1.0" }, {});
|
|
155
|
+
|
|
156
|
+
server.widget(
|
|
157
|
+
"search-voyage",
|
|
158
|
+
{},
|
|
159
|
+
{
|
|
160
|
+
inputSchema: { destination: z.string() },
|
|
161
|
+
},
|
|
162
|
+
async ({ destination }) => {
|
|
163
|
+
return {
|
|
164
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
server.registerTool(
|
|
170
|
+
"calculate-price",
|
|
171
|
+
{
|
|
172
|
+
inputSchema: { tripId: z.string() },
|
|
173
|
+
},
|
|
174
|
+
async ({ tripId }) => {
|
|
175
|
+
return { content: [{ type: "text", text: `Price for ${tripId}` }] };
|
|
176
|
+
}
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
export type AppType = typeof server; // ❌ Type inference fails - tool registry is empty
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
_Server setup (server/src/index.ts)_
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
import { McpServer } from "skybridge/server";
|
|
186
|
+
import { z } from "zod";
|
|
187
|
+
|
|
188
|
+
const server = new McpServer({ name: "my-app", version: "1.0" }, {})
|
|
189
|
+
.widget(
|
|
190
|
+
"search-voyage",
|
|
191
|
+
{},
|
|
192
|
+
{
|
|
193
|
+
description: "Search for trips",
|
|
194
|
+
inputSchema: {
|
|
195
|
+
destination: z.string(),
|
|
196
|
+
departureDate: z.string().optional(),
|
|
197
|
+
},
|
|
198
|
+
outputSchema: {
|
|
199
|
+
results: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
200
|
+
totalCount: z.number(),
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
async ({ destination }) => {
|
|
204
|
+
// Your tool logic here...
|
|
205
|
+
return {
|
|
206
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
)
|
|
210
|
+
.widget(
|
|
211
|
+
"get-details",
|
|
212
|
+
{},
|
|
213
|
+
{
|
|
214
|
+
inputSchema: { tripId: z.string() },
|
|
215
|
+
},
|
|
216
|
+
async ({ tripId }) => {
|
|
217
|
+
return { content: [{ type: "text", text: `Details for ${tripId}` }] };
|
|
218
|
+
}
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
// Export the server type for the client
|
|
222
|
+
export type AppType = typeof server;
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
_One-time setup (web/src/skybridge.ts)_
|
|
226
|
+
|
|
227
|
+
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:
|
|
228
|
+
|
|
229
|
+
```ts
|
|
230
|
+
import type { AppType } from "../server"; // type-only import
|
|
231
|
+
import { generateHelpers } from "skybridge/web";
|
|
232
|
+
|
|
233
|
+
export const { useCallTool, useToolInfo } = generateHelpers<AppType>();
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
_Usage in widgets (web/src/widgets/search.tsx)_
|
|
237
|
+
|
|
238
|
+
```tsx
|
|
239
|
+
import { useCallTool, useToolInfo } from "../skybridge"; // import typed hooks
|
|
240
|
+
|
|
241
|
+
export function SearchWidget() {
|
|
242
|
+
const { callTool, data, isPending } = useCallTool("search-voyage");
|
|
243
|
+
// ^ autocomplete for tool names
|
|
244
|
+
const toolInfo = useToolInfo<"search-voyage">();
|
|
245
|
+
// ^ autocomplete for widget names
|
|
246
|
+
|
|
247
|
+
const handleSearch = () => {
|
|
248
|
+
callTool({ destination: "Spain" });
|
|
249
|
+
// ^ autocomplete for input fields
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<div>
|
|
254
|
+
<button onClick={handleSearch} disabled={isPending}>
|
|
255
|
+
Search
|
|
256
|
+
</button>
|
|
257
|
+
{toolInfo.isSuccess && (
|
|
258
|
+
<div>Found {toolInfo.output.structuredContent.totalCount} results</div>
|
|
259
|
+
// ^ typed output
|
|
260
|
+
)}
|
|
261
|
+
</div>
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
100
266
|
**Hooks**
|
|
101
267
|
|
|
102
268
|
The `skybridge/web` package comes with a set of hooks to help you build your widgets :
|
|
@@ -104,7 +270,9 @@ The `skybridge/web` package comes with a set of hooks to help you build your wid
|
|
|
104
270
|
- `useOpenAiGlobal`: A generic hook to get any global data from the OpenAI iFrame skybridge runtime (in `window.openai`).
|
|
105
271
|
- `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.
|
|
106
272
|
- `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.
|
|
273
|
+
- `useToolInfo`: A hook to get the tool input, output, and response metadata with type inference. Provides a discriminated union based on status (pending/success).
|
|
107
274
|
- `useCallTool`: A @tanstack/react-query inspired hook to send make additional tool calls inside a widget.
|
|
275
|
+
- `generateHelpers`: A factory that creates typed versions of `useCallTool` and `useToolInfo` with full type inference from your server type.
|
|
108
276
|
|
|
109
277
|
_useOpenAiGlobal_
|
|
110
278
|
|
|
@@ -130,6 +298,53 @@ import { useToolResponseMetadata } from "skybridge/web";
|
|
|
130
298
|
const toolResponseMetadata = useToolResponseMetadata();
|
|
131
299
|
```
|
|
132
300
|
|
|
301
|
+
_useToolInfo_
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
import { useToolInfo } from "skybridge/web";
|
|
305
|
+
|
|
306
|
+
const toolInfo = useToolInfo<{
|
|
307
|
+
input: { query: string };
|
|
308
|
+
output: { results: string[] };
|
|
309
|
+
responseMetadata: { id: number };
|
|
310
|
+
}>();
|
|
311
|
+
|
|
312
|
+
// toolInfo.input is typed based on the input type
|
|
313
|
+
// toolInfo.output.structuredContent is typed based on the output type (undefined when pending)
|
|
314
|
+
// toolInfo.status narrows correctly: "pending" | "success"
|
|
315
|
+
|
|
316
|
+
if (toolInfo.isPending) {
|
|
317
|
+
// toolInfo.output is undefined here (pending state)
|
|
318
|
+
console.log(toolInfo.input.query);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (toolInfo.isSuccess) {
|
|
322
|
+
// toolInfo.output.structuredContent is typed here
|
|
323
|
+
console.log(toolInfo.output.structuredContent.results);
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
_useToolInfo_ with typed hooks (recommended)
|
|
328
|
+
|
|
329
|
+
```tsx
|
|
330
|
+
import { useToolInfo } from "../skybridge"; // import typed hooks
|
|
331
|
+
|
|
332
|
+
export function SearchWidget() {
|
|
333
|
+
const toolInfo = useToolInfo<"search-voyage">();
|
|
334
|
+
// ^ autocomplete for widget names
|
|
335
|
+
// toolInfo.input is typed as { destination: string; departureDate?: string; ... }
|
|
336
|
+
// toolInfo.output.structuredContent is typed as { results: Array<...>; totalCount: number; }
|
|
337
|
+
|
|
338
|
+
if (toolInfo.isSuccess) {
|
|
339
|
+
return (
|
|
340
|
+
<div>Found {toolInfo.output.structuredContent.totalCount} results</div>
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
return <div>Searching for {toolInfo.input.destination}...</div>;
|
|
345
|
+
}
|
|
346
|
+
```
|
|
347
|
+
|
|
133
348
|
_useCallTool_ in synchronous mode
|
|
134
349
|
|
|
135
350
|
```ts
|
|
@@ -180,6 +395,50 @@ export const TestTool: React.FunctionComponent = () => {
|
|
|
180
395
|
};
|
|
181
396
|
```
|
|
182
397
|
|
|
398
|
+
**State Management**
|
|
399
|
+
|
|
400
|
+
The `skybridge/web` package provides `createStore` for creating Zustand stores with automatic persistence to `window.openai.widgetState`. This is useful when you need more advanced state management than what `useWidgetState` provides.
|
|
401
|
+
|
|
402
|
+
_createStore_
|
|
403
|
+
|
|
404
|
+
```ts
|
|
405
|
+
import { createStore } from "skybridge/web";
|
|
406
|
+
|
|
407
|
+
type CounterState = {
|
|
408
|
+
count: number;
|
|
409
|
+
increment: () => void;
|
|
410
|
+
decrement: () => void;
|
|
411
|
+
};
|
|
412
|
+
|
|
413
|
+
const useCounterStore = createStore<CounterState>(
|
|
414
|
+
(set) => ({
|
|
415
|
+
count: 0,
|
|
416
|
+
increment: () => set((state) => ({ count: state.count + 1 })),
|
|
417
|
+
decrement: () => set((state) => ({ count: state.count - 1 })),
|
|
418
|
+
}),
|
|
419
|
+
{ count: 0 } // Optional default state
|
|
420
|
+
);
|
|
421
|
+
|
|
422
|
+
// Use in your component
|
|
423
|
+
function CounterWidget() {
|
|
424
|
+
const { count, increment, decrement } = useCounterStore();
|
|
425
|
+
|
|
426
|
+
return (
|
|
427
|
+
<div>
|
|
428
|
+
<p>Count: {count}</p>
|
|
429
|
+
<button onClick={increment}>Increment</button>
|
|
430
|
+
<button onClick={decrement}>Decrement</button>
|
|
431
|
+
</div>
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
The `createStore` function:
|
|
437
|
+
|
|
438
|
+
- Automatically persists state to `window.openai.widgetState`
|
|
439
|
+
- Syncs with external state changes from the host
|
|
440
|
+
- Only serializes state data, not methods
|
|
441
|
+
|
|
183
442
|
## Migrate your existing MCP server to a ChatGPT app
|
|
184
443
|
|
|
185
444
|
If you're already using the `@modelcontextprotocol/sdk` to build a MCP server, you can migrate to a ChatGPT app by following these steps:
|
|
@@ -1,2 +1,4 @@
|
|
|
1
1
|
export { McpServer } from "./server.js";
|
|
2
2
|
export { widgetsDevServer } from "./widgetsDevServer.js";
|
|
3
|
+
export type { ToolDef, McpServerTypes } from "./server.js";
|
|
4
|
+
export type { InferTools, AnyToolRegistry, ToolNames, ToolInput, ToolOutput, } from "./inferUtilityTypes.js";
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { McpServerTypes, 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
|
+
* 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).
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* type SearchInput = ToolInput<MyServer, "search">;
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export type ToolInput<ServerType, ToolName extends ToolNames<ServerType>> = ExtractTool<ServerType, ToolName>["input"];
|
|
45
|
+
/**
|
|
46
|
+
* Get the output type for a specific tool (widget or regular tool).
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* type SearchOutput = ToolOutput<MyServer, "search">;
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export type ToolOutput<ServerType, ToolName extends ToolNames<ServerType>> = ExtractTool<ServerType, ToolName>["output"];
|
|
54
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inferUtilityTypes.js","sourceRoot":"","sources":["../../../src/server/inferUtilityTypes.ts"],"names":[],"mappings":""}
|
|
@@ -1,13 +1,48 @@
|
|
|
1
|
-
import { McpServer as McpServerBase, type
|
|
2
|
-
import type { Resource } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
-
import type {
|
|
1
|
+
import { McpServer as McpServerBase, type RegisteredTool } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
2
|
+
import type { Resource, ToolAnnotations, CallToolResult, ServerRequest, ServerNotification } from "@modelcontextprotocol/sdk/types.js";
|
|
3
|
+
import type { RequestHandlerExtra } from "@modelcontextprotocol/sdk/shared/protocol.js";
|
|
4
|
+
import type { ZodRawShape, ZodObject, infer as Infer } from "zod";
|
|
5
|
+
export type ToolDef<TInput = unknown, TOutput = unknown> = {
|
|
6
|
+
input: TInput;
|
|
7
|
+
output: TOutput;
|
|
8
|
+
};
|
|
4
9
|
type McpServerOriginalResourceConfig = Omit<Resource, "uri" | "name" | "mimeType">;
|
|
5
|
-
type McpServerOriginalToolConfig = Omit<Parameters<
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
type McpServerOriginalToolConfig = Omit<Parameters<McpServerBase["registerTool"]>[1], "inputSchema" | "outputSchema">;
|
|
11
|
+
type ExtractStructuredContent<T> = T extends {
|
|
12
|
+
structuredContent: infer SC;
|
|
13
|
+
} ? SC : never;
|
|
14
|
+
/**
|
|
15
|
+
* Type-level marker interface for cross-package type inference.
|
|
16
|
+
* This enables TypeScript to infer tool types across package boundaries
|
|
17
|
+
* using structural typing on the $types property, rather than relying on
|
|
18
|
+
* class generic inference which fails when McpServer comes from different
|
|
19
|
+
* package installations.
|
|
20
|
+
*
|
|
21
|
+
* Inspired by tRPC's _def pattern and Hono's type markers.
|
|
22
|
+
*/
|
|
23
|
+
export interface McpServerTypes<TTools extends Record<string, ToolDef> = {}> {
|
|
24
|
+
readonly tools: TTools;
|
|
25
|
+
}
|
|
26
|
+
type AddTool<TTools, TName extends string, TInput extends ZodRawShape, TOutput> = McpServer<TTools & {
|
|
27
|
+
[K in TName]: ToolDef<Infer<ZodObject<TInput>>, TOutput>;
|
|
28
|
+
}>;
|
|
29
|
+
type ToolConfig<TInput extends ZodRawShape> = {
|
|
30
|
+
title?: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
inputSchema?: TInput;
|
|
33
|
+
outputSchema?: ZodRawShape;
|
|
34
|
+
annotations?: ToolAnnotations;
|
|
35
|
+
_meta?: Record<string, unknown>;
|
|
36
|
+
};
|
|
37
|
+
type ToolHandler<TInput extends ZodRawShape, TReturn extends CallToolResult = CallToolResult> = (args: Infer<ZodObject<TInput>>, extra: RequestHandlerExtra<ServerRequest, ServerNotification>) => TReturn | Promise<TReturn>;
|
|
38
|
+
export declare class McpServer<TTools extends Record<string, ToolDef> = {}> extends McpServerBase {
|
|
39
|
+
readonly $types: McpServerTypes<TTools>;
|
|
40
|
+
registerWidget<TName extends string, TInput extends ZodRawShape, TReturn extends CallToolResult>(name: TName, resourceConfig: McpServerOriginalResourceConfig, toolConfig: McpServerOriginalToolConfig & {
|
|
41
|
+
inputSchema?: TInput;
|
|
42
|
+
outputSchema?: ZodRawShape;
|
|
43
|
+
}, toolCallback: ToolHandler<TInput, TReturn>): AddTool<TTools, TName, TInput, ExtractStructuredContent<TReturn>>;
|
|
44
|
+
registerTool<TName extends string, InputArgs extends ZodRawShape, TReturn extends CallToolResult>(name: TName, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs, TReturn>): AddTool<TTools, TName, InputArgs, ExtractStructuredContent<TReturn>>;
|
|
45
|
+
registerTool<InputArgs extends ZodRawShape>(name: string, config: ToolConfig<InputArgs>, cb: ToolHandler<InputArgs>): RegisteredTool;
|
|
11
46
|
private lookupDistFile;
|
|
12
47
|
}
|
|
13
48
|
export {};
|
|
@@ -3,9 +3,11 @@ import { templateHelper } from "./templateHelper.js";
|
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
export class McpServer extends McpServerBase {
|
|
6
|
-
|
|
6
|
+
registerWidget(name, resourceConfig, toolConfig, toolCallback) {
|
|
7
7
|
const uri = `ui://widgets/${name}.html`;
|
|
8
|
-
const resourceMetadata = {
|
|
8
|
+
const resourceMetadata = {
|
|
9
|
+
...(resourceConfig._meta ?? {}),
|
|
10
|
+
};
|
|
9
11
|
if (toolConfig.description !== undefined) {
|
|
10
12
|
resourceMetadata["openai/widgetDescription"] = toolConfig.description;
|
|
11
13
|
}
|
|
@@ -40,11 +42,17 @@ export class McpServer extends McpServerBase {
|
|
|
40
42
|
const toolMeta = {
|
|
41
43
|
...toolConfig._meta,
|
|
42
44
|
"openai/outputTemplate": uri,
|
|
45
|
+
"ui/resourceUri": uri,
|
|
43
46
|
};
|
|
44
47
|
this.registerTool(name, {
|
|
45
48
|
...toolConfig,
|
|
46
49
|
_meta: toolMeta,
|
|
47
50
|
}, toolCallback);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
registerTool(name, config, cb) {
|
|
54
|
+
super.registerTool(name, config, cb);
|
|
55
|
+
return this;
|
|
48
56
|
}
|
|
49
57
|
lookupDistFile(key) {
|
|
50
58
|
const manifest = JSON.parse(readFileSync(path.join(process.cwd(), "dist", "assets", ".vite", "manifest.json"), "utf-8"));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,aAAa,
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../../src/server/server.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,IAAI,aAAa,GAG3B,MAAM,yCAAyC,CAAC;AAUjD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,IAAI,MAAM,WAAW,CAAC;AAkG7B,MAAM,OAAO,SAEX,SAAQ,aAAa;IAGrB,cAAc,CAKZ,IAAW,EACX,cAA+C,EAC/C,UAGC,EACD,YAA0C;QAE1C,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,IAAyE,CAAC;IACnF,CAAC;IAkBQ,YAAY,CACnB,IAAY,EACZ,MAA6B,EAC7B,EAA2B;QAE3B,KAAK,CAAC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,CAAC;QACrC,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"}
|
package/dist/src/test/utils.d.ts
CHANGED
|
@@ -8,6 +8,67 @@ export declare function createMockMcpServer(): {
|
|
|
8
8
|
mockResource: MockInstance<McpServer["resource"]>;
|
|
9
9
|
mockRegisterTool: MockInstance<McpServer["registerTool"]>;
|
|
10
10
|
};
|
|
11
|
+
export declare function createTestServer(): McpServer<{
|
|
12
|
+
"search-voyage": import("../server/server.js").ToolDef<{
|
|
13
|
+
destination: string;
|
|
14
|
+
departureDate?: string | undefined;
|
|
15
|
+
maxPrice?: number | undefined;
|
|
16
|
+
}, {
|
|
17
|
+
results: {
|
|
18
|
+
id: string;
|
|
19
|
+
name: string;
|
|
20
|
+
price: number;
|
|
21
|
+
}[];
|
|
22
|
+
totalCount: number;
|
|
23
|
+
}>;
|
|
24
|
+
} & {
|
|
25
|
+
"get-trip-details": import("../server/server.js").ToolDef<{
|
|
26
|
+
tripId: string;
|
|
27
|
+
}, {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
images: string[];
|
|
31
|
+
}>;
|
|
32
|
+
} & {
|
|
33
|
+
"no-input-widget": import("../server/server.js").ToolDef<{}, {}>;
|
|
34
|
+
} & {
|
|
35
|
+
"inferred-output-widget": import("../server/server.js").ToolDef<{
|
|
36
|
+
query: string;
|
|
37
|
+
}, {
|
|
38
|
+
inferredResults: {
|
|
39
|
+
id: string;
|
|
40
|
+
score: number;
|
|
41
|
+
}[];
|
|
42
|
+
inferredCount: number;
|
|
43
|
+
}>;
|
|
44
|
+
} & {
|
|
45
|
+
"calculate-price": import("../server/server.js").ToolDef<{
|
|
46
|
+
tripId: string;
|
|
47
|
+
passengers: number;
|
|
48
|
+
}, {
|
|
49
|
+
totalPrice: number;
|
|
50
|
+
currency: string;
|
|
51
|
+
}>;
|
|
52
|
+
} & {
|
|
53
|
+
"inferred-tool": import("../server/server.js").ToolDef<{
|
|
54
|
+
itemId: string;
|
|
55
|
+
}, {
|
|
56
|
+
itemDetails: {
|
|
57
|
+
name: string;
|
|
58
|
+
available: boolean;
|
|
59
|
+
};
|
|
60
|
+
fetchedAt: string;
|
|
61
|
+
}>;
|
|
62
|
+
}>;
|
|
63
|
+
export declare function createMinimalTestServer(): McpServer<{
|
|
64
|
+
"search-voyage": import("../server/server.js").ToolDef<{
|
|
65
|
+
destination: string;
|
|
66
|
+
}, {
|
|
67
|
+
results: {
|
|
68
|
+
id: string;
|
|
69
|
+
}[];
|
|
70
|
+
}>;
|
|
71
|
+
}>;
|
|
11
72
|
/**
|
|
12
73
|
* Mock extra parameter for resource callback
|
|
13
74
|
*/
|
package/dist/src/test/utils.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { vi } from "vitest";
|
|
2
|
-
import { McpServer
|
|
2
|
+
import { McpServer } from "../server/server.js";
|
|
3
|
+
import { z } from "zod";
|
|
3
4
|
/**
|
|
4
5
|
* Creates a real McpServer instance for testing
|
|
5
6
|
*/
|
|
@@ -18,6 +19,126 @@ export function createMockMcpServer() {
|
|
|
18
19
|
mockRegisterTool,
|
|
19
20
|
};
|
|
20
21
|
}
|
|
22
|
+
export function createTestServer() {
|
|
23
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {})
|
|
24
|
+
.registerWidget("search-voyage", {}, {
|
|
25
|
+
description: "Search for voyages",
|
|
26
|
+
inputSchema: {
|
|
27
|
+
destination: z.string(),
|
|
28
|
+
departureDate: z.string().optional(),
|
|
29
|
+
maxPrice: z.number().optional(),
|
|
30
|
+
},
|
|
31
|
+
outputSchema: {
|
|
32
|
+
results: z.array(z.object({
|
|
33
|
+
id: z.string(),
|
|
34
|
+
name: z.string(),
|
|
35
|
+
price: z.number(),
|
|
36
|
+
})),
|
|
37
|
+
totalCount: z.number(),
|
|
38
|
+
},
|
|
39
|
+
}, async ({ destination }) => {
|
|
40
|
+
return {
|
|
41
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
42
|
+
structuredContent: {
|
|
43
|
+
results: [{ id: "1", name: "Trip", price: 1000 }],
|
|
44
|
+
totalCount: 1,
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
})
|
|
48
|
+
.registerWidget("get-trip-details", {}, {
|
|
49
|
+
description: "Get trip details",
|
|
50
|
+
inputSchema: {
|
|
51
|
+
tripId: z.string(),
|
|
52
|
+
},
|
|
53
|
+
outputSchema: {
|
|
54
|
+
name: z.string(),
|
|
55
|
+
description: z.string(),
|
|
56
|
+
images: z.array(z.string()),
|
|
57
|
+
},
|
|
58
|
+
}, async ({ tripId }) => {
|
|
59
|
+
return {
|
|
60
|
+
content: [{ type: "text", text: `Details for ${tripId}` }],
|
|
61
|
+
structuredContent: {
|
|
62
|
+
name: "Trip",
|
|
63
|
+
description: "A great trip",
|
|
64
|
+
images: ["image1.jpg"],
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
})
|
|
68
|
+
.registerWidget("no-input-widget", {}, {
|
|
69
|
+
description: "Widget with no input",
|
|
70
|
+
inputSchema: {},
|
|
71
|
+
outputSchema: {},
|
|
72
|
+
}, async () => {
|
|
73
|
+
return {
|
|
74
|
+
content: [{ type: "text", text: "No input needed" }],
|
|
75
|
+
structuredContent: {},
|
|
76
|
+
};
|
|
77
|
+
})
|
|
78
|
+
.registerWidget("inferred-output-widget", {}, {
|
|
79
|
+
description: "Widget with output inferred from callback",
|
|
80
|
+
inputSchema: {
|
|
81
|
+
query: z.string(),
|
|
82
|
+
},
|
|
83
|
+
}, async ({ query }) => {
|
|
84
|
+
return {
|
|
85
|
+
content: [{ type: "text", text: `Query: ${query}` }],
|
|
86
|
+
structuredContent: {
|
|
87
|
+
inferredResults: [{ id: "inferred-1", score: 0.95 }],
|
|
88
|
+
inferredCount: 1,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
})
|
|
92
|
+
.registerTool("calculate-price", {
|
|
93
|
+
description: "Calculate trip price",
|
|
94
|
+
inputSchema: {
|
|
95
|
+
tripId: z.string(),
|
|
96
|
+
passengers: z.number(),
|
|
97
|
+
},
|
|
98
|
+
outputSchema: {
|
|
99
|
+
totalPrice: z.number(),
|
|
100
|
+
currency: z.string(),
|
|
101
|
+
},
|
|
102
|
+
}, async ({ tripId, passengers }) => {
|
|
103
|
+
return {
|
|
104
|
+
content: [{ type: "text", text: `Price for ${tripId}` }],
|
|
105
|
+
structuredContent: {
|
|
106
|
+
totalPrice: 1000 * passengers,
|
|
107
|
+
currency: "USD",
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
})
|
|
111
|
+
.registerTool("inferred-tool", {
|
|
112
|
+
description: "Tool with output inferred from callback",
|
|
113
|
+
inputSchema: {
|
|
114
|
+
itemId: z.string(),
|
|
115
|
+
},
|
|
116
|
+
}, async ({ itemId }) => {
|
|
117
|
+
return {
|
|
118
|
+
content: [{ type: "text", text: `Item: ${itemId}` }],
|
|
119
|
+
structuredContent: {
|
|
120
|
+
itemDetails: { name: "Inferred Item", available: true },
|
|
121
|
+
fetchedAt: "2024-01-01",
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
export function createMinimalTestServer() {
|
|
127
|
+
return new McpServer({ name: "test-app", version: "1.0.0" }, {}).registerWidget("search-voyage", {}, {
|
|
128
|
+
description: "Search for voyages",
|
|
129
|
+
inputSchema: {
|
|
130
|
+
destination: z.string(),
|
|
131
|
+
},
|
|
132
|
+
outputSchema: {
|
|
133
|
+
results: z.array(z.object({ id: z.string() })),
|
|
134
|
+
},
|
|
135
|
+
}, async ({ destination }) => {
|
|
136
|
+
return {
|
|
137
|
+
content: [{ type: "text", text: `Found trips to ${destination}` }],
|
|
138
|
+
structuredContent: { results: [{ id: "1" }] },
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
}
|
|
21
142
|
/**
|
|
22
143
|
* Mock extra parameter for resource callback
|
|
23
144
|
*/
|