How omnisharp/csharp-language-server-protocol works
The project is a highly modular .NET implementation of the Language Server Protocol (LSP) along with related protocols (Debug Adapter Protocol – DAP – and JSON-RPC), built around a MediatR-based request/response model with extensive use of Microsoft.Extensions for configuration, dependency injection, and logging. In other words, this is a library (and suite of libraries) that implements protocol handling and communication layers in C#, with clear separations between protocol definitions, request routing, serialization, and the actual client/server implementations.
Core Architectural Components
• JSON-RPC Core Layer: This is at the heart of the library and includes the JSON-RPC abstractions, messaging, serialization (e.g., DapSerializer, optional converters) as well as the generated components in the JsonRpc.Generators directory.
• MediatR Request/Response Pipeline: Represent the usage of MediatR for managing request handling and routing. This sits on top of the JSON-RPC layer and acts as the central hub for dispatching protocol-specific requests.
• Protocol Abstractions: A dedicated layer for protocol definitions and models (the “Protocol” directory) that encapsulates the types, notifications, and requests in strict conformance with the Language Server Protocol. Similarly, the Debug Adapter Protocol is separated into its own DAP modules.
• Language Client/Server Components: This includes the language client (LSP Client) and language server abstractions, along with supporting classes (for example, in /src/Client, /src/Server, and /src/Shared). These are built on top of the JSON-RPC and MediatR infrastructure.
• Debug Adapter Components: Comprising both the client and server sides of the Debug Adapter Protocol (from the /src/Dap.Client, /src/Dap.Protocol, and /src/Dap.Server directories), these modules implement the debug-specific parts of the overall protocol strategy.
Relationships and Interactions
• The JSON-RPC layer provides the fundamental communication – it both serializes messages and routes JSON messages into MediatR requests.
• MediatR acts as the “glue” that links protocol requests (LSP/DAP) to the appropriate handlers. It does so via dependency injection configured with Microsoft.Extensions.DependencyInjection, found in the various LanguageServer and LanguageClient ServiceCollection extensions.
• The protocol-specific layers (LSP and DAP) are built as distinct modules that depend on and extend the JSON-RPC base.
• There is a clear boundary between the protocol definitions (data models, serialization, and protocol structures in the “Protocol” directories) and the implementation code (handlers and runtime in “Server”, “Client”, “Dap.Server”, “Dap.Client”, etc.).
• Test and sample projects (e.g., those under /test, /benchmarks, /sample, and VS Code/Atom extensions) validate integration points and ensure that the components interact correctly.
Architectural Patterns & Principles
• Layered/Modular Architecture: Separation between core protocol abstractions (models, serialization), transport (JSON-RPC), and higher-level protocol implementations (LSP and DAP).
• Dependency Injection: The heavy use of Microsoft.Extensions.DependencyInjection ensures that dependencies among the communication, handler registration, and business logic are decoupled.
• MediatR Pattern: Centralized routing of requests and responses promotes clean separation of concerns and makes extension via new handlers straightforward.
• Code Generation: The inclusion of JsonRpc.Generators indicates that part of the system’s functionality is auto-generated based on reflective or declarative attributes.