@modelcontextprotocol/ext-apps 0.4.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +196 -1
- package/README.md +21 -10
- package/dist/src/app-bridge.d.ts +84 -71
- package/dist/src/app-bridge.js +2 -2
- package/dist/src/app-with-deps.js +2 -2
- package/dist/src/app.d.ts +33 -23
- package/dist/src/app.js +2 -2
- package/dist/src/generated/schema.d.ts +9 -9
- package/dist/src/message-transport.d.ts +20 -22
- package/dist/src/react/index.js +8 -8
- package/dist/src/react/react-with-deps.js +8 -8
- package/dist/src/react/useApp.d.ts +22 -17
- package/dist/src/react/useAutoResize.d.ts +5 -3
- package/dist/src/react/useDocumentTheme.d.ts +3 -1
- package/dist/src/react/useHostStyles.d.ts +19 -18
- package/dist/src/server/index.d.ts +106 -13
- package/dist/src/server/index.js +9 -9
- package/dist/src/spec.types.d.ts +15 -15
- package/dist/src/styles.d.ts +5 -5
- package/dist/src/types.d.ts +7 -6
- package/package.json +5 -3
package/dist/src/app-bridge.js
CHANGED
|
@@ -55,7 +55,7 @@ Individual style keys are optional - hosts may provide any subset of these value
|
|
|
55
55
|
Values are strings containing CSS values (colors, sizes, font stacks, etc.).
|
|
56
56
|
|
|
57
57
|
Note: This type uses \`Record<K, string | undefined>\` rather than \`Partial<Record<K, string>>\`
|
|
58
|
-
for compatibility with Zod schema generation. Both are functionally equivalent for validation.`),be=g.object({method:g.literal("ui/open-link"),params:g.object({url:g.string().describe("URL to open in the host's browser")})}),Ue=g.object({isError:g.boolean().optional().describe("True if the host failed to open the URL (e.g., due to security policy).")}).passthrough(),ke=g.object({isError:g.boolean().optional().describe("True if the host rejected or failed to deliver the message.")}).passthrough(),De=g.object({method:g.literal("ui/notifications/sandbox-proxy-ready"),params:g.object({})}),Dv=g.object({connectDomains:g.array(g.string()).optional().describe("Origins for network requests (fetch/XHR/WebSocket)."),resourceDomains:g.array(g.string()).optional().describe("Origins for static resources (scripts, images, styles, fonts)."),frameDomains:g.array(g.string()).optional().describe("Origins for nested iframes (frame-src directive)."),baseUriDomains:g.array(g.string()).optional().describe("Allowed base URIs for the document (base-uri directive).")}),wv=g.object({camera:g.object({}).optional().describe("Request camera access (Permission Policy `camera` feature)."),microphone:g.object({}).optional().describe("Request microphone access (Permission Policy `microphone` feature)."),geolocation:g.object({}).optional().describe("Request geolocation access (Permission Policy `geolocation` feature)."),clipboardWrite:g.object({}).optional().describe("Request clipboard write access (Permission Policy `clipboard-write` feature).")}),we=g.object({method:g.literal("ui/notifications/size-changed"),params:g.object({width:g.number().optional().describe("New width in pixels."),height:g.number().optional().describe("New height in pixels.")})}),Ne=g.object({method:g.literal("ui/notifications/tool-input"),params:g.object({arguments:g.record(g.string(),g.unknown().describe("Complete tool call arguments as key-value pairs.")).optional().describe("Complete tool call arguments as key-value pairs.")})}),ze=g.object({method:g.literal("ui/notifications/tool-input-partial"),params:g.object({arguments:g.record(g.string(),g.unknown().describe("Partial tool call arguments (incomplete, may change).")).optional().describe("Partial tool call arguments (incomplete, may change).")})}),Oe=g.object({method:g.literal("ui/notifications/tool-cancelled"),params:g.object({reason:g.string().optional().describe('Optional reason for the cancellation (e.g., "user action", "timeout").')})}),QI=g.object({fonts:g.string().optional()
|
|
58
|
+
for compatibility with Zod schema generation. Both are functionally equivalent for validation.`),be=g.object({method:g.literal("ui/open-link"),params:g.object({url:g.string().describe("URL to open in the host's browser")})}),Ue=g.object({isError:g.boolean().optional().describe("True if the host failed to open the URL (e.g., due to security policy).")}).passthrough(),ke=g.object({isError:g.boolean().optional().describe("True if the host rejected or failed to deliver the message.")}).passthrough(),De=g.object({method:g.literal("ui/notifications/sandbox-proxy-ready"),params:g.object({})}),Dv=g.object({connectDomains:g.array(g.string()).optional().describe("Origins for network requests (fetch/XHR/WebSocket)."),resourceDomains:g.array(g.string()).optional().describe("Origins for static resources (scripts, images, styles, fonts)."),frameDomains:g.array(g.string()).optional().describe("Origins for nested iframes (frame-src directive)."),baseUriDomains:g.array(g.string()).optional().describe("Allowed base URIs for the document (base-uri directive).")}),wv=g.object({camera:g.object({}).optional().describe("Request camera access (Permission Policy `camera` feature)."),microphone:g.object({}).optional().describe("Request microphone access (Permission Policy `microphone` feature)."),geolocation:g.object({}).optional().describe("Request geolocation access (Permission Policy `geolocation` feature)."),clipboardWrite:g.object({}).optional().describe("Request clipboard write access (Permission Policy `clipboard-write` feature).")}),we=g.object({method:g.literal("ui/notifications/size-changed"),params:g.object({width:g.number().optional().describe("New width in pixels."),height:g.number().optional().describe("New height in pixels.")})}),Ne=g.object({method:g.literal("ui/notifications/tool-input"),params:g.object({arguments:g.record(g.string(),g.unknown().describe("Complete tool call arguments as key-value pairs.")).optional().describe("Complete tool call arguments as key-value pairs.")})}),ze=g.object({method:g.literal("ui/notifications/tool-input-partial"),params:g.object({arguments:g.record(g.string(),g.unknown().describe("Partial tool call arguments (incomplete, may change).")).optional().describe("Partial tool call arguments (incomplete, may change).")})}),Oe=g.object({method:g.literal("ui/notifications/tool-cancelled"),params:g.object({reason:g.string().optional().describe('Optional reason for the cancellation (e.g., "user action", "timeout").')})}),QI=g.object({fonts:g.string().optional()}),FI=g.object({variables:f6.optional().describe("CSS variables for theming the app."),css:QI.optional().describe("CSS blocks that apps can inject.")}),Se=g.object({method:g.literal("ui/resource-teardown"),params:g.object({})}),Pe=g.record(g.string(),g.unknown()),_e=g.object({text:g.object({}).optional().describe("Host supports text content blocks."),image:g.object({}).optional().describe("Host supports image content blocks."),audio:g.object({}).optional().describe("Host supports audio content blocks."),resource:g.object({}).optional().describe("Host supports resource content blocks."),resourceLink:g.object({}).optional().describe("Host supports resource link content blocks."),structuredContent:g.object({}).optional().describe("Host supports structured content.")}),TI=g.object({experimental:g.object({}).optional().describe("Experimental features (structure TBD)."),openLinks:g.object({}).optional().describe("Host supports opening external URLs."),serverTools:g.object({listChanged:g.boolean().optional().describe("Host supports tools/list_changed notifications.")}).optional().describe("Host can proxy tool calls to the MCP server."),serverResources:g.object({listChanged:g.boolean().optional().describe("Host supports resources/list_changed notifications.")}).optional().describe("Host can proxy resource reads to the MCP server."),logging:g.object({}).optional().describe("Host accepts log messages."),sandbox:g.object({permissions:wv.optional().describe("Permissions granted by the host (camera, microphone, geolocation)."),csp:Dv.optional().describe("CSP domains approved by the host.")}).optional().describe("Sandbox configuration applied by the host."),updateModelContext:_e.optional().describe("Host accepts context updates (ui/update-model-context) to be included in the model's context for future turns."),message:_e.optional().describe("Host supports receiving content messages (ui/message) from the Guest UI.")}),qI=g.object({experimental:g.object({}).optional().describe("Experimental features (structure TBD)."),tools:g.object({listChanged:g.boolean().optional().describe("App supports tools/list_changed notifications.")}).optional().describe("App exposes MCP-style tools that the host can call.")}),je=g.object({method:g.literal("ui/notifications/initialized"),params:g.object({}).optional()}),y6=g.object({csp:Dv.optional().describe("Content Security Policy configuration."),permissions:wv.optional().describe("Sandbox permissions requested by the UI."),domain:g.string().optional().describe("Dedicated origin for widget sandbox."),prefersBorder:g.boolean().optional().describe("Visual boundary preference - true if UI prefers a visible border.")}),Nv=g.object({method:g.literal("ui/request-display-mode"),params:g.object({mode:kv.describe("The display mode being requested.")})}),Je=g.object({mode:kv.describe("The display mode that was actually set. May differ from requested if not supported.")}).passthrough(),HI=g.union([g.literal("model"),g.literal("app")]).describe("Tool visibility scope - who can access the tool."),h6=g.object({resourceUri:g.string().optional(),visibility:g.array(HI).optional().describe(`Who can access this tool. Default: ["model", "app"]
|
|
59
59
|
- "model": Tool visible to and callable by the agent
|
|
60
60
|
- "app": Tool callable by the app from this server only`)}),Le=g.object({method:g.literal("ui/message"),params:g.object({role:g.literal("user").describe('Message role, currently only "user" is supported.'),content:g.array(VI).describe("Message content blocks (text, image, etc.).")})}),a6=g.object({method:g.literal("ui/notifications/sandbox-resource-ready"),params:g.object({html:g.string().describe("HTML content to load into the inner iframe."),sandbox:g.string().optional().describe("Optional override for the inner iframe's sandbox attribute."),csp:Dv.optional().describe("CSP configuration from resource metadata."),permissions:wv.optional().describe("Sandbox permissions from resource metadata.")})}),Ee=g.object({method:g.literal("ui/notifications/tool-result"),params:x6.describe("Standard MCP tool execution result.")}),Ge=g.object({toolInfo:g.object({id:Z6.optional().describe("JSON-RPC id of the tools/call request."),tool:d6.describe("Tool definition including name, inputSchema, etc.")}).optional().describe("Metadata of the tool call that instantiated this App."),theme:YI.optional().describe("Current color theme preference."),styles:FI.optional().describe("Style configuration for theming the app."),displayMode:kv.optional().describe("How the UI is currently displayed."),availableDisplayModes:g.array(g.string()).optional().describe("Display modes the host supports."),containerDimensions:g.union([g.object({height:g.number().describe("Fixed container height in pixels.")}),g.object({maxHeight:g.union([g.number(),g.undefined()]).optional().describe("Maximum container height in pixels.")})]).and(g.union([g.object({width:g.number().describe("Fixed container width in pixels.")}),g.object({maxWidth:g.union([g.number(),g.undefined()]).optional().describe("Maximum container width in pixels.")})])).optional().describe(`Container dimensions. Represents the dimensions of the iframe or other
|
|
61
|
-
container holding the app. Specify either width or maxWidth, and either height or maxHeight.`),locale:g.string().optional().describe("User's language and region preference in BCP 47 format."),timeZone:g.string().optional().describe("User's timezone in IANA format."),userAgent:g.string().optional().describe("Host application identifier."),platform:g.union([g.literal("web"),g.literal("desktop"),g.literal("mobile")]).optional().describe("Platform type for responsive design decisions."),deviceCapabilities:g.object({touch:g.boolean().optional().describe("Whether the device supports touch input."),hover:g.boolean().optional().describe("Whether the device supports hover interactions.")}).optional().describe("Device input capabilities."),safeAreaInsets:g.object({top:g.number().describe("Top safe area inset in pixels."),right:g.number().describe("Right safe area inset in pixels."),bottom:g.number().describe("Bottom safe area inset in pixels."),left:g.number().describe("Left safe area inset in pixels.")}).optional().describe("Mobile safe area boundaries in pixels.")}).passthrough(),We=g.object({method:g.literal("ui/notifications/host-context-changed"),params:Ge.describe("Partial context update containing only changed fields.")}),Ae=g.object({method:g.literal("ui/update-model-context"),params:g.object({content:g.array(VI).optional().describe("Context content blocks (text, image, etc.)."),structuredContent:g.record(g.string(),g.unknown().describe("Structured content for machine-readable context data.")).optional().describe("Structured content for machine-readable context data.")})}),Xe=g.object({method:g.literal("ui/initialize"),params:g.object({appInfo:KI.describe("App identification (name and version)."),appCapabilities:qI.describe("Features and capabilities this app provides."),protocolVersion:g.string().describe("Protocol version this app supports.")})}),Ve=g.object({protocolVersion:g.string().describe('Negotiated protocol version string (e.g., "2025-11-21").'),hostInfo:KI.describe("Host application identification and version."),hostCapabilities:TI.describe("Features and capabilities provided by the host."),hostContext:Ge.describe("Rich context about the host environment.")}).passthrough();import{Protocol as s6}from"@modelcontextprotocol/sdk/shared/protocol.js";import{CallToolRequestSchema as r_,CallToolResultSchema as n_,EmptyResultSchema as i_,ListToolsRequestSchema as v_,PingRequestSchema as o_}from"@modelcontextprotocol/sdk/types.js";import{JSONRPCMessageSchema as p6}from"@modelcontextprotocol/sdk/types.js";class Qn{eventTarget;eventSource;messageListener;constructor(r=window.parent,i){this.eventTarget=r;this.eventSource=i;this.messageListener=(o)=>{if(i&&o.source!==this.eventSource){console.error("Ignoring message from unknown source",o);return}let t=p6.safeParse(o.data);if(t.success)console.debug("Parsed message",t.data),this.onmessage?.(t.data);else console.error("Failed to parse message",t.error.message,o),this.onerror?.(Error("Invalid JSON-RPC message received: "+t.error.message))}}async start(){window.addEventListener("message",this.messageListener)}async send(r,i){console.debug("Sending message",r),this.eventTarget.postMessage(r,"*")}async close(){window.removeEventListener("message",this.messageListener),this.onclose?.()}onclose;onerror;onmessage;sessionId;setProtocolVersion}var Ke="ui/resourceUri",t_="text/html;profile=mcp-app";class $_ extends s6{_appInfo;_capabilities;options;_hostCapabilities;_hostInfo;_hostContext;constructor(r,i={},o={autoResize:!0}){super(o);this._appInfo=r;this._capabilities=i;this.options=o;this.setRequestHandler(o_,(t)=>{return console.log("Received ping:",t.params),{}}),this.onhostcontextchanged=()=>{}}getHostCapabilities(){return this._hostCapabilities}getHostVersion(){return this._hostInfo}getHostContext(){return this._hostContext}set ontoolinput(r){this.setNotificationHandler(Ne,(i)=>r(i.params))}set ontoolinputpartial(r){this.setNotificationHandler(ze,(i)=>r(i.params))}set ontoolresult(r){this.setNotificationHandler(Ee,(i)=>r(i.params))}set ontoolcancelled(r){this.setNotificationHandler(Oe,(i)=>r(i.params))}set onhostcontextchanged(r){this.setNotificationHandler(We,(i)=>{this._hostContext={...this._hostContext,...i.params},r(i.params)})}set onteardown(r){this.setRequestHandler(Se,(i,o)=>r(i.params,o))}set oncalltool(r){this.setRequestHandler(r_,(i,o)=>r(i.params,o))}set onlisttools(r){this.setRequestHandler(v_,(i,o)=>r(i.params,o))}assertCapabilityForMethod(r){}assertRequestHandlerCapability(r){switch(r){case"tools/call":case"tools/list":if(!this._capabilities.tools)throw Error(`Client does not support tool capability (required for ${r})`);return;case"ping":case"ui/resource-teardown":return;default:throw Error(`No handler for method ${r} registered`)}}assertNotificationCapability(r){}assertTaskCapability(r){throw Error("Tasks are not supported in MCP Apps")}assertTaskHandlerCapability(r){throw Error("Task handlers are not supported in MCP Apps")}async callServerTool(r,i){return await this.request({method:"tools/call",params:r},n_,i)}sendMessage(r,i){return this.request({method:"ui/message",params:r},ke,i)}sendLog(r){return this.notification({method:"notifications/message",params:r})}updateModelContext(r,i){return this.request({method:"ui/update-model-context",params:r},i_,i)}openLink(r,i){return this.request({method:"ui/open-link",params:r},Ue,i)}sendOpenLink=this.openLink;requestDisplayMode(r,i){return this.request({method:"ui/request-display-mode",params:r},Je,i)}sendSizeChanged(r){return this.notification({method:"ui/notifications/size-changed",params:r})}setupSizeChangedNotifications(){let r=!1,i=0,o=0,t=()=>{if(r)return;r=!0,requestAnimationFrame(()=>{r=!1;let v=document.documentElement,$=v.style.width,u=v.style.height;v.style.width="fit-content",v.style.height="fit-content";let l=v.getBoundingClientRect();v.style.width=$,v.style.height=u;let e=window.innerWidth-v.clientWidth,c=Math.ceil(l.width+e),b=Math.ceil(l.height);if(c!==i||b!==o)i=c,o=b,this.sendSizeChanged({width:c,height:b})})};t();let n=new ResizeObserver(t);return n.observe(document.documentElement),n.observe(document.body),()=>n.disconnect()}async connect(r=new Qn(window.parent,window.parent),i){await super.connect(r);try{let o=await this.request({method:"ui/initialize",params:{appCapabilities:this._capabilities,appInfo:this._appInfo,protocolVersion:Sr}},Ve,i);if(o===void 0)throw Error(`Server sent invalid initialize result: ${o}`);if(this._hostCapabilities=o.hostCapabilities,this._hostInfo=o.hostInfo,this._hostContext=o.hostContext,await this.notification({method:"ui/notifications/initialized"}),this.options?.autoResize)this.setupSizeChangedNotifications()}catch(o){throw this.close(),o}}}function yD(r){let o=r._meta?.ui?.resourceUri;if(o===void 0)o=r._meta?.[Ke];if(typeof o==="string"&&o.startsWith("ui://"))return o;else if(o!==void 0)throw Error(`Invalid UI resource URI: ${JSON.stringify(o)}`);return}function hD(r){if(!r)return"";let i=[];if(r.camera)i.push("camera");if(r.microphone)i.push("microphone");if(r.geolocation)i.push("geolocation");if(r.clipboardWrite)i.push("clipboard-write");return i.join("; ")}var P_=[Sr];class j_ extends S_{_client;_hostInfo;_capabilities;_appCapabilities;_hostContext={};_appInfo;constructor(r,i,o,t){super(t);this._client=r;this._hostInfo=i;this._capabilities=o;this._hostContext=t?.hostContext||{},this.setRequestHandler(Xe,(n)=>this._oninitialize(n)),this.setRequestHandler(k_,(n,v)=>{return this.onping?.(n.params,v),{}}),this.setRequestHandler(Nv,(n)=>{return{mode:this._hostContext.displayMode??"inline"}})}getAppCapabilities(){return this._appCapabilities}getAppVersion(){return this._appInfo}onping;set onsizechange(r){this.setNotificationHandler(we,(i)=>r(i.params))}set onsandboxready(r){this.setNotificationHandler(De,(i)=>r(i.params))}set oninitialized(r){this.setNotificationHandler(je,(i)=>r(i.params))}set onmessage(r){this.setRequestHandler(Le,async(i,o)=>{return r(i.params,o)})}set onopenlink(r){this.setRequestHandler(be,async(i,o)=>{return r(i.params,o)})}set onrequestdisplaymode(r){this.setRequestHandler(Nv,async(i,o)=>{return r(i.params,o)})}set onloggingmessage(r){this.setNotificationHandler(U_,async(i)=>{r(i.params)})}set onupdatemodelcontext(r){this.setRequestHandler(Ae,async(i,o)=>{return r(i.params,o)})}set oncalltool(r){this.setRequestHandler(u_,async(i,o)=>{return r(i.params,o)})}sendToolListChanged(r={}){return this.notification({method:"notifications/tools/list_changed",params:r})}set onlistresources(r){this.setRequestHandler(I_,async(i,o)=>{return r(i.params,o)})}set onlistresourcetemplates(r){this.setRequestHandler(__,async(i,o)=>{return r(i.params,o)})}set onreadresource(r){this.setRequestHandler(w_,async(i,o)=>{return r(i.params,o)})}sendResourceListChanged(r={}){return this.notification({method:"notifications/resources/list_changed",params:r})}set onlistprompts(r){this.setRequestHandler(e_,async(i,o)=>{return r(i.params,o)})}sendPromptListChanged(r={}){return this.notification({method:"notifications/prompts/list_changed",params:r})}assertCapabilityForMethod(r){}assertRequestHandlerCapability(r){}assertNotificationCapability(r){}assertTaskCapability(r){throw Error("Tasks are not supported in MCP Apps")}assertTaskHandlerCapability(r){throw Error("Task handlers are not supported in MCP Apps")}getCapabilities(){return this._capabilities}async _oninitialize(r){let i=r.params.protocolVersion;return this._appCapabilities=r.params.appCapabilities,this._appInfo=r.params.appInfo,{protocolVersion:P_.includes(i)?i:Sr,hostCapabilities:this.getCapabilities(),hostInfo:this._hostInfo,hostContext:this._hostContext}}setHostContext(r){let i={},o=!1;for(let t of Object.keys(r)){let n=this._hostContext[t],v=r[t];if(J_(n,v))continue;i[t]=v,o=!0}if(o)this._hostContext=r,this.sendHostContextChange(i)}sendHostContextChange(r){return this.notification({method:"ui/notifications/host-context-changed",params:r})}sendToolInput(r){return this.notification({method:"ui/notifications/tool-input",params:r})}sendToolInputPartial(r){return this.notification({method:"ui/notifications/tool-input-partial",params:r})}sendToolResult(r){return this.notification({method:"ui/notifications/tool-result",params:r})}sendToolCancelled(r){return this.notification({method:"ui/notifications/tool-cancelled",params:r})}sendSandboxResourceReady(r){return this.notification({method:"ui/notifications/sandbox-resource-ready",params:r})}teardownResource(r,i){return this.request({method:"ui/resource-teardown",params:r},Pe,i)}sendResourceTeardown=this.teardownResource;async connect(r){if(this._client){let i=this._client.getServerCapabilities();if(!i)throw Error("Client server capabilities not available");if(i.tools){if(this.oncalltool=async(o,t)=>{return this._client.request({method:"tools/call",params:o},g_,{signal:t.signal})},i.tools.listChanged)this._client.setNotificationHandler(O_,(o)=>this.sendToolListChanged(o.params))}if(i.resources){if(this.onlistresources=async(o,t)=>{return this._client.request({method:"resources/list",params:o},c_,{signal:t.signal})},this.onlistresourcetemplates=async(o,t)=>{return this._client.request({method:"resources/templates/list",params:o},b_,{signal:t.signal})},this.onreadresource=async(o,t)=>{return this._client.request({method:"resources/read",params:o},N_,{signal:t.signal})},i.resources.listChanged)this._client.setNotificationHandler(z_,(o)=>this.sendResourceListChanged(o.params))}if(i.prompts){if(this.onlistprompts=async(o,t)=>{return this._client.request({method:"prompts/list",params:o},l_,{signal:t.signal})},i.prompts.listChanged)this._client.setNotificationHandler(D_,(o)=>this.sendPromptListChanged(o.params))}}return super.connect(r)}}function J_(r,i){return JSON.stringify(r)===JSON.stringify(i)}export{yD as getToolUiResourceUri,hD as buildAllowAttribute,aI as TOOL_RESULT_METHOD,hI as TOOL_INPUT_PARTIAL_METHOD,yI as TOOL_INPUT_METHOD,pI as TOOL_CANCELLED_METHOD,P_ as SUPPORTED_PROTOCOL_VERSIONS,fI as SIZE_CHANGED_METHOD,CI as SANDBOX_RESOURCE_READY_METHOD,dI as SANDBOX_PROXY_READY_METHOD,Ke as RESOURCE_URI_META_KEY,r4 as RESOURCE_TEARDOWN_METHOD,t_ as RESOURCE_MIME_TYPE,v4 as REQUEST_DISPLAY_MODE_METHOD,Qn as PostMessageTransport,xI as OPEN_LINK_METHOD,Ae as McpUiUpdateModelContextRequestSchema,HI as McpUiToolVisibilitySchema,Ee as McpUiToolResultNotificationSchema,h6 as McpUiToolMetaSchema,ze as McpUiToolInputPartialNotificationSchema,Ne as McpUiToolInputNotificationSchema,Oe as McpUiToolCancelledNotificationSchema,YI as McpUiThemeSchema,_e as McpUiSupportedContentBlockModalitiesSchema,we as McpUiSizeChangedNotificationSchema,a6 as McpUiSandboxResourceReadyNotificationSchema,De as McpUiSandboxProxyReadyNotificationSchema,Pe as McpUiResourceTeardownResultSchema,Se as McpUiResourceTeardownRequestSchema,wv as McpUiResourcePermissionsSchema,y6 as McpUiResourceMetaSchema,Dv as McpUiResourceCspSchema,Je as McpUiRequestDisplayModeResultSchema,Nv as McpUiRequestDisplayModeRequestSchema,Ue as McpUiOpenLinkResultSchema,be as McpUiOpenLinkRequestSchema,ke as McpUiMessageResultSchema,Le as McpUiMessageRequestSchema,je as McpUiInitializedNotificationSchema,Ve as McpUiInitializeResultSchema,Xe as McpUiInitializeRequestSchema,FI as McpUiHostStylesSchema,QI as McpUiHostCssSchema,Ge as McpUiHostContextSchema,We as McpUiHostContextChangedNotificationSchema,TI as McpUiHostCapabilitiesSchema,kv as McpUiDisplayModeSchema,qI as McpUiAppCapabilitiesSchema,ZI as MESSAGE_METHOD,Sr as LATEST_PROTOCOL_VERSION,n4 as INITIALIZE_METHOD,i4 as INITIALIZED_METHOD,sI as HOST_CONTEXT_CHANGED_METHOD,j_ as AppBridge};
|
|
61
|
+
container holding the app. Specify either width or maxWidth, and either height or maxHeight.`),locale:g.string().optional().describe("User's language and region preference in BCP 47 format."),timeZone:g.string().optional().describe("User's timezone in IANA format."),userAgent:g.string().optional().describe("Host application identifier."),platform:g.union([g.literal("web"),g.literal("desktop"),g.literal("mobile")]).optional().describe("Platform type for responsive design decisions."),deviceCapabilities:g.object({touch:g.boolean().optional().describe("Whether the device supports touch input."),hover:g.boolean().optional().describe("Whether the device supports hover interactions.")}).optional().describe("Device input capabilities."),safeAreaInsets:g.object({top:g.number().describe("Top safe area inset in pixels."),right:g.number().describe("Right safe area inset in pixels."),bottom:g.number().describe("Bottom safe area inset in pixels."),left:g.number().describe("Left safe area inset in pixels.")}).optional().describe("Mobile safe area boundaries in pixels.")}).passthrough(),We=g.object({method:g.literal("ui/notifications/host-context-changed"),params:Ge.describe("Partial context update containing only changed fields.")}),Ae=g.object({method:g.literal("ui/update-model-context"),params:g.object({content:g.array(VI).optional().describe("Context content blocks (text, image, etc.)."),structuredContent:g.record(g.string(),g.unknown().describe("Structured content for machine-readable context data.")).optional().describe("Structured content for machine-readable context data.")})}),Xe=g.object({method:g.literal("ui/initialize"),params:g.object({appInfo:KI.describe("App identification (name and version)."),appCapabilities:qI.describe("Features and capabilities this app provides."),protocolVersion:g.string().describe("Protocol version this app supports.")})}),Ve=g.object({protocolVersion:g.string().describe('Negotiated protocol version string (e.g., "2025-11-21").'),hostInfo:KI.describe("Host application identification and version."),hostCapabilities:TI.describe("Features and capabilities provided by the host."),hostContext:Ge.describe("Rich context about the host environment.")}).passthrough();import{Protocol as s6}from"@modelcontextprotocol/sdk/shared/protocol.js";import{CallToolRequestSchema as r_,CallToolResultSchema as n_,EmptyResultSchema as i_,ListToolsRequestSchema as v_,PingRequestSchema as o_}from"@modelcontextprotocol/sdk/types.js";import{JSONRPCMessageSchema as p6}from"@modelcontextprotocol/sdk/types.js";class Qn{eventTarget;eventSource;messageListener;constructor(r=window.parent,i){this.eventTarget=r;this.eventSource=i;this.messageListener=(o)=>{if(i&&o.source!==this.eventSource){console.debug("Ignoring message from unknown source",o);return}let t=p6.safeParse(o.data);if(t.success)console.debug("Parsed message",t.data),this.onmessage?.(t.data);else console.error("Failed to parse message",t.error.message,o),this.onerror?.(Error("Invalid JSON-RPC message received: "+t.error.message))}}async start(){window.addEventListener("message",this.messageListener)}async send(r,i){console.debug("Sending message",r),this.eventTarget.postMessage(r,"*")}async close(){window.removeEventListener("message",this.messageListener),this.onclose?.()}onclose;onerror;onmessage;sessionId;setProtocolVersion}var Ke="ui/resourceUri",t_="text/html;profile=mcp-app";class $_ extends s6{_appInfo;_capabilities;options;_hostCapabilities;_hostInfo;_hostContext;constructor(r,i={},o={autoResize:!0}){super(o);this._appInfo=r;this._capabilities=i;this.options=o;this.setRequestHandler(o_,(t)=>{return console.log("Received ping:",t.params),{}}),this.onhostcontextchanged=()=>{}}getHostCapabilities(){return this._hostCapabilities}getHostVersion(){return this._hostInfo}getHostContext(){return this._hostContext}set ontoolinput(r){this.setNotificationHandler(Ne,(i)=>r(i.params))}set ontoolinputpartial(r){this.setNotificationHandler(ze,(i)=>r(i.params))}set ontoolresult(r){this.setNotificationHandler(Ee,(i)=>r(i.params))}set ontoolcancelled(r){this.setNotificationHandler(Oe,(i)=>r(i.params))}set onhostcontextchanged(r){this.setNotificationHandler(We,(i)=>{this._hostContext={...this._hostContext,...i.params},r(i.params)})}set onteardown(r){this.setRequestHandler(Se,(i,o)=>r(i.params,o))}set oncalltool(r){this.setRequestHandler(r_,(i,o)=>r(i.params,o))}set onlisttools(r){this.setRequestHandler(v_,(i,o)=>r(i.params,o))}assertCapabilityForMethod(r){}assertRequestHandlerCapability(r){switch(r){case"tools/call":case"tools/list":if(!this._capabilities.tools)throw Error(`Client does not support tool capability (required for ${r})`);return;case"ping":case"ui/resource-teardown":return;default:throw Error(`No handler for method ${r} registered`)}}assertNotificationCapability(r){}assertTaskCapability(r){throw Error("Tasks are not supported in MCP Apps")}assertTaskHandlerCapability(r){throw Error("Task handlers are not supported in MCP Apps")}async callServerTool(r,i){return await this.request({method:"tools/call",params:r},n_,i)}sendMessage(r,i){return this.request({method:"ui/message",params:r},ke,i)}sendLog(r){return this.notification({method:"notifications/message",params:r})}updateModelContext(r,i){return this.request({method:"ui/update-model-context",params:r},i_,i)}openLink(r,i){return this.request({method:"ui/open-link",params:r},Ue,i)}sendOpenLink=this.openLink;requestDisplayMode(r,i){return this.request({method:"ui/request-display-mode",params:r},Je,i)}sendSizeChanged(r){return this.notification({method:"ui/notifications/size-changed",params:r})}setupSizeChangedNotifications(){let r=!1,i=0,o=0,t=()=>{if(r)return;r=!0,requestAnimationFrame(()=>{r=!1;let v=document.documentElement,$=v.style.width,u=v.style.height;v.style.width="fit-content",v.style.height="fit-content";let l=v.getBoundingClientRect();v.style.width=$,v.style.height=u;let e=window.innerWidth-v.clientWidth,c=Math.ceil(l.width+e),b=Math.ceil(l.height);if(c!==i||b!==o)i=c,o=b,this.sendSizeChanged({width:c,height:b})})};t();let n=new ResizeObserver(t);return n.observe(document.documentElement),n.observe(document.body),()=>n.disconnect()}async connect(r=new Qn(window.parent,window.parent),i){await super.connect(r);try{let o=await this.request({method:"ui/initialize",params:{appCapabilities:this._capabilities,appInfo:this._appInfo,protocolVersion:Sr}},Ve,i);if(o===void 0)throw Error(`Server sent invalid initialize result: ${o}`);if(this._hostCapabilities=o.hostCapabilities,this._hostInfo=o.hostInfo,this._hostContext=o.hostContext,await this.notification({method:"ui/notifications/initialized"}),this.options?.autoResize)this.setupSizeChangedNotifications()}catch(o){throw this.close(),o}}}function hD(r){let o=r._meta?.ui?.resourceUri;if(o===void 0)o=r._meta?.[Ke];if(typeof o==="string"&&o.startsWith("ui://"))return o;else if(o!==void 0)throw Error(`Invalid UI resource URI: ${JSON.stringify(o)}`);return}function aD(r){if(!r)return"";let i=[];if(r.camera)i.push("camera");if(r.microphone)i.push("microphone");if(r.geolocation)i.push("geolocation");if(r.clipboardWrite)i.push("clipboard-write");return i.join("; ")}var P_=[Sr];class j_ extends S_{_client;_hostInfo;_capabilities;_appCapabilities;_hostContext={};_appInfo;constructor(r,i,o,t){super(t);this._client=r;this._hostInfo=i;this._capabilities=o;this._hostContext=t?.hostContext||{},this.setRequestHandler(Xe,(n)=>this._oninitialize(n)),this.setRequestHandler(k_,(n,v)=>{return this.onping?.(n.params,v),{}}),this.setRequestHandler(Nv,(n)=>{return{mode:this._hostContext.displayMode??"inline"}})}getAppCapabilities(){return this._appCapabilities}getAppVersion(){return this._appInfo}onping;set onsizechange(r){this.setNotificationHandler(we,(i)=>r(i.params))}set onsandboxready(r){this.setNotificationHandler(De,(i)=>r(i.params))}set oninitialized(r){this.setNotificationHandler(je,(i)=>r(i.params))}set onmessage(r){this.setRequestHandler(Le,async(i,o)=>{return r(i.params,o)})}set onopenlink(r){this.setRequestHandler(be,async(i,o)=>{return r(i.params,o)})}set onrequestdisplaymode(r){this.setRequestHandler(Nv,async(i,o)=>{return r(i.params,o)})}set onloggingmessage(r){this.setNotificationHandler(U_,async(i)=>{r(i.params)})}set onupdatemodelcontext(r){this.setRequestHandler(Ae,async(i,o)=>{return r(i.params,o)})}set oncalltool(r){this.setRequestHandler(u_,async(i,o)=>{return r(i.params,o)})}sendToolListChanged(r={}){return this.notification({method:"notifications/tools/list_changed",params:r})}set onlistresources(r){this.setRequestHandler(I_,async(i,o)=>{return r(i.params,o)})}set onlistresourcetemplates(r){this.setRequestHandler(__,async(i,o)=>{return r(i.params,o)})}set onreadresource(r){this.setRequestHandler(w_,async(i,o)=>{return r(i.params,o)})}sendResourceListChanged(r={}){return this.notification({method:"notifications/resources/list_changed",params:r})}set onlistprompts(r){this.setRequestHandler(e_,async(i,o)=>{return r(i.params,o)})}sendPromptListChanged(r={}){return this.notification({method:"notifications/prompts/list_changed",params:r})}assertCapabilityForMethod(r){}assertRequestHandlerCapability(r){}assertNotificationCapability(r){}assertTaskCapability(r){throw Error("Tasks are not supported in MCP Apps")}assertTaskHandlerCapability(r){throw Error("Task handlers are not supported in MCP Apps")}getCapabilities(){return this._capabilities}async _oninitialize(r){let i=r.params.protocolVersion;return this._appCapabilities=r.params.appCapabilities,this._appInfo=r.params.appInfo,{protocolVersion:P_.includes(i)?i:Sr,hostCapabilities:this.getCapabilities(),hostInfo:this._hostInfo,hostContext:this._hostContext}}setHostContext(r){let i={},o=!1;for(let t of Object.keys(r)){let n=this._hostContext[t],v=r[t];if(J_(n,v))continue;i[t]=v,o=!0}if(o)this._hostContext=r,this.sendHostContextChange(i)}sendHostContextChange(r){return this.notification({method:"ui/notifications/host-context-changed",params:r})}sendToolInput(r){return this.notification({method:"ui/notifications/tool-input",params:r})}sendToolInputPartial(r){return this.notification({method:"ui/notifications/tool-input-partial",params:r})}sendToolResult(r){return this.notification({method:"ui/notifications/tool-result",params:r})}sendToolCancelled(r){return this.notification({method:"ui/notifications/tool-cancelled",params:r})}sendSandboxResourceReady(r){return this.notification({method:"ui/notifications/sandbox-resource-ready",params:r})}teardownResource(r,i){return this.request({method:"ui/resource-teardown",params:r},Pe,i)}sendResourceTeardown=this.teardownResource;async connect(r){if(this._client){let i=this._client.getServerCapabilities();if(!i)throw Error("Client server capabilities not available");if(i.tools){if(this.oncalltool=async(o,t)=>{return this._client.request({method:"tools/call",params:o},g_,{signal:t.signal})},i.tools.listChanged)this._client.setNotificationHandler(O_,(o)=>this.sendToolListChanged(o.params))}if(i.resources){if(this.onlistresources=async(o,t)=>{return this._client.request({method:"resources/list",params:o},c_,{signal:t.signal})},this.onlistresourcetemplates=async(o,t)=>{return this._client.request({method:"resources/templates/list",params:o},b_,{signal:t.signal})},this.onreadresource=async(o,t)=>{return this._client.request({method:"resources/read",params:o},N_,{signal:t.signal})},i.resources.listChanged)this._client.setNotificationHandler(z_,(o)=>this.sendResourceListChanged(o.params))}if(i.prompts){if(this.onlistprompts=async(o,t)=>{return this._client.request({method:"prompts/list",params:o},l_,{signal:t.signal})},i.prompts.listChanged)this._client.setNotificationHandler(D_,(o)=>this.sendPromptListChanged(o.params))}}return super.connect(r)}}function J_(r,i){return JSON.stringify(r)===JSON.stringify(i)}export{hD as getToolUiResourceUri,aD as buildAllowAttribute,aI as TOOL_RESULT_METHOD,hI as TOOL_INPUT_PARTIAL_METHOD,yI as TOOL_INPUT_METHOD,pI as TOOL_CANCELLED_METHOD,P_ as SUPPORTED_PROTOCOL_VERSIONS,fI as SIZE_CHANGED_METHOD,CI as SANDBOX_RESOURCE_READY_METHOD,dI as SANDBOX_PROXY_READY_METHOD,Ke as RESOURCE_URI_META_KEY,r4 as RESOURCE_TEARDOWN_METHOD,t_ as RESOURCE_MIME_TYPE,v4 as REQUEST_DISPLAY_MODE_METHOD,Qn as PostMessageTransport,xI as OPEN_LINK_METHOD,Ae as McpUiUpdateModelContextRequestSchema,HI as McpUiToolVisibilitySchema,Ee as McpUiToolResultNotificationSchema,h6 as McpUiToolMetaSchema,ze as McpUiToolInputPartialNotificationSchema,Ne as McpUiToolInputNotificationSchema,Oe as McpUiToolCancelledNotificationSchema,YI as McpUiThemeSchema,_e as McpUiSupportedContentBlockModalitiesSchema,we as McpUiSizeChangedNotificationSchema,a6 as McpUiSandboxResourceReadyNotificationSchema,De as McpUiSandboxProxyReadyNotificationSchema,Pe as McpUiResourceTeardownResultSchema,Se as McpUiResourceTeardownRequestSchema,wv as McpUiResourcePermissionsSchema,y6 as McpUiResourceMetaSchema,Dv as McpUiResourceCspSchema,Je as McpUiRequestDisplayModeResultSchema,Nv as McpUiRequestDisplayModeRequestSchema,Ue as McpUiOpenLinkResultSchema,be as McpUiOpenLinkRequestSchema,ke as McpUiMessageResultSchema,Le as McpUiMessageRequestSchema,je as McpUiInitializedNotificationSchema,Ve as McpUiInitializeResultSchema,Xe as McpUiInitializeRequestSchema,FI as McpUiHostStylesSchema,QI as McpUiHostCssSchema,Ge as McpUiHostContextSchema,We as McpUiHostContextChangedNotificationSchema,TI as McpUiHostCapabilitiesSchema,kv as McpUiDisplayModeSchema,qI as McpUiAppCapabilitiesSchema,ZI as MESSAGE_METHOD,Sr as LATEST_PROTOCOL_VERSION,n4 as INITIALIZE_METHOD,i4 as INITIALIZED_METHOD,sI as HOST_CONTEXT_CHANGED_METHOD,j_ as AppBridge};
|