flipped-aurora/gin-vue-admin · error

apis 参数是必需的,且至少包含一条

Error message

apis 参数是必需的,且至少包含一条

What it means

RoleAPIBatchAssigner.Handle parses 'apis' via parseAPIItems and requires the resulting list to contain at least one item. The error is thrown when 'apis' is missing, resolves to an empty array, or is an empty/whitespace string (which parseAPIItems treats as nil, hitting the length check).

Source

Thrown at server/mcp/role_api_batch_assigner.go:76

			mcp.Required(),
			mcp.Description("API列表,JSON数组[{path,method}]或逗号分隔字符串\"METHOD /path, ...\""),
		),
	)
}

func (r *RoleAPIBatchAssigner) Handle(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
	args := request.GetArguments()

	authorityID, err := parseAuthorityID(args["authorityId"])
	if err != nil {
		return nil, err
	}
	items, err := parseAPIItems(args["apis"])
	if err != nil {
		return nil, err
	}
	if len(items) == 0 {
		return nil, errors.New("apis 参数是必需的,且至少包含一条")
	}
	if len(items) > orgBatchLimit {
		return nil, fmt.Errorf("单次批量授权不能超过 %d 条,收到 %d 条", orgBatchLimit, len(items))
	}

	currentResp, err := postUpstream[map[string][]systemReq.CasbinInfo](ctx, "/casbin/getPolicyPathByAuthorityId", map[string]any{
		"authorityId": authorityID,
	})
	if err != nil {
		return nil, fmt.Errorf("获取角色当前API权限失败: %w", err)
	}

	updated := currentResp.Data["paths"]
	result := roleAPIBatchAssignResponse{Success: true, AuthorityID: authorityID}
	anyAdded := false
	for _, item := range items {
		var added bool
		updated, added = appendPolicyIfMissing(updated, item.Path, item.Method)

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Provide an 'apis' array containing at least one {path,method} object or "METHOD /path" string
  2. Verify the submitting UI/caller actually includes selected items
  3. Check len(apis) > 0 client-side before calling

Example fix

// before
args["apis"] = []
// after
args["apis"] = []any{map[string]any{"path": "/api/user/list", "method": "GET"}}
Defensive patterns

Strategy: validation

Validate before calling

const apis = args["apis"]
if (!Array.isArray(apis) || apis.length === 0) {
  throw new Error("apis 参数是必需的,且至少包含一条")
}

Type guard

function hasItems(v: unknown): v is unknown[] {
  return Array.isArray(v) && v.length > 0
}

Try / catch

try {
  await callTool("role_api_batch_assigner", { apis })
} catch (e) {
  if (String(e.message).includes("apis 参数")) {
    // 引导用户至少选择一条 API
  }
  throw e
}

Prevention

When it happens

Trigger: Calling with no 'apis', apis = [], apis = "" or " ", or apis parsing to an empty item list.

Common situations: Batch authorization flow with no APIs selected; UI submit where a method-only field was provided; upstream form sending empty string instead of omitting the field.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/82fba21de2ef7e4f. Report an issue: GitHub.