SKILL: ImageGen MCP + Semantic Kernel Integration Guide
Purpose
This guide defines how to use ImageGenMcp.SemanticKernel.Plugin correctly from both:
- Human developers
- AI agents (Copilot, autonomous coding agents, LLM workflows)
It standardises responsibility boundaries, invocation patterns, safety rules, and expected implementation quality.
Scope
Applies to projects that:
- target
.NET 10+ - use
Microsoft.SemanticKernel - use AI image generation via the MCP server (
imagegen-mcp, fromImageGen.McpHost)
MCP Tool Surface
The ImageGen MCP server exposes three tools:
| Tool | Description |
|---|---|
image_gen_generate | Generate an image from a text prompt |
image_gen_get_credits | Get remaining provider credits |
image_gen_info | Get provider/model/output-directory info |
In expanded SK registration, each tool maps to a kernel function (e.g. imagegen_image_gen_generate).
Core Mental Model
Two valid execution modes:
- Manual invocation — call
ImageGenPlugin.GenerateImageAsync(...)directly. Deterministic and explicit. - Automatic invocation (tool-calling) — configure SK function calling (
FunctionChoiceBehavior.Auto()). The model decides when to call image generation tools.
Responsibility Boundary
| Responsibility | Owner |
|---|---|
| Connect to MCP server | Plugin |
| Discover and register tools | Plugin |
| Validate and convert parameters | Plugin |
| Invoke tools safely | Plugin |
| Host/kernel/model configuration | Client |
| Enable automatic function calling | Client |
| Prompt/chat orchestration | Client |
The plugin exposes callable functions. The client decides orchestration mode.
Required Runtime Preconditions
Before invoking tools:
imagegen-mcpis onPATH(orExecutablePathis set).- A provider key is set:
STABILITY_API_KEYorGEMINI_API_KEY. ImageGenPlugin.InitializeAsync()has completed successfully.
Standard Setup (Developer)
- Install the server:
dotnet tool install -g ImageGen.McpHost - Set your API key environment variable.
- Register services:
services.AddImageGenMcp(configuration). - Resolve
ImageGenPluginfrom DI. - Call
await plugin.InitializeAsync(). - Choose invocation mode:
- Direct:
await plugin.GenerateImageAsync("a cat") - SK expanded:
await kernel.RegisterImageGenToolsAsync(services) - SK router:
kernel.RegisterImageGenPlugin(services)
- Direct:
Standard Setup (AI Agent)
When generating code:
- Ensure DI registration exists (
AddImageGenMcp(...)). - Ensure plugin initialization is present and awaited.
- If the user asks for "automatic", configure SK execution settings for auto function calling in the client — not in the plugin.
- Keep the manual path available as a deterministic fallback.
Do not claim automatic invocation is plugin-only.
Plugin Attribute Guidance
ImageGenPlugin methods are decorated with [KernelFunction] and [Description].
If adding new callable methods:
- Add
[KernelFunction("unique_name")] - Add
[Description("...")]to the method and all parameters - Delegate validation to
InputValidator
Safety and Validation Rules
- Never bypass
InputValidator. - Never hardcode API keys in source.
- Treat tool inputs as untrusted; log through
LogSanitizer. - Prefer the typed methods (
GenerateImageAsync, etc.) over rawInvokeToolAsync.
Error Handling Policy
Catch and classify:
ConfigurationException— bad settingsProcessException—imagegen-mcpprocess failureNetworkException— transport failureTimeoutException— operation exceeded deadlineMcpServerException— server returned JSON-RPC errorProtocolException— malformed JSON-RPCImageGenMcpException— any other plugin error
Return actionable diagnostics. Do not expose API keys or internal paths.
Reliability Rules
- Use configurable timeouts (
ConnectionTimeoutSeconds,RequestTimeoutSeconds). - Use retry for transient failures only (configure
MaxRetryAttempts). - Support cancellation tokens on all async methods.
- Keep
EnableMessageLogging: falsein production.
Testing Expectations
For any feature touching invocation:
- Unit test parameter validation with
InputValidatorTests. - Unit test failure paths using
NSubstitutemocks ofIMcpClient. - Integration test at least one end-to-end tool call (opt-in, tagged
[Trait("Category","Integration")]).
Anti-Patterns
Avoid:
- Assuming SK auto-calls tools without client function-calling config.
- Calling tools before
InitializeAsync(). - Logging raw API keys or prompt text at
Informationlevel. - Writing examples that cannot compile as shown.
Quick Decision Table
| Goal | Use |
|---|---|
| Deterministic image generation | GenerateImageAsync(...) direct call |
| LLM decides when to generate | SK auto function-calling in client |
| Dynamic tool discovery | await kernel.RegisterImageGenToolsAsync(services) |
| Single plugin entry point | kernel.RegisterImageGenPlugin(services) |
| Custom tool not yet wrapped | InvokeToolAsync(toolName, args) |