Skip to content

matlab-deep-learning/mcpHTTPClient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MATLAB MCP HTTP Client

Create MCP client in MATLAB® to call external tools in LLM workflows

Model Context Protocol (MCP) is a framework for communication between AI agents and external tools. Typically, a large language model (LLM) application sets up one or more MCP clients that each connect to an MCP server. The MCP server provides context and tools the LLM application can use.

This add-on allows you to:

Setup

Using this add-on requires MATLAB R2025a or newer.

To generate and execute tool calls from MATLAB, you also need the Large Language Models (LLMs) with MATLAB add-on.

Use MATLAB Online

You can use the add-on in MATLAB Online by clicking this link: Open in MATLAB Online

Install Using Add-On Explorer

The recommended way of using the add-on in an installed version of MATLAB is to use the Add-On Explorer.

  1. In MATLAB, go to the Home tab, and in the Environment section, click the Add-Ons icon.
  2. In the Add-On Explorer, search for "MATLAB MCP HTTP Client".
  3. Select Install.

Examples

Create MCP Client

Create an MCP client using the mcpHTTPClient function. Specify the URL of the MCP server to which you want to connect, for example, endpoint = "https://mcp.example.com/mcp".

client = mcpHTTPClient(endpoint);

The MCP client stores information about the available tools from the server in the ServerTools property.

serverTools = client.ServerTools;

Inspect the first tool.

tool1 = serverTools{1};

To call the first tool, use the callTool function and specify the name of the tool as a positional input argument. Then, specify the tool arguments as additional name-value arguments.

toolName = tool1.name;
output = callTool(client,toolName,Argument1=Value1,...,ArgumentN=ValueN);

Automatically Call External Tool Using LLM

This example shows how to use an LLM together with an MCP client to automatically call and execute external tools. The example requires the Large Language Models (LLMs) with MATLAB add-on.

First, configure the connection to the OpenAI® Chat Completion API following the Large Language Models (LLMs) with MATLAB documentation: OpenAI.

Create an MCP client using the mcpHTTPClient function. Specify the URL of the MCP server to which you want to connect, for example, endpoint = "https://mcp.example.com/mcp".

client = mcpHTTPClient(endpoint);

The MCP client stores information about the available tools from the server in the ServerTools property.

serverTools = client.ServerTools;

To use the tools provided by the MCP Client with the LLM, first convert the tools to an openAIFunction object. Connect to the OpenAI Chat Completion API. Give the model access to the server tools using the Tools argument.

f = openAIFunction(serverTools)
model = openAIChat(Tools=f);

Generate output using the generate function. Specify a prompt that could result in a tool call. For example, if you provide the model with tools that return information about the weather, then you can ask the model questions about the local weather.

userPrompt = "Is it raining in Cambridge?";
[generatedText,completeOutput] = generate(model,userPrompt);

If the model detects one or more tool calls, then the generate function returns information about the names and any input arguments in the tool_calls field of the completeOutput output structure.

Execute the first function call using the callTool function.

toolRequest = completeOutput.tool_calls(1).function;
output = callTool(client,toolRequest);

Functions

mcpHTTPClient

client = mcpHTTPClient(endpoint) returns an MCP client based on the MCP server URL endpoint.

The mcpHTTPClient object stores the tools associated with the MCP server in the ServerTools property, specified as a cell array of structs. Each struct contains information about one tool, including the tool name and arguments.

callTool

result = callTool(client,toolName,argumentName1=x1,argumentName2=x2,...) calls a tool with name toolName with input argument argumentName1 specified as x1, etc.

result = callTool(client,toolRequest) calls a tool request toolRequest returned by an LLM. For example, you can specify toolRequest as the completeOutput.tool_calls.function output of the generate (Large Language Models (LLMs) with MATLAB) function.

When using the MATLAB HTTP MCP Client, you should thoroughly review and validate all tool calls before you run them. Always keep a human in the loop for important actions and only proceed once you’re confident the call will do exactly what you expect. For more information, see information on trust, safety and security with MCP and MCP security considerations.

Copyright 2025 The MathWorks, Inc.

About

An MCP client in pure MATLAB code

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages