You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Connection error at client with McpClientFactory.CreateAsync #385
I've tried this with the latest preview 11 and it still happens.
I've created an mcp server (app service, windows) with the ModelContextProtocol library and am trying to connect the client.
It was working previously, but now the connection hangs connecting the SSE transport. This is on a direct connection between client and hosted azure app service.
The remoteUrl parameter is my app services /sse endpoint which is apparently necessary according to 208 and 330 may have an impact here too.
Ultimately, I'm trying to get secure authentication by using Azure API Management service (we can't have unsecured network endpoints on the network), but the 200 OK
doesn't get back to the client due to a transport connection drop on the response to the middle tier so the client never gets it.
In debugging that issue, I found the hang at the app service.
Any ideas what is wrong here?
// Create the logging handler and wrap it around the default HttpClientHandlervarloggingHandler=newLoggingHandler(newHttpClientHandler(){CheckCertificateRevocationList=true});// Create HttpClient with our logging handlerHttpClienthttpClient=newHttpClient(loggingHandler);SseClientTransportOptionssseClientTransportOptions=newSseClientTransportOptions{Endpoint=remoteUrl,AdditionalHeaders=newDictionary<string,string>(){{"Ocp-Apim-Trace","true"}},Name="MCP"};varclientTransport=newSseClientTransport(sseClientTransportOptions,httpClient,ownsHttpClient:true);varclient=awaitMcpClientFactory.CreateAsync(clientTransport);foreach(vartoolinawaitclient.ListToolsAsync()){Console.WriteLine($"{tool.Name} ({tool.Description})");}
I added a delegating handler to inspect the responses to the MCP client from the API Management service (traces are given given in the header of the responses from the policies / routing XML). It's hard to find what you need in the logs on the server side.
There was a call to response.Content.ReadAsync in the delegating handler that wasn't preceded by a check that content length was > 0. Which of course hangs waiting for input when there is no content after the initial GET /sse. This somehow led to a connection timeout or something waiting for the connection to establish (?).
I've also upgraded to preview.12.
Sorry for the spam. Here is the fixed handler:
publicclassLoggingHandler:DelegatingHandler{publicList<string>ResponseLog{get;}=new();publicList<string>ResponseHeadersLog{get;}=new();publicLoggingHandler(HttpMessageHandlerinnerHandler):base(innerHandler){}protectedoverrideasyncTask<HttpResponseMessage>SendAsync(HttpRequestMessagerequest,CancellationTokencancellationToken){varresponse=awaitbase.SendAsync(request,cancellationToken);if(response.StatusCode!=System.Net.HttpStatusCode.OK&&response.StatusCode!=System.Net.HttpStatusCode.Accepted){// Log the status code if it's not OKResponseLog.Add($"Status Code: {response.StatusCode}");// Read and log content if availableif(response.Content!=null&&response.Content.Headers.ContentLength.HasValue&&response.Content.Headers.ContentLength.Value>0){varcontent=awaitresponse.Content.ReadAsStringAsync();ResponseLog.Add(content);}}// Collect all headers from the responseforeach(varheaderinresponse.Headers){ResponseHeadersLog.Add($"{header.Key}: {string.Join(", ",header.Value)}");}returnresponse;}`
Good job figuring out the issue was caused by the DelegatingHandler.
This somehow led to a connection timeout or something waiting for the connection to establish (?).
This is expected if the HttpClient doesn't return from SendAsync before receiving the complete /sse response body, because CreateAsync does not complete until the client processes the initialization response from the server which arrives as an SSE event while the /sse response body remains incomplete.
I am a little surprised that response.Content.Headers.ContentLength.HasValue is true given we don't know the Content-Length up front. SSE relies on chunked transfer encoding for HTTP/1.1 and data frames for HTTP/2 and HTTP/3 so it doesn't have to determine the Content-Length up front when sending response headers.
Regardless, do you think we can close this issue? While it would be nice if we could raise a better error than timing out in this case, I'm not sure it's really feasible to detect if a hang was caused by a buggy DelegatingHandler vs an unresponsive server.
Activity
stephentoub commentedon May 5, 2025
cc: @halter73
nschuessler commentedon May 6, 2025
Ok. Problem solved.
I added a delegating handler to inspect the responses to the MCP client from the API Management service (traces are given given in the header of the responses from the policies / routing XML). It's hard to find what you need in the logs on the server side.
There was a call to
response.Content.ReadAsyncin the delegating handler that wasn't preceded by a check that content length was > 0. Which of course hangs waiting for input when there is no content after the initialGET /sse. This somehow led to a connection timeout or something waiting for the connection to establish (?).I've also upgraded to preview.12.
Sorry for the spam. Here is the fixed handler:
halter73 commentedon May 13, 2025
Good job figuring out the issue was caused by the DelegatingHandler.
This is expected if the HttpClient doesn't return from SendAsync before receiving the complete /sse response body, because CreateAsync does not complete until the client processes the initialization response from the server which arrives as an SSE event while the /sse response body remains incomplete.
I am a little surprised that response.Content.Headers.ContentLength.HasValue is true given we don't know the Content-Length up front. SSE relies on chunked transfer encoding for HTTP/1.1 and data frames for HTTP/2 and HTTP/3 so it doesn't have to determine the Content-Length up front when sending response headers.
Regardless, do you think we can close this issue? While it would be nice if we could raise a better error than timing out in this case, I'm not sure it's really feasible to detect if a hang was caused by a buggy DelegatingHandler vs an unresponsive server.
nschuessler commentedon May 16, 2025