UNPKG

deepsource-mcp-server

Version:
114 lines 3.33 kB
/** * @fileoverview Response builder utility for MCP tool handlers. * Provides a consistent way to build tool responses with proper error handling. * @packageDocumentation */ import { createLogger } from './logging/logger.js'; const logger = createLogger('DeepSourceMCP:ResponseBuilder'); /** * Builder class for creating consistent tool responses */ export class ToolResponseBuilder { content = []; isError = false; structuredContent; options; constructor(options = {}) { this.options = options; } /** * Create a success response builder */ static success(data, options) { const builder = new ToolResponseBuilder(options); return builder.withData(data); } /** * Create an error response builder */ static error(error, options) { const builder = new ToolResponseBuilder(options); return builder.withError(error); } /** * Add data to the response */ withData(data) { this.content.push({ type: 'text', text: JSON.stringify(data), }); if (this.options.log && this.options.toolName) { logger.info(`${this.options.toolName} response built successfully`); } return this; } /** * Add an error to the response */ withError(error) { this.isError = true; const errorMessage = error instanceof Error ? error.message : error; this.content.push({ type: 'text', text: JSON.stringify({ error: errorMessage, details: 'Operation failed', }), }); if (this.options.log && this.options.toolName) { logger.error(`${this.options.toolName} response contains error`, { error: errorMessage }); } return this; } /** * Set structured content */ withStructuredContent(content) { this.structuredContent = content; return this; } /** * Build the final response */ build() { const response = { content: this.content, isError: this.isError, }; if (this.options.structuredContent !== undefined) { response.structuredContent = this.options.structuredContent; } else if (this.structuredContent !== undefined) { response.structuredContent = this.structuredContent; } return response; } } /** * Helper function to create a success response */ export function successResponse(data, options) { return ToolResponseBuilder.success(data, options).build(); } /** * Helper function to create an error response */ export function errorResponse(error, options) { return ToolResponseBuilder.error(error, options).build(); } /** * Helper function to parse and validate a tool response */ export function parseToolResponse(response, validator) { if (response.isError) { const errorData = JSON.parse(response.content[0].text); throw new Error(errorData.error || 'Unknown error'); } const data = JSON.parse(response.content[0].text); if (validator && !validator(data)) { throw new Error('Response data failed validation'); } return data; } //# sourceMappingURL=response-builder.js.map