-
Notifications
You must be signed in to change notification settings - Fork 235
ModelContextProtocol.McpException: Transport is not connected #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you add example code or repository? |
MCP Server is hosted on Azure App Service. Below is the code of my client which also hosted on another App Service which is utilizing Semantic Kernel to register tools as plugins. Once registered, after that it is Semantic Kernal which utilizes McpClient to invoke the tool. If I restart my client App Service, it works for few minutes but after that it starts failing. `/// /// Creates an MCP client and connects it to the MCPServer server. /// /// An instance of . static Task CreateSSeMcpClientAsync(string mcpServerUrl) { return McpClientFactory.CreateAsync(new SseClientTransport(new() { Name = "MCPServer",
} static async Task AddTools(string mcpServerUrl, IKernelBuilder kernelBuilder)
} |
Do you use .NET dependency injection (DI)? Both Kernel and McpClient should be registered as transient. In my application, I use Azure Container Apps where the API and MCP are deployed as separate Azure Container Apps. I noticed that when I stopped and restarted the MCP container, the API began throwing the same error. Initially, I had McpClient registered as a singleton, but after changing it to transient, everything started working correctly again. I'm still wondering about the exact root cause, but it might be related to how McpClient manages its connections. For example: builder.Services.AddTransient(sp =>
{
var mcpClient = McpClientFactory.CreateAsync(new SseClientTransport(new SseClientTransportOptions
{ Endpoint = new Uri(mcpServerUrl) })).GetAwaiter().GetResult();
return mcpClient;
});
builder.Services.AddTransient(sp =>
{
KernelPluginCollection pluginCollection = [];
var mcpTools = sp.GetRequiredService<IMcpClient>().ListToolsAsync().GetAwaiter().GetResult();
pluginCollection.AddFromFunctions("McpServer",
mcpTools.Select(aiFunction => aiFunction.AsKernelFunction()));
return new Kernel(sp, pluginCollection);
}); |
Can you able to access the MCP Server from Azure App service in your local dev environment? I hosted one and I am able to access it without any issues. |
@Matonen - I used the below syntax to add Kernel and I believe it is adding as transient. MCPClient is hidden for me as Sementic Kernel uses it when LLM asks for a function call. // Add Kernel |
I am also able to access but it starts failing after sometime! |
You probably shouldn't do this. It's usually better to register a factory service type with a
As of 0.1.0-preview.10, you should no longer see this exception now that McpSession.SendRequestAsync doesn't check if the transport is connected. It's still possible for StdioClientSessionTransport.SendMessageAsync to throw a similar exception, but it now should at least include a more informative inner exception for why the process may have exited: csharp-sdk/src/ModelContextProtocol/Protocol/Transport/StdioClientSessionTransport.cs Lines 40 to 53 in 0c9e91f
Can you try upgrading and tell us what happens? Another thing that might help would be lowering the log-level to |
I deployed my MCP server and client in Azure App Service - And it is working properly. Here is the client code. I am using GitHub models in my Semantic Kernel. builder.Services.AddSingleton(serviceProvider =>
{
var clientTransport = new SseClientTransport(new SseClientTransportOptions
{
Name = "McpServer",
Endpoint = new Uri(mcpSseUrl)
});
var mcpClient = McpClientFactory.CreateAsync(clientTransport).Result;
var githubToken = builder.Configuration["GithubToken"];
var kernelBuilder = Kernel.CreateBuilder()
.AddOpenAIChatCompletion("gpt-4o", new Uri("https://models.inference.ai.azure.com"), githubToken);
kernelBuilder.Plugins.AddFromFunctions("McpServer", mcpClient.ListToolsAsync()
.Result
.Select(t => t.AsKernelFunction()));
return kernelBuilder.Build();
}); |
I am using SementicKernel to consume MCP tools.
Keep on getting the below 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, JsonTypeInfo
1 parametersTypeInfo, JsonTypeInfo
1 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(NonNullCollection
1 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)
The text was updated successfully, but these errors were encountered: