帮你快速理解、总结文档立即下载

创建自定义安全护栏策略

最近更新时间:2026-11-03 02:55:01
我的收藏

1. 接口描述

接口请求域名: tse.tencentcloudapi.com 。

创建自定义外部审核策略,并把资源绑定配置同步到 Kong Route。建议先调用 DescribeAIGWCustomGuardrailCapabilities、DescribeAIGWCustomGuardrailBindableResources 和 TestAIGWCustomGuardrailPolicy。支持模型 API、MCP Streamable HTTP、A2A Agent API;当前不开放 Dify、Custom Agent、Legacy SSE MCP。请检查 Result.Success、SyncStatus、FailedResources;部分同步失败不等于策略未创建,应按返回 PolicyId 查询并处理,避免重复创建。网关需满足最低版本 3.9.6,且已安装、启用并加载 cngw-ai-custom-guardrail。创建、修改、删除要求网关处于稳定 Running 状态,升级/回滚/撤销升级期间不能修改策略。

本产品为 tse,版本为 2020-12-07。公共参数及签名按云 API 规范配置;在 API Explorer 选择目标环境,示例正文仅为业务参数,中文诊断示例以公共参数 Language=zh-CN 为前提。响应业务字段统一位于 Response.Result,Response.RequestId 为云 API 请求 ID;HTTP 成功不代表 Result.Success=true。示例 ID、域名和鉴权值均为占位内容,须替换为实际值。

输入约束:新配置只接受 field_mapping,不再默认为 standard_v2。创建时鉴权和检测行为必须完整填写;修改为 PATCH,省略保留,显式空值不补默认值。存量 standard_v2 策略仍可查询、删除或直接停用;修改其判定配置或重新启用前需迁移到 field_mapping。条件必填由后端按完整配置校验,非法配置不写库、不下发。

默认接口请求频率限制:20次/秒。

推荐使用 API Explorer
点击调试
API Explorer 提供了在线调用、签名验证、SDK 代码生成和快速检索接口等能力。您可查看每次调用的请求内容和返回结果以及自动生成 SDK 调用示例。

2. 输入参数

以下请求参数列表仅列出了接口请求参数和部分公共参数,完整公共参数列表见 公共请求参数。

参数名称 必选 类型 描述
Action 是 String 公共参数,本接口取值:CreateAIGWCustomGuardrailPolicy。
Version 是 String 公共参数,本接口取值:2020-12-07。
Region 是 String 公共参数,详见产品支持的 地域列表,本接口仅支持其中的: ap-bangkok, ap-beijing, ap-chengdu, ap-guangzhou, ap-hongkong, ap-jakarta, ap-nanjing, ap-seoul, ap-shanghai, ap-shanghai-fsi, ap-shenzhen-fsi, ap-singapore, ap-tokyo, eu-frankfurt, na-ashburn, na-siliconvalley 。
GatewayId 是 String

网关实例 ID


示例值:gateway-12345678
Name 是 String

策略名称,同一网关内唯一,长度 1~64


示例值:mcp-custom-audit
ServiceConfig 是 CustomGuardrailServiceConfigInput

审核服务配置

DecisionConfig 是 CustomGuardrailDecisionConfigInput

审核响应判定配置

Scope 是 CustomGuardrailScopeInput

资源绑定范围,global 或 resource。修改时传入的是新的完整范围,不是增量追加。

AuthConfig 是 CustomGuardrailAuthConfigInput

必填,必须显式选择鉴权类型;无鉴权填写 {"AuthType":"none"}。

BehaviorConfig 是 CustomGuardrailBehaviorConfigInput

必填,Direction、HandleAction、TimeoutMs、FailureMode 四项均须显式填写。

Description 否 String

策略描述,最长 200


示例值:MCP Streamable HTTP 输入审核
Enabled 否 Boolean

是否启用策略,默认 true


示例值:true
FilterConfig 否 CustomGuardrailFilterConfigInput

过滤与采样配置

3. 输出参数

参数名称 类型 描述
Result CreateAIGWCustomGuardrailPolicyResult

接口业务结果,位于完整云 API 响应 Response.Result。存在 Success 时仍须单独判断;Response.RequestId 是本次云 API 请求标识。

RequestId String 唯一请求 ID,由服务端生成,每次请求都会返回(若请求因其他原因未能抵达服务端,则该次请求不会获得 RequestId)。定位问题时需要提供该次请求的 RequestId。

4. 示例

示例1 创建 MCP 输入审核策略(自定义映射、无鉴权)

审核引擎返回 {"decision":"block","message":"示例风险提示"} 时命中,返回 {"decision":"allow"} 时不命中。失败关闭和 5000 ms 均为显式示例值。 这是说明性示例,未执行真实资源操作;响应字段以目标环境实际部署版本为准。

输入示例

POST / HTTP/1.1
Host: tse.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: CreateAIGWCustomGuardrailPolicy
X-TC-Version: 2020-12-07
X-TC-Region: ap-guangzhou
<其他公共请求参数与 TC3 签名>

{
    "GatewayId": "gateway-12345678",
    "Name": "mcp-custom-audit",
    "Description": "MCP Streamable HTTP 输入审核",
    "Enabled": true,
    "ServiceConfig": {
        "AddressMode": "manual",
        "Endpoint": "https://audit.example.com/v2/audit",
        "SSLVerify": true,
        "QueryPassthroughEnabled": false,
        "HeaderAllowList": []
    },
    "AuthConfig": {
        "AuthType": "none"
    },
    "DecisionConfig": {
        "ContractMode": "field_mapping",
        "BlockMessageMode": "response",
        "BlockMessage": "您的请求可能包含不安全内容,已被安全策略拦截。请修改后重试。",
        "RewriteEnabled": false,
        "FieldMapping": {
            "HitFieldPath": "decision",
            "HitValue": "\"block\"",
            "MessageFieldPath": "message"
        }
    },
    "BehaviorConfig": {
        "Direction": "input",
        "HandleAction": "block",
        "TimeoutMs": 5000,
        "FailureMode": "closed"
    },
    "FilterConfig": {
        "RequestStreamMode": "all",
        "ResponseStreamMode": "all",
        "RequestKeywords": [],
        "ResponseKeywords": [],
        "KeywordMatchMode": "any",
        "KeywordCaseSensitive": false,
        "SamplingEnabled": false,
        "RequestSamplingRate": 1,
        "ResponseSamplingRate": 1
    },
    "Scope": {
        "BindType": "resource",
        "Resources": [
            {
                "ResourceType": "mcp_server",
                "ResourceId": "22222222-2222-4222-8222-222222222222"
            }
        ]
    }
}

输出示例

{
    "Response": {
        "Result": {
            "Success": true,
            "PolicyId": "11111111-1111-4111-8111-111111111111",
            "AddedResources": [
                {
                    "ResourceType": "mcp_server",
                    "ResourceId": "22222222-2222-4222-8222-222222222222"
                }
            ],
            "FailedResources": [],
            "SyncStatus": "synced"
        },
        "RequestId": "77777777-7777-4777-8777-777777777777"
    }
}

示例2 创建模型流式输出审核策略

审核输入及流式输出。若输出审核时引擎返回 content.body,值应为完整 SSE 事件文本,替换当前审核批次;不是把任意自然语言字符串直接当作模型响应。 这是说明性示例,未执行真实资源操作;响应字段以目标环境实际部署版本为准。

输入示例

POST / HTTP/1.1
Host: tse.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: CreateAIGWCustomGuardrailPolicy
X-TC-Version: 2020-12-07
X-TC-Region: ap-guangzhou
<其他公共请求参数与 TC3 签名>

{
    "GatewayId": "gateway-12345678",
    "Name": "llm-stream-audit",
    "Description": "模型 SSE 响应审核",
    "Enabled": true,
    "ServiceConfig": {
        "AddressMode": "manual",
        "Endpoint": "https://audit.example.com/v2/audit",
        "SSLVerify": true,
        "QueryPassthroughEnabled": false,
        "HeaderAllowList": []
    },
    "AuthConfig": {
        "AuthType": "none"
    },
    "DecisionConfig": {
        "ContractMode": "field_mapping",
        "BlockMessageMode": "response",
        "BlockMessage": "您的请求可能包含不安全内容,已被安全策略拦截。请修改后重试。",
        "RewriteEnabled": true,
        "FieldMapping": {
            "HitFieldPath": "decision",
            "HitValue": "\"block\"",
            "MessageFieldPath": "message",
            "RewriteFieldPath": "content.body"
        }
    },
    "BehaviorConfig": {
        "Direction": "both",
        "HandleAction": "block",
        "TimeoutMs": 5000,
        "FailureMode": "closed"
    },
    "FilterConfig": {
        "RequestStreamMode": "all",
        "ResponseStreamMode": "streaming",
        "RequestKeywords": [],
        "ResponseKeywords": [],
        "KeywordMatchMode": "any",
        "KeywordCaseSensitive": false,
        "SamplingEnabled": false,
        "RequestSamplingRate": 1,
        "ResponseSamplingRate": 1
    },
    "Scope": {
        "BindType": "resource",
        "Resources": [
            {
                "ResourceType": "model_api",
                "ResourceId": "66666666666646668666666666666666"
            }
        ]
    }
}

输出示例

{
    "Response": {
        "Result": {
            "Success": true,
            "PolicyId": "11111111-1111-4111-8111-111111111111",
            "AddedResources": [
                {
                    "ResourceType": "model_api",
                    "ResourceId": "66666666666646668666666666666666"
                }
            ],
            "FailedResources": [],
            "SyncStatus": "synced"
        },
        "RequestId": "77777777-7777-4777-8777-777777777777"
    }
}

示例3 引用已配置审核服务,创建 A2A 双向审核与输出改写

服务的 path 需已配置为审核接口路径。引擎正常响应带有 content.body 时,输出方向优先替换;输入方向只按命中规则判断是否阻断。Bearer 值使用审核引擎自己的令牌,不是网关客户端密钥。 这是说明性示例,未执行真实资源操作;响应字段以目标环境实际部署版本为准。

输入示例

POST / HTTP/1.1
Host: tse.tencentcloudapi.com
Content-Type: application/json
X-TC-Action: CreateAIGWCustomGuardrailPolicy
X-TC-Version: 2020-12-07
X-TC-Region: ap-guangzhou
<其他公共请求参数与 TC3 签名>

{
    "GatewayId": "gateway-12345678",
    "Name": "a2a-custom-audit",
    "Description": "A2A 输入输出审核",
    "Enabled": true,
    "ServiceConfig": {
        "AddressMode": "service",
        "ServiceId": "33333333-3333-4333-8333-333333333333",
        "SSLVerify": true
    },
    "AuthConfig": {
        "AuthType": "api_key",
        "HeaderName": "Authorization",
        "ApiKey": "Bearer <AUDIT_ENGINE_TOKEN>"
    },
    "DecisionConfig": {
        "ContractMode": "field_mapping",
        "BlockMessageMode": "response",
        "BlockMessage": "您的请求可能包含不安全内容,已被安全策略拦截。请修改后重试。",
        "RewriteEnabled": true,
        "FieldMapping": {
            "HitFieldPath": "decision",
            "HitValue": "\"block\"",
            "MessageFieldPath": "message",
            "RewriteFieldPath": "content.body"
        }
    },
    "BehaviorConfig": {
        "Direction": "both",
        "HandleAction": "block",
        "TimeoutMs": 5000,
        "FailureMode": "closed"
    },
    "FilterConfig": {
        "RequestStreamMode": "all",
        "ResponseStreamMode": "all",
        "RequestKeywords": [],
        "ResponseKeywords": [],
        "KeywordMatchMode": "any",
        "KeywordCaseSensitive": false,
        "SamplingEnabled": false,
        "RequestSamplingRate": 1,
        "ResponseSamplingRate": 1
    },
    "Scope": {
        "BindType": "resource",
        "Resources": [
            {
                "ResourceType": "agent_api",
                "ResourceId": "55555555-5555-4555-8555-555555555555"
            }
        ]
    }
}

输出示例

{
    "Response": {
        "Result": {
            "Success": true,
            "PolicyId": "11111111-1111-4111-8111-111111111111",
            "AddedResources": [
                {
                    "ResourceType": "agent_api",
                    "ResourceId": "55555555-5555-4555-8555-555555555555"
                }
            ],
            "FailedResources": [],
            "SyncStatus": "synced"
        },
        "RequestId": "77777777-7777-4777-8777-777777777777"
    }
}

5. 开发者资源

腾讯云 API 平台

腾讯云 API 平台 是综合 API 文档、错误码、API Explorer 及 SDK 等资源的统一查询平台,方便您从同一入口查询及使用腾讯云提供的所有 API 服务。

API Inspector

用户可通过 API Inspector 查看控制台每一步操作关联的 API 调用情况,并自动生成各语言版本的 API 代码,也可前往 API Explorer 进行在线调试。

SDK

云 API 3.0 提供了配套的开发工具集(SDK),支持多种编程语言,能更方便的调用 API。

命令行工具

6. 错误码

以下仅列出了接口业务逻辑相关的错误码,其他错误码详见 公共错误码。

错误码 描述
FailedOperation 操作失败。
FailedOperation.FailedOperation 操作失败。
FailedOperation.InternalError 操作失败,内部错误。
InternalError.InternalError 服务内部错误。
InvalidParameterValue.BadRequestFormat 请求格式不正确。
InvalidParameterValue.CreateError 无效请求参数导致创建失败。
InvalidParameterValue.InvalidParameterValue 无效的参数值。
LimitExceeded.LimitExceeded 参数超过限制。
MissingParameter.MissParameter 缺少参数。
OperationDenied.OperationDenied 不允许的操作。
ResourceInUse.CustomGuardrailGlobal ResourceInUse.CustomGuardrailGlobal
ResourceInUse.CustomGuardrailRoute ResourceInUse.CustomGuardrailRoute
ResourceNotFound 资源不存在。
ResourceNotFound.InstanceNotFound 实例不存在。
ResourceNotFound.ResourceNotFound 资源不存在。
UnauthorizedOperation 未授权操作。
UnauthorizedOperation.UnauthorizedOperation 未授权的操作。