import type { ToolResponse } from "../schemas.js";
/**
* Standard response format for MCP tool calls.
*
* This interface represents the structure of responses returned by MCP tools.
* The content array contains the text output that will be displayed to users.
*
* @property content - Array of text content blocks for the response.
* @property isError - Optional flag indicating if the tool call resulted in an error.
* @property structuredContent - Optional parsed version of the content for programmatic access.
*/
export interface ToolCallResult extends Record<string, unknown> {
readonly content: Array<{ type: "text"; text: string }>;
readonly isError?: boolean;
readonly structuredContent?: Record<string, unknown>;
}
/**
* Wrap tool data into MCP text content.
*/
export function successResponse<T>(payload: ToolResponse<T>): ToolCallResult {
return {
content: [{ type: "text", text: JSON.stringify(payload) }],
structuredContent: payload.data as Record<string, unknown>,
};
}
/**
* Wrap tool errors into MCP text content.
*/
export function errorResponse(message: string, code?: string): ToolCallResult {
return {
content: [
{
type: "text",
text: JSON.stringify({
summary: message,
error: { message, ...(code ? { code } : {}) },
}),
},
],
isError: true,
};
}