{"id":"db9611d6-e6ec-4500-8708-bd9845ddf834","slug":"csharp-mcp","kind":"mcp","name":"csharp-mcp","summary":"An MCP Server to evaluate C#.","owner":"InfinityFlowApp","repoUrl":"https://github.com/InfinityFlowApp/csharp-mcp","license":"Apache-2.0","licenseTier":"permissive","trust":"community","stars":5,"match":null,"fewStars":false,"format":"mcp","createdAt":"2025-08-21T00:47:42.000Z","addedAt":"2026-10-21T15:44:09.305Z","logoUrl":"https://avatars.githubusercontent.com/u/135573873?v=4","linkOnly":false,"collection":null,"skillPath":null,"categories":["cloud-infra"],"tags":[],"runtime":"binary","local":true,"cloudCapable":false,"cloudCapability":"unknown","cloudReadyOnPlatform":false,"needsApiKey":false,"reviewed":false,"syncedAt":"2026-10-21T15:46:26.357Z","sample":false,"url":"https://conduits.dev/tools/csharp-mcp","language":"C#","transports":["stdio"],"install":{},"requirements":{"env":[],"upstreamMayCharge":false},"tools":null,"readme":"# InfinityFlow C# Eval MCP Server\n\nAn MCP (Model Context Protocol) server that evaluates and executes C# scripts using Roslyn. This tool allows AI assistants to run C# code dynamically, either from direct input or from .csx script files.\n\n## Features\n\n- 🚀 Execute C# scripts directly or from files\n- 📦 Full Roslyn scripting support with common namespaces pre-imported\n- 📚 NuGet package support via `#r \"nuget: PackageName, Version\"` directives\n- 🔒 Console output capture (safe for MCP stdio protocol)\n- ⚡ Comprehensive error handling for compilation and runtime errors\n- 🐳 Available as both Docker container and dnx package with volume mounting support\n- ✅ Full test coverage with NUnit and FluentAssertions\n\n## Installation\n\n### Using dnx\n\n```bash\n# Install via dnx\ndnx InfinityFlow.CSharp.Eval --version 1.0.0 --yes\n\n# Run the tool\ndnx run InfinityFlow.CSharp.Eval\n```\n\n### Using Docker\n\n```bash\n# Pull from GitHub Container Registry\ndocker pull ghcr.io/infinityflowapp/csharp-mcp:latest\n\n# Run interactively\ndocker run -it ghcr.io/infinityflowapp/csharp-mcp:latest\n```\n\n### From source\n\n```bash\n# Clone the repository\ngit clone https://github.com/InfinityFlowApp/csharp-mcp.git\ncd csharp-mcp\n\n# Build and run\ndotnet build\ndotnet run --project src/InfinityFlow.CSharp.Eval\n```\n\n## Usage\n\nThe MCP server exposes a single tool: `EvalCSharp`\n\n### Parameters\n\n- `csxFile` (optional): Full path to a .csx file to execute\n- `csx` (optional): C# script code to execute directly\n- `timeoutSeconds` (optional): Maximum execution time in seconds (default: 30)\n\nEither `csxFile` or `csx` must be provided, but not both.\n\n### Examples\n\nFor comprehensive examples, see the [examples directory](examples/):\n\n- [Basic Execution](examples/basic-execution/) - Simple C# script execution\n- [Fibonacci Sequence](examples/fibonacci-sequence/) - Generating number sequences\n- [Data Processing](examples/data-processing/) - LINQ and data manipulation\n- [NuGet Packages](examples/nuget-packages/) - Using external NuGet packages\n- [NUnit Testing](examples/nunit-testing/) - Running tests programmatically\n\n#### Direct code execution\n\n```json\n{\n  \"tool\": \"EvalCSharp\",\n  \"parameters\": {\n    \"csx\": \"Console.WriteLine(\\\"Hello World!\\\"); 2 + 2\"\n  }\n}\n```\n\nOutput:\n\n```text\nHello World!\nResult: 4\n```\n\n#### Execute from file\n\n```json\n{\n  \"tool\": \"EvalCSharp\",\n  \"parameters\": {\n    \"csxFile\": \"/scripts/example.csx\"\n  }\n}\n```\n\n#### Complex example with LINQ\n\n```csharp\nvar numbers = Enumerable.Range(1, 10);\nvar evenSum = numbers.Where(n => n % 2 == 0).Sum();\nConsole.WriteLine($\"Sum of even numbers: {evenSum}\");\nevenSum * 2\n```\n\nOutput:\n\n```text\nSum of even numbers: 30\nResult: 60\n```\n\n### Pre-imported namespaces\n\nThe following namespaces are automatically available:\n\n- `System`\n- `System.IO`\n- `System.Linq`\n- `System.Text`\n- `System.Collections.Generic`\n- `System.Threading.Tasks`\n- `System.Net.Http`\n- `System.Text.Json`\n- `System.Text.RegularExpressions`\n\n### NuGet Package Support\n\nYou can reference NuGet packages directly in your scripts using the `#r` directive:\n\n```csharp\n#r \"nuget: Newtonsoft.Json, 13.0.3\"\n#r \"nuget: Humanizer, 2.14.1\"\n\nusing Newtonsoft.Json;\nusing Humanizer;\n\nvar json = JsonConvert.SerializeObject(new { Message = \"Hello World\" });\nConsole.WriteLine(json);\nConsole.WriteLine(\"5 days\".Humanize());\n```\n\nThe tool will automatically:\n\n- Download the specified packages from NuGet.org\n- Resolve and download dependencies\n- Cache packages for faster subsequent runs\n- Provide detailed error messages for invalid package specifications\n\n## MCP Configuration\n\n### Cursor\n\nAdd to your Cursor settings (`.cursor/mcp_settings.json` or via Settings UI):\n\n```json\n{\n  \"mcpServers\": {\n    \"csharp-eval\": {\n      \"command\": \"docker\",\n      \"args\": [\"run\", \"-i\", \"--rm\", \"ghcr.io/infinityflowapp/csharp-mcp:latest\"],\n      \"env\": {\n        \"CSX_ALLOWED_PATH\": \"/scripts\"\n      }\n    }\n  }\n}\n```\n\nOr if installed via dnx:\n\n```json\n{\n  \"mcpServers\": {\n    \"csharp-eval\": {\n      \"command\": \"dnx\",\n      \"args\": [\"run\", \"InfinityFlow.CSharp.Eval\"],\n      \"env\": {\n        \"CSX_ALLOWED_PATH\": \"${workspaceFolder}/scripts\"\n      }\n    }\n  }\n}\n```\n\n### Claude Code\n\nAdd the MCP server using the CLI:\n\n**Using Docker:**\n\n*Basic setup:*\n\n```bash\nclaude mcp add csharp-eval docker -- run -i --rm ghcr.io/infinityflowapp/csharp-mcp:latest\n```\n\n*With file system access:*\n\n```bash\nclaude mcp add csharp-eval docker -- run -i --rm --pull=always -v \"${HOME}:${HOME}\" -w \"${PWD}\" ghcr.io/infinityflowapp/csharp-mcp:latest\n```\n\n*With restricted script directory:*\n\n```bash\nclaude mcp add csharp-eval -e CSX_ALLOWED_PATH=\"/scripts\" docker -- run -i --rm -v \"${HOME}/scripts:/scripts:ro\" ghcr.io/infinityflowapp/csharp-mcp:latest\n```\n\n**Using dnx:**\n\n```bash\nclaude mcp add csharp-eval -e CSX_ALLOWED_PATH=\"/Users/your-username/scripts\" dnx -- run InfinityFlow.CSharp.Eval\n```\n\nThe volume mounting (`-v ${HOME}:${HOME}`) allows the tool to access .csx files from your filesystem.\n\n### VS Code\n\nCreate `.vscode/mcp.json`:\n\n```json\n{\n  \"servers\": {\n    \"csharp-eval\": {\n      \"type\": \"stdio\",\n      \"command\": \"dnx\",\n      \"args\": [\"run\", \"InfinityFlow.CSharp.Eval\"]\n    }\n  }\n}\n```\n\n### Visual Studio\n\nCreate `.mcp.json` in solution directory:\n\n```json\n{\n  \"servers\": {\n    \"csharp-eval\": {\n      \"type\": \"stdio\",\n      \"command\": \"dnx\",\n      \"args\": [\n        \"InfinityFlow.CSharp.Eval\",\n        \"--version\",\n        \"1.0.0\",\n        \"--yes\"\n      ]\n    }\n  }\n}\n```\n\n## Development\n\n### Prerequisites\n\n- .NET 9.0 SDK or later\n- Docker (optional, for containerization)\n\n### Building\n\n```bash\n# Restore dependencies\ndotnet restore\n\n# Build the project\ndotnet build\n\n# Run tests\ndotnet test\n\n# Pack as MCP package\ndotnet pack -c Release\n```\n\n### Testing\n\nThe project includes comprehensive unit tests using NUnit and FluentAssertions:\n\n```bash\ndotnet test\n```\n\n### Docker Build\n\n```bash\n# Build the Docker image\ndocker build -t infinityflow/csharp-eval-mcp .\n\n# Run the container\ndocker run -it infinityflow/csharp-eval-mcp\n```\n\n## Project Structure\n\n```text\ncsharp-mcp/\n├── src/\n│   └── InfinityFlow.CSharp.Eval/     # Main MCP server implementation\n│       ├── Tools/\n│       │   ├── CSharpEvalTools.cs    # Roslyn script evaluation tool\n│       │   └── NuGetPackageResolver.cs # NuGet package resolution\n│       ├── Program.cs                # MCP server entry point\n│       └── .mcp/\n│           └── server.json           # MCP server configuration\n├── tests/\n│   └── InfinityFlow.CSharp.Eval.Tests/  # Unit tests\n│       ├── CSharpEvalToolsTests.cs      # Core functionality tests\n│       └── ExamplesTests.cs             # Example validation tests\n├── examples/                         # Example scripts with documentation\n│   ├── basic-execution/             # Simple C# script examples\n│   ├── fibonacci-sequence/          # Algorithm demonstrations\n│   ├── data-processing/             # LINQ and data manipulation\n│   ├── nuget-packages/              # External package usage\n│   └── nunit-testing/               # Programmatic test execution\n├── Directory.Packages.props          # Central package management\n├── Dockerfile                        # Docker containerization\n└── .github/\n    └── workflows/                   # GitHub Actions CI/CD\n        ├── ci-cd.yml               # Main CI/CD pipeline\n        ├── validate-pr.yml         # PR validation\n        └── release-drafter.yml     # Automated release notes\n```\n\n## CI/CD\n\nThe project uses GitHub Actions for continuous integration and deployment:\n\n- **CI/CD Pipeline**: Automated testing, Docker builds, and NuGet publishing\n- **PR Validation**: Build and test validation for all pull requests\n- **Release Drafter**: Automated release notes generation\n- **Dependabot**: Automated dependency updates\n\n### Releases\n\nReleases are automatically published when a version tag is pushed:\n\n```bash\ngit tag v1.0.0\ngit push origin v1.0.0\n```\n\nThis will:\n\n1. Build and test the project\n2. Publish Docker image to GitHub Container Registry\n3. Publish NuGet package to NuGet.org\n4. Create a GitHub release with auto-generated notes\n\n## Security Considerations\n\n- ⚠️ Scripts run in the same process context as the MCP server\n- 🔐 Console output is captured to prevent interference with MCP stdio protocol\n- 🐳 Docker container runs as non-root user for additional security\n- 🛡️ Use appropriate sandboxing when running untrusted scripts\n- 📁 File access can be restricted via `CSX_ALLOWED_PATH` environment variable\n- 🔒 Only .csx files are allowed for execution\n- ⏱️ Scripts have a configurable timeout (default 30 seconds)\n\n### File Access Restrictions\n\nThe `CSX_ALLOWED_PATH` environment variable restricts which directories can be accessed when executing .csx files:\n\n```bash\n# Restrict to specific directory\nexport CSX_ALLOWED_PATH=/path/to/allowed/scripts\n\n# Multiple paths (colon-separated on Linux/Mac, semicolon on Windows)\nexport CSX_ALLOWED_PATH=/path/one:/path/two:/path/three\n```\n\n**Important Notes:**\n\n- Path restrictions are **disabled inside Docker containers** (when `DOTNET_RUNNING_IN_CONTAINER=true`)\n- This is because Docker already provides isolation via volume mounts\n- If not set, file access is unrestricted (use with caution)\n- Paths are checked recursively - subdirectories are allowed\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## License\n\nMIT\n\n## Links\n\n- [Model Context Protocol](https://modelcontextprotocol.io/)\n- [MCP C# SDK](https://www.nuget.org/packages/ModelContextProtocol)\n- [Roslyn Scripting](https://github.com/dotnet/roslyn/wiki/Scripting-API-Samples)\n"}