/**
* Utility for formatting MCP tool responses
* Provides consistent response structure for all tools
*/
export interface McpResponse {
content: Array<{
type: 'text';
text: string;
}>;
}
export interface SuccessResponseData {
success: true;
message?: string;
data?: any;
pagination?: Record<string, any>;
}
export interface ErrorResponseData {
success: false;
error: string;
}
type ResponseData = SuccessResponseData | ErrorResponseData;
export class ResponseUtil {
/**
* Format a successful response for MCP
* @param message - Success message to display
* @param data - Response data to include
* @param pagination - Optional pagination info
*/
static success(
message: string,
data?: any,
pagination?: Record<string, any>,
): McpResponse {
const response: SuccessResponseData = {
success: true,
message,
};
if (data !== undefined) {
response.data = data;
}
if (pagination !== undefined) {
response.pagination = pagination;
}
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(response, null, 2),
},
],
};
}
/**
* Format an error response for MCP
* @param error - Error message to display
*/
static error(error: string): McpResponse {
const response: ErrorResponseData = {
success: false,
error,
};
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(response, null, 2),
},
],
};
}
/**
* Format unauthenticated response
*/
static unauthenticated(): McpResponse {
return this.error('User not authenticated. Please login first.');
}
}