#!/usr/bin/env node
import http from 'http';
import { CopyPasteHandler } from './handlers/copyPasteHandler.js';
import { FileSystemStorage } from './services/fileSystemStorage.js';
import { CopyPasteParams } from './types/index.js';
/**
* HTTP wrapper для clipboard MCP server для testing
*/
class ClipboardHTTPServer {
private copyPasteHandler: CopyPasteHandler;
private server: http.Server;
constructor() {
const storage = new FileSystemStorage();
this.copyPasteHandler = new CopyPasteHandler(storage);
this.server = http.createServer((req, res) => {
this.handleRequest(req, res);
});
}
private async handleRequest(req: http.IncomingMessage, res: http.ServerResponse) {
// Enable CORS for testing
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Content-Type', 'application/json');
if (req.method === 'OPTIONS') {
res.writeHead(200);
res.end();
return;
}
if (req.method !== 'POST') {
res.writeHead(405);
res.end(JSON.stringify({ error: 'Method not allowed' }));
return;
}
if (req.url !== '/copy_paste') {
res.writeHead(404);
res.end(JSON.stringify({ error: 'Endpoint not found' }));
return;
}
try {
// Read request body
const body = await this.readBody(req);
const params = JSON.parse(body) as CopyPasteParams;
// Execute operation
const result = await this.copyPasteHandler.execute(params);
// Send response
res.writeHead(200);
res.end(JSON.stringify(result, null, 2));
} catch (error) {
console.error('Server error:', error);
res.writeHead(500);
res.end(JSON.stringify({
success: false,
message: `Server error: ${error instanceof Error ? error.message : 'Unknown error'}`
}));
}
}
private async readBody(req: http.IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let body = '';
req.on('data', (chunk) => {
body += chunk.toString();
});
req.on('end', () => {
resolve(body);
});
req.on('error', (error) => {
reject(error);
});
});
}
public start(port: number = 3000): void {
this.server.listen(port, () => {
console.log(`Clipboard HTTP Server running on http://localhost:${port}`);
console.log(`Test with: curl -X POST http://localhost:${port}/copy_paste -H "Content-Type: application/json" -d '{...}'`);
});
}
public stop(): void {
this.server.close();
}
}
// Start server if run directly
if (import.meta.url === `file://${process.argv[1]}`) {
const server = new ClipboardHTTPServer();
server.start();
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\nShutting down server...');
server.stop();
process.exit(0);
});
}