Skip to content

Instantly share code, notes, and snippets.

@amul047
Last active September 16, 2025 06:27
Show Gist options
  • Select an option

  • Save amul047/7ea2e3a1ca6bf7f646ee332adc242173 to your computer and use it in GitHub Desktop.

Select an option

Save amul047/7ea2e3a1ca6bf7f646ee332adc242173 to your computer and use it in GitHub Desktop.
AI chat with multiple MCP servers
// Forked from https://learn.microsoft.com/en-us/dotnet/ai/quickstarts/build-mcp-client
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;
const string AzureOpenAIEndpoint = "https://<your-openai-instance-name>.openai.azure.com/";
const string AzureOpenAIModelDeploymentName = "<your-model-deployment-name>";
Dictionary<string, string> AdditionalHeaders = new Dictionary<string, string>{};
const string StreamableHttpMcpEndpointUsuallyEndingWithMcp = "https://<your-mcp-server>/mcp";
// Create an IChatClient using Azure OpenAI.
IChatClient client = new ChatClientBuilder(
new AzureOpenAIClient(new Uri(AzureOpenAIEndpoint),
new DefaultAzureCredential())
.GetChatClient(AzureOpenAIModelDeploymentName).AsIChatClient())
.UseFunctionInvocation() // THIS IS THE SECRET SAUCE THAT CONNECTS THE MCP SERVER/S TO THE CHAT CLIENT
.Build();
// Create the MCP client
// Configure it to start and connect to your MCP server.
IMcpClient mcpClient1 = await McpClientFactory.CreateAsync(
new StdioClientTransport(new StdioClientTransportOptions()
{
Command = "npx",
Arguments = ["-y", "@azure-devops/mcp", "myadoproject"]
}));
IMcpClient mcpClient2 = await McpClientFactory.CreateAsync(
new SseClientTransport(new SseClientTransportOptions
{
Endpoint = new Uri(StreamableHttpMcpEndpointUsuallyEndingWithMcp),
AdditionalHeaders = AdditionalHeaders
})
);
// List all available tools from the MCP server 1.
Console.WriteLine("Available tools from MCP Server 1:");
List<McpClientTool> tools = await mcpClient1.ListToolsAsync();
foreach (McpClientTool tool in tools)
{
Console.WriteLine($"{tool}");
}
// List all available tools from the MCP server 2.
Console.WriteLine();
Console.WriteLine("Available tools from MCP Server 2:");
IList<McpClientTool> tools2 = await mcpClient2.ListToolsAsync();
foreach (McpClientTool tool in tools2)
{
Console.WriteLine($"{tool}");
}
Console.WriteLine();
tools.AddRange(tools2);
// Conversational loop that can utilize the tools via prompts.
List<ChatMessage> messages = [];
while (true)
{
Console.Write("Prompt: ");
messages.Add(new(ChatRole.User, Console.ReadLine()));
List<ChatResponseUpdate> updates = [];
await foreach (ChatResponseUpdate update in client
.GetStreamingResponseAsync(messages, new() { Tools = [.. tools] }))
{
Console.Write(update);
updates.Add(update);
}
Console.WriteLine();
messages.AddMessages(updates);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment