|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 6 | + <title>Weather Dashboard</title> |
| 7 | + <style> |
| 8 | + * { margin: 0; padding: 0; box-sizing: border-box; } |
| 9 | + body { |
| 10 | + font-family: var(--font-sans, system-ui, sans-serif); |
| 11 | + background: var(--color-background-primary, #ffffff); |
| 12 | + color: var(--color-text-primary, #1a1a1a); |
| 13 | + padding: 1rem; |
| 14 | + } |
| 15 | + .card { |
| 16 | + border: 1px solid var(--color-border-primary, #e0e0e0); |
| 17 | + border-radius: var(--border-radius-md, 8px); |
| 18 | + padding: 1rem; |
| 19 | + margin-bottom: 1rem; |
| 20 | + } |
| 21 | + h1 { font-size: var(--font-heading-md-size, 1.25rem); margin-bottom: 0.5rem; } |
| 22 | + .weather-data { font-size: var(--font-text-lg-size, 1.125rem); } |
| 23 | + button { |
| 24 | + background: var(--color-background-info, #0066cc); |
| 25 | + color: var(--color-text-inverse, #ffffff); |
| 26 | + border: none; |
| 27 | + border-radius: var(--border-radius-sm, 4px); |
| 28 | + padding: 0.5rem 1rem; |
| 29 | + cursor: pointer; |
| 30 | + font-size: var(--font-text-md-size, 1rem); |
| 31 | + } |
| 32 | + input { |
| 33 | + border: 1px solid var(--color-border-primary, #e0e0e0); |
| 34 | + border-radius: var(--border-radius-sm, 4px); |
| 35 | + padding: 0.5rem; |
| 36 | + font-size: var(--font-text-md-size, 1rem); |
| 37 | + margin-right: 0.5rem; |
| 38 | + } |
| 39 | + </style> |
| 40 | +</head> |
| 41 | +<body> |
| 42 | + <div class="card"> |
| 43 | + <h1>Weather Dashboard</h1> |
| 44 | + <div> |
| 45 | + <input type="text" id="city" placeholder="Enter city name" value="London"> |
| 46 | + <button onclick="fetchWeather()">Get Weather</button> |
| 47 | + </div> |
| 48 | + </div> |
| 49 | + <div class="card" id="result" style="display:none"> |
| 50 | + <div class="weather-data" id="weather-data"></div> |
| 51 | + </div> |
| 52 | + <script> |
| 53 | + // MCP Apps communicate with the host via window.parent.postMessage |
| 54 | + // using JSON-RPC 2.0 messages. |
| 55 | + let requestId = 0; |
| 56 | + const pending = new Map(); |
| 57 | + |
| 58 | + window.addEventListener('message', (event) => { |
| 59 | + try { |
| 60 | + const msg = JSON.parse(event.data); |
| 61 | + if (msg.id && pending.has(msg.id)) { |
| 62 | + pending.get(msg.id)(msg); |
| 63 | + pending.delete(msg.id); |
| 64 | + } |
| 65 | + // Handle tool input notification |
| 66 | + if (msg.method === 'ui/notifications/tool-input') { |
| 67 | + document.getElementById('city').value = msg.params.arguments.city || ''; |
| 68 | + } |
| 69 | + // Handle tool result notification |
| 70 | + if (msg.method === 'ui/notifications/tool-result') { |
| 71 | + displayResult(msg.params); |
| 72 | + } |
| 73 | + } catch (e) { /* ignore non-JSON messages */ } |
| 74 | + }); |
| 75 | + |
| 76 | + function sendRpc(method, params) { |
| 77 | + return new Promise((resolve) => { |
| 78 | + const id = ++requestId; |
| 79 | + pending.set(id, resolve); |
| 80 | + window.parent.postMessage(JSON.stringify({ |
| 81 | + jsonrpc: '2.0', id, method, params |
| 82 | + }), '*'); |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + async function fetchWeather() { |
| 87 | + const city = document.getElementById('city').value; |
| 88 | + const response = await sendRpc('tools/call', { |
| 89 | + name: 'get_weather', |
| 90 | + arguments: { city } |
| 91 | + }); |
| 92 | + if (response.result) { |
| 93 | + displayResult(response.result); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + function displayResult(result) { |
| 98 | + const el = document.getElementById('result'); |
| 99 | + const data = document.getElementById('weather-data'); |
| 100 | + if (result.content && result.content[0]) { |
| 101 | + data.textContent = result.content[0].text; |
| 102 | + } |
| 103 | + el.style.display = 'block'; |
| 104 | + } |
| 105 | + </script> |
| 106 | +</body> |
| 107 | +</html> |
0 commit comments