1 line
5.2 KiB
JavaScript
1 line
5.2 KiB
JavaScript
!function(){const t=-32601,n=-32603;let o=0;const e=new Map,i={},s=["ontoolinput","ontoolinputpartial","ontoolresult","ontoolcancelled","onhostcontextchanged"].reduce(function(t,n){return t[n]=[],t},{}),r={hostContext:null,hostInfo:null,hostCapabilities:null};let c=null;function u(){c&&(c.disconnect(),c=null)}const l={"ui/notifications/host-context-changed":C,"ui/notifications/tool-input":function(t){w("ontoolinput",t??{})},"ui/notifications/tool-input-partial":function(t){w("ontoolinputpartial",t??{})},"ui/notifications/tool-result":function(t){w("ontoolresult",t??{})},"ui/notifications/tool-cancelled":function(t){w("ontoolcancelled",t??{})}};function a(t){t.jsonrpc="2.0",window.parent.postMessage(t,"*")}function d(t,n){return new Promise(function(i,s){const r=++o;e.set(r,{resolve:i,reject:s}),a({id:r,method:t,params:n})})}function f(t,n){const o={method:t};void 0!==n&&(o.params=n),a(o)}function m(t,n){a({id:t,result:n})}function p(t,n,o){a({id:t,error:{code:n,message:o}})}function h(t){return null!==t&&"object"==typeof t&&!Array.isArray(t)}function g(t,n){return h(t)?t:{[n]:t}}function y(t,n){return Object.assign({},t||{},n||{})}function w(t,n){const o=i[t];o?o(n):s[t]&&s[t].push(n)}function v(t,n){i[t]=n,function(t){const n=i[t],o=s[t];if(n&&o&&0!==o.length)for(;o.length>0;)n(o.shift())}(t)}function C(t){const n=function(t){if(!t)return r.hostContext;const n=r.hostContext||{},o=y(n,t);if(n.styles||t.styles){const e=n.styles||{},i=t.styles||{};o.styles=y(e,i),o.styles.variables=y(e.variables,i.variables),o.styles.css=y(e.css,i.css)}return r.hostContext=o,o}(t);var o,e;n&&((o=n.theme)&&(document.documentElement.setAttribute("data-theme",o),document.documentElement.style.colorScheme=o),e=n.styles?.variables,e&&Object.keys(e).filter(t=>void 0!==e[t]).forEach(function(t){document.documentElement.style.setProperty(t,e[t])}),function(t){if(!t)return;let n=document.getElementById("__mcp-host-fonts");n||(n=document.createElement("style"),n.id="__mcp-host-fonts",document.head.appendChild(n)),n.textContent=t}(n.styles?.css?.fonts),w("onhostcontextchanged",n))}function b(){f("ui/notifications/size-changed",{width:document.documentElement.scrollWidth,height:document.documentElement.scrollHeight})}function x(){if("undefined"!=typeof ResizeObserver&&document.body)return u(),c=new ResizeObserver(b),c.observe(document.body),u}const E={"ui/resource-teardown":function(t){!async function(t){try{u(),m(t,await(i.onteardown?.()??{}))}catch(o){p(t,n,o instanceof Error?o.message:"Unknown teardown error")}}(t.id)},"tools/call":function(o){!async function(o,e){if(i.oncalltool)try{m(o,await i.oncalltool(e))}catch(t){p(o,n,t instanceof Error?t.message:"Unknown tool error")}else p(o,t,"No tool handler registered.")}(o.id,o.params)},"tools/list":function(t){!async function(t,o){try{m(t,await(i.onlisttools?.(o)??{tools:[]}))}catch(o){p(t,n,o instanceof Error?o.message:"Unknown list tools error")}}(t.id,t.params)}};window.addEventListener("message",function(n){if(n.source!==window.parent)return;const o=function(t){if("string"==typeof t)try{return JSON.parse(t)}catch(t){return null}return t&&"object"==typeof t?t:null}(n.data);o&&"2.0"===o.jsonrpc&&(function(t){if(void 0===t.id||!e.has(t.id))return!1;const n=e.get(t.id);return e.delete(t.id),t.error?n.reject(new Error(t.error.message)):n.resolve(t.result),!0}(o)||(void 0!==o.id?function(n){const o=E[n.method];o?o(n):p(n.id,t,"Method not found: "+n.method)}(o):function(t){const n=l[t.method];n&&n(t.params)}(o)))}),window.createMcpApp=async function(t){const n=await d("ui/initialize",{protocolVersion:"2026-01-26",appInfo:{name:document.title||"MCP App",version:"1.0.0"},appCapabilities:{}});r.hostInfo=n?.hostInfo??null,r.hostCapabilities=n?.hostCapabilities??null,C(n?.hostContext??null),f("ui/notifications/initialized"),await t({getHostContext:function(){return r.hostContext},getHostInfo:function(){return r.hostInfo},getHostCapabilities:function(){return r.hostCapabilities},callServerTool:function(t,n){return d("tools/call",h(t)?{name:t.name,arguments:t.arguments||{}}:{name:t,arguments:n||{}})},listResources:function(t){return d("resources/list",t?g(t,"cursor"):void 0)},readResource:function(t){return d("resources/read",g(t,"uri"))},sendMessage:function(t,n){return d("ui/message",h(t)&&("content"in t||"role"in t)?{role:t.role||"user",content:t.content}:{role:n||"user",content:t})},openLink:function(t){return d("ui/open-link",g(t,"url"))},downloadFile:function(t){return d("ui/download-file",h(t)&&"contents"in t?t:{contents:t})},requestDisplayMode:function(t){return d("ui/request-display-mode",g(t,"mode"))},updateModelContext:function(t){return d("ui/update-model-context",t||{})},requestTeardown:function(){f("ui/notifications/request-teardown")},sendLog:function(t,n,o){const e=h(t)?t:{level:t,data:n};return h(t)||void 0===o||(e.logger=o),f("notifications/message",e),Promise.resolve()},resize:b,autoResize:x,onTeardown:function(t){i.onteardown=t},onCallTool:function(t){i.oncalltool=t},onListTools:function(t){i.onlisttools=t},onToolInput:function(t){v("ontoolinput",t)},onToolInputPartial:function(t){v("ontoolinputpartial",t)},onToolResult:function(t){v("ontoolresult",t)},onToolCancelled:function(t){v("ontoolcancelled",t)},onHostContextChanged:function(t){v("onhostcontextchanged",t)}})}}(); |