← Files Sulekha EventsARCHIVED FILE
source/scripts/demo-local.js
3.69 KB · Nov 30, 2026 · 07:53 UTC
import { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
const html = await readFile(new URL('../public/events-widget.html', import.meta.url), 'utf8');
const js = await readFile(new URL('../public/events-widget.js', import.meta.url), 'utf8');
const host = `<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Sulekha local demo</title><style>body{font-family:system-ui;margin:0;background:#faf7f3;color:#242424}header{padding:24px;max-width:1000px;margin:auto}h1{margin:0 0 8px}input,button{padding:12px;font:inherit;border:1px solid #ccc;border-radius:8px}button{background:#b9470b;color:white;cursor:pointer}iframe{width:100%;height:780px;border:0}p{color:#666}</style></head><body><header><h1>Sulekha Events · Local demo</h1><p>Live results within 60 miles of Edison, New Jersey. Nearby states may appear.</p><form><input aria-label="Event or artist" placeholder="Event or artist (optional)"><button>Search events</button></form><p id="status">Loading live events…</p></header><iframe title="Sulekha events" src="/widget"></iframe><script>
const frame=document.querySelector('iframe'),status=document.querySelector('#status');
let latest;
function deliver(){if(latest)frame.contentWindow.postMessage({jsonrpc:'2.0',method:'ui/notifications/tool-result',params:latest},location.origin)}
window.addEventListener('message',e=>{if(e.source!==frame.contentWindow||e.origin!==location.origin)return;const m=e.data;if(m.method==='ui/initialize'){frame.contentWindow.postMessage({jsonrpc:'2.0',id:m.id,result:{}},location.origin);deliver()}else if(m.method==='ui/open-link'){const u=new URL(m.params.url);if(['https:','http:'].includes(u.protocol))window.open(u.href,'_blank','noopener');frame.contentWindow.postMessage({jsonrpc:'2.0',id:m.id,result:{}},location.origin)}});
async function search(){status.textContent='Searching live Sulekha API…';try{const r=await fetch('/search?q='+encodeURIComponent(document.querySelector('input').value));latest=await r.json();deliver();status.textContent=latest.isError?latest.content[0].text:latest.structuredContent.count+' live results · served through the local MCP app';}catch(e){status.textContent='Search failed: '+e.message}}
document.querySelector('form').onsubmit=e=>{e.preventDefault();search()};frame.onload=deliver;search();
</script></body></html>`;
createServer(async(req,res)=>{
const url=new URL(req.url,'http://127.0.0.1:8796');
res.setHeader('Cache-Control','no-store');
if(url.pathname==='/search'){
const client=new Client({name:'local-demo',version:'1.0.0'});
try{await client.connect(new StreamableHTTPClientTransport(new URL('http://127.0.0.1:8795/mcp')));const result=await client.callTool({name:'search_sulekha_events',arguments:{query:url.searchParams.get('q')||'',cityUrl:'edison-nj',latitude:40.5187,longitude:-74.4121,distance:60,limit:12}});res.writeHead(200,{'Content-Type':'application/json'}).end(JSON.stringify(result));}
catch{res.writeHead(502,{'Content-Type':'application/json'}).end(JSON.stringify({isError:true,content:[{type:'text',text:'Local MCP server unavailable.'}]}));}finally{await client.close()}return;
}
if(url.pathname==='/widget.js'){res.writeHead(200,{'Content-Type':'text/javascript'}).end(js);return;}
if(url.pathname==='/'||url.pathname==='/widget'){res.writeHead(200,{'Content-Type':'text/html'}).end(url.pathname==='/'?host:html.replace('__WIDGET_SCRIPT_URL__','/widget.js'));return;}
res.writeHead(404).end();
}).listen(8796,'127.0.0.1',()=>console.log('Local demo: http://127.0.0.1:8796'));
SHA-256: b04e443549a864a917d49adddf3aef4710caa4897020fb2b09020f8e75173823