Make a C# REPL for your Business in 2 minutes
Does your Business have a .NET codebase? Did you know that You can spin up an Administrator CLI that lets you write C# expressions using the service classes and methods you know so well.
What is Mykeels.CSharpRepl?
Mykeels.CSharpRepl is a powerful, embeddable C# REPL (Read-Eval-Print Loop) for .NET applications. It lets you spin up an interactive C# environment—right inside your business app—so you (or your team) can run C# expressions, scripts, and even invoke your own service classes and methods on the fly.
This is perfect for admin CLIs, debugging, or building custom automation tools for your business. It supports syntax highlighting, code completion, custom output formatting, and more.
Getting Started
Add the NuGet package to your project:
dotnet add package Mykeels.CSharpRepl
Quick Start
To launch a REPL in your app, just add:
using Mykeels.CSharpRepl;
await Repl.Run();
This starts an interactive C# REPL with sensible defaults.
Every C# expression entered is evaluated and the result is displayed.
You can chain C# commands.
Async C# expressions work as well.
Customizing the REPL
You can customize references, namespaces, output formatting, and more by passing 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 run commands before the REPL starts—great for importing commonly used types or setting up your environment:
await Repl.Run(
commands: [
// Import ScriptGlobals to make its methods available directly
"using static Mykeels.CSharpRepl.Sample.ScriptGlobals;"
]
);
ScriptGlobals
You can expose your own static methods and properties to the REPL by adding a static class (e.g., ScriptGlobals
). Then, import it with a pre-execution command:
"using static Mykeels.CSharpRepl.Sample.ScriptGlobals;"
This makes your business logic instantly available in the REPL.
Running as an MCP Server
You can also launch an MCP server, which lets you list members of your ScriptGlobals
class and invoke C# code remotely (e.g., from tools like Cursor):
await McpServer.Run(typeof(ScriptGlobals));
Best Practices
- Assembly References: Include all necessary assemblies in the
references
array - Namespaces: Add commonly used namespaces to the
usings
array - Error Handling: Implement custom error handling in
logError
for better debugging - Output Formatting: Use
AnsiConsole
for colored output and better readability - Pre-execution Commands: Use
commands
to set up your environment and import commonly used types
Conclusion
With Mykeels.CSharpRepl, you can empower your business with a flexible, interactive C# CLI in minutes. Whether for admin tasks, debugging, or automation, it's a game-changer for .NET teams.
Check out the GitHub repo for more details, and start building your own REPL today!
What will you build with this?