Microsoft Agent Framework Microsoft.Extensions.AI Created: 12 Jul 2026 Updated: 12 Jul 2026

Calling Tools with MCP (Model Context Protocol)

MCP (Model Context Protocol) is the standard way to expose external tools/services to a model through a client-server protocol. We can connect to an MCP server, retrieve the tools it offers, and pass them directly into ChatOptions.Tools; FunctionInvokingChatClient / UseFunctionInvocation then invokes these tools automatically, just as if they were regular AIFunction instances.

This example uses the free, official sample MCP server (@modelcontextprotocol/server-everything), which does not require a token or API key; running it requires Node.js/npx.

References

  1. Get started with MCP
  2. Build an MCP client

Step-by-Step Walkthrough

1. Setup

First, the API key is read from the OPEN_AI_KEY environment variable, and an IChatClient is built with UseFunctionInvocation() added, which enables function/tool calling.

2. Creating the MCP Client and Listing Tools

A connection is made to the MCP server over stdio (standard input/output). The npx command downloads and runs the server package, which requires no token or authentication. The tools offered by the server are automatically converted into AITool instances.

3. Chatting While Using the Tools

The retrieved tools are assigned to ChatOptions.Tools, and a message is sent to the user. When needed, the model automatically calls the add tool and returns the result.

Full Code Example

using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;
using OpenAI;

namespace MicrosoftAgentFrameworkLesson.ConsoleApp
{
// MCP (Model Context Protocol): The standard way to expose external tools/services
// to a model through a client-server protocol. We can connect to an MCP server,
// retrieve the tools it offers, and pass them directly into ChatOptions.Tools;
// FunctionInvokingChatClient/UseFunctionInvocation then invokes these tools
// automatically, just as if they were regular AIFunction instances.
// This example uses the free, official sample MCP server
// (@modelcontextprotocol/server-everything), which does not require a token/API key;
// running it requires Node.js/npx.
// Source: https://learn.microsoft.com/en-us/dotnet/ai/get-started-mcp
// Source: https://learn.microsoft.com/en-us/dotnet/ai/quickstarts/build-mcp-client
public static class McpToolCallingDemo
{
public static async Task RunAsync()
{
#region Setup
var apiKey = Environment.GetEnvironmentVariable("OPEN_AI_KEY")
?? throw new InvalidOperationException("Please set the OPEN_AI_KEY environment variable.");

IChatClient client = new ChatClientBuilder(
new OpenAIClient(apiKey)
.GetChatClient("gpt-4o-mini")
.AsIChatClient())
.UseFunctionInvocation()
.Build();

Console.WriteLine("====== MCP Tool Calling Example ======\n");
#endregion

#region Creating the MCP Client and Listing Tools
// A connection is made to the MCP server over stdio (standard input/output).
// The npx command downloads and runs the server package, which requires
// no token/authentication.
await using McpClient mcpClient = await McpClient.CreateAsync(
new StdioClientTransport(new()
{
Name = "Everything MCP Server",
Command = "npx",
Arguments = ["-y", "@modelcontextprotocol/server-everything"],
}));

// The tools offered by the server are automatically converted into AITool.
IList<McpClientTool> tools = await mcpClient.ListToolsAsync();

Console.WriteLine("Tools received from the server:");
foreach (McpClientTool tool in tools)
{
Console.WriteLine($" - {tool.Name}: {tool.Description}");
}
Console.WriteLine();
#endregion

#region Chatting Using the Tools
var chatOptions = new ChatOptions
{
Tools = [.. tools]
};

List<ChatMessage> chatHistory =
[
new(ChatRole.User, "Use the 'add' tool to add 7 and 35, and tell me the result.")
];

ChatResponse response = await client.GetResponseAsync(chatHistory, chatOptions);

Console.WriteLine($"Model response: {response.Text}");
#endregion
}
}
}



Share this lesson: