Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Microsoft Semantic kernel ImportPluginFromOpenApiAsync

From Leeroopedia
Revision as of 11:39, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Microsoft_Semantic_kernel_ImportPluginFromOpenApiAsync.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Sources
Domains AI_Orchestration, Plugin_Architecture, API_Integration
Last Updated 2026-02-11 19:00 GMT

Overview

Concrete tool for dynamically generating and registering a kernel plugin from an OpenAPI specification document in Microsoft Semantic Kernel.

Description

Kernel.ImportPluginFromOpenApiAsync() is an extension method that loads an OpenAPI specification (from a file path, URI, or stream), parses it to discover REST API operations, creates a KernelPlugin with a KernelFunction for each operation, and adds the plugin to the kernel's Plugins collection. Each generated function, when invoked, constructs and sends the corresponding HTTP request with the AI-model-provided arguments.

The method has three async overloads corresponding to different specification sources:

  1. File path overload: Reads the specification from a local file.
  2. URI overload: Fetches the specification from a remote URL, supporting authentication callbacks and custom user agents.
  3. Stream overload: Parses the specification from an in-memory stream.

Additionally, a synchronous ImportPluginFromOpenApi() overload accepts a pre-parsed RestApiSpecification model.

Internally, all overloads delegate to OpenApiKernelPluginFactory which handles specification parsing, operation-to-function mapping, and HTTP client configuration. The OpenApiFunctionExecutionParameters parameter allows customization of HTTP client, authentication, server URL override, and other execution behaviors.

Usage

Use ImportPluginFromOpenApiAsync() when you have an OpenAPI specification for a REST API and want to make its endpoints available as AI-callable functions without writing native C# plugin code.

Code Reference

Source Location

  • Repository: semantic-kernel
  • File: dotnet/src/Functions/Functions.OpenApi/Extensions/OpenApiKernelExtensions.cs:L72-82

Signature (Stream Overload)

public static async Task<KernelPlugin> ImportPluginFromOpenApiAsync(
    this Kernel kernel,
    string pluginName,
    Stream stream,
    OpenApiFunctionExecutionParameters? executionParameters = null,
    CancellationToken cancellationToken = default)

Signature (File Path Overload)

public static async Task<KernelPlugin> ImportPluginFromOpenApiAsync(
    this Kernel kernel,
    string pluginName,
    string filePath,
    OpenApiFunctionExecutionParameters? executionParameters = null,
    CancellationToken cancellationToken = default)

Signature (URI Overload)

public static async Task<KernelPlugin> ImportPluginFromOpenApiAsync(
    this Kernel kernel,
    string pluginName,
    Uri uri,
    OpenApiFunctionExecutionParameters? executionParameters = null,
    CancellationToken cancellationToken = default)

Import

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.OpenApi;

I/O Contract

Inputs

Name Type Required Description
kernel Kernel Yes The kernel instance to which the plugin will be added (provided via extension method receiver).
pluginName string Yes The name to assign to the generated plugin. Must be unique within the kernel.
stream / filePath / uri Stream, string, or Uri Yes The source of the OpenAPI specification document.
executionParameters OpenApiFunctionExecutionParameters? No Optional parameters for controlling HTTP execution, authentication, server URL override, and logging.
cancellationToken CancellationToken No Token for cancelling the async operation.

Outputs

Name Type Description
return Task<KernelPlugin> The generated plugin containing one KernelFunction per OpenAPI operation. The plugin is also added to kernel.Plugins.

Usage Examples

Import from File Path

using Microsoft.SemanticKernel;

Kernel kernel = Kernel.CreateBuilder()
    .AddOpenAIChatClient(modelId: "gpt-4o", apiKey: "your-api-key")
    .Build();

// Import plugin from a local OpenAPI spec file
KernelPlugin weatherPlugin = await kernel.ImportPluginFromOpenApiAsync(
    pluginName: "WeatherApi",
    filePath: "specs/weather-api.json");

// All operations from the spec are now available as kernel functions

Import from URI with Authentication

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Plugins.OpenApi;

Kernel kernel = Kernel.CreateBuilder()
    .AddOpenAIChatClient(modelId: "gpt-4o", apiKey: "your-api-key")
    .Build();

// Configure execution parameters with authentication
var executionParams = new OpenApiFunctionExecutionParameters
{
    AuthCallback = async (request, cancellationToken) =>
    {
        request.Headers.Authorization =
            new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "your-api-token");
    }
};

KernelPlugin plugin = await kernel.ImportPluginFromOpenApiAsync(
    pluginName: "PetStore",
    uri: new Uri("https://petstore3.swagger.io/api/v3/openapi.json"),
    executionParameters: executionParams);

Import from Stream and Use with Function Calling

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;

Kernel kernel = Kernel.CreateBuilder()
    .AddOpenAIChatClient(modelId: "gpt-4o", apiKey: "your-api-key")
    .Build();

// Load spec from embedded resource or other stream source
using var stream = File.OpenRead("specs/repair-service.json");
await kernel.ImportPluginFromOpenApiAsync("RepairService", stream);

// Use with auto function calling -- the AI can now call any RepairService endpoint
OpenAIPromptExecutionSettings settings = new()
{
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};

Console.WriteLine(await kernel.InvokePromptAsync(
    "List all open repair tickets", new(settings)));

Related Pages

Implements Principle

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment