- Notifications
You must be signed in to change notification settings - Fork0
An MCP client in pure MATLAB code
License
matlab-deep-learning/mcpHTTPClient
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
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:
Create MCP clients to connect to streamable HTTP servers from MATLAB.
Call external tools.
Use external tools with theLarge Language Models (LLMs) with MATLAB add-on.
Using this add-on requires MATLAB R2025a or newer.
To generate and execute tool calls from MATLAB, you also need theLarge Language Models (LLMs) with MATLAB add-on.
You can use the add-on in MATLAB Online by clicking this link:
The recommended way of using the add-on in an installed version of MATLAB is to use the Add-On Explorer.
- In MATLAB, go to theHome tab, and in theEnvironment section, click theAdd-Ons icon.
- In the Add-On Explorer, search for "MATLAB MCP HTTP Client".
- SelectInstall.
Create an MCP client using themcpHTTPClient 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 theServerTools property.
serverTools=client.ServerTools;
Inspect the first tool.
tool1= serverTools{1};
To call the first tool, use thecallTool 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);
This example shows how to use an LLM together with an MCP client to automatically call and execute external tools. The example requires theLarge 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 themcpHTTPClient 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 theServerTools property.
serverTools=client.ServerTools;
To use the tools provided by the MCP Client with the LLM, first convert the tools to anopenAIFunction object. Connect to the OpenAI Chat Completion API. Give the model access to the server tools using theTools argument.
f= openAIFunction(serverTools)model= openAIChat(Tools=f);
Generate output using thegenerate 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 thegenerate function returns information about the names and any input arguments in thetool_calls field of thecompleteOutput output structure.
Execute the first function call using thecallTool function.
toolRequest=completeOutput.tool_calls(1).function;output= callTool(client,toolRequest);
client = mcpHTTPClient(endpoint) returns an MCP client based on the MCP server URLendpoint.
ThemcpHTTPClient object stores the tools associated with the MCP server in theServerTools property, specified as a cell array of structs. Each struct contains information about one tool, including the tool name and arguments.
result = callTool(client,toolName,argumentName1=x1,argumentName2=x2,...) calls a tool with nametoolName with input argumentargumentName1 specified asx1, etc.
result = callTool(client,toolRequest) calls a tool requesttoolRequest returned by an LLM. For example, you can specifytoolRequest as thecompleteOutput.tool_calls.function output of thegenerate (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 ontrust, safety and security with MCP andMCP security considerations.
Copyright 2025 The MathWorks, Inc.
About
An MCP client in pure MATLAB code
Resources
License
Uh oh!
There was an error while loading.Please reload this page.