generate_matlab_code
Generate MATLAB code from natural language descriptions to automate script creation and execution tasks.
Instructions
Generate MATLAB code from a natural language description
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| description | Yes | Natural language description of what the code should do | |
| saveScript | No | Whether to save the generated MATLAB script | |
| scriptPath | No | Custom path to save the MATLAB script (optional) |
Implementation Reference
- src/index.ts:403-446 (handler)MCP tool handler for 'generate_matlab_code': extracts parameters, calls generateCode helper, formats response with code block, optionally saves script to file.case 'generate_matlab_code': { const description = String(request.params.arguments?.description || ''); const saveScript = Boolean(request.params.arguments?.saveScript || false); const scriptPath = request.params.arguments?.scriptPath as string | undefined; if (!description) { throw new McpError( ErrorCode.InvalidParams, 'Description is required' ); } try { const generatedCode = this.matlabHandler.generateCode(description); let responseText = `Generated MATLAB code for: "${description}"\n\n\`\`\`matlab\n${generatedCode}\n\`\`\``; // Save the generated code if requested if (saveScript) { const targetPath = scriptPath || path.join(process.cwd(), `matlab_generated_${Date.now()}.m`); fs.writeFileSync(targetPath, generatedCode); responseText += `\n\nGenerated MATLAB script saved to: ${targetPath}`; } return { content: [ { type: 'text', text: responseText, }, ], }; } catch (error) { return { content: [ { type: 'text', text: `Error generating MATLAB code: ${error instanceof Error ? error.message : String(error)}`, }, ], isError: true, }; } }
- src/index.ts:118-140 (helper)Core implementation of code generation: returns a placeholder MATLAB function template based on the input description. Notes it should use LLM in real impl.generateCode(description: string): string { // This is a placeholder. In a real implementation, this would use an LLM or other method // to generate MATLAB code from the description. // For now, we'll return a simple template based on the description. return `% MATLAB code generated from description: ${description} % Generated on: ${new Date().toISOString()} % Your code here: % This is a placeholder implementation. % In a real system, this would be generated based on the description. function result = generatedFunction() % Based on description: ${description} disp('Executing function based on description: ${description}'); % Placeholder implementation result = 'Function executed successfully'; end % Call the function generatedFunction()`; }
- src/index.ts:313-335 (registration)Tool registration in listTools handler: defines name, description, and input schema including optional save options.{ name: 'generate_matlab_code', description: 'Generate MATLAB code from a natural language description', inputSchema: { type: 'object', properties: { description: { type: 'string', description: 'Natural language description of what the code should do', }, saveScript: { type: 'boolean', description: 'Whether to save the generated MATLAB script', }, scriptPath: { type: 'string', description: 'Custom path to save the MATLAB script (optional)', }, }, required: ['description'], }, }, ],
- src/index.ts:316-333 (schema)Input schema for generate_matlab_code tool: requires description, optional saveScript and scriptPath.inputSchema: { type: 'object', properties: { description: { type: 'string', description: 'Natural language description of what the code should do', }, saveScript: { type: 'boolean', description: 'Whether to save the generated MATLAB script', }, scriptPath: { type: 'string', description: 'Custom path to save the MATLAB script (optional)', }, }, required: ['description'], },