Skip to content

Commit 16fb2e4

Browse files
ochafikclaude
andcommitted
refactor(types): use explicit type unions for AppBridge protocol types
- Add AppBridgeRequest, AppBridgeNotification, AppBridgeResult type unions that explicitly list all valid types for the protocol - Replace generic SDK Request/Notification/Result with our specific types - Remove all type casts by using `as const` on method literals - Add index signature to McpUiHostContext for forward compatibility This improves type safety and documents exactly what the AppBridge supports. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent f589304 commit 16fb2e4

File tree

5 files changed

+107
-31
lines changed

5 files changed

+107
-31
lines changed

src/app-bridge.ts

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CallToolRequestSchema,
66
CallToolResult,
77
CallToolResultSchema,
8+
EmptyResult,
89
Implementation,
910
ListPromptsRequest,
1011
ListPromptsRequestSchema,
@@ -18,9 +19,10 @@ import {
1819
ListResourceTemplatesRequestSchema,
1920
ListResourceTemplatesResult,
2021
ListResourceTemplatesResultSchema,
22+
ListToolsRequest,
23+
ListToolsResult,
2124
LoggingMessageNotification,
2225
LoggingMessageNotificationSchema,
23-
Notification,
2426
PingRequest,
2527
PingRequestSchema,
2628
PromptListChangedNotification,
@@ -29,10 +31,8 @@ import {
2931
ReadResourceRequestSchema,
3032
ReadResourceResult,
3133
ReadResourceResultSchema,
32-
Request,
3334
ResourceListChangedNotification,
3435
ResourceListChangedNotificationSchema,
35-
Result,
3636
ToolListChangedNotification,
3737
ToolListChangedNotificationSchema,
3838
} from "@modelcontextprotocol/sdk/types.js";
@@ -66,6 +66,7 @@ import {
6666
McpUiOpenLinkRequestSchema,
6767
McpUiOpenLinkResult,
6868
McpUiResourceTeardownRequest,
69+
McpUiResourceTeardownResult,
6970
McpUiResourceTeardownResultSchema,
7071
McpUiSandboxProxyReadyNotification,
7172
McpUiSandboxProxyReadyNotificationSchema,
@@ -75,6 +76,73 @@ export * from "./types";
7576
export { RESOURCE_URI_META_KEY, RESOURCE_MIME_TYPE } from "./app";
7677
export { PostMessageTransport } from "./message-transport";
7778

79+
/**
80+
* All request types in the MCP Apps protocol.
81+
*
82+
* Includes:
83+
* - MCP UI requests (initialize, open-link, message, resource-teardown)
84+
* - MCP server requests forwarded from the app (tools/call, resources/*, prompts/list)
85+
* - Protocol requests (ping)
86+
*/
87+
export type AppRequest =
88+
| McpUiInitializeRequest
89+
| McpUiOpenLinkRequest
90+
| McpUiMessageRequest
91+
| McpUiResourceTeardownRequest
92+
| CallToolRequest
93+
| ListToolsRequest
94+
| ListResourcesRequest
95+
| ListResourceTemplatesRequest
96+
| ReadResourceRequest
97+
| ListPromptsRequest
98+
| PingRequest;
99+
100+
/**
101+
* All notification types in the MCP Apps protocol.
102+
*
103+
* Host to app:
104+
* - Tool lifecycle (input, input-partial, result, cancelled)
105+
* - Host context changes
106+
* - MCP list changes (tools, resources, prompts)
107+
* - Sandbox resource ready
108+
*
109+
* App to host:
110+
* - Initialized, size-changed, sandbox-proxy-ready
111+
* - Logging messages
112+
*/
113+
export type AppNotification =
114+
// Sent to app
115+
| McpUiHostContextChangedNotification
116+
| McpUiToolInputNotification
117+
| McpUiToolInputPartialNotification
118+
| McpUiToolResultNotification
119+
| McpUiToolCancelledNotification
120+
| McpUiSandboxResourceReadyNotification
121+
| ToolListChangedNotification
122+
| ResourceListChangedNotification
123+
| PromptListChangedNotification
124+
// Received from app
125+
| McpUiInitializedNotification
126+
| McpUiSizeChangedNotification
127+
| McpUiSandboxProxyReadyNotification
128+
| LoggingMessageNotification;
129+
130+
/**
131+
* All result types in the MCP Apps protocol.
132+
*/
133+
export type AppResult =
134+
| McpUiInitializeResult
135+
| McpUiOpenLinkResult
136+
| McpUiMessageResult
137+
| McpUiResourceTeardownResult
138+
| CallToolResult
139+
| ListToolsResult
140+
| ListResourcesResult
141+
| ListResourceTemplatesResult
142+
| ReadResourceResult
143+
| ListPromptsResult
144+
| EmptyResult;
145+
78146
/**
79147
* Options for configuring AppBridge behavior.
80148
*
@@ -164,7 +232,11 @@ type RequestHandlerExtra = Parameters<
164232
* await bridge.connect(transport);
165233
* ```
166234
*/
167-
export class AppBridge extends Protocol<Request, Notification, Result> {
235+
export class AppBridge extends Protocol<
236+
AppRequest,
237+
AppNotification,
238+
AppResult
239+
> {
168240
private _appCapabilities?: McpUiAppCapabilities;
169241
private _hostContext: McpUiHostContext = {};
170242
private _appInfo?: Implementation;
@@ -805,23 +877,23 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
805877
* Verify that the guest supports the capability required for the given request method.
806878
* @internal
807879
*/
808-
assertCapabilityForMethod(method: Request["method"]): void {
880+
assertCapabilityForMethod(method: AppRequest["method"]): void {
809881
// TODO
810882
}
811883

812884
/**
813885
* Verify that a request handler is registered and supported for the given method.
814886
* @internal
815887
*/
816-
assertRequestHandlerCapability(method: Request["method"]): void {
888+
assertRequestHandlerCapability(method: AppRequest["method"]): void {
817889
// TODO
818890
}
819891

820892
/**
821893
* Verify that the host supports the capability required for the given notification method.
822894
* @internal
823895
*/
824-
assertNotificationCapability(method: Notification["method"]): void {
896+
assertNotificationCapability(method: AppNotification["method"]): void {
825897
// TODO
826898
}
827899

@@ -939,7 +1011,7 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
9391011
return this.notification({
9401012
method: "ui/notifications/host-context-changed" as const,
9411013
params,
942-
} as Notification);
1014+
});
9431015
}
9441016

9451017
/**
@@ -965,8 +1037,8 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
9651037
* @see {@link sendToolResult} for sending results after execution
9661038
*/
9671039
sendToolInput(params: McpUiToolInputNotification["params"]) {
968-
return this.notification(<McpUiToolInputNotification>{
969-
method: "ui/notifications/tool-input",
1040+
return this.notification({
1041+
method: "ui/notifications/tool-input" as const,
9701042
params,
9711043
});
9721044
}
@@ -999,8 +1071,8 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
9991071
* @see {@link sendToolInput} for sending complete arguments
10001072
*/
10011073
sendToolInputPartial(params: McpUiToolInputPartialNotification["params"]) {
1002-
return this.notification(<McpUiToolInputPartialNotification>{
1003-
method: "ui/notifications/tool-input-partial",
1074+
return this.notification({
1075+
method: "ui/notifications/tool-input-partial" as const,
10041076
params,
10051077
});
10061078
}
@@ -1030,8 +1102,8 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
10301102
* @see {@link sendToolInput} for sending tool arguments before results
10311103
*/
10321104
sendToolResult(params: McpUiToolResultNotification["params"]) {
1033-
return this.notification(<McpUiToolResultNotification>{
1034-
method: "ui/notifications/tool-result",
1105+
return this.notification({
1106+
method: "ui/notifications/tool-result" as const,
10351107
params,
10361108
});
10371109
}
@@ -1067,8 +1139,8 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
10671139
* @see {@link sendToolInput} for sending tool arguments
10681140
*/
10691141
sendToolCancelled(params: McpUiToolCancelledNotification["params"]) {
1070-
return this.notification(<McpUiToolCancelledNotification>{
1071-
method: "ui/notifications/tool-cancelled",
1142+
return this.notification({
1143+
method: "ui/notifications/tool-cancelled" as const,
10721144
params,
10731145
});
10741146
}
@@ -1091,8 +1163,8 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
10911163
sendSandboxResourceReady(
10921164
params: McpUiSandboxResourceReadyNotification["params"],
10931165
) {
1094-
return this.notification(<McpUiSandboxResourceReadyNotification>{
1095-
method: "ui/notifications/sandbox-resource-ready",
1166+
return this.notification({
1167+
method: "ui/notifications/sandbox-resource-ready" as const,
10961168
params,
10971169
});
10981170
}
@@ -1126,8 +1198,8 @@ export class AppBridge extends Protocol<Request, Notification, Result> {
11261198
options?: RequestOptions,
11271199
) {
11281200
return this.request(
1129-
<McpUiResourceTeardownRequest>{
1130-
method: "ui/resource-teardown",
1201+
{
1202+
method: "ui/resource-teardown" as const,
11311203
params,
11321204
},
11331205
McpUiResourceTeardownResultSchema,

src/app.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ import {
1313
ListToolsRequest,
1414
ListToolsRequestSchema,
1515
LoggingMessageNotification,
16-
Notification,
1716
PingRequestSchema,
18-
Request,
19-
Result,
2017
} from "@modelcontextprotocol/sdk/types.js";
18+
import {
19+
AppNotification,
20+
AppRequest,
21+
AppResult,
22+
} from "./app-bridge";
2123
import {
2224
LATEST_PROTOCOL_VERSION,
2325
McpUiAppCapabilities,
@@ -189,7 +191,7 @@ type RequestHandlerExtra = Parameters<
189191
* });
190192
* ```
191193
*/
192-
export class App extends Protocol<Request, Notification, Result> {
194+
export class App extends Protocol<AppRequest, AppNotification, AppResult> {
193195
private _hostCapabilities?: McpUiHostCapabilities;
194196
private _hostInfo?: Implementation;
195197
private _hostContext?: McpUiHostContext;
@@ -642,15 +644,15 @@ export class App extends Protocol<Request, Notification, Result> {
642644
* Verify that the host supports the capability required for the given request method.
643645
* @internal
644646
*/
645-
assertCapabilityForMethod(method: Request["method"]): void {
647+
assertCapabilityForMethod(method: AppRequest["method"]): void {
646648
// TODO
647649
}
648650

649651
/**
650652
* Verify that the app declared the capability required for the given request method.
651653
* @internal
652654
*/
653-
assertRequestHandlerCapability(method: Request["method"]): void {
655+
assertRequestHandlerCapability(method: AppRequest["method"]): void {
654656
switch (method) {
655657
case "tools/call":
656658
case "tools/list":
@@ -672,7 +674,7 @@ export class App extends Protocol<Request, Notification, Result> {
672674
* Verify that the app supports the capability required for the given notification method.
673675
* @internal
674676
*/
675-
assertNotificationCapability(method: Notification["method"]): void {
677+
assertNotificationCapability(method: AppNotification["method"]): void {
676678
// TODO
677679
}
678680

src/generated/schema.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@
381381
"additionalProperties": false
382382
}
383383
},
384-
"additionalProperties": false
384+
"additionalProperties": {}
385385
}
386386
},
387387
"required": ["method", "params"],
@@ -667,7 +667,7 @@
667667
"additionalProperties": false
668668
}
669669
},
670-
"additionalProperties": false
670+
"additionalProperties": {}
671671
},
672672
"McpUiInitializeRequest": {
673673
"$schema": "https://json-schema.org/draft/2020-12/schema",
@@ -1135,7 +1135,7 @@
11351135
"additionalProperties": false
11361136
}
11371137
},
1138-
"additionalProperties": false
1138+
"additionalProperties": {}
11391139
}
11401140
},
11411141
"required": [

src/generated/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ export const McpUiToolResultNotificationSchema = z.object({
337337
/**
338338
* @description Rich context about the host environment provided to Guest UIs.
339339
*/
340-
export const McpUiHostContextSchema = z.object({
340+
export const McpUiHostContextSchema = z.looseObject({
341341
/** @description Metadata of the tool call that instantiated this App. */
342342
toolInfo: z
343343
.object({

src/spec.types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ export interface McpUiToolCancelledNotification {
184184
* @description Rich context about the host environment provided to Guest UIs.
185185
*/
186186
export interface McpUiHostContext {
187+
/** @description Allow additional properties for forward compatibility. */
188+
[key: string]: unknown;
187189
/** @description Metadata of the tool call that instantiated this App. */
188190
toolInfo?: {
189191
/** @description JSON-RPC id of the tools/call request. */

0 commit comments

Comments
 (0)