Advanced

πŸ”Œ MCP Tools β€” Model Context Protocol

Connect your agent to any MCP server and give it access to an entire ecosystem of standardised tools β€” GitHub, filesystem, databases, and more.

The Model Context Protocol (MCP) is an open standard that lets AI models call tools hosted by external servers, regardless of the language or runtime they are written in. Agent Framework RC-1 integrates with MCP through the ModelContextProtocol NuGet package (the official .NET MCP SDK).

The workflow is simple: create an McpClient, call ListToolsAsync() to discover what the server exposes, then pass those tools directly into .AsAIAgent(). The agent framework handles tool dispatch automatically β€” just like hand-crafted AIFunction tools.

The example below connects to the GitHub MCP server (via npx / Stdio) and asks the agent to summarise recent commits from a public repository.

Key Concepts

  • McpClient β€” opens and manages a connection to an MCP server
  • StdioClientTransport β€” launches a local server process over stdin/stdout (e.g. npx packages)
  • SseClientTransport β€” alternative for remote HTTP/SSE servers
  • ListToolsAsync() β€” discovers all tools the server exposes; returns IEnumerable<McpClientTool>
  • McpClientTool β€” implements AITool, so it plugs directly into .AsAIAgent(tools:)

Prerequisites

  • Node.js & npx β€” required to run the GitHub MCP server (@modelcontextprotocol/server-github)
  • GITHUB_PERSONAL_ACCESS_TOKEN β€” a GitHub PAT with repo read scope
  • Azure OpenAI endpoint and a function-calling capable model (e.g. gpt-4o-mini)

NuGet Packages

dotnet add package Microsoft.Agents.AI.OpenAI --prerelease
dotnet add package Azure.AI.OpenAI --prerelease
dotnet add package Azure.Identity
dotnet add package ModelContextProtocol --prerelease

Environment Variables

# PowerShell
$env:AZURE_OPENAI_ENDPOINT            = "https://<your-resource>.openai.azure.com/"
$env:AZURE_OPENAI_DEPLOYMENT_NAME     = "gpt-4o-mini"
# The GitHub MCP server reads this directly from the process environment.
# Set it in your shell before running β€” do NOT hard-code it in source code.
$env:GITHUB_PERSONAL_ACCESS_TOKEN     = "ghp_..."

Code Sample β€” GitHub MCP Server (Stdio)

using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using ModelContextProtocol.Client;

var endpoint   = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")!;
var deployment = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";

// Launch the GitHub MCP server as a local stdio child process via npx.
// McpClient manages the process lifecycle and JSON-RPC communication.
// GITHUB_PERSONAL_ACCESS_TOKEN is read directly from the process environment
// by the npx child process β€” set it in your shell before running.
await using var mcpClient = await McpClient.CreateAsync(
    new StdioClientTransport(new()
    {
        Name      = "MCPServer",
        Command   = "npx",
        Arguments = ["-y", "--verbose", "@modelcontextprotocol/server-github"],
    }));

// Discover all tools the server exposes (search_repositories, list_commits, etc.)
var mcpTools = await mcpClient.ListToolsAsync();

// Hand the MCP tools to the agent β€” they implement AITool so no adapter is needed.
AIAgent agent = new AzureOpenAIClient(
        new Uri(endpoint),
        new DefaultAzureCredential())
    .GetChatClient(deployment)
    .AsAIAgent(
        instructions: "You answer questions about GitHub repositories only.",
        tools: [.. mcpTools.Cast<AITool>()]);

// Ask the agent β€” it will call the MCP server's list_commits tool automatically.
string answer = await agent.RunAsync(
    "Summarise the last four commits to the microsoft/semantic-kernel repository.");

Console.WriteLine(answer);

Alternative: Remote HTTP/SSE Server

For MCP servers hosted over HTTP (SSE transport) instead of a local process, swap StdioClientTransport for SseClientTransport:

// Connect to a remote MCP server over HTTP / Server-Sent Events.
await using var mcpClient = await McpClient.CreateAsync(
    new SseClientTransport(new SseClientTransportOptions
    {
        Name    = "Remote MCP Server",
        Endpoint = new Uri("https://mcp.example.com/sse")
    }));

var mcpTools = await mcpClient.ListToolsAsync();

AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
    .GetChatClient(deployment)
    .AsAIAgent(instructions: "You are a helpful assistant.", tools: [.. mcpTools.Cast<AITool>()]);

How It Works

  1. McpClient.CreateAsync() launches or connects to the MCP server and establishes the JSON-RPC session.
  2. ListToolsAsync() queries the server for its tool manifest β€” each tool includes its name, description, and JSON Schema for its parameters.
  3. The model decides which tool(s) to call based on the user's request and the tool descriptions. Agent Framework invokes the tool call on the MCP client and passes the result back to the model.
  4. The model synthesises a reply using the tool's response and returns it to the caller via RunAsync().

Next Steps