Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sdks/typescript/server/scripts/bundle-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const __dirname = dirname(__filename);
try {
const result = await build({
entryPoints: [join(__dirname, '../src/adapters/appssdk/adapter-runtime.ts')],
bundle: true,
bundle: false,
write: false,
format: 'iife',
format: 'esm',
platform: 'browser',
target: 'es2020',
minify: false, // Keep readable for debugging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,27 @@ describe('Apps SDK Adapter', () => {
expect(script).toContain('https://test.example.com');
});

it('should set MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL flag', () => {
const script = getAppsSdkAdapterScript();

// Should prevent the bundled code from auto-initializing
expect(script).toContain('MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL');
expect(script).toContain('window.MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL = true');
});

it('should expose global MCPUIAppsSdkAdapter API', () => {
const script = getAppsSdkAdapterScript();

expect(script).toContain('window.MCPUIAppsSdkAdapter');
expect(script).toContain('init: initAdapter');
expect(script).toContain('initWithConfig: () => initAdapter({})')
expect(script).toContain('uninstall: uninstallAdapter');
});

it('should expose an initWithConfig with multiple config options', () => {
const config: AppsSdkAdapterConfig = {
timeout: 10000,
intentHandling: 'prompt',
hostOrigin: 'https://test.example.com',
};

const script = getAppsSdkAdapterScript(config);

expect(script).toContain(`initWithConfig: () => initAdapter(${JSON.stringify(config)})`);
});

it('should check for window before initialization', () => {
const script = getAppsSdkAdapterScript();

Expand Down
21 changes: 4 additions & 17 deletions sdks/typescript/server/src/adapters/appssdk/adapter-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import type {
import type { UIActionResult } from '../../types.js';

type ParentPostMessage = Window['postMessage'];
type AdapterWindow = Window & {
MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL?: boolean;
};

/**
* Main adapter class that handles protocol translations
Expand Down Expand Up @@ -456,7 +453,8 @@ let adapterInstance: MCPUIAppsSdkAdapter | null = null;
* @param config - Optional configuration
* @returns true if adapter was initialized, false if Apps SDK not detected
*/
export function initAdapter(config?: AppsSdkAdapterConfig): boolean {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function initAdapter(config?: AppsSdkAdapterConfig): boolean {
if (adapterInstance) {
console.warn('[MCPUI-Apps SDK Adapter] Adapter already initialized');
return true;
Expand All @@ -469,21 +467,10 @@ export function initAdapter(config?: AppsSdkAdapterConfig): boolean {
/**
* Uninstall the adapter and restore original behavior
*/
export function uninstallAdapter(): void {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function uninstallAdapter(): void {
if (adapterInstance) {
adapterInstance.uninstall();
adapterInstance = null;
}
}

/**
* Auto-install the adapter when this module is loaded
* Can be disabled by setting window.MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL = true before loading
*/
if (typeof window !== 'undefined') {
const adapterWindow = window as AdapterWindow;
if (!adapterWindow.MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL) {
initAdapter();
}
}

11 changes: 8 additions & 3 deletions sdks/typescript/server/src/adapters/appssdk/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ export function getAppsSdkAdapterScript(config?: AppsSdkAdapterConfig): string {

// Override auto-init from runtime and initialize with provided config
if (typeof window !== 'undefined') {
window.MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL = true; // Prevent auto-init from bundled code
// If the functions are not defined, just return, we can't do anything.
if (typeof initAdapter !== 'function' || typeof uninstallAdapter !== 'function') {
console.warn('[MCPUI-Apps SDK Adapter] Adapter runtime not found with the correct methods. Adapter will not activate.')
return;
}

// Initialize with config from server
if (typeof initAdapter === 'function') {
// If auto-init is enabled, initialize with config from server
if (!window.MCP_APPSSDK_ADAPTER_NO_AUTO_INSTALL) {
initAdapter(${configJson});
}

// Expose functions globally
if (typeof window.MCPUIAppsSdkAdapter === 'undefined') {
window.MCPUIAppsSdkAdapter = {
init: initAdapter,
initWithConfig: () => initAdapter(${configJson}),
uninstall: uninstallAdapter,
};
}
Expand Down
Loading