promptrix-mcp
Version:
MCP Server for Promptrix - AI prompt enhancement and optimization for Claude Code
98 lines • 4.48 kB
JavaScript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ErrorCode, ListToolsRequestSchema, McpError, } from "@modelcontextprotocol/sdk/types.js";
import { TOOL_DEFINITIONS } from './config/tools.js';
import { ApiClient } from './utils/api-client.js';
import { QualityAnalyzer } from './lib/quality-analyzer.js';
import { getApiUrl, getQualityThreshold } from './lib/constants.js';
import { handleEnhancePrompt, handleAutoAnalyzePrompt } from './handlers/index.js';
export class PromptEnhancerMCPServer {
server;
apiClient;
qualityAnalyzer;
apiUrl;
requestCounter;
constructor() {
this.server = new Server({
name: "promptrix-prompt-enhancer",
version: "promptrix-mcp0.1.1",
}, {
capabilities: {
tools: {},
},
});
this.apiUrl = process.env.PROMPTRIX_API || getApiUrl();
this.apiClient = new ApiClient(this.apiUrl);
this.qualityAnalyzer = new QualityAnalyzer();
this.requestCounter = 0;
console.error(`[PROMPTRIX-MCP] Initializing with API URL: ${this.apiUrl}`);
this.setupToolHandlers();
this.setupErrorHandling();
}
generateRequestId() {
return `req-${++this.requestCounter}-${Date.now()}`;
}
getTimestamp() {
return new Date().toISOString();
}
setupToolHandlers() {
this.server.setRequestHandler(ListToolsRequestSchema, async () => {
console.error(`[PROMPTRIX-MCP] Tools list requested`);
return TOOL_DEFINITIONS;
});
this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
const requestId = this.generateRequestId();
const timestamp = this.getTimestamp();
console.error(`[PROMPTRIX-MCP] [${timestamp}] [${requestId}] Tool call requested: ${name}`);
console.error(`[PROMPTRIX-MCP] [${timestamp}] [${requestId}] Arguments:`, JSON.stringify(args, null, 2));
try {
let result;
switch (name) {
case "enhance_prompt":
result = await handleEnhancePrompt(args, this.apiClient, requestId);
break;
case "auto_analyze_prompt":
result = await handleAutoAnalyzePrompt(args, this.apiClient, this.qualityAnalyzer, requestId);
break;
default:
console.error(`[PROMPTRIX-MCP] [${timestamp}] [${requestId}] ERROR: Unknown tool: ${name}`);
throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`);
}
console.error(`[PROMPTRIX-MCP] [${this.getTimestamp()}] [${requestId}] Tool call completed successfully: ${name}`);
return result;
}
catch (error) {
console.error(`[PROMPTRIX-MCP] [${this.getTimestamp()}] [${requestId}] ERROR executing tool ${name}:`, error);
if (error instanceof McpError) {
throw error;
}
throw new McpError(ErrorCode.InternalError, `Error executing tool ${name}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
});
}
setupErrorHandling() {
this.server.onerror = (error) => {
console.error("[MCP Error]", error);
};
process.on('SIGINT', async () => {
await this.server.close();
process.exit(0);
});
process.on('SIGTERM', async () => {
await this.server.close();
process.exit(0);
});
}
async run() {
const transport = new StdioServerTransport();
await this.server.connect(transport);
// Log server startup (to stderr so it doesn't interfere with MCP protocol)
const defaultThreshold = getQualityThreshold();
console.error(`AI Prompt Enhancer MCP Server v0.1.0 started`);
console.error(`API URL: ${this.apiUrl}`);
console.error(`Auto-detection threshold: ${defaultThreshold} (set via PROMPTRIX_AUTO_THRESHOLD)`);
console.error(`Available tools: enhance_prompt, auto_analyze_prompt`);
}
}
//# sourceMappingURL=server.js.map