DEV Community

Prashant Patil
Prashant Patil

Posted on

Roslyn-Powered C# Analysis, Real NuGet DLL Reflection, and Safe Live File Editing for Claude — Privately, On Your Machine via MCP

I built an MCP server that gives Claude surgical access to your C# codebase — locally, token-efficiently, without leaking code

If you've ever watched Claude hallucinate a NuGet API that hasn't existed since .NET 6, or burned your entire context window just loading a few service classes — this is for you.


The real problem

AI coding tools have three silent killers in .NET:

  1. Hallucinated APIs — models are frozen at training cutoff. NuGet packages ship breaking changes constantly.
  2. Context bloat — a 500-line service class costs ~2,000 tokens raw. Ten files and you're done before writing a single line.
  3. Your code leaves your machine — every cloud AI tool sends source to an external server. Every. Request.

What this does differently

dotnet-mcp-server is a self-hosted MCP server that exposes your C# codebase as structured tools. Claude never gets a raw file dump — it calls exactly what it needs, one class at a time, one method at a time.

Claude ──► ngrok ──► dotnet-mcp-server ──► Roslyn ──► Your C# source
                            │
                            ├── Redis  (AST cache, ~300ms warmup)
                            └── NuGet  (reflects actual installed DLLs)
Enter fullscreen mode Exit fullscreen mode

Token cost comparison:

What you're doing Raw dump This server
Explore a class ~2,000 tokens ~400 tokens
Fetch one method ~2,000 tokens ~120 tokens
Explore a NuGet namespace ~6,000 tokens ~250 tokens

Features that actually matter

⚡ Redis-backed AST cache

Roslyn parses your .cs files once, serializes the AST metadata to Redis, and serves every subsequent call in milliseconds. A FileSystemWatcher debounces file saves (300ms) and evicts/rewrites only the changed file — so Claude always sees your current code, never stale.

🔬 Roslyn analysis — not grep

analyze_c_sharp_file returns structured metadata: DI constructor graphs, method signatures with line ranges, attributes, XML docs, public/private toggle. fetch_method_implementation returns exact method bodies with line numbers Claude uses directly in edit_lines patch operations.

📦 NuGet reflection against your actual DLL

This one's different. When you ask "how do I use this method", it doesn't use training data. It:

  1. Downloads the .nupkg for the exact version in your .csproj
  2. Loads it into a sandboxed MetadataLoadContext (binary inspection — never executed)
  3. Returns valid, copy-paste-ready signatures from your version
  4. Caches the result in Redis for 7 days

No training cutoff. No hallucinated overloads. No deprecated methods that still "work" in Claude's memory.

🛡️ File operations with real safety guarantees

Claude can write files — but with: per-file semaphore locking, atomic batch-move validation, path sandboxing to project root (traversal structurally impossible), and permanent blocks on bin/, obj/, .git/, and any file matching secret/token patterns.

🔭 Method call graph — before you break anything

analyze_method_call_graph tells you every caller (file, class, line) before you touch a signature. The difference between a safe refactor and a CI failure at 11pm.

🌐 Multi-project from day one

Register all your microservices. Every tool scopes to projectName. Claude can skeleton one service, read a method from another, edit a third — in one conversation.


The privacy angle

Your source code never leaves your machine. Claude receives structured metadata — class names, method signatures, line ranges. Not your business logic. Not your algorithms. Not your customer data.

For anyone in finance, healthcare, or any regulated industry — this isn't a nice-to-have.


Stack

.NET 10 · Roslyn · Redis · ModelContextProtocol · NuGet.Protocol · MetadataLoadContext · ngrok (SSE transport)

22 MCP tools total: code analysis, file ops, NuGet exploration, dotnet CLI (clean + build with structured diagnostics).


Get started

git clone https://github.com/patilprashant6792-official/dotnet-mcp-server
cd dotnet-mcp-server/LocalMcpServer
dotnet run
Enter fullscreen mode Exit fullscreen mode

Then spin up Redis, expose with ngrok, add the /sse URL to Claude.ai → Settings → Connectors. Full setup in the README.

Configure your projects

Once the server is running, open the web UI:
http://localhost:5000/config.html

OR

http://your ngrok link/config.html
Add your project paths here — name, root path, description. Config persists in Redis so you register once and it survives restarts.

No separate frontend, no config files to hand-edit. It's served by the server itself.

If you've hit any of these walls — drop a comment. Curious what .NET patterns people are trying to get Claude to reason about.

Top comments (0)