这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .mcp.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
"run",
"-i",
"--rm",
"--pull=always",
"-v", "${HOME}:${HOME}",
"-w", "${PWD}",
"ghcr.io/infinityflowapp/csharp-mcp:latest"
],
"env": {}
}
}
}
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"infinityflow"
]
}
4 changes: 4 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<!-- Roslyn Scripting -->
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.14.0" />

<!-- NuGet Client Libraries -->
<PackageVersion Include="NuGet.Protocol" Version="6.12.1" />
<PackageVersion Include="NuGet.Resolver" Version="6.12.1" />

<!-- Testing -->
<PackageVersion Include="NUnit" Version="4.4.0" />
<PackageVersion Include="NUnit.Analyzers" Version="4.10.0" />
Expand Down
14 changes: 12 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ RUN dotnet test -c Release --no-build --verbosity normal
# Publish
RUN dotnet publish src/InfinityFlow.CSharp.Eval/InfinityFlow.CSharp.Eval.csproj -c Release -o /app/publish --no-restore

# Runtime stage
FROM mcr.microsoft.com/dotnet/runtime:9.0 AS runtime
# Runtime stage - Use SDK for NuGet package resolution
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS runtime
WORKDIR /app

# Install required dependencies for Roslyn scripting
Expand All @@ -34,11 +34,21 @@ RUN apt-get update && apt-get install -y \
# Copy published app
COPY --from=build /app/publish .

# Create directories for NuGet packages
RUN mkdir -p /tmp/csharp-mcp-packages && \
chmod 777 /tmp/csharp-mcp-packages

# Create non-root user
RUN useradd -m -s /bin/bash mcpuser && \
chown -R mcpuser:mcpuser /app

USER mcpuser

# Set NuGet package cache directory
ENV NUGET_PACKAGES=/tmp/csharp-mcp-packages

# Indicate we're running in a container
ENV DOTNET_RUNNING_IN_CONTAINER=true

# The MCP server uses stdio for communication
ENTRYPOINT ["dotnet", "InfinityFlow.CSharp.Eval.dll"]
73 changes: 63 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ An MCP (Model Context Protocol) server that evaluates and executes C# scripts us

- 🚀 Execute C# scripts directly or from files
- 📦 Full Roslyn scripting support with common namespaces pre-imported
- 📚 NuGet package support via `#r "nuget: PackageName, Version"` directives
- 🔒 Console output capture (safe for MCP stdio protocol)
- ⚡ Comprehensive error handling for compilation and runtime errors
- 🐳 Available as both Docker container and dotnet tool
- 🐳 Available as both Docker container and dotnet tool with volume mounting support
- ✅ Full test coverage with NUnit and FluentAssertions

## Installation
Expand Down Expand Up @@ -56,12 +57,21 @@ The MCP server exposes a single tool: `EvalCSharp`

- `csxFile` (optional): Full path to a .csx file to execute
- `csx` (optional): C# script code to execute directly
- `timeoutSeconds` (optional): Maximum execution time in seconds (default: 30)

Either `csxFile` or `csx` must be provided, but not both.

### Examples

For comprehensive examples, see the [examples directory](examples/):
- [Basic Execution](examples/basic-execution/) - Simple C# script execution
- [Fibonacci Sequence](examples/fibonacci-sequence/) - Generating number sequences
- [Data Processing](examples/data-processing/) - LINQ and data manipulation
- [NuGet Packages](examples/nuget-packages/) - Using external NuGet packages
- [NUnit Testing](examples/nunit-testing/) - Running tests programmatically

#### Direct code execution

```json
{
"tool": "EvalCSharp",
Expand All @@ -72,12 +82,14 @@ Either `csxFile` or `csx` must be provided, but not both.
```

Output:
```

```text
Hello World!
Result: 4
```

#### Execute from file

```json
{
"tool": "EvalCSharp",
Expand All @@ -88,6 +100,7 @@ Result: 4
```

#### Complex example with LINQ

```csharp
var numbers = Enumerable.Range(1, 10);
var evenSum = numbers.Where(n => n % 2 == 0).Sum();
Expand All @@ -96,20 +109,47 @@ evenSum * 2
```

Output:
```

```text
Sum of even numbers: 30
Result: 60
```

### Pre-imported namespaces

The following namespaces are automatically available:

- `System`
- `System.IO`
- `System.Linq`
- `System.Text`
- `System.Collections.Generic`
- `System.Threading.Tasks`
- `System.Net.Http`
- `System.Text.Json`
- `System.Text.RegularExpressions`

### NuGet Package Support

You can reference NuGet packages directly in your scripts using the `#r` directive:

```csharp
#r "nuget: Newtonsoft.Json, 13.0.3"
#r "nuget: Humanizer, 2.14.1"

using Newtonsoft.Json;
using Humanizer;

var json = JsonConvert.SerializeObject(new { Message = "Hello World" });
Console.WriteLine(json);
Console.WriteLine("5 days".Humanize());
```

The tool will automatically:
- Download the specified packages from NuGet.org
- Resolve and download dependencies
- Cache packages for faster subsequent runs
- Provide detailed error messages for invalid package specifications

## MCP Configuration

Expand Down Expand Up @@ -159,15 +199,19 @@ Add to your Claude Code configuration (`claude_desktop_config.json`):
"mcpServers": {
"csharp-eval": {
"command": "docker",
"args": ["run", "-i", "--rm", "ghcr.io/infinityflowapp/csharp-mcp:latest"],
"env": {
"CSX_ALLOWED_PATH": "/scripts"
}
"args": [
"run", "-i", "--rm", "--pull=always",
"-v", "${HOME}:${HOME}",
"-w", "${PWD}",
"ghcr.io/infinityflowapp/csharp-mcp:latest"
]
}
}
}
```

Note: The volume mounting (`-v ${HOME}:${HOME}`) allows the tool to access .csx files from your filesystem.

Or if installed as a dotnet tool:

```json
Expand Down Expand Up @@ -262,18 +306,26 @@ docker run -it infinityflow/csharp-eval-mcp

## Project Structure

```
```text
csharp-mcp/
├── src/
│ └── InfinityFlow.CSharp.Eval/ # Main MCP server implementation
│ ├── Tools/
│ │ └── CSharpEvalTools.cs # Roslyn script evaluation tool
│ │ ├── CSharpEvalTools.cs # Roslyn script evaluation tool
│ │ └── NuGetPackageResolver.cs # NuGet package resolution
│ ├── Program.cs # MCP server entry point
│ └── .mcp/
│ └── server.json # MCP server configuration
├── tests/
│ └── InfinityFlow.CSharp.Eval.Tests/ # Unit tests
│ └── CSharpEvalToolsTests.cs # Comprehensive test suite
│ ├── CSharpEvalToolsTests.cs # Core functionality tests
│ └── ExamplesTests.cs # Example validation tests
├── examples/ # Example scripts with documentation
│ ├── basic-execution/ # Simple C# script examples
│ ├── fibonacci-sequence/ # Algorithm demonstrations
│ ├── data-processing/ # LINQ and data manipulation
│ ├── nuget-packages/ # External package usage
│ └── nunit-testing/ # Programmatic test execution
├── Directory.Packages.props # Central package management
├── Dockerfile # Docker containerization
├── docker-compose.yml # Docker compose configuration
Expand Down Expand Up @@ -303,6 +355,7 @@ git push origin v1.0.0
```

This will:

1. Build and test the project
2. Publish Docker image to GitHub Container Registry
3. Publish NuGet package to NuGet.org
Expand Down
56 changes: 56 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# C# MCP Examples

This directory contains example C# scripts demonstrating various features of the C# MCP server.

## Examples

### 1. [Basic Execution](./basic-execution)
Simple script demonstrating console output, variables, loops, and return values.

### 2. [Fibonacci Sequence](./fibonacci-sequence)
Recursive functions and LINQ operations for calculating Fibonacci numbers.

### 3. [Data Processing](./data-processing)
Complex data manipulation with custom classes, LINQ queries, and JSON serialization.

### 4. [NuGet Packages](./nuget-packages)
Using external NuGet packages with the `#r` directive (requires SDK runtime).

## Running Examples

Each example can be run in several ways:

### Using the MCP Tool Directly
```bash
cd examples/basic-execution
dotnet run --project ../../src/InfinityFlow.CSharp.Eval -- eval-csharp --csx-file script.csx
```

### Using Docker
```bash
docker run -i --rm -v $(pwd):/scripts ghcr.io/infinityflowapp/csharp-mcp:latest \
eval-csharp --csx-file /scripts/examples/basic-execution/script.csx
```

### Via MCP Client
When using an MCP client like Claude or Cursor, provide the full path to the script file.

## Testing Examples

All examples are automatically tested to ensure they produce the expected output:

```bash
dotnet test --filter "FullyQualifiedName~ExamplesTests"
```

## Structure

Each example directory contains:
- `script.csx` - The C# script to execute
- `README.md` - Documentation for the example
- `expected-output.txt` - Expected output for validation

The test suite validates that each example:
1. Executes without errors
2. Produces output matching the expected output
3. Contains all required files
29 changes: 29 additions & 0 deletions examples/basic-execution/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Basic Execution Example

This example demonstrates basic C# script execution with console output and return values.

## Features

- Console output
- Variable declarations
- Simple loops
- Return values

## Running the Example

```bash
# Using the MCP tool directly
dotnet run --project ../../src/InfinityFlow.CSharp.Eval -- eval-csharp --csx-file script.csx

# Or via MCP client
# Provide the full path to script.csx when using the eval_c_sharp tool
```

## Expected Output

The script will output:

- A greeting message
- Current timestamp
- Sum calculation result
- Return value with the final sum
5 changes: 5 additions & 0 deletions examples/basic-execution/expected-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Hello from C# MCP!
Script is running...
Sum of 1 to 10: 55

Result: Script completed successfully with sum = 55
17 changes: 17 additions & 0 deletions examples/basic-execution/script.csx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;

// Basic C# script execution example
Console.WriteLine("Hello from C# MCP!");
Console.WriteLine($"Script is running...");

// Simple calculation
var sum = 0;
for (int i = 1; i <= 10; i++)
{
sum += i;
}

Console.WriteLine($"Sum of 1 to 10: {sum}");

// Return a value
return $"Script completed successfully with sum = {sum}";
28 changes: 28 additions & 0 deletions examples/data-processing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Data Processing Example

This example demonstrates complex data processing with classes, LINQ, and JSON serialization.

## Features
- Custom class definitions
- Collection manipulation
- LINQ queries and aggregations
- Anonymous types
- GroupBy operations
- JSON serialization (System.Text.Json)

## Running the Example

```bash
# Using the MCP tool directly
dotnet run --project ../../src/InfinityFlow.CSharp.Eval -- eval-csharp --csx-file script.csx

# Or via MCP client
# Provide the full path to script.csx when using the eval_c_sharp tool
```

## Expected Output
The script will output:
- List of people with their hobbies
- Statistical analysis
- Age group distribution
- Return value with processing summary
Loading
Loading