Skip to main content

How to Connect to an MCP Server in C# with the MCP C# SDK

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

An MCP server offers tools that any MCP client can use, and the official MCP C# SDK makes a .NET app one of those clients. This guide connects a console app to a remote MCP server over Streamable HTTP, and asks the server which tools it has.

Syntax​

Syntax
McpClient <client> = await McpClient.CreateAsync(
new HttpClientTransport(new HttpClientTransportOptions
{
Endpoint = new Uri("<endpoint>"),
TransportMode = HttpTransportMode.StreamableHttp,
}),
new McpClientOptions { ProtocolVersion = "<version>" });

IList<McpClientTool> <tools> = await <client>.ListToolsAsync();
PartWhat it is
<client>The connection to the server, an McpClient. The SDK reference describes CreateAsync as the method that Creates an McpClient, connecting it to the specified server. Dispose it when you're done, with await using
HttpClientTransportHow the client reaches a server over HTTP. A server on the same machine, started as a program, uses StdioClientTransport instead
<endpoint>The server's MCP address, a full https URL
TransportModeWhich kind of HTTP. StreamableHttp is what the SDK's documentation calls the recommended transport for remote servers; the default, AutoDetect, tries it first and falls back to an older style
<version>The MCP protocol version the server supports, such as 2025-06-18. Optional: without it, the client probes for the newest revision first, and logs a warning when the server doesn't support it
<tools>The server's tools. ListToolsAsync Retrieves a list of available tools from the server, each as an McpClientTool

The Example​

A console app connects to the Microsoft Learn MCP server, https://learn.microsoft.com/api/mcp, which needs no account and no key. It prints the protocol version the two sides agreed on, then the name and title of each tool the server offers.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

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

Terminal
dotnet new console -n McpToolList
cd McpToolList
dotnet add package ModelContextProtocol.Core --version 2.4.0

The SDK comes in several packages. Its Getting Started guide recommends ModelContextProtocol.Core when You only need the client or low-level server APIs and want the minimum set of dependencies. Version 2.4.0 is the newest stable release. Open the McpToolList folder in VS Code, or McpToolList.csproj in Visual Studio.

Step 2: Connect and List the Tools​

Replace everything in Program.cs with:

Program.cs
using ModelContextProtocol.Client;

// Say where the server is, and that it speaks MCP over Streamable HTTP.
HttpClientTransport transport = new(new HttpClientTransportOptions
{
Endpoint = new Uri("https://learn.microsoft.com/api/mcp"),
TransportMode = HttpTransportMode.StreamableHttp,
});

// Connect, with the protocol version the server supports.
await using McpClient client = await McpClient.CreateAsync(
transport,
new McpClientOptions { ProtocolVersion = "2025-06-18" });

Console.WriteLine($"Connected over MCP {client.NegotiatedProtocolVersion}.");

// Ask the server which tools it offers.
IList<McpClientTool> tools = await client.ListToolsAsync();
foreach (McpClientTool tool in tools)
{
Console.WriteLine($"- {tool.Name} ({tool.Title})");
}
  • HttpClientTransport holds the address and the transport mode. Creating it doesn't contact the server yet.
  • McpClient.CreateAsync connects: it sends the protocol's first request and agrees on a version with the server. await using disconnects at the end of the program, and ends the session on the server.
  • NegotiatedProtocolVersion is the version the two sides agreed on.
  • ListToolsAsync sends the protocol's tools/list request. Each McpClientTool has the tool's Name, which a model uses to call it, and a Title for people.

Run the app:

In the terminal, in the McpToolList folder, run:

Terminal
dotnet run
Output
Connected over MCP 2025-06-18.
- microsoft_docs_search (Microsoft Docs Search)
- microsoft_code_sample_search (Microsoft Code Sample Search)
- microsoft_docs_fetch (Microsoft Docs Fetch)

macOS-style terminal window titled Terminal — McpToolList showing dotnet run printing Connected over MCP 2025-06-18, then three tools: microsoft_docs_search (Microsoft Docs Search), microsoft_code_sample_search (Microsoft Code Sample Search) and microsoft_docs_fetch (Microsoft Docs Fetch)

The client and the server agreed on 2025-06-18, and the server offers three tools: a documentation search, a code sample search and one that fetches a whole page.

The Complete File​

The file from Step 2 is the whole program:

macOS-style code window titled Program.cs showing a C# console app that creates an HttpClientTransport for https://learn.microsoft.com/api/mcp in Streamable HTTP mode, connects with McpClient.CreateAsync and protocol version 2025-06-18, prints the negotiated version, and prints each tool from ListToolsAsync with its name and title

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out ProtocolVersionBefore connecting, the client tries the newest protocol revision; a server that doesn't support it answers with an error, and the SDK logs a warning before falling backSet the version the server supports
Setting a version the server doesn't speakThe connection fails with McpException: Server protocol version mismatchConnect once without it and read NegotiatedProtocolVersion
Forgetting await usingThe client never tells the server that the session is overDispose the client when you're done
Copying the tool list into your codeThe code breaks when the server renames or changes a toolList the tools every time you connect

Troubleshooting​

SymptomLikely causeFix
warn: … request failed for method 'server/discover': Method 'server/discover' is not available. (-32601).ProtocolVersion isn't set, and the server predates the newest revisionFixed: Request failed for method 'server/discover'
HttpRequestException with 405 (Method Not Allowed)The endpoint isn't the server's MCP address, such as a mistyped pathUse the address from the server's documentation
ArgumentException from EndpointThe address isn't absolute, or doesn't start with http or httpsWrite the full URL

FAQ​

Where do I see what the SDK does?​

Pass an ILoggerFactory as the third argument of CreateAsync. With a console logger, the SDK writes a line when it connects and a warning when something goes wrong. An ASP.NET Core app can pass the factory it already has.

How do I connect to a server on my own machine?​

When the server is a program you start, such as a Node.js or Python package, use StdioClientTransport with the command that starts it instead of HttpClientTransport. The rest of the code stays the same.

Can I keep one client for a long time?​

Yes. A web app usually connects once and keeps the client for as long as it runs, then disposes it when it stops. Pass your own long-lived HttpClient to HttpClientTransport when you do, so you control its settings.

Conclusion​

McpClient.CreateAsync with an HttpClientTransport connects a .NET app to a remote MCP server, and ListToolsAsync asks the server which tools it offers. Set TransportMode to StreamableHttp for a remote server, set ProtocolVersion to the version the server speaks, and dispose the client with await using.

Additional Resources​