STARA INSIGHTS · SEO

How to Validate AI Tool Inputs and Outputs Before Use

Design AI tool input and output validation with schemas, business rules, authorization, error handling and pre-production tests.

AI tool validation has two boundaries: before execution, check each argument's structure, value, business rules and permissions; after execution, check the result's status, structure, completeness, origin and plausibility before passing it to a model or another system. JSON Schema helps validate data shape, but does not prove identity, authorization, factual correctness or the safety of text.

StaraBridge is Stara Labs R&D exploring connections between LLMs, tools, APIs, MCP and databases. The following is a proposed implementation and test design, not a claim that these features or test results already exist.

Why can valid-looking input still be wrong?

A model can generate correctly formatted arguments that refer to the wrong customer or period, or an operation beyond the user's authority. An API can also return a structurally valid response with stale, partial or cross-tenant data. Passing it directly onward can carry the mistake into an answer or subsequent action.

Hypothetical illustration: a user asks for an order status. The tool accepts an order number as a string and returns a schema-compliant status, but that order belongs to another customer. Schema validation can pass while an ownership check must deny access. This is not a StaraBridge incident or test result.

Define the input contract

The MCP tools specification describes tool metadata, including input schema, and support for output schema for structured results. JSON Schema describes data structures and constraints. Define the schema version and behavior for required fields, types, lengths, allowed values and additional properties as appropriate.

Also check size limits, item counts, date ranges, formats actually enforced by the validator and safe normalization. Do not assume every library enforces every keyword identically; specify the version and configuration and test them. Validate in a trusted server-side component, not only in a browser or prompt.

Schemas alone cannot enforce every cross-field rule. An end date preceding a start date, a document belonging to another tenant, or a business status that disallows a change requires domain checks. OWASP Input Validation distinguishes syntactic and semantic validation and recommends validating early.

Check authorization at the execution boundary

Use verified identity and server-side policy to check access to the tool, object, tenant and requested action. Do not treat model-generated identity or roles as proof of permission. For consequential operations, also check whether approval is required and whether it applies to the current action and parameters.

Validation and authorization are separate layers. A structurally valid request can still be prohibited. See preventing unauthorized AI tool execution.

Validate results before using them

Distinguish protocol or transport errors from errors returned by the tool and successful results. A success status does not prove data is complete or correct. Where an output schema exists, validate structured results against the agreed contract version. For text or mixed-format output, define suitable checks and limits; do not assume all tools provide an output schema.

Check object and tenant identity against the request, data age, units, completeness, pagination and partial-result indicators where available. If essential fields are missing or results conflict, stop dependent actions or disclose the limitation under the applicable policy. Do not silently guess missing values.

Treat response content as untrusted data. OWASP describes indirect prompt injection through external content; text can pass a schema while attempting to direct the model. Separate data from instructions, restrict tool privileges and reauthorize each action. These measures reduce risk but cannot guarantee detection of every injection.

Failures and contract changes

When input fails, return an actionable error without revealing sensitive data. When output fails validation, safely record the contract version and error code, then stop dependent processing. Do not blindly retry operations that may already have changed data; uncertain outcomes require status checks and reconciliation. See handling MCP tool failures.

A strict contract can expose changes early, but a new field or upstream change can interrupt an integration. A permissive contract tolerates change but can miss important differences. Decide how to handle additional properties, versions, compatibility testing and rollback according to business consequences.

Proposed pre-production tests

Use mock tools and synthetic or approved data. Define expected outcomes for valid input, missing fields, wrong types, out-of-range values, another tenant's object and an unauthorized user. Also test partial output, schema changes, stale data, transport errors, timeouts after writes and text containing malicious instructions.

Check that invalid or unauthorized data is stopped before execution or before onward transmission, errors are understandable, and writes are not repeated without certainty. Measure false acceptance, false rejection, additional latency and version compatibility. Record the number of cases and risk-based acceptance thresholds; repeat after schema or tool changes.

This is a proposed experiment, not a completed test. Small datasets and mocks do not prove all real failures are covered. Follow with integration and security reviews in an approved environment.

Implementation checklist

  • Define input and, where available, output schemas and versions.
  • Validate size, type, values and business rules in a trusted component.
  • Check identity, permissions, tenant and approval independently.
  • Distinguish success, errors, partial results and uncertain operation status.
  • Treat tool response text as untrusted data.
  • Test contract changes, failures, retries and latency impact.

FAQ

Does JSON Schema guarantee a correct tool result?

No. A schema checks defined constraints, not factual correctness, authorization or every business rule.

Does every MCP tool output have an output schema?

Not necessarily. Check the actual tool contract and decide how to handle outputs without a schema.

Can validation stop every prompt injection?

No. Format validation and separating instructions reduce some risks but require layered controls, limited privileges and ongoing testing.

← Kembali ke Stara Insights