browser-automation-mcp-server-clean.tsā¢3.63 kB
#!/usr/bin/env node
/**
* Browser Automation MCP Server - Clean Output Version
* All logging goes to stderr to avoid interfering with MCP JSON protocol
*/
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
CallToolRequestSchema,
ErrorCode,
ListToolsRequestSchema,
McpError,
} from '@modelcontextprotocol/sdk/types.js';
import { z } from 'zod';
// Import the browser automation components
import { StableBrowserManager } from './stable-browser-manager.js';
import { InjectAndViewComposition } from './mcp-tools/inject-and-view-composition-v2.0.0.js';
// All logging goes to stderr to avoid interfering with MCP protocol
const log = (...args: any[]) => console.error('[MCP]', ...args);
class BrowserAutomationMCPServer {
private server: Server;
private browserManager: StableBrowserManager;
private compositionTool: InjectAndViewComposition;
constructor() {
this.server = new Server(
{
name: 'browser-automation-mcp-server',
version: '2.0.0',
},
{
capabilities: {
tools: {},
},
}
);
this.browserManager = new StableBrowserManager();
this.compositionTool = new InjectAndViewComposition(this.browserManager);
this.setupToolHandlers();
// Handle graceful shutdown
process.on('SIGINT', async () => {
log('Shutting down gracefully...');
await this.browserManager.cleanup();
process.exit(0);
});
}
private setupToolHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'create-intelligent-composition',
description: 'Create an intelligent educational composition with automatic widget selection',
inputSchema: {
type: 'object',
properties: {
prompt: {
type: 'string',
description: 'The educational content prompt in natural language'
},
title: {
type: 'string',
description: 'Optional title for the composition'
}
},
required: ['prompt']
}
}
]
}));
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === 'create-intelligent-composition') {
try {
const args = request.params.arguments as any;
// Create composition using the tool
const result = await this.compositionTool.createComposition(
args.prompt,
args.title
);
return {
content: [
{
type: 'text',
text: JSON.stringify(result, null, 2)
}
]
};
} catch (error: any) {
log('Error in create-intelligent-composition:', error);
throw new McpError(
ErrorCode.InternalError,
`Failed to create composition: ${error.message}`
);
}
}
throw new McpError(
ErrorCode.MethodNotFound,
`Unknown tool: ${request.params.name}`
);
});
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
log('Browser Automation MCP Server running - Clean output version');
}
}
// Start the server
const server = new BrowserAutomationMCPServer();
server.run().catch((error) => {
console.error('Failed to start server:', error);
process.exit(1);
});