Skip to content

Bug: ModelContextProtocol.McpException: Transport is not connected #11649

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

Open
hemantkathuria opened this issue Apr 19, 2025 · 3 comments
Open
Assignees
Labels
bug Something isn't working triage

Comments

@hemantkathuria
Copy link

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 hemantkathuria added the bug Something isn't working label Apr 19, 2025
@SergeyMenshykh
Copy link
Member

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
Copy link
Member

Link to the same issue in MCP C# repo - modelcontextprotocol/csharp-sdk#333

@SergeyMenshykh SergeyMenshykh self-assigned this Apr 22, 2025
@SergeyMenshykh SergeyMenshykh moved this to Sprint: Planned in Semantic Kernel Apr 22, 2025
@hemantkathuria
Copy link
Author

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((sp) =>
{
return new ChatCompletionAgent()
{
Name = AgentName,
Instructions = AgentInstructions,
Kernel = sp.GetRequiredService(),
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 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 })
//};

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
Status: No status
Development

No branches or pull requests

3 participants