Lesson 5 of 5 Building Tool Ecosystems with MCP

Validating Tool Results Before They Are Trusted

This lesson covers the structural, semantic, provenance and safety checks that validate tool results before they enter the model's context, where a wrong, malformed or malicious result could derail the whole workflow.

Learning outcomes

After completing this lesson, you will be able to:

  • Apply structural and semantic validation to tool results.
  • Check the provenance and freshness of results.
  • Apply safety checks against indirect prompt injection in tool results.

Structural and semantic validation

Structural validation checks that the result has the expected shape: required fields present, correct types, values in allowed enumerations. Pydantic models or JSON Schema validation in the tool wrapper reject malformed results. Semantic validation checks that the result makes sense for the request: the returned order ID matches the requested one, amounts are within plausible ranges, dates are not in the future when they should be in the past, and an empty result for an entity that must exist is treated as an error.

Sort into groups

Structural or semantic check?

Exam skills 2.3.4

Classify each validation check on a tool result.

Log in to try this activity Interactive activities are free with an account.

Provenance and freshness

Provenance checks confirm that the result came from the expected system and version, which matters when several sources or fallbacks are possible. Freshness checks compare timestamps with the required currency, so a cached or replicated result that is too old is flagged. Results carry their source and timestamp into the agent's context so that the agent can cite them and the trace records them.

True or false

Where a result came from and how old it is

Exam skills 2.3.4

Check provenance and freshness practices for tool results.

Log in to try this activity Interactive activities are free with an account.

Safety checks and trimming

Tool results that contain text from outside the organization (emails, web pages, documents, third-party APIs) can carry indirect prompt injection. Foundry guardrails at the tool response intervention point (preview) and Prompt Shields for documents detect such attacks before the content reaches the model; content is also delimited as data in the prompt. Large results are trimmed to the fields the agent needs, which reduces tokens and the surface for injection.

When a check fails, the wrapper rejects the result and returns a structured error, retries with another source, or flags the case for review, as the following list shows.

  • Schema failure: reject and retry once, then report a tool error.
  • Semantic failure: return a structured error so the model can correct the request.
  • Stale data: use it only if marked as stale and allowed for the task.
  • Injection detected: block the content and log a security event.
Match the pairs

When a check fails

Exam skills 2.3.4

Match each failed check with the right response.

Log in to try this activity Interactive activities are free with an account.
Worked example

A plausible but wrong result

An agent that approves supplier invoices calls a tool that returns the purchase order for an invoice. Occasionally the tool returns a different purchase order because of a database replication issue, and the agent approves the invoice against it.

Semantic validation in the tool wrapper should check that the returned purchase order references the invoice's supplier and PO number and that amounts are consistent, rejecting mismatches. A freshness check on replication timestamps adds protection. Asking the model to "double-check the purchase order" would rely on the model noticing a plausible mismatch.

Check yourself

Why trim tool results before they enter the context?

Show answer

Only the fields the agent needs should use tokens; extra data adds cost and room for injection or confusion.

Exam Tips

Remember for the exam

  • Validate structure, business meaning, provenance and freshness in the tool wrapper.
  • Scan tool responses with untrusted text for indirect prompt injection.
  • Trim results to the needed fields before they enter the context.