Mykeels.CSharpRepl
                             
                            
                                0.0.8
                            
                        
                    dotnet add package Mykeels.CSharpRepl --version 0.0.8
NuGet\Install-Package Mykeels.CSharpRepl -Version 0.0.8
<PackageReference Include="Mykeels.CSharpRepl" Version="0.0.8" />
<PackageVersion Include="Mykeels.CSharpRepl" Version="0.0.8" />
<PackageReference Include="Mykeels.CSharpRepl" />
paket add Mykeels.CSharpRepl --version 0.0.8
#r "nuget: Mykeels.CSharpRepl, 0.0.8"
#:package Mykeels.CSharpRepl@0.0.8
#addin nuget:?package=Mykeels.CSharpRepl&version=0.0.8
#tool nuget:?package=Mykeels.CSharpRepl&version=0.0.8
Mykeels.CSharpRepl
This library is a stripped-down, plug-n-play version of CSharpRepl.Services. It is a powerful C# REPL (Read-Eval-Print Loop) that can be embedded into any .NET application, providing an interactive C# REPL environment with syntax highlighting, code completion, MCP server, and more.
Installation
dotnet add package Mykeels.CSharpRepl
Quick Start
Here's a minimal example of how to use Mykeels.CSharpRepl in your application to launch a REPL:
using Mykeels.CSharpRepl;
await Repl.Run();
This will start an interactive C# REPL with default settings.
Configuration
You can customize the REPL by providing a Configuration object:
using CSharpRepl.Services;
using Mykeels.CSharpRepl;
using Spectre.Console;
await Repl.Run(
    new Configuration(
        // Add references to assemblies
        references: AppDomain
            .CurrentDomain.GetAssemblies()
            .Select(a => $"{a.GetName().Name}.dll")
            .ToArray(),
        
        // Add default namespaces
        usings: [
            "System",
            "System.Collections.Generic",
            "System.IO",
            "System.Linq",
            "System.Net.Http",
            "System.Threading",
            "System.Threading.Tasks"
        ],
        
        // Set application name
        applicationName: "MyApp.CSharpRepl",
        
        // Customize success output
        logSuccess: (message, result) => {
            Console.WriteLine($"<< {message}");
            string output = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            AnsiConsole.MarkupLine($"[green]>> {output}[/]");
        },
        
        // Customize error output
        logError: (message, exception, _) => {
            Console.WriteLine($"<< {message}");
            string output = Newtonsoft.Json.JsonConvert.SerializeObject(
                new { Error = exception }
            );
            Console.WriteLine($">> {output}");
            AnsiConsole.MarkupLine($"[red]>> {output.EscapeMarkup()}[/]");
        }
    )
);
Pre-execution Commands
You can specify commands to be executed before the REPL starts. This is useful for setting up the environment or importing commonly used types:
await Repl.Run(
    commands: [
        // Import ScriptGlobals to make its methods available directly
        "using static Mykeels.CSharpRepl.Sample.ScriptGlobals;"
    ]
);
ScriptGlobals
You can add your own ScriptGlobals by adding a static class with static methods and properties, and then running a pre-execution command on REPL startup.
"using static Mykeels.CSharpRepl.Sample.ScriptGlobals;"
MCP Server
You can also launch a MCP server that can be used to:
- list members of the ScriptGlobals class
- invoke arbitrary C# code, written with the ScriptGlobals class as the globals context
await McpServer.Run(typeof(ScriptGlobals));
Such an MCP server can be used by a tool like Cursor to give Cursor Chat the ability to execute C# code.
Features
- Syntax Highlighting: Code is colorized for better readability
- Code Completion: Intelligent code completion with IntelliSense
- Error Handling: Detailed error messages with stack traces
- JSON Output: Results are automatically serialized to JSON
- Customizable: Configure references, namespaces, and output formatting
- Interactive: Full C# interactive environment with REPL capabilities
Configuration Options
The Configuration class supports the following options:
- references: Array of assembly references to load
- usings: Array of namespaces to import by default
- applicationName: Name of your application
- logSuccess: Callback for handling successful evaluations
- logError: Callback for handling evaluation errors
- commands: Array of commands to execute before starting the REPL
Examples
Basic Usage
await Repl.Run();
With Custom References
await Repl.Run(
    new Configuration(
        references: ["MyApp.dll", "MyApp.Models.dll"]
    )
);
With Custom Output Formatting
await Repl.Run(
    new Configuration(
        logSuccess: (message, result) => {
            Console.WriteLine($"Input: {message}");
            Console.WriteLine($"Result: {result}");
        }
    )
);
With Pre-execution Commands
await Repl.Run(
    commands: [
        "using static Mykeels.CSharpRepl.Sample.ScriptGlobals;",
        "var greeting = \"Hello, World!\";"
    ]
);
Best Practices
- Assembly References: Include all necessary assemblies in the referencesarray
- Namespaces: Add commonly used namespaces to the usingsarray
- Error Handling: Implement custom error handling in logErrorfor better debugging
- Output Formatting: Use AnsiConsolefor colored output and better readability
- Pre-execution Commands: Use commandsto set up your environment and import commonly used types
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
| Product | Versions Compatible and additional computed target framework versions. | 
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. | 
- 
                                                    net8.0- Ben.Demystifier (>= 0.4.1)
- ICSharpCode.Decompiler (>= 8.2.0.7535)
- Microsoft.Build.Locator (>= 1.7.8)
- Microsoft.CodeAnalysis.CSharp.Features (>= 4.11.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.11.0)
- Microsoft.CodeAnalysis.Workspaces.MSBuild (>= 4.11.0)
- Microsoft.Extensions.Caching.Memory (>= 9.0.0)
- Microsoft.Extensions.DependencyModel (>= 9.0.0)
- Microsoft.Extensions.Hosting (>= 9.0.4)
- Microsoft.SymbolStore (>= 1.0.555801)
- ModelContextProtocol (>= 0.1.0-preview.12)
- PrettyPrompt (>= 4.1.1)
- Spectre.Console.Cli (>= 0.49.1)
- System.Configuration.ConfigurationManager (>= 9.0.0)
- System.IO.Abstractions (>= 22.0.14)
 
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | 
|---|---|---|
| 0.0.8 | 108 | 8/17/2025 | 
| 0.0.7 | 100 | 8/17/2025 | 
| 0.0.6 | 205 | 8/6/2025 | 
| 0.0.5-alpha.13 | 203 | 8/6/2025 | 
| 0.0.4-alpha.1031060f | 218 | 5/14/2025 | 
| 0.0.4-alpha.12 | 205 | 8/6/2025 | 
| 0.0.4-alpha.10 | 216 | 5/14/2025 | 
| 0.0.3 | 215 | 5/4/2025 | 
| 0.0.3-alpha.5290079a | 208 | 5/14/2025 | 
| 0.0.2 | 177 | 5/4/2025 | 
| 0.0.1 | 176 | 5/4/2025 | 
| 0.0.0 | 173 | 5/4/2025 |