bio_img_community

MATLAB Community

MATLAB, community & more

MATLAB Code Generation with the Help of an MCP Server

Everyone knows by now that AI services can generate code. For example, with the recent release of MATLAB R2025a, you can use the new Copilot feature to generate code for you from directly inside MATLAB. And all of the major Large Language Models (LLMs) do a remarkably good job writing MATLAB on demand.

It's less well known that you can get an AI service to write MATLAB code for you, then run it, then detect and fix any errors, and finally write a demo file for it. All without your intervention. What is the magic behind this? It's something called a Model Context Protocol, a standard originally introduced by Anthropic in late 2024. MCP defines how an LLM can find and use tools, including MATLAB. If we build an MCP Server that knows how to ask MATLAB to read, write, and run code, then we can close the loop and have the LLM complete an entire project before it checks in with us. MCP is one of the technologies that unlocks agents as a powerful new way of getting things done with AI.

Anyone who's generated code with an LLM knows that hallucination can be a serious problem. The AI can invent functions that simply don't exist. But an agentic workflow using MCP can make sure the code runs before you even see it. There might still be issues with the code you get back, but at least you'll know it runs and passes your tests.

Let's do an experiment. I've installed Claude Desktop on my laptop so it has access to my file system. I also have MATLAB R2025a installed directly on the same machine. With these elements in place, I can set up an MCP Server that can see both of them. The server is a little Python program that passes requests from Claude to MATLAB. Now we have all the ingredients we need to let Claude write code that it can run and debug without my intervention.

I was thinking about this recently and came across a perfect test case. I saw this Discussion note from retired MathWorker Steve Eddins. Steve says: I want to turn a Markdown nested list of text labels into a MATLAB directed graph (see his full blog post here: Fun with Simple Text Processing and Directed Graphs).

This looked like an ideal challenge for MCP-enabled Claude. I presented the problem and then sat back. Here's my initial prompt. It's literally the image and the sample Markdown from Steve's post along with these words: "Write MATLAB code that will parse this and turn it into a diagraph like this image."

It got right to work. Sure enough, the initial code had problems. But from each of three separate issues, it persevered and recovered. The first of these were errors that stopped the interpreter with an error message.

I was intrigued by the last issue. The code ran without error, but Claude was smart enough to notice that the result looked strange (an unconnected graph of nine nodes) and it fixed the code before returning to me. Eventually I was rewarded with this.

Look at the images, and you'll see that Claude's solution (using the Sonnet 3.7 model) was an excellent match for Steve's output. I won't make the case that the code quality is better than Steve's, given his many years of experience, and of course you'll want to be very cautious with any process that can create and run code on your machine. But the MCP process saves a lot of hassle for those of us who want to try out automatic code generation.

|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。