11/**
22 * @file App that demonstrates a few features using MCP Apps SDK + React.
33 */
4- import type { App } from "@modelcontextprotocol/ext-apps" ;
4+ import type { App , McpUiHostContext } from "@modelcontextprotocol/ext-apps" ;
55import { useApp } from "@modelcontextprotocol/ext-apps/react" ;
66import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js" ;
77import { StrictMode , useCallback , useEffect , useState } from "react" ;
@@ -29,6 +29,9 @@ function extractTime(callToolResult: CallToolResult): string {
2929
3030function GetTimeApp ( ) {
3131 const [ toolResult , setToolResult ] = useState < CallToolResult | null > ( null ) ;
32+ const [ hostContext , setHostContext ] = useState <
33+ McpUiHostContext | undefined
34+ > ( ) ;
3235 const { app, error } = useApp ( {
3336 appInfo : IMPLEMENTATION ,
3437 capabilities : { } ,
@@ -49,9 +52,19 @@ function GetTimeApp() {
4952 } ;
5053
5154 app . onerror = log . error ;
55+
56+ app . onhostcontextchanged = ( params ) => {
57+ setHostContext ( ( prev ) => ( { ...prev , ...params } ) ) ;
58+ } ;
5259 } ,
5360 } ) ;
5461
62+ useEffect ( ( ) => {
63+ if ( app ) {
64+ setHostContext ( app . getHostContext ( ) ) ;
65+ }
66+ } , [ app ] ) ;
67+
5568 if ( error )
5669 return (
5770 < div >
@@ -60,14 +73,25 @@ function GetTimeApp() {
6073 ) ;
6174 if ( ! app ) return < div > Connecting...</ div > ;
6275
63- return < GetTimeAppInner app = { app } toolResult = { toolResult } /> ;
76+ return (
77+ < GetTimeAppInner
78+ app = { app }
79+ toolResult = { toolResult }
80+ hostContext = { hostContext }
81+ />
82+ ) ;
6483}
6584
6685interface GetTimeAppInnerProps {
6786 app : App ;
6887 toolResult : CallToolResult | null ;
88+ hostContext ?: McpUiHostContext ;
6989}
70- function GetTimeAppInner ( { app, toolResult } : GetTimeAppInnerProps ) {
90+ function GetTimeAppInner ( {
91+ app,
92+ toolResult,
93+ hostContext,
94+ } : GetTimeAppInnerProps ) {
7195 const [ serverTime , setServerTime ] = useState ( "Loading..." ) ;
7296 const [ messageText , setMessageText ] = useState ( "This is message text." ) ;
7397 const [ logText , setLogText ] = useState ( "This is log text." ) ;
@@ -120,7 +144,15 @@ function GetTimeAppInner({ app, toolResult }: GetTimeAppInnerProps) {
120144 } , [ app , linkUrl ] ) ;
121145
122146 return (
123- < main className = { styles . main } >
147+ < main
148+ className = { styles . main }
149+ style = { {
150+ paddingTop : hostContext ?. safeAreaInsets ?. top ,
151+ paddingRight : hostContext ?. safeAreaInsets ?. right ,
152+ paddingBottom : hostContext ?. safeAreaInsets ?. bottom ,
153+ paddingLeft : hostContext ?. safeAreaInsets ?. left ,
154+ } }
155+ >
124156 < p className = { styles . notice } > Watch activity in the DevTools console!</ p >
125157
126158 < div className = { styles . action } >
0 commit comments