UNPKG

promptrix-mcp

Version:

MCP Server for Promptrix - AI prompt enhancement and optimization for Claude Code

63 lines 2.68 kB
/** * MCP-specific API client */ import { generateRequestId, logApiRequest } from '../lib/utils.js'; export class ApiClient { apiUrl; apiKey; constructor(apiUrl) { this.apiUrl = apiUrl; this.apiKey = process.env.PROMPTRIX_API_KEY; } async enhancePrompt(args, requestId) { const reqId = requestId || generateRequestId(); const timestamp = () => new Date().toISOString(); console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Enhancing prompt: "${args.prompt.substring(0, 50)}${args.prompt.length > 50 ? '...' : ''}"`); console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Options - style: ${args.style}, format: ${args.format}, language: ${args.language}, level: ${args.level}`); const startTime = Date.now(); try { const url = `${this.apiUrl}/api/optimize`; const headers = { 'Content-Type': 'application/json', }; if (this.apiKey) { headers['Authorization'] = `Bearer ${this.apiKey}`; } console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Sending request to: ${url}`); const response = await fetch(url, { method: 'POST', headers, body: JSON.stringify(args), }); if (!response.ok) { const errorText = await response.text(); throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`); } const data = await response.json(); const responseTime = Date.now() - startTime; logApiRequest('POST', '/api/optimize', reqId, responseTime, 'MCP'); console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] API response data:`, JSON.stringify(data, null, 2)); return data; } catch (error) { const responseTime = Date.now() - startTime; console.error(`[PROMPTRIX-MCP] [${timestamp()}] [${reqId}] Enhancement error after ${responseTime}ms: ${error instanceof Error ? error.message : error}`); throw error; } } async healthCheck() { try { const url = `${this.apiUrl}/health`; const response = await fetch(url); if (!response.ok) { throw new Error(`Health check failed: ${response.status}`); } return await response.json(); } catch (error) { console.error('[PROMPTRIX-MCP] Health check error:', error); throw error; } } } //# sourceMappingURL=api-client.js.map