Skip to main content

MCP Tools

An MCP server is a program that offers tools to any app that speaks the Model Context Protocol. Your agent can use those tools as its own without a line of tool code. This tutorial starts Microsoft's NuGet MCP server on your computer, keeps the one tool the agent needs, and watches the agent call it to look up package versions.

How MCP Tools Work​

Microsoft's Agent Framework documentation describes MCP in one sentence: Model Context Protocol is an open standard that defines how applications provide tools and contextual data to large language models (LLMs). Three parts work together:

  • An MCP server offers tools. Someone else writes and updates them: in this tutorial, Microsoft's NuGet team.
  • An MCP client connects to the server, asks for its tools with a tools/list request, and runs one with a tools/call request. Here, your app is the client, through the official MCP C# SDK.
  • The agent gets the server's tools like any other tools. The SDK's getting-started guide explains why that works: McpClientTool inherits from AIFunction, so the tools returned by ListToolsAsync can be given directly to any IChatClient. An AIFunction is what Function Tools made from a C# method.

The difference is what runs when the model picks a tool. A function tool runs your method. An MCP tool sends a tools/call to the server, which runs the tool and sends the result back:

Two ways to reach a server​

The MCP C# SDK's documentation describes two transports for this:

  • stdio: The stdio transport communicates over standard input and output streams. It is best suited for local integrations, as the MCP server runs as a child process of the client. Your app starts the server program and talks to it through the program's input and output. This tutorial uses stdio.
  • Streamable HTTP: the server is a web service, and the SDK calls this the recommended transport for remote servers. How to Give a Microsoft Agent Framework Agent the Tools of an MCP Server in C# connects to Microsoft Learn's server that way.

Only the transport changes between the two. Listing the tools and giving them to the agent work the same.

Your app runs the tools​

Agent Framework's tools overview names two kinds of MCP tools. Hosted MCP tools are MCP servers invoked by the provider runtime: the model service calls the server for you. Local MCP tools are MCP servers running locally or on custom hosts, with your own code as the client. This tutorial uses local ones, so every call passes through your app. Your app decides which tools the agent gets, sees every call it makes, and can ask a person first: Tool Approval works with providers whose clients invoke tools locally. A later tutorial, Tools the Model Service Runs, covers the hosted kind.

The Example​

A console app called PackageHelper has an agent that answers questions about NuGet packages. It starts the NuGet MCP server, which Microsoft publishes on nuget.org and which looks packages up on the package sources your computer is set up to use. The server offers six tools. The agent needs one, get_latest_package_version, to answer one question: What are the newest stable versions of ModelContextProtocol.Core and Azure.Identity?

Prerequisites​

  • The agent from Images and Files, and the setup it uses: the .NET 10 SDK, the Azure CLI signed in, a Foundry project endpoint, a model deployment and the Foundry User role.

Step 1: Create a Console App​

In a terminal, in the folder where you keep your projects, run:

Terminal
dotnet new console -n PackageHelper
cd PackageHelper
dotnet add package Microsoft.Agents.AI.Foundry --version 1.28.0-preview.261106.1
dotnet add package Azure.Identity --version 1.21.0
dotnet add package ModelContextProtocol.Core --version 2.4.0

The first two packages are the ones from the previous tutorials. ModelContextProtocol.Core is the client package of the official MCP C# SDK. The SDK's getting-started guide recommends it when You only need the client or low-level server APIs and want the minimum set of dependencies. Version 2.4.0 is its newest stable release.

Step 2: Start the Server and Connect​

Replace everything in Program.cs, starting with this:

Program.cs (start the server and connect)
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;

// 1. Start Microsoft's NuGet MCP server on this computer, and connect to it.
await using McpClient nuget = await McpClient.CreateAsync(
new StdioClientTransport(new StdioClientTransportOptions
{
Name = "NuGet",
Command = "dnx",
Arguments = ["NuGet.Mcp.Server@1.4.23", "--yes"],
InheritEnvironmentVariables = false,
EnvironmentVariables = StdioClientTransportOptions.GetDefaultEnvironmentVariables(),
}));
  • StdioClientTransport starts a program and speaks MCP through its input and output. Command and Arguments make up the command line it runs: dnx NuGet.Mcp.Server@1.4.23 --yes. Name is a transport identifier used for logging purposes.
  • dnx comes with the .NET 10 SDK. Microsoft's NuGet documentation: This version of .NET adds a command, dnx, which is used to download, install, and run the MCP server from https://nuget.org. The first run downloads the server into your NuGet cache, and later runs start it from there.
  • @1.4.23 pins the server's version. Without it, dnx runs the newest version every time, so the program that runs on your computer could change without you noticing.
  • --yes answers yes to the question dnx can ask before it downloads and runs a tool. Microsoft's .NET 10 release notes describe it: By default, users are prompted to confirm the download if the tool doesn't already exist locally. A server started by an MCP client can't answer a question, because its input is the MCP connection, and Microsoft's own setup for the NuGet MCP server passes --yes too. The 10.0.404 SDK used here no longer asks, and still accepts --yes. The dotnet tool exec reference warns: Use this option only with packages and package sources that you trust.
  • InheritEnvironmentVariables = false and GetDefaultEnvironmentVariables() decide what the server sees of your environment. The SDK's transports guide says a stdio server, by default, inherits all environment variables from the current process, and environment variables can hold keys and tokens. GetDefaultEnvironmentVariables Returns a curated set of environment variables from the current process that are safe to forward to a child MCP server process. On my Mac those were HOME, LOGNAME, PATH, SHELL and USER, all that dnx and the server needed.
  • McpClient.CreateAsync starts the server and connects: it sends MCP's first request, initialize, and the two sides agree on a protocol version. With this server, that was 2025-11-25.
  • await using closes the connection when the app ends. Microsoft's documentation: Using await using ensures the MCP client connection is properly closed when it goes out of scope.
Trust a server before you start it

A stdio server is a program that runs on your computer with your permissions. Microsoft's Agent Framework documentation: Before connecting, verify that you trust the server command or endpoint URL and the organization or publisher that operates or distributes the server. Microsoft documents the NuGet MCP server and publishes it on nuget.org. Check the publisher of any other server before you add it to an app.

Step 3: List the Server's Tools​

Program.cs (list the tools)
// 2. Ask the server which tools it offers.
IList<McpClientTool> tools = await nuget.ListToolsAsync();
Console.WriteLine($"The NuGet MCP server offers {tools.Count} tools:");
foreach (McpClientTool tool in tools)
{
Console.WriteLine($" {tool.Name}");
}
Console.WriteLine();

ListToolsAsync sends tools/list and returns each tool as an McpClientTool, with the name, description and parameters the server gave it. The app prints the names.

The descriptions are what the model reads to decide when to call a tool, and this server's are long: get_latest_package_version has 707 characters of description and a 518-character parameter schema. The description also says what the tool returns: a text summary with the latest version, package source, and published date, or an error message if the package is not found.

Step 4: Keep Only the Tools the Agent Needs​

Program.cs (keep one tool)
// 3. Keep only the tool this agent needs.
McpClientTool[] versionTools = [.. tools.Where(tool => tool.Name == "get_latest_package_version")];

The agent answers version questions, so it gets get_latest_package_version and nothing else, for two reasons:

  • Every tool costs tokens on every request. Its name, description and parameters go to the model each time, whether the model calls it or not. In a separate check with the same model, a one-word request took 25 input tokens with no tools, 257 with get_latest_package_version, and 2,351 with all six. How to Measure How Many Input Tokens an Agent's Tools Add in C# shows how.
  • The model can only call what it's given. Agent Framework's tools overview warns: Tool calls and arguments are generated by the model. The other five tools review a repository's supply-chain security, fetch package documentation and plan package updates. This agent has no reason to call them, so it doesn't get them.

Some servers mark their tools with hints, such as read-only, and How to Keep Only the Read-Only Tools of an MCP Server in C# filters on that. The NuGet MCP server's tools carry no hints, so this app picks its tool by name.

Step 5: Give the Tool to the Agent​

Program.cs (the agent)
// 4. Give it to an agent, like any other tool.
AIAgent agent = new AIProjectClient(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential())
.AsAIAgent(
model: "gpt-5.6-luna",
instructions: $"You answer questions about NuGet packages. Look versions up with your tools, never from memory, and answer in one sentence. The solution directory is {Environment.CurrentDirectory}.",
name: "PackageHelper",
tools: [.. versionTools]);
  • tools: [.. versionTools] gives the agent the MCP tool the same way Function Tools gave it a C# method.
  • The instructions name the solution directory. get_latest_package_version requires a solutionDirectory, the Absolute path to the solution directory or repository root used to resolve NuGet.Config package sources. The model can't know that path, so the instructions give it Environment.CurrentDirectory, the folder dotnet run runs the app in. The model fills a tool's parameters only from what it has been told.
  • Never from memory: a model knows package versions only up to the end of its training, so the instructions make it look them up instead.

Step 6: Ask, and Show the Tool Calls​

Program.cs (ask and show the calls)
// 5. Ask a question, and show the tool calls the agent made.
string question = "What are the newest stable versions of ModelContextProtocol.Core and Azure.Identity?";
AgentResponse response = await agent.RunAsync(question);
Console.WriteLine($"> {question}");

foreach (FunctionCallContent call in response.Messages.SelectMany(message => message.Contents).OfType<FunctionCallContent>())
{
string arguments = string.Join(", ", call.Arguments?.Select(a => $"{a.Key}: {a.Value}") ?? []);
Console.WriteLine($" called {call.Name}({arguments})");
}

Console.WriteLine($"{response.Text} ({response.Usage?.InputTokenCount} tokens in)");

RunAsync runs the whole loop: the model asks for tool calls, the agent runs them through the MCP client, and the model answers from the results. The loop afterwards prints every FunctionCallContent in the response's messages: each call the model asked for, with its arguments.

Step 7: Run It​

Terminal
dotnet run
Output
The NuGet MCP server offers 6 tools:
review_supply_chain_security
get_latest_package_version
get_package_context
fix_vulnerable_packages
update_package_version
upgrade_packages_to_latest

> What are the newest stable versions of ModelContextProtocol.Core and Azure.Identity?
called get_latest_package_version(solutionDirectory: /Users/you/source/PackageHelper, packageName: ModelContextProtocol.Core, includePrerelease: False)
called get_latest_package_version(solutionDirectory: /Users/you/source/PackageHelper, packageName: Azure.Identity, includePrerelease: False)
The newest stable versions are **ModelContextProtocol.Core 2.4.0** and **Azure.Identity 1.21.0**. (879 tokens in)

macOS-style terminal window titled Terminal — PackageHelper showing dotnet run: The NuGet MCP server offers 6 tools, review_supply_chain_security, get_latest_package_version, get_package_context, fix_vulnerable_packages, update_package_version and upgrade_packages_to_latest, then the question about ModelContextProtocol.Core and Azure.Identity, two calls to get_latest_package_version with the solution directory, each package name and includePrerelease False, and the answer: The newest stable versions are ModelContextProtocol.Core 2.4.0 and Azure.Identity 1.21.0, with 879 tokens in

  • Six tools: the server's whole list, before the app keeps one.
  • Two calls: the model asked for both packages at once, each with the solution directory from the instructions, and with includePrerelease: False, because the question asked for stable versions.
  • The answer: 2.4.0 and 1.21.0, the same versions Step 1 installed. The double asterisks are Markdown bold, which a chat page would show as bold text.
  • 879 input tokens: the model was called twice, once to choose the calls and once to answer from their results, and both requests carried the instructions, the question and the one tool. At the input price in What Is Microsoft Agent Framework, that's about $0.0002.

The Complete File​

Program.cs
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;

// 1. Start Microsoft's NuGet MCP server on this computer, and connect to it.
await using McpClient nuget = await McpClient.CreateAsync(
new StdioClientTransport(new StdioClientTransportOptions
{
Name = "NuGet",
Command = "dnx",
Arguments = ["NuGet.Mcp.Server@1.4.23", "--yes"],
InheritEnvironmentVariables = false,
EnvironmentVariables = StdioClientTransportOptions.GetDefaultEnvironmentVariables(),
}));

// 2. Ask the server which tools it offers.
IList<McpClientTool> tools = await nuget.ListToolsAsync();
Console.WriteLine($"The NuGet MCP server offers {tools.Count} tools:");
foreach (McpClientTool tool in tools)
{
Console.WriteLine($" {tool.Name}");
}
Console.WriteLine();

// 3. Keep only the tool this agent needs.
McpClientTool[] versionTools = [.. tools.Where(tool => tool.Name == "get_latest_package_version")];

// 4. Give it to an agent, like any other tool.
AIAgent agent = new AIProjectClient(
new Uri("https://foundry-devblogs-exp.services.ai.azure.com/api/projects/proj-devblogs-exp"),
new AzureCliCredential())
.AsAIAgent(
model: "gpt-5.6-luna",
instructions: $"You answer questions about NuGet packages. Look versions up with your tools, never from memory, and answer in one sentence. The solution directory is {Environment.CurrentDirectory}.",
name: "PackageHelper",
tools: [.. versionTools]);

// 5. Ask a question, and show the tool calls the agent made.
string question = "What are the newest stable versions of ModelContextProtocol.Core and Azure.Identity?";
AgentResponse response = await agent.RunAsync(question);
Console.WriteLine($"> {question}");

foreach (FunctionCallContent call in response.Messages.SelectMany(message => message.Contents).OfType<FunctionCallContent>())
{
string arguments = string.Join(", ", call.Arguments?.Select(a => $"{a.Key}: {a.Value}") ?? []);
Console.WriteLine($" called {call.Name}({arguments})");
}

Console.WriteLine($"{response.Text} ({response.Usage?.InputTokenCount} tokens in)");

macOS-style code window titled Program.cs — PackageHelper showing the whole app: McpClient.CreateAsync with a StdioClientTransport that runs dnx NuGet.Mcp.Server@1.4.23 --yes, with InheritEnvironmentVariables false and the default environment variables, ListToolsAsync and a loop that prints each tool name, a Where that keeps only get_latest_package_version, the PackageHelper agent with instructions that name the solution directory and the one tool, RunAsync with the question, and a loop that prints each FunctionCallContent with its arguments before the answer and its input tokens

What You Can Do Now​

  • Start a stdio MCP server from C# with StdioClientTransport, and connect to it with McpClient.CreateAsync.
  • List a server's tools with ListToolsAsync, and give an agent only the ones it needs.
  • Keep your environment variables away from a server with InheritEnvironmentVariables = false.
  • Pin a server's version, and check who publishes it before you run it.

Common Mistakes​

MistakeWhat happensDo this instead
Giving the agent every tool a server offersEvery request carries all of their descriptions, 2,351 input tokens instead of 257 here, and the model can call tools you didn't mean it toKeep only the tools the agent needs, by name
Starting a server without a version, such as dnx NuGet.Mcp.Serverdnx runs the newest version every time, so what runs can change without you noticingPin the version: NuGet.Mcp.Server@1.4.23
Letting a stdio server inherit your environmentEvery environment variable, including keys and tokens, reaches the serverSet InheritEnvironmentVariables = false and pass GetDefaultEnvironmentVariables()
Leaving a required parameter that only your app knows, such as solutionDirectory, out of the instructionsThe model has nothing to fill it fromPut the value in the instructions

Troubleshooting​

SymptomLikely causeFix
System.IO.IOException: Failed to connect transport., with an inner Win32Exception that says An error occurred trying to start process and No such file or directoryThe command in Command couldn't be startedCheck that dnx --help works in a terminal. dnx comes with the .NET 10 SDK
The first run takes longer than the next onesdnx downloads the server into your NuGet cache the first timeNothing to fix: later runs start it from the cache
The output shows no called linesThe agent got no tools: the name in Where matched none of the server's toolsCompare it with the names the app prints

Frequently Asked Questions​

Does the NuGet MCP server cost anything to use?​

The server runs on your computer and looks packages up on your package sources, such as nuget.org. What you pay for is the model's tokens, as in Step 7.

Can a person approve MCP tool calls?​

Yes. An McpClientTool is an AIFunction, so you can wrap it in an ApprovalRequiredAIFunction, as Tool Approval does with a C# method. That works because your app runs the calls.

Where does dnx keep the server?​

In your NuGet cache. The dotnet tool exec reference says it Downloads the specified package to the NuGet cache (if not already present). On my Mac, that was ~/.nuget/packages/nuget.mcp.server/1.4.23.

Conclusion​

An MCP server's tools are function tools that run somewhere else. Connect an McpClient, here over stdio by starting Microsoft's NuGet MCP server with dnx, list the server's tools, keep the ones the agent needs and give them to it like any other tool. Pin the server's version and keep your environment variables to yourself, and the agent answers from live data instead of its memory: 2.4.0 and 1.21.0, looked up as it ran.

Next: Tools the Model Service Runs (coming soon) — let the model service run code, search files or search the web for the agent.

Additional Resources​