UNPKG

screen-view-mcp

Version:

MCP tool for capturing screenshots and analyzing them with Claude Vision API

93 lines (92 loc) 4.04 kB
#!/usr/bin/env node "use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // Screen capture MCP server with direct module imports const path_1 = __importDefault(require("path")); const zod_1 = require("zod"); const screenshot_1 = require("./services/screenshot"); const vision_1 = require("./services/vision"); // Use dynamic require for MCP SDK to avoid ESM/CommonJS issues const sdkBasePath = path_1.default.join(__dirname, '../node_modules/@modelcontextprotocol/sdk'); const mcpPath = path_1.default.join(sdkBasePath, 'dist/cjs/server/mcp.js'); const stdioPath = path_1.default.join(sdkBasePath, 'dist/cjs/server/stdio.js'); // Import MCP SDK modules using require const mcp = require(mcpPath); const stdio = require(stdioPath); /** * Main function to initialize and run the MCP server */ async function main() { try { console.log('Starting screen capture MCP server...'); console.log('All modules loaded'); console.log('API_KEY_SET:', !!process.env.ANTHROPIC_API_KEY); // Create server const server = new mcp.McpServer({ name: "screen-view-mcp", version: "1.0.0", description: "Screen capture and analysis with Claude Vision" }); // Register the hello world tool for testing server.tool("helloWorld", { message: zod_1.z.string().optional() }, async ({ message }) => { console.log('Hello world tool invoked with message:', message); return { content: [{ type: "text", text: `Hello! You said: "${message || 'No message provided'}"` }] }; }); // Register the screen capture and analysis tool server.tool("captureAndAnalyzeScreen", { prompt: zod_1.z.string().optional(), modelName: zod_1.z.string().optional(), saveScreenshot: zod_1.z.boolean().optional() }, async ({ prompt, modelName, saveScreenshot }) => { try { console.log('Capturing screenshot...'); const screenshotBase64 = await (0, screenshot_1.captureScreenshot)(); const analysisPrompt = prompt || 'What do you see in this screenshot? Describe it in detail.'; const model = modelName || 'claude-3-opus-20240229'; console.log('Analyzing image with Claude...'); const analysis = await (0, vision_1.analyzeImage)(screenshotBase64, analysisPrompt, model); console.log('Analysis complete'); return { content: [{ type: "text", text: analysis }] }; } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; console.error('Error in screen_capture tool:', errorMessage); return { content: [{ type: "text", text: `Error analyzing screenshot: ${errorMessage}` }] }; } }); // Connect with stdio transport console.log('Connecting server with stdio transport...'); const transport = new stdio.StdioServerTransport(); server.connect(transport) .then(() => console.log('MCP Server connected successfully')) .catch((err) => { console.error('Connection error:', err); process.exit(1); }); } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; console.error('Fatal error initializing MCP server:', errorMessage); console.error(error instanceof Error ? error.stack : ''); process.exit(1); } } // Run the main function main();