[Python] Minimum working example of an MCP Server?

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I have been playing around with browsermcp today and I can't manage to get a minimal working version in Python or NodeJS - i.e. something which will "Go to YouTube and search for "MCP servers" and hit enter.."

    I have correctly got a working version of the MCP in cursor but for me it sometimes does not do what I want. I just want something which will replicate the cursor approach to using the MCP but programatically.

    The MCP.json file for cursor is the following:

    {
    "mcpServers": {
    "browsermcp": {
    "command": "npx",
    "args": ["@browsermcp/mcp@latest"]
    }
    }
    }


    Here is ChatGPT generated code I tried earlier but its garbage and does not work:

    const { spawn } = require('child_process');

    // You already confirmed this is correct on your system:
    const npxPath = 'C:\\nvm4w\\nodejs\\npx.cmd';

    // Use full path to cmd.exe for extra reliability
    const cmdPath = 'C:\\Windows\\System32\\cmd.exe';

    const browserMcp = spawn(cmdPath, ['/c', npxPath, '@browsermcp/mcp@latest'], {
    stdio: ['pipe', 'pipe', 'pipe'], // All pipes so you can read/write if needed
    windowsVerbatimArguments: true // Sometimes helps on Windows, can omit if not needed
    });

    // Handle stdout
    browserMcp.stdout.on('data', (data) => {
    console.log('[browsermcp stdout]:', data.toString());
    });

    // Handle stderr
    browserMcp.stderr.on('data', (data) => {
    console.error('[browsermcp stderr]:', data.toString());
    });

    // Handle process errors (like not found, spawn errors)
    browserMcp.on('error', (err) => {
    console.error('Failed to start browsermcp process:', err);
    process.exit(1);
    });

    // Handle exit
    browserMcp.on('close', (code) => {
    console.log(`[browsermcp process exited with code ${code}]`);
    });

    // Example: Sending a command after a delay (as in your original script)
    function sendCommand(command) {
    console.log('[Sending to browsermcp]:', JSON.stringify(command));
    browserMcp.stdin.write(JSON.stringify(command) + '\n');
    }

    async function automateBrowser() {
    console.log('[Automation starting...]');
    await new Promise(resolve => setTimeout(resolve, 8000)); // Wait for MCP to boot

    sendCommand({
    type: 'tool_code',
    tool_name: 'browser_goto',
    args: {
    url: 'https://youtube.com/'
    }
    });

    await new Promise(resolve => setTimeout(resolve, 10000)); // Wait for page to load

    sendCommand({
    type: 'tool_code',
    tool_name: 'browser_type',
    args: {
    element: 'textarea[id="prompt-textarea"]',
    text: 'generate an image of a dog',
    submit: true
    }
    });
    }

    // Start automation
    automateBrowser();

    // Graceful shutdown on Ctrl+C
    process.on('SIGINT', () => {
    console.log('Ctrl+C detected. Terminating browsermcp process...');
    if (browserMcp) {
    browserMcp.kill('SIGINT');
    }
    process.exit();
    });


    It gave the impression it could work but the chrome browser does nothing...

    [Sending to browsermcp]: {"type":"tool_code","tool_name":"browser_goto","args":{"url":"https://youtube.com/"}}
    [Sending to browsermcp]: {"type":"tool_code","tool_name":"browser_type","args":{"element":"textarea[id=\"prompt-textarea\"]","text":"generate an image of a dog","submit":true}}

    Continue reading...

Compartilhe esta Página