# Obfuscating a Semantic Kernel plugin safely


Most "do not obfuscate this" advice is about serialization: a member name is also the wire key, so renaming it breaks the contract. A Semantic Kernel plugin is the same story wearing new clothes. You write a plain C# class, decorate some methods with `[KernelFunction]`, and the Kernel turns that class — *by reflection* — into a JSON tool schema that it ships to the language model. The function name, the parameter names, and the descriptions are the contract the model reads and calls back against. The method bodies are your intellectual property. Obfuscating a plugin safely is entirely about telling those two apart.

## How Semantic Kernel turns a class into tools

A plugin is a class whose methods carry `[KernelFunction]`. Each method's signature and attributes become one tool in the schema sent to the model:

```csharp
using System.ComponentModel;
using Microsoft.SemanticKernel;

public sealed class ForecastPlugin
{
    private readonly IWeatherSource _source;
    public ForecastPlugin(IWeatherSource source) => _source = source;

    [KernelFunction("get_forecast")]
    [Description("Gets the multi-day weather forecast for a city.")]
    public async Task<string> GetForecastAsync(
        [Description("City name, e.g. 'Berlin'.")] string city,
        [Description("Number of days, 1-7.")] int days = 3)
    {
        var readings = await _source.QueryAsync(city, days);
        return Blend(readings);          // proprietary model-blending — your IP
    }

    private string Blend(IReadOnlyList<Reading> readings) { /* ... */ }
}
```

You import it under a plugin name and let the model call it:

```csharp
var builder = Kernel.CreateBuilder();
builder.Services.AddSingleton<IWeatherSource, NoaaWeatherSource>();
var kernel = builder.Build();
kernel.ImportPluginFromType<ForecastPlugin>("weather");
```

At call time the Kernel reflects over the plugin and emits a tool definition. With the OpenAI connector it looks like this — note the `-` separator joining the plugin name to the function name, because OpenAI tool names must match `^[a-zA-Z0-9_-]+$`:

```json
{
  "type": "function",
  "function": {
    "name": "weather-get_forecast",
    "description": "Gets the multi-day weather forecast for a city.",
    "parameters": {
      "type": "object",
      "properties": {
        "city": { "type": "string",  "description": "City name, e.g. 'Berlin'." },
        "days": { "type": "integer", "description": "Number of days, 1-7." }
      },
      "required": ["city"]
    }
  }
}
```

Every string in that JSON came from reflection: `weather` from the import call, `get_forecast` from the attribute, `city`/`days` from the parameter names, and the descriptions from `[Description]`. The CLR identifiers `ForecastPlugin`, `GetForecastAsync`, `_source`, and `Blend` appear **nowhere** in it. That separation is the whole opportunity.

## The three name tiers

Every identifier in a plugin falls into one of three tiers, and each tier has a different obfuscation answer.

<figure class="my-8"><svg viewBox="0 0 700 340" role="img" aria-label="Three tiers of a Semantic Kernel plugin: the LLM-visible contract that must be pinned, the parameter names and descriptions that are read but should be kept readable, and the implementation that is renamed, flattened, and string-encrypted" style="width:100%;height:auto"><defs><marker id="sk" markerWidth="9" markerHeight="9" refX="7" refY="3" orient="auto"><path d="M0 0l6 3-6 3z" fill="#64748b"/></marker></defs><text x="16" y="24" style="font:600 13px system-ui;fill:#475569">Reflected into the JSON tool schema the model reads</text><rect x="14" y="36" width="392" height="156" rx="8" fill="#6366f1" fill-opacity="0.08" stroke="#6366f1" stroke-width="1.5"/><text x="30" y="60" style="font:600 12px system-ui;fill:#6366f1">TIER 1 — PIN with strings (identifiers then rename)</text><text x="30" y="82" style="font:12px ui-monospace,monospace;fill:#475569">ImportPluginFromType&lt;T&gt;(&quot;weather&quot;)</text><text x="30" y="102" style="font:12px ui-monospace,monospace;fill:#475569">[KernelFunction(&quot;get_forecast&quot;)]</text><text x="30" y="128" style="font:600 12px system-ui;fill:#0891b2">TIER 2 — KEEP readable (already seen by the LLM)</text><text x="30" y="150" style="font:12px ui-monospace,monospace;fill:#475569">(string city, int days)  — param names</text><text x="30" y="170" style="font:12px ui-monospace,monospace;fill:#475569">[Description(&quot;...&quot;)]  — in metadata blob</text><rect x="452" y="60" width="232" height="92" rx="8" fill="#64748b" fill-opacity="0.08" stroke="#64748b" stroke-width="1.5"/><text x="468" y="84" style="font:600 12px system-ui;fill:#475569">The language model</text><text x="468" y="108" style="font:12px ui-monospace,monospace;fill:#475569">calls: weather-get_forecast</text><text x="468" y="128" style="font:12px ui-monospace,monospace;fill:#475569">args: { city, days }</text><path d="M406 114 h44" stroke="#64748b" stroke-width="1.5" fill="none" marker-end="url(#sk)"/><text x="410" y="108" style="font:11px system-ui;fill:#64748b">schema</text><rect x="14" y="214" width="670" height="110" rx="8" fill="#10b981" fill-opacity="0.08" stroke="#10b981" stroke-width="1.5"/><text x="30" y="238" style="font:600 12px system-ui;fill:#059669">TIER 3 — your IP · full rename + control-flow + string encryption</text><text x="30" y="262" style="font:12px ui-monospace,monospace;fill:#475569">class ForecastPlugin → p0        GetForecastAsync → a</text><text x="30" y="284" style="font:12px ui-monospace,monospace;fill:#475569">Blend()  IWeatherSource  NoaaWeatherSource → renamed</text><text x="30" y="306" style="font:11px system-ui;fill:#64748b">method bodies flattened · route + threshold literals encrypted · nothing here reaches the model</text></svg></figure>

### Tier 1 — pin the plugin and function names, then let the identifiers rename

The plugin name and the function name are the model-visible identity of the tool. You make them independent of your CLR names by supplying them as **strings**:

- `kernel.ImportPluginFromType<ForecastPlugin>("weather")` pins the plugin name to the literal `"weather"`.
- `[KernelFunction("get_forecast")]` pins the function name to the literal `"get_forecast"`.

Once both are explicit, the type name and method name carry no contract. An obfuscator can rename `ForecastPlugin` to `p0` and `GetForecastAsync` to `a`, and the Kernel still emits `weather-get_forecast`, because the schema is built from the attribute string and the import string, not from the identifiers. This is exactly the `[AliasAs]` / `[ProtoMember(N)]` habit from serialization contracts, applied to tool metadata: pin the wire name and the code name goes free.

### Tier 2 — parameter names and descriptions: keep them, don't hide them

Parameter names are different from the function name, because Semantic Kernel has no attribute to override them — it reads `ParameterInfo.Name` straight into the schema. So `city` and `days` become the schema property keys verbatim. You *could* let the obfuscator rename them: SK binds the model's returned arguments back by the same reflected name, so `a` and `b` would still round-trip mechanically. Don't. Two reasons:

1. **Security value is zero.** These names are already sent to the model in plaintext on every call. Renaming them in the binary hides nothing an attacker couldn't read by watching one request.
2. **Quality cost is real.** The model chooses arguments partly from the property names. A schema of `{ "a": ..., "b": ... }` with good descriptions still calls worse than `{ "city": ..., "days": ... }`. You would be paying accuracy to obscure something already public.

The `[Description]` strings are in the same tier and take care of themselves. A `[Description("...")]` argument is stored in the custom-attribute blob in metadata, not as an `ldstr` in a method body, so string encryption — which rewrites method-body string loads — never touches it. SK reads it by reflection and ships it to the model unchanged. That is the behaviour you want.

### Tier 3 — the implementation is the IP

Everything that does *not* reach the model is where obfuscation earns its keep: the method bodies of your `[KernelFunction]`s, the private `Blend` helper, the `IWeatherSource` abstraction and its `NoaaWeatherSource` implementation, DTOs like `Reading`, and every string literal inside those bodies — API routes, prompt fragments, ranking thresholds. Rename them all, flatten the control flow, and encrypt the body strings. None of it is part of the tool schema, so none of it is constrained by the model boundary.

## The trap: an unnamed function renames silently

Here is the failure that produces no exception. Suppose you skipped both explicit names:

```csharp
[KernelFunction]                                   // name defaults to the method name
public async Task<string> GetForecastAsync(...) { }

kernel.ImportPluginFromType<ForecastPlugin>();     // plugin name defaults to the type name
```

Now the tool identity is derived from your identifiers: the model sees `ForecastPlugin-GetForecastAsync`. Run the obfuscator and those identifiers rename to `p0` and `a`, so the schema silently becomes:

```json
{ "function": { "name": "p0-a", "parameters": { "properties": { "a": {...}, "b": {...} } } } }
```

Nothing throws. The Kernel generates the schema from whatever the members are now called and binds responses back by the same names, so a round trip still *works*. But everything that referred to the tool by its old name is now broken:

- A prompt-template literal like `{{ForecastPlugin.GetForecastAsync $city}}` resolves against a function that no longer exists.
- System prompts, few-shot examples, and planner hints that name the tool are pointing at a ghost.
- Conversation logs and offline evals keyed on `ForecastPlugin-GetForecastAsync` quietly stop matching.

It is the classic serialization-rename bug: no error, just a contract that moved underneath everyone who depended on it. The fix is Tier 1 — pin the names with strings and the rename becomes invisible to the model.

## The Nebula configuration

Because the function identity is pinned by strings, there is almost nothing to keep. You do **not** need to preserve the plugin type or its method names — those are free to rename. The only thing reflection pulls into the schema that you must protect is **parameter names**. Scope a preserve rule to the methods that carry `[KernelFunction]` and let everything else rename. In MSBuild:

```xml
<Nebula>
  <!-- Plugin name and function names are pinned by string literals
       (the ImportPluginFromType("weather") argument and
       [KernelFunction("get_forecast")]), so the type and method
       identifiers rename freely. The one thing reflection turns into
       the model-visible schema is parameter names — keep those. -->
  <Preserve parameters="true"
            attribute="Microsoft.SemanticKernel.KernelFunctionAttribute" />
</Nebula>
```

If you drive protection from a `nebula.config.json` instead, the same intent reads as a narrow keep entry plus full transforms everywhere else:

```json
{
  "input": "bin/Release/net8.0/Acme.Ai.Plugins.dll",
  "output": "dist/Acme.Ai.Plugins.dll",
  "rename": {
    "enabled": true,
    "renameParameters": true
  },
  "stringEncryption": { "enabled": true },
  "controlFlow": { "enabled": true },
  "keep": [
    {
      "comment": "Keep parameter names on KernelFunction methods — reflection makes them the JSON tool-schema property names the model reads. Type and method names still rename.",
      "attribute": "Microsoft.SemanticKernel.KernelFunctionAttribute",
      "keepParameters": true
    }
  ]
}
```

Note what is *not* in that keep rule: `keepName` and `keepMembers` are absent, so the plugin class and its methods rename normally. Then run the protector against the compiled assembly:

```
nebula protect --config nebula.config.json
```

Build-time integration is a Nebula **Pro** feature; the free edition runs the same transforms from the CLI with caps.

## Verifying the tool schema is unchanged

The proof that obfuscation did not touch the contract is that the schema the model receives is byte-for-byte the same before and after. Semantic Kernel exposes it without a network call through `KernelFunctionMetadata`, so you can assert on it directly:

```csharp
var fn = kernel.Plugins["weather"]["get_forecast"];
var md = fn.Metadata;

Console.WriteLine(md.Name);                       // get_forecast
Console.WriteLine(string.Join(",", md.Parameters.Select(p => p.Name)));  // city,days
```

Run that against the original assembly, then against the output of `nebula protect` — with `ForecastPlugin` renamed to `p0`, `GetForecastAsync` renamed to `a`, the `Blend` helper renamed and its body flattened, and the route literals inside it encrypted. Both runs print `get_forecast` and `city,days`, because the function name came from the attribute string, the parameter names were kept, and the descriptions live in the metadata blob. The model sees an identical tool; your implementation is unreadable.

## Summary

A Semantic Kernel plugin is a serialization contract aimed at a language model. Sort every identifier into three tiers: pin the plugin and function names with string arguments so the type and method names rename freely (Tier 1); keep the parameter names and let the descriptions ride in metadata, because the model already reads them and hiding them only costs accuracy (Tier 2); and turn the full weight of renaming, control-flow flattening, and string encryption on the method bodies and helpers that are your actual IP (Tier 3). Pin the names, keep the parameters, harden the rest — and let Nebula Pro protect the implementation while the model never notices.

