Skip to main content
Glama

execute

Execute commands in MCP, RPC, or RUN contexts to analyze Node.js applications via Chrome Debug Protocol for runtime type validation.

Instructions

Execute a command in the specified context (MCP, RPC, or RUN)

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
contextYesExecution context: MCP (local), RPC (remote/CDP), or RUN (VS Code HTTP)
commandYesCommand name to execute
messageYesCommand Message or string of arguments, or empty string

Implementation Reference

  • This function handles the execution logic for commands, either by requiring them (if they export a 'run' function) or by evaluating them as an IIFE within a context.
    async function executeCommand (
    	filePath: string,
    	args: unknown
    ): Promise<unknown> {
    	if (!fs.existsSync(filePath)) {
    		throw new Error(`Command file not found: ${filePath}`);
    	}
    
    	// Read the code first to check pattern
    	const code = fs.readFileSync(filePath, 'utf-8');
    
    	// Build context - passed to command
    	// eslint-disable-next-line @typescript-eslint/no-explicit-any
    	const g = global as Record<string, any>;
    	const ctx: any = {
    		require,
    		store: g.StrategyMCP,
    		args,
    	} as unknown;
    
    	// If it has module.exports with run function, use require
    	if (isLocalCommand(code)) {
    		// Clear require cache to allow reloading
    		delete require.cache[require.resolve(filePath)];
    
    		// Require the module
    		// eslint-disable-next-line @typescript-eslint/no-var-requires
    		const commandModule = require(filePath);
    
    		if (typeof commandModule.run === 'function') {
    			return await commandModule.run(ctx);
    		}
    
    		throw new Error(`Command does not export a 'run' function`);
    	}
    
    	// Otherwise, treat as remote-style IIFE command
    	// Wrap it to capture the result - always wrap in async IIFE
    	const wrappedBody = `
    		return (async function() {
    			${code}
    		})();
    	`;
    
    	// Execute in a sandbox-like way using Function with ctx parameter
    	// eslint-disable-next-line @typescript-eslint/no-var-requires
    	const fn = new Function('ctx', wrappedBody);
    	const result = fn(ctx);
    
    	// Always await the result since we wrap in async IIFE
    	return await result;
    }
  • src/server.ts:125-147 (registration)
    Registration of the 'execute' tool within the MCP server.
    {
    	name: 'execute',
    	description: 'Execute a command in the specified context (MCP, RPC, or RUN)',
    	inputSchema: {
    		type: 'object',
    		properties: {
    			context: {
    				type: 'string',
    				enum: ['MCP', 'RPC', 'RUN'],
    				description: 'Execution context: MCP (local), RPC (remote/CDP), or RUN (VS Code HTTP)',
    			},
    			command: {
    				type: 'string',
    				description: 'Command name to execute',
    			},
    			message: {
    				type: 'string',
    				description: 'Command Message or string of arguments, or empty string',
    			},
    		},
    		required: ['context', 'command', 'message'],
    	},
    },
  • The handler logic for the 'execute' tool inside the CallToolRequest switch statement.
    case 'execute': {
    	console.error('[SERVER DEBUG] request.params:', JSON.stringify(request.params));
    	const { context, command } = args as {
    		context: CommandContext;
    		command: string;
    	} & unknown;
    
    	if (!context || !command) {
    		return {
    			content: [{ type: 'text', text: 'Error: context and command are required' }],
    			isError: true,
    		};
    	}
    
    	// Get command file path
    	const filePath = getCommandPath(context, command);
    
    	if (!filePath) {
    		return {
    			content: [{ type: 'text', text: `Error: Command '${command}' not found in context '${context}'` }],
    			isError: true,
    		};
    	}
    
    	// Execute command
    	try {
    		const result = await executeCommand(filePath, args);
    		const text = result !== undefined ? JSON.stringify(result, null, 2) : 'Command executed (no return value)';
    		return {
    			content: [{ type: 'text', text }],
    		};
    	} catch (e) {
    		return {
    			content: [{ type: 'text', text: `Error executing command: ${e instanceof Error ? e.message : String(e)}` }],
    			isError: true,
    		};
    	}
    }
Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mythographica/strategy'

If you have feedback or need assistance with the MCP directory API, please join our Discord server