|
| 1 | +/** |
| 2 | + * @file App that demonstrates a few features using MCP Apps SDK + Preact. |
| 3 | + */ |
| 4 | +import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps"; |
| 5 | +import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 6 | +import { useCallback, useEffect, useState } from "preact/hooks"; |
| 7 | +import { render } from "preact"; |
| 8 | +import styles from "./mcp-app.module.css"; |
| 9 | + |
| 10 | + |
| 11 | +const IMPLEMENTATION = { name: "Get Time App", version: "1.0.0" }; |
| 12 | + |
| 13 | + |
| 14 | +const log = { |
| 15 | + info: console.log.bind(console, "[APP]"), |
| 16 | + warn: console.warn.bind(console, "[APP]"), |
| 17 | + error: console.error.bind(console, "[APP]"), |
| 18 | +}; |
| 19 | + |
| 20 | + |
| 21 | +function extractTime(callToolResult: CallToolResult): string { |
| 22 | + const { text } = callToolResult.content?.find((c) => c.type === "text")!; |
| 23 | + return text; |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +function GetTimeApp() { |
| 28 | + const [app, setApp] = useState<App | null>(null); |
| 29 | + const [error, setError] = useState<Error | null>(null); |
| 30 | + const [toolResult, setToolResult] = useState<CallToolResult | null>(null); |
| 31 | + |
| 32 | + useEffect(() => { |
| 33 | + const instance = new App(IMPLEMENTATION); |
| 34 | + |
| 35 | + instance.ontoolinput = async (input) => { |
| 36 | + log.info("Received tool call input:", input); |
| 37 | + }; |
| 38 | + |
| 39 | + instance.ontoolresult = async (result) => { |
| 40 | + log.info("Received tool call result:", result); |
| 41 | + setToolResult(result); |
| 42 | + }; |
| 43 | + |
| 44 | + instance.onerror = log.error; |
| 45 | + |
| 46 | + instance |
| 47 | + .connect(new PostMessageTransport(window.parent)) |
| 48 | + .then(() => setApp(instance)) |
| 49 | + .catch(setError); |
| 50 | + }, []); |
| 51 | + |
| 52 | + if (error) return <div><strong>ERROR:</strong> {error.message}</div>; |
| 53 | + if (!app) return <div>Connecting...</div>; |
| 54 | + |
| 55 | + return <GetTimeAppInner app={app} toolResult={toolResult} />; |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +interface GetTimeAppInnerProps { |
| 60 | + app: App; |
| 61 | + toolResult: CallToolResult | null; |
| 62 | +} |
| 63 | +function GetTimeAppInner({ app, toolResult }: GetTimeAppInnerProps) { |
| 64 | + const [serverTime, setServerTime] = useState("Loading..."); |
| 65 | + const [messageText, setMessageText] = useState("This is message text."); |
| 66 | + const [logText, setLogText] = useState("This is log text."); |
| 67 | + const [linkUrl, setLinkUrl] = useState("https://modelcontextprotocol.io/"); |
| 68 | + |
| 69 | + useEffect(() => { |
| 70 | + if (toolResult) { |
| 71 | + setServerTime(extractTime(toolResult)); |
| 72 | + } |
| 73 | + }, [toolResult]); |
| 74 | + |
| 75 | + const handleGetTime = useCallback(async () => { |
| 76 | + try { |
| 77 | + log.info("Calling get-time tool..."); |
| 78 | + const result = await app.callServerTool({ name: "get-time", arguments: {} }); |
| 79 | + log.info("get-time result:", result); |
| 80 | + setServerTime(extractTime(result)); |
| 81 | + } catch (e) { |
| 82 | + log.error(e); |
| 83 | + setServerTime("[ERROR]"); |
| 84 | + } |
| 85 | + }, [app]); |
| 86 | + |
| 87 | + const handleSendMessage = useCallback(async () => { |
| 88 | + const signal = AbortSignal.timeout(5000); |
| 89 | + try { |
| 90 | + log.info("Sending message text to Host:", messageText); |
| 91 | + const { isError } = await app.sendMessage( |
| 92 | + { role: "user", content: [{ type: "text", text: messageText }] }, |
| 93 | + { signal }, |
| 94 | + ); |
| 95 | + log.info("Message", isError ? "rejected" : "accepted"); |
| 96 | + } catch (e) { |
| 97 | + log.error("Message send error:", signal.aborted ? "timed out" : e); |
| 98 | + } |
| 99 | + }, [app, messageText]); |
| 100 | + |
| 101 | + const handleSendLog = useCallback(async () => { |
| 102 | + log.info("Sending log text to Host:", logText); |
| 103 | + await app.sendLog({ level: "info", data: logText }); |
| 104 | + }, [app, logText]); |
| 105 | + |
| 106 | + const handleOpenLink = useCallback(async () => { |
| 107 | + log.info("Sending open link request to Host:", linkUrl); |
| 108 | + const { isError } = await app.openLink({ url: linkUrl }); |
| 109 | + log.info("Open link request", isError ? "rejected" : "accepted"); |
| 110 | + }, [app, linkUrl]); |
| 111 | + |
| 112 | + return ( |
| 113 | + <main className={styles.main}> |
| 114 | + <p className={styles.notice}>Watch activity in the DevTools console!</p> |
| 115 | + |
| 116 | + <div className={styles.action}> |
| 117 | + <p> |
| 118 | + <strong>Server Time:</strong> <code id="server-time">{serverTime}</code> |
| 119 | + </p> |
| 120 | + <button onClick={handleGetTime}>Get Server Time</button> |
| 121 | + </div> |
| 122 | + |
| 123 | + <div className={styles.action}> |
| 124 | + <textarea value={messageText} onChange={(e) => setMessageText(e.currentTarget.value)} /> |
| 125 | + <button onClick={handleSendMessage}>Send Message</button> |
| 126 | + </div> |
| 127 | + |
| 128 | + <div className={styles.action}> |
| 129 | + <input type="text" value={logText} onChange={(e) => setLogText(e.currentTarget.value)} /> |
| 130 | + <button onClick={handleSendLog}>Send Log</button> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div className={styles.action}> |
| 134 | + <input type="url" value={linkUrl} onChange={(e) => setLinkUrl(e.currentTarget.value)} /> |
| 135 | + <button onClick={handleOpenLink}>Open Link</button> |
| 136 | + </div> |
| 137 | + </main> |
| 138 | + ); |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +render(<GetTimeApp />, document.getElementById("root")!); |
0 commit comments