-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAppRenderer.tsx
More file actions
154 lines (138 loc) · 3.96 KB
/
AppRenderer.tsx
File metadata and controls
154 lines (138 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useMemo, useState } from "react";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import {
Tool,
ContentBlock,
CompatibilityCallToolResult,
CallToolResult,
CallToolResultSchema,
ServerNotification,
LoggingMessageNotificationParams,
} from "@modelcontextprotocol/sdk/types.js";
import {
AppRenderer as McpUiAppRenderer,
type McpUiHostContext,
type RequestHandlerExtra,
} from "@mcp-ui/client";
import {
type McpUiMessageRequest,
type McpUiMessageResult,
} from "@modelcontextprotocol/ext-apps/app-bridge";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { AlertCircle } from "lucide-react";
import { useToast } from "@/lib/hooks/useToast";
interface AppRendererProps {
sandboxPath: string;
tool: Tool;
mcpClient: Client | null;
toolInput?: Record<string, unknown>;
toolResult?: CompatibilityCallToolResult | null;
onNotification?: (notification: ServerNotification) => void;
}
const AppRenderer = ({
sandboxPath,
tool,
mcpClient,
toolInput,
toolResult,
onNotification,
}: AppRendererProps) => {
const [error, setError] = useState<string | null>(null);
const { toast } = useToast();
const normalizedToolResult = useMemo<CallToolResult | undefined>(() => {
if (!toolResult) {
return undefined;
}
if ("content" in toolResult) {
const parsedResult = CallToolResultSchema.safeParse(toolResult);
return parsedResult.success ? parsedResult.data : undefined;
}
if ("toolResult" in toolResult) {
const parsedResult = CallToolResultSchema.safeParse(
toolResult.toolResult,
);
return parsedResult.success ? parsedResult.data : undefined;
}
return undefined;
}, [toolResult]);
const hostContext: McpUiHostContext = useMemo(
() => ({
theme: document.documentElement.classList.contains("dark")
? "dark"
: "light",
}),
[],
);
const handleOpenLink = async ({ url }: { url: string }) => {
let isError = true;
if (url.startsWith("https://") || url.startsWith("http://")) {
window.open(url, "_blank", "noopener,noreferrer");
isError = false;
}
return { isError };
};
const handleMessage = async (
params: McpUiMessageRequest["params"],
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_extra: RequestHandlerExtra,
): Promise<McpUiMessageResult> => {
const message = params.content
.filter((block): block is ContentBlock & { type: "text" } =>
Boolean(block.type === "text"),
)
.map((block) => block.text)
.join("\n");
if (message) {
toast({
description: message,
});
}
return {};
};
const handleLoggingMessage = (params: LoggingMessageNotificationParams) => {
if (onNotification) {
onNotification({
method: "notifications/message",
params,
} as ServerNotification);
}
};
if (!mcpClient) {
return (
<Alert>
<AlertCircle className="h-4 w-4" />
<AlertDescription>Waiting for MCP client...</AlertDescription>
</Alert>
);
}
return (
<div className="flex flex-col h-full">
{error && (
<Alert variant="destructive" className="mb-4">
<AlertCircle className="h-4 w-4" />
<AlertDescription>{error}</AlertDescription>
</Alert>
)}
<div
className="flex-1 border rounded overflow-hidden"
style={{ minHeight: "400px" }}
>
<McpUiAppRenderer
client={mcpClient}
onOpenLink={handleOpenLink}
onMessage={handleMessage}
onLoggingMessage={handleLoggingMessage}
toolName={tool.name}
hostContext={hostContext}
toolInput={toolInput}
toolResult={normalizedToolResult}
sandbox={{
url: new URL(sandboxPath, window.location.origin),
}}
onError={(err) => setError(err.message)}
/>
</div>
</div>
);
};
export default AppRenderer;