|
| 1 | +<script lang="ts"> |
| 2 | +import { onMount } from "svelte"; |
| 3 | +import { App, PostMessageTransport } from "@modelcontextprotocol/ext-apps"; |
| 4 | +import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js"; |
| 5 | +
|
| 6 | +const log = { |
| 7 | + info: console.log.bind(console, "[APP]"), |
| 8 | + warn: console.warn.bind(console, "[APP]"), |
| 9 | + error: console.error.bind(console, "[APP]"), |
| 10 | +}; |
| 11 | +
|
| 12 | +function extractTime(result: CallToolResult): string { |
| 13 | + const text = result.content! |
| 14 | + .filter((c): c is { type: "text"; text: string } => c.type === "text") |
| 15 | + .map((c) => c.text) |
| 16 | + .join(""); |
| 17 | + const { time } = JSON.parse(text) as { time: string }; |
| 18 | + return time; |
| 19 | +} |
| 20 | +
|
| 21 | +let app = $state<App | null>(null); |
| 22 | +let serverTime = $state("Loading..."); |
| 23 | +let messageText = $state("This is message text."); |
| 24 | +let logText = $state("This is log text."); |
| 25 | +let linkUrl = $state("https://modelcontextprotocol.io/"); |
| 26 | +
|
| 27 | +onMount(async () => { |
| 28 | + const instance = new App({ name: "Get Time App", version: "1.0.0" }); |
| 29 | +
|
| 30 | + instance.ontoolinput = (params) => { |
| 31 | + log.info("Received tool call input:", params); |
| 32 | + }; |
| 33 | +
|
| 34 | + instance.ontoolresult = (result) => { |
| 35 | + log.info("Received tool call result:", result); |
| 36 | + serverTime = extractTime(result); |
| 37 | + }; |
| 38 | +
|
| 39 | + instance.onerror = log.error; |
| 40 | +
|
| 41 | + await instance.connect(new PostMessageTransport(window.parent)); |
| 42 | + app = instance; |
| 43 | +}); |
| 44 | +
|
| 45 | +async function handleGetTime() { |
| 46 | + if (!app) return; |
| 47 | + try { |
| 48 | + log.info("Calling get-time tool..."); |
| 49 | + const result = await app.callServerTool({ name: "get-time", arguments: {} }); |
| 50 | + log.info("get-time result:", result); |
| 51 | + serverTime = extractTime(result); |
| 52 | + } catch (e) { |
| 53 | + log.error(e); |
| 54 | + serverTime = "[ERROR]"; |
| 55 | + } |
| 56 | +} |
| 57 | +
|
| 58 | +async function handleSendMessage() { |
| 59 | + if (!app) return; |
| 60 | + const signal = AbortSignal.timeout(5000); |
| 61 | + try { |
| 62 | + log.info("Sending message text to Host:", messageText); |
| 63 | + const { isError } = await app.sendMessage( |
| 64 | + { role: "user", content: [{ type: "text", text: messageText }] }, |
| 65 | + { signal }, |
| 66 | + ); |
| 67 | + log.info("Message", isError ? "rejected" : "accepted"); |
| 68 | + } catch (e) { |
| 69 | + log.error("Message send error:", signal.aborted ? "timed out" : e); |
| 70 | + } |
| 71 | +} |
| 72 | +
|
| 73 | +async function handleSendLog() { |
| 74 | + if (!app) return; |
| 75 | + log.info("Sending log text to Host:", logText); |
| 76 | + await app.sendLog({ level: "info", data: logText }); |
| 77 | +} |
| 78 | +
|
| 79 | +async function handleOpenLink() { |
| 80 | + if (!app) return; |
| 81 | + log.info("Sending open link request to Host:", linkUrl); |
| 82 | + const { isError } = await app.sendOpenLink({ url: linkUrl }); |
| 83 | + log.info("Open link request", isError ? "rejected" : "accepted"); |
| 84 | +} |
| 85 | +</script> |
| 86 | + |
| 87 | +<main class="main"> |
| 88 | + <p class="notice">Watch activity in the DevTools console!</p> |
| 89 | + |
| 90 | + <div class="action"> |
| 91 | + <p><strong>Server Time:</strong> <code>{serverTime}</code></p> |
| 92 | + <button onclick={handleGetTime}>Get Server Time</button> |
| 93 | + </div> |
| 94 | + |
| 95 | + <div class="action"> |
| 96 | + <textarea bind:value={messageText}></textarea> |
| 97 | + <button onclick={handleSendMessage}>Send Message</button> |
| 98 | + </div> |
| 99 | + |
| 100 | + <div class="action"> |
| 101 | + <input type="text" bind:value={logText}> |
| 102 | + <button onclick={handleSendLog}>Send Log</button> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div class="action"> |
| 106 | + <input type="url" bind:value={linkUrl}> |
| 107 | + <button onclick={handleOpenLink}>Open Link</button> |
| 108 | + </div> |
| 109 | +</main> |
| 110 | + |
| 111 | +<style> |
| 112 | +.main { |
| 113 | + --color-primary: #2563eb; |
| 114 | + --color-primary-hover: #1d4ed8; |
| 115 | + --color-notice-bg: #eff6ff; |
| 116 | +
|
| 117 | + min-width: 425px; |
| 118 | +
|
| 119 | + > * { |
| 120 | + margin-top: 0; |
| 121 | + margin-bottom: 0; |
| 122 | + } |
| 123 | +
|
| 124 | + > * + * { |
| 125 | + margin-top: 1.5rem; |
| 126 | + } |
| 127 | +} |
| 128 | +
|
| 129 | +.action { |
| 130 | + > * { |
| 131 | + margin-top: 0; |
| 132 | + margin-bottom: 0; |
| 133 | + width: 100%; |
| 134 | + } |
| 135 | +
|
| 136 | + > * + * { |
| 137 | + margin-top: 0.5rem; |
| 138 | + } |
| 139 | +
|
| 140 | + textarea, |
| 141 | + input { |
| 142 | + font-family: inherit; |
| 143 | + font-size: inherit; |
| 144 | + } |
| 145 | +
|
| 146 | + button { |
| 147 | + padding: 0.5rem 1rem; |
| 148 | + border: none; |
| 149 | + border-radius: 6px; |
| 150 | + color: white; |
| 151 | + font-weight: bold; |
| 152 | + background-color: var(--color-primary); |
| 153 | + cursor: pointer; |
| 154 | +
|
| 155 | + &:hover, |
| 156 | + &:focus-visible { |
| 157 | + background-color: var(--color-primary-hover); |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | +
|
| 162 | +.notice { |
| 163 | + padding: 0.5rem 0.75rem; |
| 164 | + color: var(--color-primary); |
| 165 | + text-align: center; |
| 166 | + font-style: italic; |
| 167 | + background-color: var(--color-notice-bg); |
| 168 | +
|
| 169 | + &::before { |
| 170 | + content: "ℹ️ "; |
| 171 | + font-style: normal; |
| 172 | + } |
| 173 | +} |
| 174 | +</style> |
0 commit comments