Skip to main content

Fixed: warning CS8602: Dereference of a possibly null reference on McpClient.ServerInfo

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Writing the name of an MCP server with client.ServerInfo.Name turned a clean build into warning CS8602: Dereference of a possibly null reference. In the MCP C# SDK, McpClient.ServerInfo is nullable, because a server that connects with the protocol's newest handshake doesn't have to describe itself. A null check with a pattern fixed the warning without hiding that case.

What I Was Doing​

I was writing a class that connects an AI agent's API to the Microsoft Learn MCP server with the official MCP C# SDK, ModelContextProtocol.Core 2.4.0, and logs which server answered: its name and its version. This is the smallest program that shows the same thing, a .NET 10 console app:

Terminal
dotnet new console -n ServerInfoDemo
cd ServerInfoDemo
dotnet add package ModelContextProtocol.Core --version 2.4.0
Program.cs
using ModelContextProtocol.Client;

// Connect to the Microsoft Learn MCP server.
await using McpClient client = await McpClient.CreateAsync(
new HttpClientTransport(new HttpClientTransportOptions
{
Endpoint = new Uri("https://learn.microsoft.com/api/mcp"),
TransportMode = HttpTransportMode.StreamableHttp,
}),
new McpClientOptions { ProtocolVersion = "2025-06-18" });

// Show which server answered.
Console.WriteLine($"Connected to {client.ServerInfo.Name} {client.ServerInfo.Version}.");

New .NET projects turn on nullable reference types (<Nullable>enable</Nullable> in the project file), so the compiler warns when code might use null as if it were an object.

What I Expected​

A build without warnings. The server always sends its name in my tests.

What I Got​

Terminal
dotnet build
Output
Restore complete (0.3s)
ServerInfoDemo net10.0 succeeded with 1 warning(s) (2.0s) → bin/Debug/net10.0/ServerInfoDemo.dll
/Users/you/source/ServerInfoDemo/Program.cs(13,35): warning CS8602: Dereference of a possibly null reference.

Build succeeded with 1 warning(s) in 2.6s

macOS-style terminal window showing dotnet build in the ServerInfoDemo folder: ServerInfoDemo net10.0 succeeded with 1 warning, Program.cs(13,35): warning CS8602: Dereference of a possibly null reference, and Build succeeded with 1 warning(s)

Line 13, column 35 is client.ServerInfo.Name, the first time the line reads ServerInfo.

What I Checked​

1. The property's type. In the SDK's source, McpClient declares it as public abstract Implementation? ServerInfo { get; }. The ? means it can be null, and the compiler warns when code reads .Name from something that can be null.

2. When it is null. The SDK's documentation of ServerInfo explains it: It is populated during the initialization handshake or from server/discover result metadata. A server using server/discover may omit this optional metadata, in which case the value is null. server/discover belongs to the protocol's newest revision, 2026-07-28. In the older handshake, initialize, the description is required: the 2025-06-18 schema lists serverInfo without a question mark. My connection uses 2025-06-18, so the server did describe itself. The type has to allow both kinds of connection, though.

3. What Microsoft recommends for CS8602. Microsoft's page on nullable warnings names three ways to fix a possible dereference of null: Add a missing null check, add null-analysis attributes to the API, or Apply the null forgiving operator !. I can't change the SDK's attributes, and ! would only tell the compiler that null never happens, which the SDK's documentation says it can.

The same documentation adds a warning for anyone tempted to rely on this value: This self-reported information can be useful for logging, debugging, and displaying server information to users. It should not be used for security decisions.

Why It Happens​

The MCP C# SDK 2.4.0 supports protocol revisions with two different handshakes. With initialize, a server must describe itself. With server/discover, it may leave that out. McpClient.ServerInfo has one type for every connection, so it's Implementation?, and the compiler treats every read of it as one that might find null.

The Fix​

Check for null before reading the description. A pattern does it in one line: client.ServerInfo is { } info is true only when ServerInfo isn't null, and then info holds it. Microsoft's guide to nullable warnings lists patterns such as is { } among the checks the compiler understands.

Program.cs
using ModelContextProtocol.Client;

// Connect to the Microsoft Learn MCP server.
await using McpClient client = await McpClient.CreateAsync(
new HttpClientTransport(new HttpClientTransportOptions
{
Endpoint = new Uri("https://learn.microsoft.com/api/mcp"),
TransportMode = HttpTransportMode.StreamableHttp,
}),
new McpClientOptions { ProtocolVersion = "2025-06-18" });

// Show which server answered. A server doesn't have to describe itself, so check first.
string server = client.ServerInfo is { } info ? $"{info.Name} {info.Version}" : "an MCP server";
Console.WriteLine($"Connected to {server}.");

When the server describes itself, the line shows its name and version. When it doesn't, it says an MCP server instead of crashing with a NullReferenceException.

After the Fix​

Output
$ dotnet build
Restore complete (0.3s)
ServerInfoDemo net10.0 succeeded (1.4s) → bin/Debug/net10.0/ServerInfoDemo.dll

Build succeeded in 2.0s

$ dotnet run
Connected to Microsoft Learn MCP Server 1.0.0.

macOS-style terminal window showing dotnet build in the ServerInfoDemo folder succeeding without warnings, then dotnet run printing Connected to Microsoft Learn MCP Server 1.0.0.

No warning, and the same name and version as the server sent.

Common Mistakes​

MistakeWhat happensDo this instead
Writing client.ServerInfo!.Name to silence the warningThe warning goes away, and a server that doesn't describe itself crashes the line with a NullReferenceExceptionCheck for null, as the pattern does
Using ServerInfo to decide whether to trust a serverThe name is whatever the server says it isDecide which servers to trust by their address, in your own settings

FAQ​

Can ServerInfo be null with the Microsoft Learn MCP server?​

Not with a connection that uses 2025-06-18: that handshake requires the server to describe itself, and it did. The check still belongs in the code, because the SDK's type allows null, and the same code may one day connect with server/discover.

Conclusion​

warning CS8602: Dereference of a possibly null reference on client.ServerInfo.Name is the compiler taking the MCP C# SDK at its word: ServerInfo is Implementation?, because a server that connects with server/discover may leave its description out. Check for null with is { } info, and use the description for logs and display only.

Additional Resources​