|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +class WeatherDashboardTemplate < ApplicationMCPResTemplate |
| 4 | + description "Interactive weather dashboard UI for the weather tool" |
| 5 | + uri_template "ui://weather/dashboard" |
| 6 | + mime_type ActionMCP::MIME_TYPE_APP_HTML |
| 7 | + |
| 8 | + # The spec (ext-apps apps.mdx, Metadata Location section) allows _meta.ui on |
| 9 | + # both the listing entry and the content item, with content-level taking |
| 10 | + # precedence. We intentionally emit both so the demo exercises each placement |
| 11 | + # and the wire shape stays visible to the tests. |
| 12 | + meta ui: { |
| 13 | + csp: { connectDomains: %w[api.openweathermap.org] }, |
| 14 | + prefersBorder: true |
| 15 | + } |
| 16 | + |
| 17 | + HTML = <<~HTML |
| 18 | + <!doctype html> |
| 19 | + <html lang="en"> |
| 20 | + <head> |
| 21 | + <meta charset="utf-8"> |
| 22 | + <title>Weather</title> |
| 23 | + <style> |
| 24 | + :root { color-scheme: light dark; font-family: system-ui, sans-serif; } |
| 25 | + body { margin: 0; padding: 1.25rem; } |
| 26 | + .card { max-width: 28rem; padding: 1rem 1.25rem; border-radius: 0.75rem; |
| 27 | + background: color-mix(in srgb, currentColor 6%, transparent); } |
| 28 | + h1 { margin: 0 0 0.5rem; font-size: 1.25rem; } |
| 29 | + .temp { font-size: 3rem; font-weight: 600; line-height: 1; } |
| 30 | + .row { display: flex; gap: 1rem; margin-top: 0.75rem; font-size: 0.9rem; |
| 31 | + opacity: 0.8; } |
| 32 | + .icon { font-size: 2.5rem; } |
| 33 | + </style> |
| 34 | + </head> |
| 35 | + <body> |
| 36 | + <div class="card" role="region" aria-label="Weather summary"> |
| 37 | + <h1 id="loc">Weather</h1> |
| 38 | + <div class="icon" aria-hidden="true">\u2600\ufe0f</div> |
| 39 | + <div class="temp"><span id="temp">--</span>°</div> |
| 40 | + <div class="row"> |
| 41 | + <div>Humidity <span id="hum">--</span>%</div> |
| 42 | + <div>Wind <span id="wind">--</span> km/h</div> |
| 43 | + </div> |
| 44 | + </div> |
| 45 | + <script> |
| 46 | + (function () { |
| 47 | + function set(id, v) { var el = document.getElementById(id); if (el) el.textContent = v; } |
| 48 | + window.addEventListener("message", function (event) { |
| 49 | + var data = event.data && event.data.result; |
| 50 | + if (!data || !data.current) return; |
| 51 | + if (data.metadata && data.metadata.location_found) set("loc", data.metadata.location_found); |
| 52 | + set("temp", Math.round(data.current.temperature)); |
| 53 | + set("hum", data.current.humidity); |
| 54 | + set("wind", data.current.wind_speed); |
| 55 | + }); |
| 56 | + }()); |
| 57 | + </script> |
| 58 | + </body> |
| 59 | + </html> |
| 60 | + HTML |
| 61 | + |
| 62 | + def resolve |
| 63 | + ActionMCP::Content::Resource.new( |
| 64 | + self.class.uri_template, |
| 65 | + ActionMCP::MIME_TYPE_APP_HTML, |
| 66 | + text: HTML, |
| 67 | + _meta: { |
| 68 | + ui: { |
| 69 | + csp: { connectDomains: %w[api.openweathermap.org] }, |
| 70 | + prefersBorder: true |
| 71 | + } |
| 72 | + } |
| 73 | + ) |
| 74 | + end |
| 75 | +end |
0 commit comments