semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

Bug: ModelContextProtocol.McpException: Transport is not connected

Open hemantkathuria opened this issue 8 months ago • 7 comments

Add Mcp Tools and keep on getting the below error randomly

fail: Microsoft.SemanticKernel.KernelFunction[0] Function Tools-GetCropHealthParameters failed. Error: Transport is not connected ModelContextProtocol.McpException: Transport is not connected at ModelContextProtocol.Shared.McpSession.SendRequestAsync(JsonRpcRequest request, CancellationToken cancellationToken) at ModelContextProtocol.McpEndpointExtensions.SendRequestAsync[TParameters,TResult](IMcpEndpoint endpoint, String method, TParameters parameters, JsonTypeInfo1 parametersTypeInfo, JsonTypeInfo1 resultTypeInfo, Nullable1 requestId, CancellationToken cancellationToken) at ModelContextProtocol.Client.McpClientTool.InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken) at Microsoft.SemanticKernel.ChatCompletion.AIFunctionKernelFunction.InvokeCoreAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken) at Microsoft.SemanticKernel.KernelFunction.<>c__DisplayClass31_0.<<InvokeAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.SemanticKernel.Kernel.InvokeFilterOrFunctionAsync(NonNullCollection1 functionFilters, Func2 functionCallback, FunctionInvocationContext context, Int32 index) at Microsoft.SemanticKernel.Kernel.OnFunctionInvocationAsync(KernelFunction function, KernelArguments arguments, FunctionResult functionResult, Boolean isStreaming, Func2 functionCallback, CancellationToken cancellationToken) at Microsoft.SemanticKernel.KernelFunction.InvokeAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken)

hemantkathuria avatar Apr 19 '25 06:04 hemantkathuria

Hi @hemantkathuria, it's quite difficult to tell what is going on without having the code. Please create a console app that reproduces this behavior and share it with us so we can start investigating. The first thing to look at would be the disposed McpClient that your app cached/keeps a reference to for accessing the McpServer.

SergeyMenshykh avatar Apr 22 '25 09:04 SergeyMenshykh

Link to the same issue in MCP C# repo - https://github.com/modelcontextprotocol/csharp-sdk/issues/333

SergeyMenshykh avatar Apr 22 '25 09:04 SergeyMenshykh

Hello @SergeyMenshykh It is a web app where I am facing this. Below is the code in startup. But let me also try upgrading the package as suggested in MCP repo

`// Add Kernel IKernelBuilder kernelBuilder = builder.Services.AddKernel();

string mcpServerUrl = builder.Configuration.GetSection("MCPClient").GetValue("MCPSSEServerUrl");

await AddTools(mcpServerUrl, kernelBuilder);

//Add Azure Open AI builder.Services.AddAzureOpenAIChatCompletion( deploymentName: builder.Configuration.GetSection("AIServices:AzureOpenAI").GetValue("DeploymentName"), endpoint: builder.Configuration.GetSection("AIServices:AzureOpenAI").GetValue("Endpoint"), //Use the Azure CLI (for local) or Managed Identity (for Azure running app) to authenticate to the Azure OpenAI service credentials: new DefaultAzureCredential());

builder.Services.AddTransient<ChatCompletionAgent>((sp) => { return new ChatCompletionAgent() { Name = AgentName, Instructions = AgentInstructions, Kernel = sp.GetRequiredService<Kernel>(), Arguments = new KernelArguments(new OpenAIPromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(), ResponseFormat = "json_object" })

};

});

// Register IStorage. For development, MemoryStorage is suitable. // For production Agents, persisted storage should be used so // that state survives Agent restarts, and operate correctly // in a cluster of Agent instances. builder.Services.AddSingleton<IStorage, MemoryStorage>();

var app = builder.Build();

app.MapGet("/", () => "Hello Agent!");

app.UseStaticFiles();

app.UseDeveloperExceptionPage();

app.MapControllers();//.AllowAnonymous();

app.Run();

///

/// Creates an MCP client and connects it to the MCPServer server. /// /// An instance of . static Task<IMcpClient> CreateSSeMcpClientAsync(string mcpServerUrl) {

return McpClientFactory.CreateAsync(new SseClientTransport(new()
{
    Name = "MCPServer",

    // Point the client to the MCPServer server executable
    Endpoint = new Uri(mcpServerUrl),
}));

}

static async Task AddTools(string mcpServerUrl, IKernelBuilder kernelBuilder) { IMcpClient mcpClient = await CreateSSeMcpClientAsync(mcpServerUrl);

IList<McpClientTool> tools = await mcpClient.ListToolsAsync();

kernelBuilder.Plugins.AddFromFunctions("Tools", tools.Select(aiFunction => aiFunction.AsKernelFunction()));

//// Enable automatic function calling
//OpenAIPromptExecutionSettings executionSettings = new()
//{
//    Temperature = 0,
//    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes = true })
//};

}`

hemantkathuria avatar Apr 22 '25 11:04 hemantkathuria

Hi @hemantkathuria, was able to fix the issue? If not, try to move MCP client creation one level up to the root of the main method: await using IMcpClient mcpClient = await CreateSSeMcpClientAsync(mcpServerUrl); and pass the instance to the AddTools method.

SergeyMenshykh avatar Apr 23 '25 14:04 SergeyMenshykh

This error/exception is generated in ConnectAsync in StdioClientTransport. I'd say that the STDIO connection is failing, maybe try https stream instead. May try adding logging to the application and seeing if the logged exception helps.

 catch (Exception ex)
 {
     logger.TransportConnectFailed(endpointName, ex);
     DisposeProcess(process, processStarted, logger, _options.ShutdownTimeout, endpointName);
     throw new McpTransportException("Failed to connect transport", ex);
 }

ian-work-1 avatar Apr 23 '25 14:04 ian-work-1

@SergeyMenshykh - Upgraded the package made the suggested code change. Same error.

Error

fail: Microsoft.SemanticKernel.KernelFunction[0] Function Tools-GetCropHealthParameters failed. Error: Transport is not connected ModelContextProtocol.McpException: Transport is not connected at ModelContextProtocol.Shared.McpSession.SendRequestAsync(JsonRpcRequest request, CancellationToken cancellationToken) at ModelContextProtocol.McpEndpointExtensions.SendRequestAsync[TParameters,TResult](IMcpEndpoint endpoint, String method, TParameters parameters, JsonTypeInfo1 parametersTypeInfo, JsonTypeInfo1 resultTypeInfo, Nullable1 requestId, CancellationToken cancellationToken) at ModelContextProtocol.Client.McpClientTool.InvokeCoreAsync(AIFunctionArguments arguments, CancellationToken cancellationToken) at Microsoft.SemanticKernel.ChatCompletion.AIFunctionKernelFunction.InvokeCoreAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken) at Microsoft.SemanticKernel.KernelFunction.<>c__DisplayClass31_0.<<InvokeAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.SemanticKernel.Kernel.InvokeFilterOrFunctionAsync(NonNullCollection1 functionFilters, Func2 functionCallback, FunctionInvocationContext context, Int32 index) at Microsoft.SemanticKernel.Kernel.OnFunctionInvocationAsync(KernelFunction function, KernelArguments arguments, FunctionResult functionResult, Boolean isStreaming, Func`2 functionCallback, CancellationToken cancellationToken) at Microsoft.SemanticKernel.KernelFunction.InvokeAsync(Kernel kernel, KernelArguments arguments, CancellationToken cancellationToken) info: Microsoft.SemanticKernel.KernelFunction[0] Function Tools-GetCropHealthParameters completed. Duration: 0.436099s info: System.Net.Http.HttpClient.Default.LogicalHandler[100] Start processing HTTP request POST https://oai-reference-app.openai.azure.com/openai/deployments/reference-app-model/chat/completions?* info: System.Net.Http.HttpClient.Default.ClientHandler[100] Sending HTTP request POST https://oai-reference-app.openai.azure.com/openai/deployments/reference-app-model/chat/completions?* info: System.Net.Http.HttpClient.Default.ClientHandler[101] Received HTTP response headers after 3849.6745ms - 200 info: System.Net.Http.HttpClient.Default.LogicalHandler[101] End processing HTTP request after 3861.0981ms - 200 info: Microsoft.SemanticKernel.Connectors.AzureOpenAI.AzureOpenAIChatCompletionService[0] Prompt tokens: 2563. Completion tokens: 62. Total tokens: 2625. info: System.Net.Http.HttpClient.Microsoft.Agents.Builder.RestChannelServiceClientFactory.LogicalHandler[100] Start processing HTTP request POST http://localhost:58919/v3/conversations/a5b536a0-20ed-11f0-bd9b-bd7cc83afe5f%7Clivechat/activities/4b5c70a0-20ee-11f0-b2aa-6d888ea96a0f info: System.Net.Http.HttpClient.Microsoft.Agents.Builder.RestChannelServiceClientFactory.ClientHandler[100] Sending HTTP request POST http://localhost:58919/v3/conversations/a5b536a0-20ed-11f0-bd9b-bd7cc83afe5f%7Clivechat/activities/4b5c70a0-20ee-11f0-b2aa-6d888ea96a0f info: System.Net.Http.HttpClient.Microsoft.Agents.Builder.RestChannelServiceClientFactory.ClientHandler[101] Received HTTP response headers after 130.4809ms - 200 info: System.Net.Http.HttpClient.Microsoft.Agents.Builder.RestChannelServiceClientFactory.LogicalHandler[101] End processing HTTP request after 144.9635ms - 200 info: Microsoft.Agents.Hosting.AspNetCore.IAgentHttpAdapter[0] ProcessActivityAsync info: System.Net.Http.HttpClient.Default.LogicalHandler[100] Start processing HTTP request POST https://oai-reference-app.openai.azure.com/openai/deployments/reference-app-model/chat/completions?* info: System.Net.Http.HttpClient.Default.ClientHandler[100] Sending HTTP request POST https://oai-reference-app.openai.azure.com/openai/deployments/reference-app-model/chat/completions?* info: System.Net.Http.HttpClient.Default.ClientHandler[101] Received HTTP response headers after 399.261ms - 429 info: System.Net.Http.HttpClient.Default.LogicalHandler[101] End processing HTTP request after 409.9919ms - 429 fail: Microsoft.Agents.Hosting.AspNetCore.IAgentHttpAdapter[0] HTTP 429 (: 429)

  Rate limit is exceeded. Try again in 7 seconds. => HTTP 429 (: 429)

  Rate limit is exceeded. Try again in 7 seconds.
  =====================================
  ErrorCode: -2146233088
  Source: Microsoft.SemanticKernel.Connectors.OpenAI
  Method: MoveNext
  TimeStamp: 2025-04-24T09:27:48.4157159Z
  Error: HTTP 429 (: 429)

  Rate limit is exceeded. Try again in 7 seconds.
  HelpLink Url: Not Provided
  =====================================
  Inner Exception Level 1   :
  ErrorCode: -2146233088
  Source: OpenAI
  Method: MoveNext
  TimeStamp: 2025-04-24T09:27:48.4167451Z
  Error: HTTP 429 (: 429)

  Rate limit is exceeded. Try again in 7 seconds.
  HelpLink Url: Not Provided
  =====================================`

Modified code below.

`// Add AspNet token validation builder.Services.AddAgentAspNetAuthentication(builder.Configuration);

// Add AgentApplicationOptions from config. builder.AddAgentApplicationOptions();

// Add the Agent builder.AddAgent<EchoAgentApplication>();

// Register the EchoAgent builder.Services.AddTransient<EchoAgent>();

// Add Kernel IKernelBuilder kernelBuilder = builder.Services.AddKernel();

string mcpServerUrl = builder.Configuration.GetSection("MCPClient").GetValue("MCPSSEServerUrl");

await using IMcpClient mcpClient = await CreateSSeMcpClientAsync(mcpServerUrl);

await AddTools(mcpServerUrl, kernelBuilder, mcpClient);

//Add Azure Open AI builder.Services.AddAzureOpenAIChatCompletion( deploymentName: builder.Configuration.GetSection("AIServices:AzureOpenAI").GetValue("DeploymentName"), endpoint: builder.Configuration.GetSection("AIServices:AzureOpenAI").GetValue("Endpoint"), //Use the Azure CLI (for local) or Managed Identity (for Azure running app) to authenticate to the Azure OpenAI service credentials: new DefaultAzureCredential());

builder.Services.AddTransient<ChatCompletionAgent>((sp) => { return new ChatCompletionAgent() { Name = AgentName, Instructions = AgentInstructions, Kernel = sp.GetRequiredService<Kernel>(), Arguments = new KernelArguments(new OpenAIPromptExecutionSettings() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(), ResponseFormat = "json_object" })

};

});

// Register IStorage. For development, MemoryStorage is suitable. // For production Agents, persisted storage should be used so // that state survives Agent restarts, and operate correctly // in a cluster of Agent instances. builder.Services.AddSingleton<IStorage, MemoryStorage>();

var app = builder.Build();

app.MapGet("/", () => "Hello Agent!");

app.UseStaticFiles();

app.UseDeveloperExceptionPage();

app.MapControllers();//.AllowAnonymous();

app.Run();

///

/// Creates an MCP client and connects it to the MCPServer server. /// /// An instance of . static Task<IMcpClient> CreateSSeMcpClientAsync(string mcpServerUrl) {

return McpClientFactory.CreateAsync(new SseClientTransport(new()
{
    Name = "MCPServer",

    // Point the client to the MCPServer server executable
    Endpoint = new Uri(mcpServerUrl),
}));

}

static async Task AddTools(string mcpServerUrl, IKernelBuilder kernelBuilder, IMcpClient mcpClient) { IList<McpClientTool> tools = await mcpClient.ListToolsAsync();

kernelBuilder.Plugins.AddFromFunctions("Tools", tools.Select(aiFunction => aiFunction.AsKernelFunction()));

//// Enable automatic function calling
//OpenAIPromptExecutionSettings executionSettings = new()
//{
//    Temperature = 0,
//    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(options: new() { RetainArgumentTypes = true })
//};

}`

hemantkathuria avatar Apr 24 '25 09:04 hemantkathuria

@hemantkathuria, it seems your app sent too many requests to the LLM and it responded with "HTTP 429 (: 429) Rate limit is exceeded. Try again in 7 seconds."

I wonder which part of your app sends requests to the LLM. Is it from one of the controllers or from the Tools-GetCropHealthParameters function? Please share the code of both for the full picture.

SergeyMenshykh avatar Apr 24 '25 12:04 SergeyMenshykh

Hi @hemantkathuria, we have not heard from you for a while. Do you still have the issue?

SergeyMenshykh avatar Jun 05 '25 15:06 SergeyMenshykh

Closing the issue for now; I hope it gets resolved. Feel free to reopen it if needed.

SergeyMenshykh avatar Jun 30 '25 12:06 SergeyMenshykh