Ebook

Your Guide to Agentic AI with MATLAB

Introduction: Agentic AI with MATLAB

Generative AI changed how we interact with computers, but moving beyond chat to build AI systems that can reason, act, and execute MATLAB® code autonomously requires a new approach. Today, asking an LLM to solve an engineering problem often means copying code back and forth, fixing errors manually, and hoping the AI understood your domain. Agentic AI changes this.

This guide shows you how to build AI systems that don't just suggest solutions, they execute them. By connecting LLMs to MATLAB through the Model Context Protocol (MCP), you can create AI agents that write code, run simulations, analyze results, and iterate until the job is done.

Three boxes showing the progression from Generative AI (manual copy-paste) to Generative AI + Tools (AI writes, you debug) to Agentic AI (AI writes, runs, and fixes code).

The evolution from traditional chat-based AI to fully autonomous agentic AI.


From Chat to Action: Why Agentic AI?

If you've used ChatGPT or Claude to help with MATLAB code, you know the drill: ask a question, get some code, paste it into MATLAB, hit an error, go back to the chat, explain the error, get new code, repeat. It works, but it's slow and frustrating.

Agentic AI closes this loop. Instead of being a text generator that you manually orchestrate, an AI agent can:

  • Write code and execute it directly
  • See errors and fix them automatically
  • Access your workspace and data
  • Iterate until the task is complete
  • Deliver verified, working results

Contrast this with generative AI, which:

  • Generates code snippets on request
  • Requires you to copy, paste, and run code manually
  • Relies on you to interpret errors and ask follow-ups
  • Lacks access to your files or data
  • Can't verify whether code actually works

Four Capabilities That Turn a Chatbot into a Colleague

Generative AI is like having a consultant who writes reports. Agentic AI is like having a colleague who does the work. The AI doesn't just tell you how to solve a problem—it solves it, shows you the results, and asks if you want changes.

Agentic System Capabilities

An agentic system has four key capabilities that transform it from a chatbot into an autonomous problem-solver.

Capability What It Means MATLAB Example
Reasoning Breaks complex problems into steps "To optimize this controller, I'll first analyze the plant, then design a PID, then tune it."
Planning Sequences actions toward a goal "Step 1: Load data. Step 2: Train model. Step 3: Validate. Step 4: Export."
Tool use Calls external functions and APIs Invokes trainNetwork, sim, or custom MATLAB functions
Adaptation Learns from feedback and errors "That threw an error—let me check the dimensions and try again."

Why Combine MATLAB with Agentic AI?

MATLAB is uniquely valuable for agentic AI in engineering and science because it provides:

  • Reliable computation: LLMs hallucinate math, while MATLAB computes it correctly.
  • Domain expertise: More than 100 toolboxes are available for signal processing, controls, deep learning, and more.
  • Simulation capabilities: Connect to Simulink® for system-level testing and verification.
  • Professional tools: Code analysis, visualization, and deployment capabilities are built in.

Understanding the Model Context Protocol

For an LLM to use MATLAB, it needs a way to discover what tools are available, call them with the right parameters, and receive results. This is exactly what the Model Context Protocol delivers: a common framework that enables AI-to-tool interaction. Think of MCP as a USB-C port for AI: just as USB-C lets any device connect to any peripheral, MCP lets any AI model connect to any tool—including MATLAB.

Three-step flow diagram: AI Agent (Claude, Copilot, etc.) connects to an MCP Server (Protocol Bridge), which connects to MATLAB (Executes Code).

The MCP architecture: how AI agents connect to MATLAB.

The MCP Server translates between AI requests and MATLAB execution. The AI agent decides what to do; MATLAB does the computation.

Two Ways to Use MCP with MATLAB

Approach Use Case You Need
MATLAB as a tool (MCP Core Server) Let external AI apps (Claude Desktop, GitHub Copilot, VS Code) execute MATLAB code MATLAB + MCP Core Server + AI client of your choice
MATLAB as the host (MCP Client) Build your own AI agents in MATLAB that call external tools and APIs MATLAB + MCP Client add-on + LLMs with MATLAB

Let AI Agents Use MATLAB (MCP Core Server)

The MATLAB MCP Core Server makes MATLAB available for any MCP-compatible AI application. Once installed, AI assistants such as Claude Desktop, GitHub Copilot, or Gemini CLI can write and execute MATLAB code on your behalf—with you in control.

Behind the scenes, the MCP Core Server provides five foundational capabilities that let AI agents work with MATLAB autonomously. You don't need to call these directly; the AI agent uses them automatically based on what you ask it to do:

When You Ask the AI To ... The Server Handles ...
"Write a function that filters this signal" code generation — Creates and saves .m files to your workspace
"Run this script and show me the results" code execution — Runs MATLAB code and captures output, plots, and errors
"Check my code for issues" code analysis — Uses the MATLAB built-in linter to assess style and correctness
"Fix that error and try again" iterative refinement — The AI sees the error, modifies the code, and re-runs
(Automatic) session management — Starts and maintains the MATLAB connection transparently

The Agentic Workflow in Action

A typical interaction looks like this: You ask, "Create a Butterworth filter for this noisy signal." The AI writes the code, runs it in MATLAB, sees if there are errors or unexpected results, refines its approach, and delivers working code with a plot, all with zero copying or pasting on your end.

Installation

Get the MATLAB MCP Core Server from GitHub:

# Clone the repository
git clone https://github.com/mathworks/matlab-mcp-core-server

# Follow setup instructions for your AI client
# (Claude Desktop, VS Code with Copilot, Gemini CLI, etc.)

Configuration Example (Claude Desktop)

Add to your claude_desktop_config.json:

{ 
  "mcpServers": { 
    "matlab": { 
      "command": "/path/to/matlab-mcp-server", 
      "args": [] 
    } 
  } 
} 

Understand What You're Enabling

Agentic AI tools can write and execute code on your machine with access to your files. This capability is powerful but requires trust. Review what the AI proposes before approving significant actions, especially in production environments.


Build AI Agents in MATLAB (MCP Client)

What if you want to build your own AI agent that runs inside MATLAB and can call external tools and APIs? The MATLAB MCP Client makes this possible, turning MATLAB into a platform for agentic AI development.

Four components: Your MATLAB Code (Agentic Logic), LLM API (OpenAI, Ollama, etc.), MCP Client (Tool Discovery), and External MCP Servers (Any Tool or API)

The four building blocks of an agentic AI system based on MATLAB.

Key Capabilities of MATLAB as the AI Agent Host

  • Discover tools: Query any MCP server to list available tools and their input schemas.
  • Call tools: Invoke external tools as easily as calling local MATLAB functions with callTool.
  • LLM integration: Convert tool schemas to openAIFunction objects for seamless function calling.

Next, let's take a look at an example of building an agentic workflow within MATLAB:

% Connect to an MCP server 
client = mcpClient("path/to/mcp-server"); 
 
% List available tools from the server 
tools = listTools(client); 
disp(tools); 
 
% Convert tools to OpenAI function format for the LLM 
functions = openAIFunction(tools); 
 
% Create a chat with function calling enabled 
chat = openAIChat("You are a helpful assistant.", ... 
    Tools=functions); 
 
% Send a user query - LLM decides if a tool is needed 
response = generate(chat, "What is the 10th prime number?"); 
 
% If LLM requests a tool call, execute it 
if isfield(response, 'tool_calls') 
    toolRequest = response.tool_calls; 
    result = callTool(client, toolRequest); 
     
    % Feed result back to LLM for final response 
    finalResponse = generate(chat, result); 
end 

What Can You Connect To?

Any MCP server works with the MATLAB MCP Client. This includes servers for databases, web APIs, file systems, other AI services, and custom tools you build. The ecosystem is growing rapidly.


Implement Tool Calling in MATLAB

Tool calling is the mechanism at the heart of agentic AI. The LLM doesn't execute functions directly but outputs a structured request describing which function to call and with what arguments. Your code then executes the function and returns results to the LLM.

Four-step flow: User Query, LLM Decides (requests addNumbers(212,88)), MATLAB Executes (result = 300), LLM Responds ('The sum is 300')

How an agentic AI system processes a query from user input to final response.

The LLM reasons about what to compute. MATLAB handles how to compute it correctly. When a user submits a query, the LLM interprets the intent and decides which tool to call and with what parameters. MATLAB then executes the function and returns the result. The LLM formats a natural-language response. Users never need to write or run code themselves.

Define a Tool in MATLAB

% Define a function that the LLM can call 
function result = addTwoNumbers(x1, x2) 
    result = x1 + x2; 
end 
 
% Create tool definition for the LLM 
addTool = openAIFunction("addTwoNumbers", ... 
    "Add two numbers together"); 
addTool = addParameter(addTool, "x1", "number", ... 
    "First number to add", Required=true); 
addTool = addParameter(addTool, "x2", "number", ... 
    "Second number to add", Required=true);

Build a Simple AI Agent

function aiAgent(userQuery) 
    % Initialize chat with tools 
    chat = ollamaChat("mistral-nemo", Tools=addTool); 
     
    % Get LLM response 
    response = generate(chat, userQuery); 
     
    % Check if LLM wants to call a tool 
    if isfield(response, 'tool_calls') 
        % Extract function call details 
        funcName = response.tool_calls.function.name; 
        args = response.tool_calls.function.arguments; 
         
        fprintf("AI requested: %s(%f, %f)\n", ... 
            funcName, args.x1, args.x2); 
         
        % Execute the function 
        result = addTwoNumbers(args.x1, args.x2); 
        fprintf("Result: %f\n", result); 
    else 
        % No tool call - show direct response 
        disp(response.content); 
    end 
end

Tool calling works with multiple providers via the LLMs with MATLAB add-on, including:

  • OpenAI: Latest GPT model (via API)
  • Azure OpenAI: Enterprise deployments
  • Ollama: Local models such as Mistral, GPT-OSS, DeepSeek, and Qwen

Creating Effective AI Agents

Moving from demos to production requires patterns that make agents reliable, observable, and safe. These approaches are battle-tested in real-world agentic systems.

Pattern 1: ReAct (Reasoning + Acting)

The most common agent pattern alternates between thinking and doing. The LLM reasons about what to do next, takes an action, observes the result, and repeats the process.

% ReAct loop pattern 
while ~taskComplete 
    % Thought: LLM reasons about next step 
    thought = generate(chat, [context, "What should I do next?"]); 
     
    % Action: Execute tool if needed 
    if needsTool(thought) 
        result = executeTool(thought.tool_call); 
        context = [context; result]; 
    end 
     
    % Observation: Update state based on results 
    taskComplete = checkCompletion(context); 
end 

Pattern 2: Tool Chaining

Complex tasks require multiple tools called in sequence. The output of one becomes the input to the next. For example:

Step Action Purpose
1 Generate code AI writes MATLAB code based on your request.
2 Analyze code Check for errors, style issues, and potential bugs.
3 Execute code Run in MATLAB and capture the output.
4 Evaluate results AI reviews the output; it fixes the errors or confirms success.

Pattern 3: Human in the Loop

For safety-critical applications, add approval steps before the agent takes action:

% Human-in-the-loop pattern 
proposedAction = generate(chat, query); 
 
% Display proposed action for approval 
fprintf("Proposed: %s\n", proposedAction.description); 
approval = input("Approve? (y/n): ", "s"); 
 
if strcmp(approval, "y") 
    result = executeAction(proposedAction); 
else 
    disp("Action cancelled."); 
end 

Practical Guidelines

Several guidelines serve as the keys to ensuring reliable agents:

  • Start simple: Get a single tool working before building complex chains.
  • Add observability: Log every LLM call and tool execution because you'll need it for debugging.
  • Handle errors gracefully: LLMs make mistakes; build retry logic with exponential backoff.
  • Set boundaries: Limit iterations, token budgets, and which tools an agent can access.
  • Test adversarially: Deliberately test edge cases to break your agent before it reaches users.