← all posts

.NET

Roslyn MCP: Giving AI Tools Semantic Access to .NET Code

MCP can connect AI assistants to Roslyn’s compiler APIs, providing structured access to C# symbols, diagnostics, references, and refactoring operations instead of relying only on source-code text.

AI coding assistants can read C# files as text, but text alone does not provide the complete meaning of a .NET solution. A method name may appear in several types, a class may be split across partial declarations, and changing a public symbol may affect projects throughout the solution.

Roslyn already understands these relationships. MCP provides a standard way to expose that understanding to AI tools.

What is MCP?

Model Context Protocol, or MCP, is an open standard for connecting AI applications to external systems. An MCP server can expose data, prompts, and executable tools, while an MCP client—such as an AI-enabled editor or assistant—discovers and invokes those capabilities through a defined protocol. (modelcontextprotocol.io)

This separation is important. The language model does not need custom integration code for every external system. It can ask an MCP server which tools are available, send structured parameters, and receive structured results.

For software development, an MCP server could provide operations such as:

  • Find the definition of a symbol.
  • Return compiler diagnostics for a project.
  • Find every reference to a method.
  • Rename a type across a solution.
  • Analyze dependencies or code complexity.

The model still decides when to request an operation, but the operation itself is executed by a specialized tool.

What Roslyn provides to .NET

Roslyn is the .NET compiler platform behind the C# and Visual Basic compilers. Its APIs expose the detailed model created while the compiler processes source code, including syntax trees, semantic information, symbols, compilations, diagnostics, and workspaces. These APIs also support familiar IDE features such as Find All References, Go to Definition, intelligent rename, analyzers, code fixes, and refactorings. (learn.microsoft.com)

The distinction between syntax and semantics matters here. Syntax describes the structure of the source code. Semantic analysis determines what that code means—for example, which specific method an invocation references or which type an identifier represents. (learn.microsoft.com)

An AI assistant reading files may infer these relationships. Roslyn can calculate them using the same language rules used by the compiler.

How Roslyn and MCP work together

A Roslyn MCP server acts as an adapter between an MCP client and the Roslyn APIs. It loads a project or solution, exposes selected Roslyn operations as MCP tools, and converts their results into responses the client can use.

A simplified interaction might look like this:

  1. The developer asks the assistant to rename UserService to AccountService.
  2. The assistant calls a Roslyn MCP rename tool with the solution path and symbol information.
  3. Roslyn identifies the symbol and its references across the solution.
  4. The server returns a preview or applies the changes, depending on the tool and configuration.

This is more reliable than a sequence of text replacements because symbol references are not the same as matching strings. Comments, unrelated methods, overloads, namespaces, generated code, and project boundaries all affect the operation.

Practical benefits

The first benefit is better code navigation. Roslyn can return symbol definitions, implementations, callers, references, type hierarchies, and document outlines without requiring the assistant to search every file manually.

The second is compiler-backed diagnostics. Instead of guessing whether generated code compiles, an assistant can request actual errors and warnings from the project model.

The third is safer refactoring. Some implementations offer solution-wide rename, namespace changes, method extraction, formatting, and preview modes. Previewing a diff before writing files is particularly useful when an AI client can invoke operations automatically. (github.com)

Finally, MCP creates a reusable boundary. The Roslyn integration remains in the server, while different compatible clients can consume the same set of tools.

Current Roslyn MCP implementations

Roslyn MCP currently describes an integration approach implemented by different community projects, not one uniform tool.

RoslynMcpServer focuses on refactoring, navigation, analysis, generation, and code-conversion operations. Its tools include solution-wide reference tracking and preview support. (github.com)

RoslynMCP provides operations for wildcard symbol searches, references, symbol information, dependency analysis, and complexity analysis. (github.com)

The Roslyn MCP Extension takes another approach by using Visual Studio’s live workspace. This allows it to work with the editor’s current state, including unsaved changes, rather than loading only the files stored on disk. (marketplace.visualstudio.com)

There is also an ongoing Roslyn discussion about compiler-backed AI integrations. The discussion includes an important counterpoint: MCP is not the only possible integration layer, because AI development tools can also communicate with language services through LSP. (github.com)

What to evaluate before adopting it

A Roslyn MCP server does not automatically make every AI-generated change correct. Its value depends on the operations it exposes, how it loads the workspace, whether changes can be previewed, and how errors and permissions are handled.

It is also necessary to review destructive actions, especially refactorings and dead-code removal. Reflection, dependency injection, serialization, XAML, source generation, and framework conventions can create relationships that static analysis does not always identify completely.

The practical value of Roslyn MCP is therefore specific: it gives an AI assistant access to structured, compiler-backed information about a .NET codebase. It does not replace builds, tests, code review, or developer judgment, but it can provide a stronger technical foundation than source text alone.

References