Launch price: every exam for $29.99 a year, for the first 300 buyers. See the price

CertStreak
Menu

Implement tool result validation and quality checks

A worked example

Put in order

Your Northwind Health MCP client has already initialized a session, discovered the tools and selected a compatible catalog entry for get_appointment_capacity. The entry provides an output_schema and a fallback (contact_scheduling_desk).

You must now implement result handling. Requirements:

  1. Prefer the structured content that MCP returns. Support both SDK spellings, structuredContent and structured_content.
  2. Decode the JSON text of the first content item only when no structured content is present, as a compatibility fallback.
  3. Pass only a dictionary to schema validation. Any other extracted value must produce the catalog fallback.
  4. A result that violates the output_schema must produce the catalog fallback without contacting the server again.
  5. Programming errors and configuration errors in the validation code must remain visible. They must not be hidden behind the fallback.

Which actions should you implement, and in which order, to meet all the requirements?

Your order

You didn't answer this one.

Correct order
1Call session.call_tool with the catalog entry name and the request arguments.
2Read structuredContent from the tool result, and read structured_content if structuredContent is None.
3Only if both structured fields are None and the result has content, parse the first content item's text with json.loads.
4If the extracted value is not a dictionary, return a copy of the catalog entry's fallback.
5Validate the dictionary with jsonschema validate against the entry's output_schema. Catch only ValidationError to return the fallback, and otherwise return the result.
Why

The implementation client checks structuredContent, then structured_content, and decodes the first content item's text with json.loads only when no structured content exists. Any non-dictionary value becomes the catalog fallback. The dictionary is then validated against the catalog output_schema, and only a ValidationError is converted into the fallback. Programming and configuration errors therefore stay visible. Retrying on a schema failure or catching every exception would each break a stated requirement.

  • Step 1: Call session.call_tool with the catalog entry name and the request arguments. Every later step works on the tool result, so the call must come first.
  • Step 2: Read structuredContent from the tool result, and read structured_content if structuredContent is None. These attributes exist only on the result that the call returned. Requirement 1 makes structured content the first source to check, under either SDK spelling.
  • Step 3: Only if both structured fields are None and the result has content, parse the first content item's text with json.loads. Requirement 2 allows text decoding only when no structured content is present. That can be known only after both structured fields have been checked.
  • Step 4: If the extracted value is not a dictionary, return a copy of the catalog entry's fallback. The type check needs the final extracted value, from structured content or decoded text. Requirement 3 places it before schema validation.
  • Step 5: Validate the dictionary with jsonschema validate against the entry's output_schema. Catch only ValidationError to return the fallback, and otherwise return the result. Requirement 3 allows only dictionaries into validation. Catching only ValidationError meets requirements 4 and 5.
  • Not needed: Wrap the schema validation in an except Exception handler that returns the catalog fallback for any error. This would turn programming and configuration errors into the fallback, which breaks requirement 5. Only schema violations should become the governed fallback.
  • Not needed: When the result fails schema validation, retry session.call_tool with exponential backoff and jitter before you return the fallback. Backoff with jitter is a retry strategy for transient errors in error-handling design. Requirement 4 says a schema violation must produce the fallback without contacting the server again.
  • Not needed: Validate the extracted dictionary against the tool's inputSchema from discovery instead of the catalog output_schema. The inputSchema describes the tool's input, not its output. The implementation validates returned content against the catalog entry's output_schema, so this check would not enforce the result contract.

Practise this topic until it's automatic.

Ten questions a day, every answer explained, and misses that come back until you beat them.

Start free