MCP in .NET: The Practical Guide Nobody Wrote. OAuth Bugs, Transport Gotchas, and What Visual Studio Can’t Do
Every MCP tutorial covers the same ground: add the NuGet package, register your tools, run the server.
That part genuinely does work first time.
What the tutorials don’t cover is the OAuth discovery bug in VS 2022 that silently zeroes out your tool list, why transport choice is an architecture decision not a config option, why your [Description] attributes are actually AI instructions and not developer notes, and whether VS 2022 + Copilot is something you can rely on consistently for real workflows.
This post is what I wish existed before I spent hours debugging.
~
What MCP Is and Why It’s Worth Your Time
MCP (Model Context Protocol) is a standard for exposing tools and resources to AI clients. Instead of building a custom integration for every assistant, you build one MCP server — and any MCP-compatible client (Claude Desktop, GitHub Copilot in VS, Cursor, VS Code Continue) can discover and use your tools automatically.
The protocol runs JSON-RPC 2.0 over either HTTP/SSE or STDIO. Tools are annotated methods.
The client sends a query, the AI decides which tool to call, the MCP server executes it and returns a result, the AI synthesises the response.
Simple enough in principle but the implementation details are where it gets interesting.
~
Setting Up the Server
The Microsoft MCP SDK (ModelContextProtocol.AspNetCore on NuGet) is genuinely straightforward. The basics work:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
var app = builder.Build();
app.MapMcp();
app.Run();
That gets you a running MCP server. The problems start in the decisions around it.
Transport Is an Architecture Decision, Not a Config Option
HTTP/SSE and STDIO are not interchangeable choices that you can swap later without pain. Make the decision early based on your deployment model:
- HTTP/SSE: your MCP server is a standalone web API. Multiple clients connect to it. Standard deployment, supports API keys, works with any HTTP-capable MCP client.
- STDIO: a client process spawns and owns your server process directly via stdin/stdout. No port, no URL — the client manages the lifecycle.
If you’re building a shared team server or a documentation search tool, HTTP/SSE is the right call.
If you’re building something tightly coupled to a single local client process, STDIO makes sense. But once you’ve built around one, switching transport means rethinking registration, authentication, and how clients connect.
[Description] Attributes Are AI Instructions
This is the most commonly under-appreciated thing in MCP development. The descriptions on your tool methods and parameters are not documentation for developers. They are the instructions the AI uses to decide when to call the tool and what to pass in. Vague descriptions are the most common cause of unreliable tool invocation.
Write them as instructions to the model:
[McpServerTool]
[Description("Searches documentation for a given topic and returns a synthesised answer with source references. Use this when the user asks a question about the product or API.")]
public async Task<string> Search(
[Description("The user's natural language question. Do not rewrite or shorten it.")]
string query,
[Description("Optional category filter. Use only if the user specifies one explicitly.")]
string? category = null)
Notice what’s in that description: when to use the tool, what the parameters mean, and explicit instructions about what not to do with them.
If you write "The search query", you’ll get inconsistent results. If you write instructions, you get consistent behaviour.
The VS 2022 OAuth Discovery Bug
This is the one. VS 2022 v17.14+ performs an OAuth discovery check before connecting to any MCP server. It appends /.well-known/oauth-protected-resource to the configured URL.
If your URL has a query parameter — for example, ?apiKey=xxx — VS 2022 constructs a malformed URL:
http://localhost:5125/sse?apiKey=your-key-here/.well-known/oauth-protected-resource
The server returns 404. VS 2022 silently marks the server unavailable. Your tools panel shows 0/0. No useful error appears anywhere in the UI.
How to Confirm It
Go to View → Output, change the dropdown to “GitHub Copilot”. Look for Failed to fetch protected resource metadata followed by a 404. That’s your confirmation.
The Fix
Intercept middleware in Program.cs, registered before your auth middleware and before app.MapMcp(). Use a Contains check, not an exact path match, because the path VS 2022 sends is malformed:
// Must be first — before auth middleware
app.Use(async (context, next) =>
{
if (context.Request.Path.Value?.Contains(".well-known/oauth-protected-resource") == true)
{
context.Response.StatusCode = 200;
await context.Response.WriteAsJsonAsync(new { });
return;
}
await next();
});
app.UseMiddleware<McpApiKeyMiddleware>();
app.MapMcp();
Order matters. If auth middleware runs first, it will reject the malformed OAuth request before your intercept can handle it.
VS 2022 + Copilot: An Honest Assessment
Getting tools to load is one problem. Getting them to invoke reliably is another.
The 0/0 Problem Has Multiple Causes
Once the OAuth fix is applied, if 0/0 persists, work through this list in order:
- Is
.mcp.jsonin the solution root — the same folder as the.slnfile? VS 2022 does not search subdirectories. - Is the server actually running? VS 2022 gives no indication of a connection timeout beyond showing 0/0.
- Has VS 2022 been fully restarted since adding or changing
.mcp.json? A solution reload is not enough. - Is Agent Mode on? It resets in some circumstances and tools are only invoked in Agent Mode — not standard Chat.
That last point is the single most common reason tools appear to “not work”. The mode is wrong, not the server.
Tool Invocation Is Inconsistent
Even with tools loading correctly at 5/5, Copilot doesn’t always invoke them when expected. This is model behaviour, not configuration. Copilot is making a judgement call about whether a tool is needed.
A .github/copilot-instructions.md file at the repository root helps:
# Copilot Instructions
## Tool Use
When answering questions about the API, documentation, or specific components,
always use the `Search` tool first rather than answering from general knowledge.
Prefer tool results over training data. If the tool returns a relevant answer,
use it. If it doesn't, say so explicitly rather than falling back to general knowledge.
This doesn’t guarantee invocation on every query, but it meaningfully improves consistency — particularly in Agent Mode.
Add it as a repo-level file, not an afterthought.
What VS 2022 + Copilot Cannot Be Trusted For
File writes, shell commands, and PowerShell invocation via MCP tools often enough to be unreliable consistently in VS 2022 and Copilot. The failure mode is usually silent which is a pain.
The execution chain (IDE → Copilot → MCP tool → shell) has enough layers that errors get swallowed.
A suggestion for practical split: use VS 2022 + Copilot for read-only MCP tools, documentation search, context retrieval, codebase questions.
For file system operations, running commands, or multi-step workflows, use Claude Code instead.
Claude Code owns the MCP server process directly and communicates over a well-defined transport with full lifecycle control.
When a tool fails in Claude Code, you get an actual error. When it works, it works consistently.
Connecting Other Clients
Basic configuration for connecting other clients are detailed below.
VS Code Continue
Edit ~/.continue/config.yaml:
mcpServers:
- name: my-mcp-server
transport:
type: sse
url: http://localhost:5125/sse
apiKey: your-api-key
Continue supports apiKey directly in config — no OAuth discovery problem, no malformed URLs.
Claude Code
# Register an HTTP/SSE server
claude mcp add --transport http my-mcp-server http://localhost:5125/sse
# With API key header
claude mcp add --transport http my-mcp-server http://localhost:5125/sse
--header "X-Api-Key: your-key-here"
# STDIO transport — the -- separator is required
claude mcp add --transport stdio my-mcp-server -- dotnet run --project ./MyMcpServer
# Verify
claude mcp list
Scope controls who gets the configuration:
--scope local(default): local config, git-ignored. Just you, current project.--scope project: writes to.mcp.jsonin repo root. Everyone on the team.--scope user: global user config. Just you, all projects.
For a shared team server, --scope project is the right call. .mcp.json commits to source control and every developer who clones the repo gets the same MCP configuration automatically.
Key Takeaways
Some of the main takeaways when working with the tooling include the following:
- Transport is architecture. HTTP/SSE vs STDIO is a deployment decision — make it before you start building, not when something breaks.
[Description]attributes are AI instructions. Write them that way. Vague descriptions are the most common cause of inconsistent tool invocation.- The VS 2022 OAuth bug is real and silent. The fix is a
Containsintercept middleware registered before auth and beforeapp.MapMcp(). - Agent Mode is required in Copilot. A simple setting to get wrong!
- Use VS 2022 + Copilot for read-only tools only. File writes and shell commands fail silently too often. Use Claude Code for those workflows.
--scope projectin Claude Code gives every team member the same MCP config automatically. Use it for shared servers.- Log queries from day one. You’ll want that data later and you can’t reconstruct it retroactively.
Those are just some. The gaps aren’t in the protocol. They’re in the tooling, the defaults, and the assumptions that tutorials don’t question or mention.
I’m sure , the teething problems I experienced will be quickly resolved. Hopefully these tips save you time.
~
Enjoy what you’ve read, have questions about this content, or would like to see another topic? Drop me a note below.
You can schedule a call using my Calendly link to discuss consulting and development services.
~