New Feature: IGuardrailProvider interface for policy-based function invocation control

未关闭
#13,661 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
csharp, python

调研方向

从现有的 IAutoFunctionInvocationFilter 管道及其 .NET 和 Python 入口点开始,然后比较过滤器的注册方式,以及调用上下文和结果的表示方式。完成的标准是:为两种语言一致地定义 provider contract 和过滤器集成,包括允许、拒绝、取消和注册行为。

由索引模型根据 Issue 内容生成。

描述

Feature Request

Title: New Feature: IGuardrailProvider interface for policy-based function invocation control

Is your feature request related to a problem?

Enterprise deployments of Semantic Kernel agents need a standardized way to enforce authorization policies on function invocations -- deciding whether a tool call should proceed based on caller identity, resource scope, risk level, or organizational policy. Today this is achievable through IAutoFunctionInvocationFilter, but each team builds ad-hoc policy logic inside their filter implementations with no shared contract for pluggable policy providers.

This gap has been raised repeatedly:

  • #1409 -- "Guardrails for C#" (2023, closed with TypeChat redirect, no guardrail interface shipped)
  • #5436 -- "Filters use cases to be supported before making feature non-experimental" (explicitly lists "Function Call Approval" as a core requirement)
  • #10951 -- "Agent Invocation Filter" (2025, requesting filters that wrap agent invocations "allowing for scenarios like guardrails etc." for both .NET and Python)
  • #13556 -- "Governance Policy Filter for Semantic Kernel" (2026, proposed a governance layer, closed and redirected to microsoft/agent-governance-toolkit)
  • #12294 -- Handoff orchestration triggering OpenAI jailbreak guardrails (demonstrates the practical need for pre-invocation policy checks)
Describe the solution you'd like

A thin IGuardrailProvider interface that plugs into the existing filter pipeline. It separates the policy decision (should this call proceed?) from the filter mechanics (intercepting the pipeline).

C# (.NET)
namespace Microsoft.SemanticKernel;

/// <summary>
/// Provides policy-based authorization decisions for function invocations.
/// Implementations are registered with the Kernel and consulted by a
/// built-in AutoFunctionInvocationFilter before each tool call.
/// </summary>
public interface IGuardrailProvider
{
    /// <summary>
    /// Evaluates whether a function invocation should proceed.
    /// </summary>
    Task<GuardrailDecision> EvaluateAsync(
        GuardrailContext context,
        CancellationToken cancellationToken = default);
}

public sealed class GuardrailContext
{
    public KernelFunction Function { get; init; }
    public KernelArguments Arguments { get; init; }
    public AutoFunctionInvocationContext InvocationContext { get; init; }

    /// <summary>Optional caller identity for multi-tenant scenarios.</summary>
    public ClaimsPrincipal? Principal { get; init; }
}

public sealed class GuardrailDecision
{
    public bool IsAllowed { get; init; }
    public string? Reason { get; init; }

    public static GuardrailDecision Allow() => new() { IsAllowed = true };
    public static GuardrailDecision Deny(string reason) =>
        new() { IsAllowed = false, Reason = reason };
}
Python
from abc import ABC, abstractmethod
from dataclasses import dataclass
from semantic_kernel.filters.auto_function_invocation.auto_function_invocation_context import (
    AutoFunctionInvocationContext,
)

@dataclass
class GuardrailContext:
    function_name: str
    plugin_name: str
    arguments: dict
    invocation_context: AutoFunctionInvocationContext
    principal: dict | None = None  # Caller identity claims

@dataclass
class GuardrailDecision:
    is_allowed: bool
    reason: str | None = None

    @staticmethod
    def allow() -> "GuardrailDecision":
        return GuardrailDecision(is_allowed=True)

    @staticmethod
    def deny(reason: str) -> "GuardrailDecision":
        return GuardrailDecision(is_allowed=False, reason=reason)

class GuardrailProvider(ABC):
    @abstractmethod
    async def evaluate(self, context: GuardrailContext) -> GuardrailDecision:
        """Return whether this function invocation should proceed."""
        ...
Integration with existing filters

The provider does not replace filters -- it plugs into them. A built-in GuardrailAutoFunctionInvocationFilter consults registered providers:

public class GuardrailAutoFunctionInvocationFilter : IAutoFunctionInvocationFilter
{
    private readonly IEnumerable<IGuardrailProvider> _providers;

    public async Task OnAutoFunctionInvocationAsync(
        AutoFunctionInvocationContext context,
        Func<AutoFunctionInvocationContext, Task> next)
    {
        var guardrailContext = new GuardrailContext
        {
            Function = context.Function,
            Arguments = context.Arguments,
            InvocationContext = context
        };

        foreach (var provider in _providers)
        {
            var decision = await provider.EvaluateAsync(guardrailContext);
            if (!decision.IsAllowed)
            {
                context.Result = new FunctionResult(context.Function,
                    $"Blocked by guardrail: {decision.Reason}");
                return; // skip next -- do not invoke function
            }
        }

        await next(context);
    }
}
Registration
var kernel = Kernel.CreateBuilder()
    .AddAzureOpenAIChatCompletion(...)
    .Build();

kernel.AutoFunctionInvocationFilters.Add(
    new GuardrailAutoFunctionInvocationFilter(
        new MyOrgPolicyProvider(),
        new AzureContentSafetyProvider()
    ));
Describe alternatives you've considered
  1. Raw IAutoFunctionInvocationFilter only -- Works today, but every team re-invents the allow/deny pattern. No shared contract means no ecosystem of pluggable providers.
  2. External governance toolkit -- #13556 was redirected to microsoft/agent-governance-toolkit, but a lightweight in-kernel interface would enable third-party providers without requiring a separate toolkit dependency.
Why this fits Semantic Kernel's architecture
  • Follows the existing filter pipeline pattern (IAutoFunctionInvocationFilter, IFunctionInvocationFilter, IPromptRenderFilter)
  • Does not modify the filter contract -- composes on top of it
  • Supports the "Function Call Approval" scenario explicitly called out in #5436
  • Works for both .NET and Python with identical semantics
  • Enables the agent-level guardrails requested in #10951
Additional context

A reference implementation of this provider pattern exists in APort Agent Guardrails, which enforces passport-based tool authorization policies for AI agents. The interface proposed here is deliberately provider-agnostic so that any policy backend (Azure Content Safety, OPA, custom rules, etc.) can plug in.

主要语言
C#
星标
28.8k
派生
4.9k
平均合并
2 天 17 小时
30 天内合并 PR
48

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

microsoft/semantic-kernel 的其他 Issue

查看 microsoft/semantic-kernel 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。