View the source code for this module on GitHub: https://github.com/mcp-use/mcp-use/blob/main/libraries/typescript/packages/mcp-use/src/server/utils/response-helpers.ts
CallToolResult objects for MCP tools, resources, and prompts. Instead of constructing content arrays and _meta MIME types by hand, you call a helper that produces the correct shape and content type. Every helper is importable from mcp-use/server. The server automatically converts the returned CallToolResult to GetPromptResult for prompts and ReadResourceResult for resources, so the same helper works across all three primitives.
Functions
text
Creates a plain text content response, the most common response type for tools and resources._meta.mimeType to text/plain.
ReturnsThe text content to return
SignatureResult with a singletextcontent item andtext/plainMIME type
markdown
Creates a Markdown content response. Identical shape totext, but sets the _meta.mimeType to text/markdown so clients render it as Markdown.
ReturnsThe Markdown content to return
SignatureResult withtext/markdownMIME type metadata
html
Creates an HTML content response for web display. Returns the HTML inside atext content item and sets _meta.mimeType to text/html.
ReturnsThe HTML content to return
SignatureResult withtext/htmlMIME type metadata
xml
Creates an XML content response. Returns the XML inside atext content item and sets _meta.mimeType to text/xml.
ReturnsThe XML content to return
SignatureResult withtext/xmlMIME type metadata
css
Creates a CSS stylesheet content response. Returns the CSS inside atext content item and sets _meta.mimeType to text/css.
ReturnsThe CSS content to return
SignatureResult withtext/cssMIME type metadata
javascript
Creates a JavaScript code content response. Returns the source inside atext content item and sets _meta.mimeType to text/javascript.
ReturnsThe JavaScript content to return
SignatureResult withtext/javascriptMIME type metadata
object
Creates a JSON object response that carries both a text representation and typed structured content. The text content is the pretty-printed JSON (JSON.stringify(data, null, 2)), structuredContent holds the typed data, and _meta.mimeType is set to application/json. The generic T flows through to the result type so callers get full type inference.
If data is an array, object delegates to array internally and the result is wrapped in a data field. Note that the _meta.mimeType is only set on the non-array path.
ReturnsThe object to return as JSON. An array is forwarded toarray()
SignatureResult with pretty-printed JSON text, typedstructuredContent, andapplication/jsonMIME type
array
Creates a JSON response for an array. Because the MCPstructuredContent field must be an object, the array is wrapped in a data field. The text content is the pretty-printed JSON of the original array. Unlike the other JSON helper, array does not set a _meta.mimeType.
ReturnsThe array to return as JSON
SignatureResult whosestructuredContentis{ data }and whose text content is the pretty-printed array
Arrays are automatically wrapped in an object with a
data field. If you pass an array to object, it calls array() internally.image
Creates an image content response from base64 data or a data URL. Emits animage content item with data and mimeType, and sets _meta to { mimeType, isImage: true }. The mimeType defaults to image/png when not provided.
ReturnsThe image data (data URL or base64)MIME type of the image (default: “image/png”)
SignatureResult with animagecontent item andisImagemetadata
audio
Creates an audio content response from either base64 data or a file path. The argument is treated as a file path when it contains a path separator (/ or \) or a . and is shorter than 1000 characters. In that case the file is read asynchronously and the function returns a Promise<CallToolResult>, so you must await it. Otherwise the base64 string is used directly and a synchronous CallToolResult is returned.
When a file path is read, the MIME type is taken from mimeType if provided, otherwise inferred from the file extension. The extension map covers wav (audio/wav), mp3 (audio/mpeg), ogg (audio/ogg), m4a (audio/mp4), webm (audio/webm), flac (audio/flac), and aac (audio/aac), falling back to audio/wav for anything else. On the synchronous base64 path the MIME type defaults to audio/wav. The result emits an audio content item and sets _meta to { mimeType, isAudio: true }.
ReturnsAudio data as a base64 string, or a path to an audio fileMIME type (e.g. “audio/wav”). Defaults to “audio/wav” for base64 data, or is inferred from the file extension for file paths
SignatureSynchronous result for base64 data, or a Promise for file paths
binary
Creates a binary content response from base64-encoded data. The base64 string is placed in atext content item, _meta.mimeType is set to the provided MIME type, and _meta.isBinary is set to true. Unlike image and audio, mimeType is required.
ReturnsThe base64-encoded binary dataThe MIME type of the binary content
SignatureResult with the base64 payload andisBinarymetadata
resource
Creates an embedded resource content item. It supports two call patterns selected at runtime:- Three arguments
resource(uri, mimeType, text): builds a resource from an explicit URI, MIME type, and text.mimeTypeandtextare only added to the embedded resource when truthy. - Two arguments
resource(uri, content): when the second argument is an object with acontentproperty (a result from another helper such astextorobject), the text is read from its firsttextcontent item and the MIME type is read from its_meta.mimeType, then re-embedded underuri.
resource content item and does not set _meta.
ReturnsThe resource URIMIME type (3-arg pattern) or a helper result (2-arg pattern)Optional text content, only used in the 3-arg pattern
SignatureResult with a single embeddedresourcecontent item
mix
Combines multiple helper results into a single response. Thecontent arrays of all inputs are concatenated in order. The structuredContent objects from every input that has one are shallow-merged into a single object (added only if at least one input has structuredContent), and the _meta objects are shallow-merged the same way. Use it to return text alongside images, embedded resources, or several content items together.
ReturnsThe helper results to merge, in order
SignatureResult with the flattened content array and mergedstructuredContentand_meta
mix() merges content arrays and combines the structuredContent and _meta objects from all inputs, allowing complex multi-part responses.error
Creates an error response. SetsisError: true and returns the message as a single text content item. The return type is TypedCallToolResult<never> so it composes with tools that declare an output schema. Does not set _meta.
ReturnsThe error message
SignatureResult withisError: trueand the message as text content
widget
Creates a response for a tool that renders a widget. The widget configuration (name, invoking and invoked status, and so on) is set on the tool’swidget property at registration time. widget() carries only the runtime data described by WidgetResponseConfig.
Per SEP-1865, widget data flows through standard MCP channels: tool arguments reach the widget via ui/notifications/tool-input, and the tool result (content plus structuredContent) reaches it via ui/notifications/tool-result. There is no custom mcp-use/props sideband. The fields have different visibility:
| Field | LLM sees it? | Widget sees it? | Populated by |
|---|---|---|---|
content | Yes | Yes | output or message |
structuredContent | No | Yes (as props) | props (or data) |
_meta | No | Yes (as metadata) | metadata |
- content: if
messageis set, content is a single text item with that message. Otherwise, ifoutputhas a non-empty content array, that content is used. Otherwise content falls back to a single empty text item. - _meta: set to
metadataonly whenmetadatahas at least one key. - structuredContent: set to
output.structuredContentwhen present, otherwise to the resolved props (propsor its deprecated aliasdata) when those have at least one key.
ReturnsRuntime data for the widget
SignatureResult withstructuredContent(props),content(the LLM-visible text), and_meta(metadata)
Types
TypedCallToolResult
A typedCallToolResult that constrains structuredContent to a specific type T. Used for output-schema validation and to give callers type inference on the structured data. T must be a record type (object) to satisfy the SDK’s CallToolResult interface, and defaults to Record<string, unknown>.
The properties are listed explicitly rather than via extends Omit<CallToolResult, "structuredContent"> to avoid breaking type inference, a known interaction between Zod .passthrough() and TypeScript Omit.
PropertiesThe shape ofstructuredContent
SignatureThe content array, same shape as the SDKCallToolResultTyped structured dataError flag, same shape as the SDKCallToolResultResponse metadata, same shape as the SDKCallToolResultIndex signature permitting additional properties
WidgetResponseConfig
Runtime configuration passed towidget. It carries only runtime data, not the widget’s registration-time configuration. All fields are optional.
SignatureWidget-only data sent asstructuredContentin the tool result. The widget receives it viaui/notifications/tool-result. Per spec,structuredContentis not added to model contextDeprecated. Legacy alias forpropsResponse helper result (text(),object(), and so on) that the model sees. Summarizes the tool result for the conversationExtra metadata sent in the tool result’s_meta. The widget receives it viauseWidget().metadata. Not added to model context. Use for pagination cursors, timestamps, and so onOptional override for the text message incontent. Used when you want to show different text thanoutputprovides