MATLAB MCP HTTP客户端
在MATLAB®中创建MCP客户端,以调用LLM工作流中的外部工具
模型上下文协议(MCP)是人工智能代理和外部工具之间通信的框架。通常,大型语言模型(LLM)应用程序会设置一个或多个MCP客户端,每个客户端都连接到MCP服务器。MCP服务器提供LLM应用程序可以使用的上下文和工具。
此插件允许您:
- 创建MCP客户端,以便从MATLAB连接到可流式传输的HTTP服务器。
- 调用外部工具。
- 使用外部工具 使用MATLAB的大型语言模型(LLM) 插件。
设置
使用此插件需要MATLAB R2025a或更高版本。
要从MATLAB生成和执行工具调用,您还需要4.6.0或更高版本的 使用MATLAB的大型语言模型(LLM) 插件。
在线使用MATLAB
您可以通过单击此链接在MATLAB Online中使用该插件: 
使用加载项资源管理器安装
在已安装的MATLAB版本中使用该插件的推荐方法是使用插件资源管理器。
- 在MATLAB中,转到 家 选项卡,以及 环境 部分,单击 附加组件 偶像。
- 在附加组件资源管理器中,搜索“MATLAB MCP HTTP客户端”。
- 选择 安装.
例子
创建MCP客户端
使用创建MCP客户端 mcpHTTPClient 功能。指定要连接到的MCP服务器的URL。例如,使用 IO航空航天MCP服务器 访问航空航天和天体动力学工具。
endpoint = "https://mcp.io-aerospace.org/";
client = mcpHTTPClient(endpoint);MCP客户端将服务器上可用工具的信息存储在 ServerTools 财产。
serverTools = client.ServerTools;IO Aerospace MCP服务器提供了一个名为 get_celestial_body_properties 它返回天体的地球物理特性。
toolGetCelestialBodyProperties = serverTools{23}toolGetCelestialBodyProperties = struct with fields:
name: 'get_celestial_body_properties'
title: 'Get celestial body properties'
description: 'Gets geophysical properties of a celestial body'
inputSchema: [1x1 struct]
outputSchema: [1x1 struct]
annotations: [1x1 struct]
输入模式指定了工具所需的参数。
toolGetCelestialBodyProperties.inputSchema.propertiesans = struct with fields:
celestialBodyName: [1x1 struct]
toolGetCelestialBodyProperties.inputSchema.properties.celestialBodyNameans = struct with fields:
description: 'Celestial body name'
type: 'string'
enum: {162x1 cell}
要调用此工具,请使用 callTool 函数,并指定工具的名称作为位置输入参数。然后,将工具参数指定为其他名称值参数 Argument1=Value1,...,ArgumentN=ValueN.工具 get_celestial_body_properties IO Aerospace MCP服务器提供的具有一个输入参数, celestialBodyName.
toolName = toolGetCelestialBodyProperties.name;
toolOutput = callTool(client,toolName,celestialBodyName="Mars");
prettyPrintJSON(toolOutput){
"naifId": 499,
"centerOfMotionId": 10,
"barycenterOfMotionId": 4,
"name": "MARS",
"radii": {
"x": 3.39619E+6,
"y": 3.39619E+6,
"z": 3.3762E+6
},
"gm": 4.2828373620699086E+13,
"frameName": "IAU_MARS",
"frameId": 10014,
"j2": "NaN",
"j3": "NaN",
"j4": "NaN"
}使用LLM自动调用外部工具
此示例显示了如何将LLM与MCP客户端一起使用,以自动调用和执行外部工具。该示例要求 使用MATLAB的大型语言模型(LLM) 插件。
首先,使用MATLAB文档,按照大型语言模型(LLM)配置与OpenAI®聊天完成API的连接: OpenAI.
使用创建MCP客户端 mcpHTTPClient 功能。指定要连接到的MCP服务器的URL。例如,使用 IO航空航天MCP服务器 访问航空航天和天体动力学工具。
endpoint = "https://mcp.io-aerospace.org/";
client = mcpHTTPClient(endpoint);MCP客户端将服务器上可用工具的信息存储在 ServerTools 财产。
serverTools = client.ServerTools;此MCP服务器提供的工具之一是 get_celestial_body_properties.
toolGetCelestialBodyProperties = client.ServerTools{23}toolGetCelestialBodyProperties = struct with fields:
name: 'get_celestial_body_properties'
title: 'Get celestial body properties'
description: 'Gets geophysical properties of a celestial body'
inputSchema: [1x1 struct]
outputSchema: [1x1 struct]
annotations: [1x1 struct]
要将MCP客户端提供的工具与LLM一起使用,首先将工具转换为 fArray,一系列 openAIFunction 物体。连接到OpenAI聊天完成API。请使用GPT-4.1 mini型号。使用授予模型访问服务器工具的权限 Tools 争论。
fArray = openAIFunction(serverTools);
model = openAIChat(ModelName="gpt-4.1-mini",Tools=fArray);要将对话作为上下文提供给模型,请创建 messageHistory 对象。
history = messageHistory;指定一个可能导致工具调用的提示。例如,向模型询问火星的特性。
userPrompt = "Tell me about the properties of Mars.";添加 userPrompt 与历史 addUserMessage 功能。使用生成输出 generate 功能。
history = addUserMessage(history,userPrompt);
[~,completeOutput] = generate(model,history);如果模型检测到一个或多个工具调用,则 generate 函数返回有关中的名称和任何输入参数的信息 tool_calls 领域 completeOutput 输出结构。
LLM可能会产生工具名称和参数的幻觉。因此,请根据中的工具规范验证第一个函数调用 fArray.
toolRequest = completeOutput.tool_calls(1).function;
validateToolCall(toolRequest,fArray);使用以下命令执行第一个函数调用 callTool 功能。要将工具调用结果添加到历史记录中,请使用 addToolCallToHistory 函数,在本示例底部定义。
toolOutput = callTool(client,toolRequest);
history = addToolCallToHistory(history,completeOutput,toolOutput);
prettyPrintJSON(toolOutput){
"naifId": 499,
"centerOfMotionId": 10,
"barycenterOfMotionId": 4,
"name": "MARS",
"radii": {
"x": 3.39619E+6,
"y": 3.39619E+6,
"z": 3.3762E+6
},
"gm": 4.2828373620699086E+13,
"frameName": "IAU_MARS",
"frameId": 10014,
"j2": "NaN",
"j3": "NaN",
"j4": "NaN"
}要使用工具调用结果生成自然语言响应,请使用 generate 功能与更新的历史记录。
generatedText = generate(model,history)Mars is a celestial body with the following properties:
- NAIF ID: 499
- Center of Motion ID: 10
- Barycenter of Motion ID: 4
- Radii: Approximately 3,396.19 km along the x and y axes, and 3,376.2 km along the z axis
- Standard gravitational parameter (GM): 4.2828373620699e+13 m^3/s^2
- Reference Frame Name: IAU_MARS
- Reference Frame ID: 10014
If you want more specific information or additional properties, please let me know!您可以向模型询问有关工具调用结果的后续问题。例如,向模型询问火星的引力参数。
userFollowUpPrompt = "How would these properties affect someone standing on Mars?";
history = addUserMessage(history,userFollowUpPrompt);
generatedText = generate(model,history)generatedText =
Standing on Mars would feel very different from Earth. Mars' weaker gravity
(about 38% of Earth's) means you'd weigh much less, making movement easier but
less stable.Helper函数
LLM可能会产生工具调用的幻觉,或者对工具所需的参数产生错误。因此,请验证工具名称和参数 fArray,列表 openAIFunction 包含工具规格的对象。
function validateToolCall(toolRequest,fArray)
% Validate tool name
toolName = toolRequest.name;
toolIndex = find(strcmp(toolName,[fArray.FunctionName]),1);
assert(~isempty(toolIndex),"Invalid tool name '%s'.",toolName)
% Validate arguments
try
args = jsondecode(toolRequest.arguments);
catch
error("Model returned invalid JSON syntax for arguments of tool '%s'.",toolName);
end
f = fArray(toolIndex);
argsRequired = string(fieldnames(f.Parameters));
assert(all(isfield(args,argsRequired)),"Invalid tool parameters '%s'.",strjoin(fieldnames(args),","));
endfunction history = addToolCallToHistory(history,completeOutput,toolOutput)
history = addResponseMessage(history,completeOutput);
history = addToolMessage(history,completeOutput.tool_calls.id,completeOutput.tool_calls.function.name,toolOutput);
endfunction prettyPrintJSON(output)
disp(jsonencode( ...
jsondecode(output), ...
PrettyPrint=true))
end函数
mcpHTTP客户端
client = mcpHTTPClient(endpoint) 根据MCP服务器URL返回MCP客户端 endpoint.
这 mcpHTTPClient 对象将与MCP服务器关联的工具存储在 ServerTools 属性,指定为结构的单元格数组。每个结构体都包含一个工具的信息,包括工具名称和参数。
callTool
result = callTool(client,toolName,argumentName1=x1,argumentName2=x2,...) 使用名称调用工具 toolName 带输入参数 argumentName1 指定为 x1等等。
result = callTool(client,toolRequest) 调用工具请求 toolRequest 返回LLM。例如,您可以指定 toolRequest 随着 completeOutput.tool_calls.function 的输出 generate (使用MATLAB的大型语言模型(LLM))函数。
#
使用MATLAB HTTP MCP客户端时,在运行所有工具调用之前,您应该彻底检查和验证它们。始终让一个人参与重要行动,只有当你确信电话会完全按照你的期望进行时,才能继续进行。有关更多信息,请参阅以下信息 MCP的信任、安全和安保 和 MCP安全注意事项.
*版权所有2025 The MathWorks,股份有限公司。*
