MODULE 03 · LESSON 3.6
Configure a reusable C# scripting environment
Define a reusable plugin with exact NuGet packages and run agent-written C# in a fresh worker.
Lesson 16 of 88 · FabrCore 2.0
Overview
Operations Desk can keep its existing request lookup tool and add C# for calculations over returned data. The developer defines the packages and limits in a scripting plugin; the model writes only the code for an invocation. Different agents can reuse one plugin or select different package environments.
Choose packages in developer code
FabrCore.Scripting is optional. Register AddFabrCoreScripting once in the application and derive a PluginAlias class from CSharpScriptingPluginBase. Override Configure to declare exact NuGet versions, imports, instructions and limits. The base owns initialization and disposal. Package build targets run during preparation, so these packages are trusted developer choices.
One agent might use Newtonsoft.Json for data transformations while another uses a scientific library. Use separate plugins for conflicting versions, and select one scripting plugin per agent because GetScriptingEnvironment and ExecuteCSharp have the same inherited names. Identical dependency bundles can share their prepared cache; agents never share script variables.
A fresh process for every invocation
The host starts a .NET worker with Roslyn and the declared packages. The host itself does not load those script dependencies. JSON crosses the boundary; the worker receives no live agent, DI services or Blazor circuit. There is no persistent script session.
A process boundary protects the host from many ordinary failures but is not a security sandbox. Workers run under the host OS identity and may access its files and network. Use an external restricted OS/container boundary for untrusted workloads. Package declarations and disabled #r/#load directives are not permissions.
Install and select the reporting plugin
Download the Operations Desk source. The README lists project setup, package prerequisites and local ports.
- Use a matching available FabrCore.Scripting package targeting .NET 10. Stable 2.0.0 publication is pending; use locally built packages and a local NuGet feed when necessary. Preparing a worker requires the .NET 10 SDK and package access.
- Copy ReportingScriptsPlugin.cs into the agent library and register AddFabrCoreScripting in the Host services. Ensure the plugin assembly participates in normal discovery.
- In the proxy, add reporting-scripts to config.Plugins before ResolveConfiguredToolsAsync. Use the complete ScriptingAgent below or adapt that initialization in your existing agent.
- Download Operations Desk and run the standalone OperationsDesk.Scripting/ScriptingExample.csproj checkpoint first. It invokes the registry tool directly without any model or API key.
using FabrCore.Scripting;
using FabrCore.Sdk;
[PluginAlias("reporting-scripts")]
public sealed class ReportingScriptsPlugin : CSharpScriptingPluginBase
{
protected override void Configure(ScriptingEnvironmentBuilder environment) => environment
.AddPackage("Newtonsoft.Json", "13.0.3")
.AddImports("Newtonsoft.Json.Linq")
.WithTimeout(TimeSpan.FromSeconds(30))
.WithMemoryLimit(512L * 1024 * 1024)
.WithOutputLimit(32_768)
.WithArtifactLimit(1_048_576)
.WithInstructions("Use JObject.Parse(InputJson) to read authorized query results. " +
"For orders, use data[\"orders\"]!.Values<decimal>().Sum(). " +
"Return a small JSON-serializable summary. Check that inputs are complete.");
}
using FabrCore.Scripting;
builder.Services.AddFabrCoreScripting();
using FabrCore.Core;
using FabrCore.Sdk;
using Microsoft.Agents.AI;
[AgentAlias("scripting-assistant")]
public sealed class ScriptingAgent(
AgentConfiguration config,
IServiceProvider services,
IFabrCoreAgentHost host) : FabrCoreAgentProxy(config, services, host)
{
private AIAgent agent = null!;
private AgentSession session = null!;
public override async Task OnInitialize()
{
if (!config.Plugins.Contains("reporting-scripts"))
config.Plugins.Add("reporting-scripts");
var tools = await ResolveConfiguredToolsAsync();
var created = await CreateChatClientAgent(
chatClientConfigName: config.Models ?? "default",
threadId: config.Handle ?? fabrcoreAgentHost.GetHandle(),
tools: tools);
agent = created.Agent;
session = created.Session;
}
public override async Task<AgentMessage> OnMessage(AgentMessage message)
{
var result = await agent.RunAsync(message.Message ?? string.Empty, session);
var response = message.Response();
response.Message = result.Text;
return response;
}
}
dotnet run --project OperationsDesk.Scripting/ScriptingExample.csproj
# If 2.0.0 is not published, use your local feed and matching package version:
# dotnet run --project OperationsDesk.Scripting/ScriptingExample.csproj -p:FabrCoreVersion=<your-matching-version>
Verify packages and the worker boundary
- Run the checkpoint. Confirm the JSON value contains Customer Eric and Total 400. Compare the printed host process ID with the worker process ID in Output; they should differ.
- For your model-backed agent, inspect its resolved tools and call GetScriptingEnvironment. Confirm Newtonsoft.Json 13.0.3, the namespace import and your developer instructions are present.
- Ask the agent to total the supplied orders [125,75,200]. Inspect ExecuteCSharp and its Success result before judging the final answer. This model step requires your normal model configuration; the checkpoint does not.
The same plugin pattern works for multiple agents while each execution starts with fresh state. Adding a package changes the prepared dependency environment; imports, instructions and execution limits remain per plugin.
If the result is different
A missing tool usually means alias selection or assembly discovery is wrong. A preparation failure can mean a missing .NET 10 SDK, unavailable exact package version or inaccessible feed. Supply NuGetConfigPath for a deployment-owned private-feed configuration; never pass feed credentials as script input.
Go deeper
Explore the related documentation.