UNPKG

@vapi-ai/mcp-server

Version:
68 lines (67 loc) 1.82 kB
export function createSuccessResponse(data) { return { content: [ { type: 'text', text: typeof data === 'string' ? data : JSON.stringify(data), }, ], }; } export function createErrorResponse(error) { const errorMessage = error?.message || String(error); return { content: [ { type: 'text', text: `Error: ${errorMessage}`, }, ], }; } export function createToolHandler(handler) { return async (params) => { try { const result = await handler(params); return createSuccessResponse(result); } catch (error) { return createErrorResponse(error); } }; } export function createParamsSchema(schema) { return { properties: { params: schema.shape || {}, }, required: ['params'], }; } export function createIdParamSchema(paramName, description) { return { properties: { [paramName]: { type: 'string', description }, }, required: [paramName], }; } export const withErrorHandling = (handler) => { return handler() .then((result) => createSuccessResponse(result)) .catch((error) => createErrorResponse(error)); }; export const filterResponseWithSchema = (data, schema) => { if (Array.isArray(data)) { return data.map((item) => schema.parse(item)); } return schema.parse(data); }; export const withSchemaFiltering = (handler, schema) => { return handler() .then((result) => { const filtered = filterResponseWithSchema(result, schema); return createSuccessResponse(filtered); }) .catch((error) => createErrorResponse(error)); };