Read tool results
Tool results carry a human-readable summary in content and machine-readable data in structuredContent. Programmatic clients should read structuredContent.
Every Swiggy MCP tool call returns a standard MCP CallToolResult. It can carry two payloads:
| Field | What it holds | Who it's for |
|---|---|---|
structuredContent | The tool's machine-readable data - the fields listed under Output schema on each reference page. Always present on Food and Instamart; Dineout and Scenes can omit it (see Read results robustly). | Programmatic clients: your code, UIs, and anything that reads IDs, prices, or statuses. |
content | Text blocks for a language model to read or relay to a chat user. Often a natural-language summary. | Chat clients and LLMs. |
Example: get_addresses
content carries a summary for the model to relay:
Found 24 saved addresses (page 1 of 3, showing 10):
1. [Home] ...
The saved addresses are ranked for display. Suggested/preselected address ID: ...
structuredContent carries the data your code needs:
{
"addresses": [{ "id": "...", "addressLine": "...", "addressCategory": "Home" }],
"pagination": { "page": 1, "pageSize": 10, "total": 24, "hasMore": true },
"resolution": { "resolutionSource": "ranked_saved_address", "defaultAddressId": "..." }
}
Read results robustly
Food and Instamart include structuredContent on every response, including errors. Dineout and Scenes include it on most responses; when it's missing or empty, the first content block is the JSON response envelope (success, data, message, plus any tool-specific fields next to data). A small helper covers both cases. It:
- Throws on a failed call (see Errors), so your code never mistakes an error envelope for data.
- Returns
structuredContentwhen it's present. - Otherwise parses the envelope in
contentand returnsdata, with any fields the envelope carries next todatamerged in. For example, Scenes discovery tools return the resolvedlatitudeandlongitudethere, which you reuse in follow-up calls. Fields insidedatawin if a name collides.
import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
function readToolData(result: CallToolResult): any {
const structured = result.structuredContent as any;
const block = result.content?.find((b) => b.type === "text");
const text = block?.type === "text" ? block.text : undefined;
let envelope: any;
try {
envelope = text ? JSON.parse(text) : undefined;
} catch {
// Prose summary - not machine-readable.
}
// Failed call: surface the server's message instead of returning the error envelope.
const failed = (p: any) => p?.success === false && p?.error != null;
if (result.isError || failed(structured) || failed(envelope)) {
const error = structured?.error ?? envelope?.error;
throw new Error(error?.message ?? (typeof error === "string" ? error : text) ?? "Tool call failed");
}
// Preferred: machine-readable payload.
if (structured && Object.keys(structured).length > 0) return structured;
// Fallback: the JSON envelope as text. Keep fields that sit next to `data`.
if (envelope && typeof envelope === "object") {
const { success, message, data, ...siblings } = envelope;
if (data === undefined) return siblings;
return data && typeof data === "object" && !Array.isArray(data) ? { ...siblings, ...data } : data;
}
throw new Error("Tool result has no machine-readable payload");
}
const result = await client.callTool({ name: "get_addresses", arguments: {} });
const { addresses } = readToolData(result as CallToolResult);
import json
def _failed(payload):
return isinstance(payload, dict) and payload.get("success") is False and payload.get("error") is not None
def read_tool_data(result):
structured = result.structuredContent
text = next((b.text for b in result.content or [] if b.type == "text"), None)
envelope = None
if text:
try:
envelope = json.loads(text)
except json.JSONDecodeError:
pass # Prose summary - not machine-readable.
# Failed call: surface the server's message instead of returning the error envelope.
if result.isError or _failed(structured) or _failed(envelope):
error = (structured or {}).get("error") or (envelope or {}).get("error")
message = error.get("message") if isinstance(error, dict) else error
raise RuntimeError(message or text or "Tool call failed")
# Preferred: machine-readable payload.
if structured:
return structured
# Fallback: the JSON envelope as text. Keep fields that sit next to `data`.
if isinstance(envelope, dict):
siblings = {k: v for k, v in envelope.items() if k not in ("success", "message", "data")}
if "data" not in envelope:
return siblings
data = envelope["data"]
return {**siblings, **data} if isinstance(data, dict) else data
raise ValueError("Tool result has no machine-readable payload")
result = await session.call_tool("get_addresses", arguments={})
addresses = read_tool_data(result)["addresses"]
Errors
A failed call usually sets isError: true on the result. A few plain-JSON responses report failure only in the envelope ("success": false with an error), so check both - the helper above does:
- Food and Instamart:
structuredContentholds the error envelope -{ "success": false, "error": { "message": "..." } }. AreportId/reportHint, when present, sits insideerroror next to it at the top level. - Dineout and Scenes:
structuredContentis absent or empty. The firstcontenttext block carries the error, either as a plain message or as the same JSON envelope.
See Error codes for how to classify and react to each failure.
Chat clients
If an LLM consumes the results directly, you don't need to do anything: the model reads content and relays it. Widget-capable chat clients additionally render widgets from structuredContent.