跳到正文
DEV Community· Aryan Rajput·· 4 天前AI 評分42

GitLab 的 AI 工具治理:GraphQL 執行錯誤與查詢前輸入驗證

Pre-Query Input Validation vs. GraphQL Execution Errors in AI Tool Governance

AI 導讀

GitLab 在 MR !270521 中,為 MergeRequests MCP 工具加入查詢前互斥參數驗證,避免衝突篩選條件傳入 GraphQL 後變成伺服器端執行錯誤。驗證會將這類請求記錄為 bad_request,並向 Agent 提供可用於自我修正的錯誤訊息;另將 reviewer_wildcard_id 歸類為 not_a_container,以符合工具治理規則。

正文

Aryan Rajput

The Hook

When an LLM hallucinates two mutually exclusive filter arguments in a single MCP tools/call, letting those arguments pass through to your GraphQL executor turns a deterministic client-side bad_request into an opaque server-side execution fault—corrupting your AI observability SLOs and depriving the agent of actionable self-correction feedback.

The Deep Dive

Autonomous AI agents frequently explore tool schemas by combining filter parameters. When I added the reviewer_wildcard_id filter (NONE or ANY) to list_merge_requests in MR !270521 (feat(mcp): add merge request read fields and reviewer filter), I had to account for how GitLab's underlying GraphQL MergeRequestsResolver handles conflicting reviewer filters. At the GraphQL resolver layer, passing reviewerWildcardId simultaneously with reviewerUsername—or alongside scope: "review_requested"—is a domain invariant violation that aborts query execution and returns a top-level GraphQL errors array.

However, in an MCP server architecture, where a validation failure is caught determines how it is classified in production telemetry (mcp.log) and how the LLM reacts. If Mcp::Tools::MergeRequests::ListMergeRequestsService blindly forwards conflicting parameters into GitlabSchema.execute, the resulting GraphQL error is logged by the MCP handler as tool_status: error—indistinguishable from an unhandled backend exception or database timeout. Instead, in MR !270521, I implemented explicit pre-query mutual-exclusion validation inside ListMergeRequestsService before constructing the GraphQL variables hash. Whenever reviewer_wildcard_id is paired with reviewer_username or scope: review_requested, the service short-circuits immediately with Mcp::Tools::Response.error, recording tool_status: bad_request in mcp.log and returning a crisp, self-correcting error message to the agent.

At the same time, every new parameter exposed on an enterprise MCP tool must be audited against GitLab's AI tool governance rules (ee/spec/lib/ai/tool_rules/governable_tools_container_spec.rb), which enforce tenant and resource-container scoping (such as project_id or group_id) across governable AI tools. In MR !270521 (building on the MCP spec hygiene standards I helped enforce in MR !268752), I classified reviewer_wildcard_id under not_a_container in governable_tools_container_spec.rb, ensuring that the suffix _id on reviewer_wildcard_id wasn't misidentified by policy analyzers as an unscoped resource container identifier.

The GitLab Hero Angle

GitLab's Duo Agent Platform and MCP governance framework (MR !270521) combine static container-rule verification (governable_tools_container_spec.rb) with pre-query domain validation, ensuring AI tool invocations maintain clean bad_request vs. error telemetry boundaries and strict multi-tenant policy enforcement.

The Takeaway

Intercept mutually exclusive AI tool parameters at the service boundary before invoking GraphQL so client hallucinations are logged as bad_request with deterministic self-correction hints rather than polluting 5xx-equivalent server error budgets.

來源:DEV Community · dev.to