|
| 1 | +classdefmcpHTTPClient < handle |
| 2 | +% An MCP client (streamable HTTP only) |
| 3 | + |
| 4 | +% Copyright 2025 The MathWorks, Inc. |
| 5 | +properties (SetAccess=private) |
| 6 | +Endpoint |
| 7 | +ServerTools (1,:) |
| 8 | +end |
| 9 | + |
| 10 | +properties (Access=private) |
| 11 | +counter=0; |
| 12 | +end |
| 13 | + |
| 14 | +properties (Hidden) |
| 15 | +% test seam |
| 16 | +Send= @send |
| 17 | +end |
| 18 | + |
| 19 | +methods |
| 20 | +functionobj= mcpHTTPClient(endpoint, varargin) |
| 21 | + obj.Endpoint =endpoint; |
| 22 | +% undocumented test seam |
| 23 | +ifnargin>2&& isequal(varargin{1}, "Send") |
| 24 | + obj.Send = varargin{2}; |
| 25 | +end |
| 26 | + obj.ServerTools =obj.retrieveTools; |
| 27 | +end |
| 28 | + |
| 29 | +functionresult= callTool(obj, tool, varargin) |
| 30 | +if isstruct(tool) |
| 31 | + toolName = string(tool.name); |
| 32 | +if isfield(tool,"arguments") && ~isstruct(tool.arguments) |
| 33 | + tool.arguments =mcp.util.verbatimJSON(tool.arguments); |
| 34 | +end |
| 35 | +else |
| 36 | + toolName = string(tool); |
| 37 | + tool = struct(... |
| 38 | +"name",tool,"arguments",struct(varargin{:})); |
| 39 | +end |
| 40 | +for k=1:numel(obj) |
| 41 | + toolNames = string(cellfun(@(s) s.name,obj(k).ServerTools,'UniformOutput',false)); |
| 42 | +if ~any(toolName==toolNames) |
| 43 | +continue; |
| 44 | +end |
| 45 | + sessionIDField = obj(k).initialize(); |
| 46 | + request = obj(k).makeRequest("tools/call", ... |
| 47 | +tool, sessionIDField); |
| 48 | + response = obj(k).Send(request,obj(k).Endpoint); |
| 49 | +ifresponse.StatusCode==matlab.net.http.StatusCode.Unauthorized |
| 50 | + throwError("llms:needsAuthorization"); |
| 51 | +elseifresponse.StatusCode~=matlab.net.http.StatusCode.OK |
| 52 | + error(getServerErrorText(response)); |
| 53 | +end |
| 54 | + data =response.Body.Data; |
| 55 | +if ~isstruct(data) |
| 56 | + data = erase(data,"data: ping"+optionalPattern(char(13))+newline); |
| 57 | + data = extractBetween(data,"data: ",optionalPattern(char(13))+newline); |
| 58 | + data = jsondecode(data); |
| 59 | +end |
| 60 | +if isfield(data,"error") |
| 61 | + error(data.error); |
| 62 | +end |
| 63 | +if isempty([data.result.content]) |
| 64 | + result =""; |
| 65 | +else |
| 66 | + result = string(join([data.result.content.text],"")); |
| 67 | +end |
| 68 | +if isfield(data.result,"isError") && isequal(data.result.isError,true) |
| 69 | + error(result); |
| 70 | +end |
| 71 | +return |
| 72 | +end |
| 73 | +% if we fall off the end of this for loop, nothing in the input |
| 74 | +% vector knows about that tool. |
| 75 | + throwError("llms:unknownTool") |
| 76 | +end |
| 77 | +end |
| 78 | + |
| 79 | +methods (Access=private) |
| 80 | +functionsessionIDField= initialize(obj) |
| 81 | + request =obj.makeRequest("initialize",struct(... |
| 82 | +"protocolVersion", "2025-06-18",... |
| 83 | +"capabilities",struct(),... |
| 84 | +"clientInfo",struct(... |
| 85 | +"name","MATLAB MCP HTTP Client",... |
| 86 | +"version","0.0.1"... |
| 87 | + )... |
| 88 | + ),[]); |
| 89 | + |
| 90 | + response =obj.Send(request,obj.Endpoint); |
| 91 | + |
| 92 | + values = [response.Header.Value]; |
| 93 | + sessionID = values([response.Header.Name] =="mcp-session-id"); |
| 94 | + sessionIDField =matlab.net.http.field.GenericParameterizedField("mcp-session-id",sessionID); |
| 95 | + |
| 96 | + initDone =obj.makeRequest("notifications/initialized",[],sessionIDField); |
| 97 | +obj.Send(initDone,obj.Endpoint); |
| 98 | +end |
| 99 | + |
| 100 | +functionbody= makeBody(obj,method,params) |
| 101 | + payload = struct(... |
| 102 | +"jsonrpc","2.0",... |
| 103 | +"method",method); |
| 104 | + |
| 105 | +if ~strcmp(method,"notifications/initialized") |
| 106 | + obj.counter =obj.counter+1; |
| 107 | + payload.id =obj.counter; |
| 108 | +end |
| 109 | +if ~isequal(params,[]) |
| 110 | + payload.params =params; |
| 111 | +end |
| 112 | + body =matlab.net.http.MessageBody(payload); |
| 113 | +end |
| 114 | + |
| 115 | +functionrequest= makeRequest(obj,method,params,sessionIDField) |
| 116 | + contentTypeField =matlab.net.http.field.ContentTypeField('application/json'); |
| 117 | + type1 =matlab.net.http.MediaType("application/json"); |
| 118 | + type2 =matlab.net.http.MediaType("text/event-stream"); |
| 119 | + acceptField =matlab.net.http.field.AcceptField([type1type2]); |
| 120 | + expectNothing =matlab.net.http.HeaderField('Expect', ''); |
| 121 | + header = [acceptFieldcontentTypeFieldsessionIDFieldexpectNothing]; |
| 122 | + |
| 123 | + body =obj.makeBody(method,params); |
| 124 | + |
| 125 | + method =matlab.net.http.RequestMethod.POST; |
| 126 | + request =matlab.net.http.RequestMessage(method,header,body); |
| 127 | +end |
| 128 | + |
| 129 | +functiontools= retrieveTools(obj) |
| 130 | +arguments |
| 131 | +obj (1,1) mcpHTTPClient |
| 132 | +end |
| 133 | + |
| 134 | + sessionIDField =obj.initialize(); |
| 135 | + request =obj.makeRequest("tools/list", struct(), sessionIDField); |
| 136 | + response =obj.Send(request,obj.Endpoint); |
| 137 | +ifresponse.StatusCode==matlab.net.http.StatusCode.Unauthorized |
| 138 | + throwError("llms:needsAuthorization"); |
| 139 | +elseifresponse.StatusCode~=matlab.net.http.StatusCode.OK |
| 140 | + error(getServerErrorText(response)); |
| 141 | +end |
| 142 | + tools =response.Body.Data; |
| 143 | +if ~isstruct(tools) |
| 144 | + tools = jsondecode("{"+extractBetween(tools,"data:{","}"+optionalPattern(char(13))+newline)+"}"); |
| 145 | +end |
| 146 | +if isfield(tools,"result") && isfield(tools.result,"tools") |
| 147 | + tools =tools.result.tools; |
| 148 | +end |
| 149 | +if ~iscell(tools) |
| 150 | + tools = mat2cell(tools,ones(numel(tools),1)); |
| 151 | +end |
| 152 | +end |
| 153 | + |
| 154 | +end |
| 155 | +end |
| 156 | + |
| 157 | +functionerrortxt= getServerErrorText(response) |
| 158 | + errortxt =response.Body.Data; |
| 159 | +if isstruct(errortxt) |
| 160 | +if isfield(errortxt,"error_description") |
| 161 | + errortxt =errortxt.error_description; |
| 162 | +else |
| 163 | + errortxt = jsonencode(errortxt); |
| 164 | +end |
| 165 | +end |
| 166 | + errortxt = string(errortxt); |
| 167 | +ifresponse.Body.ContentType.Subtype=="html" |
| 168 | + errortxt = extractHTMLText(errortxt); |
| 169 | +end |
| 170 | +if strlength(errortxt) <1 |
| 171 | + errortxt = string(response.StatusLine); |
| 172 | +end |
| 173 | +end |
| 174 | + |
| 175 | +function throwError(id) |
| 176 | +persistentcatalog |
| 177 | +if isempty(catalog) |
| 178 | + catalog = dictionary("string", "string"); |
| 179 | + catalog("llms:needsAuthorization") ="Servers that require authorization are not supported."; |
| 180 | + catalog("llms:unknownTool") ="Unknown tool"; |
| 181 | +end |
| 182 | +throwAsCaller(MException(id,catalog(id))); |
| 183 | +end |
| 184 | + |
0 commit comments