Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

An MCP client in pure MATLAB code

License

NotificationsYou must be signed in to change notification settings

matlab-deep-learning/mcpHTTPClient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

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:

Setup

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.

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 theHome tab, and in theEnvironment section, click theAdd-Ons icon.
  2. In the Add-On Explorer, search for "MATLAB MCP HTTP Client".
  3. SelectInstall.

Examples

Create MCP Client

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);

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 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);

Functions

mcpHTTPClient

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.

callTool

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

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp