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:
- Prefer the structured content that MCP returns. Support both SDK spellings,
structuredContentandstructured_content. - Decode the JSON text of the first content item only when no structured content is present, as a compatibility fallback.
- Pass only a dictionary to schema validation. Any other extracted value must produce the catalog fallback.
- A result that violates the
output_schemamust produce the catalog fallback without contacting the server again. - 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?
You didn't answer this one.
session.call_tool with the catalog entry name and the request arguments.structuredContent from the tool result, and read structured_content if structuredContent is None.None and the result has content, parse the first content item's text with json.loads.fallback.jsonschema validate against the entry's output_schema. Catch only ValidationError to return the fallback, and otherwise return the result.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_toolwith 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
structuredContentfrom the tool result, and readstructured_contentifstructuredContentisNone. 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
Noneand the result has content, parse the first content item's text withjson.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
jsonschemavalidateagainst the entry'soutput_schema. Catch onlyValidationErrorto return the fallback, and otherwise return the result. Requirement 3 allows only dictionaries into validation. Catching onlyValidationErrormeets requirements 4 and 5. - Not needed: Wrap the schema validation in an
except Exceptionhandler 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_toolwith 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
inputSchemafrom discovery instead of the catalogoutput_schema. TheinputSchemadescribes the tool's input, not its output. The implementation validates returned content against the catalog entry'soutput_schema, so this check would not enforce the result contract.