UNPKG

sap-b1-mcp-server

Version:

SAP Business One Service Layer MCP Server

152 lines 5.37 kB
import express from 'express'; import cors from 'cors'; export class HttpTransport { server; app; httpServer; options; constructor(server, options = {}) { this.server = server; this.options = { port: options.port || 3000, host: options.host || 'localhost', cors: options.cors ?? true, corsOptions: options.corsOptions || { origin: '*', methods: ['GET', 'POST', 'OPTIONS'], allowedHeaders: ['Content-Type', 'Authorization', 'Cache-Control'] } }; this.app = express(); this.setupMiddleware(); this.setupRoutes(); } setupMiddleware() { // Enable CORS if configured if (this.options.cors) { this.app.use(cors(this.options.corsOptions)); } // Parse JSON bodies this.app.use(express.json({ limit: '10mb' })); // Parse URL-encoded bodies this.app.use(express.urlencoded({ extended: true, limit: '10mb' })); // Add request logging middleware this.app.use((req, res, next) => { console.error(`[${new Date().toISOString()}] ${req.method} ${req.path}`); next(); }); } setupRoutes() { // Health check endpoint this.app.get('/health', (req, res) => { res.json({ status: 'healthy', service: 'SAP B1 MCP Server', timestamp: new Date().toISOString(), uptime: process.uptime() }); }); // Server info endpoint this.app.get('/info', async (req, res) => { try { res.json({ service: 'SAP B1 MCP Server', name: 'sap-b1-mcp-server', version: '1.0.0', transport: 'HTTP', endpoints: { health: '/health', info: '/info', message: '/message' } }); } catch (error) { res.status(500).json({ error: 'Failed to get server info', message: error instanceof Error ? error.message : String(error) }); } }); // Simple HTTP message endpoint for basic testing this.app.post('/message', async (req, res) => { res.json({ message: 'HTTP transport is running. For full MCP functionality, use STDIO transport.', transport: 'HTTP', note: 'This is a simplified HTTP endpoint. Full MCP protocol requires STDIO transport.' }); }); // Catch-all handler for unknown routes this.app.all('*', (req, res) => { res.status(404).json({ error: 'Not Found', message: `Route ${req.method} ${req.path} not found`, availableRoutes: ['/health', '/info', '/message'] }); }); // Error handling middleware this.app.use((err, req, res, next) => { console.error('Express error:', err); res.status(500).json({ error: 'Internal Server Error', message: err.message }); }); } /** * Start the HTTP transport */ async start() { return new Promise((resolve, reject) => { try { // Start the HTTP server this.httpServer = this.app.listen(this.options.port, this.options.host, () => { console.error(`SAP B1 MCP Server listening on http://${this.options.host}:${this.options.port}`); console.error('Available endpoints:'); console.error(` - Health: http://${this.options.host}:${this.options.port}/health`); console.error(` - Info: http://${this.options.host}:${this.options.port}/info`); console.error(` - Message: http://${this.options.host}:${this.options.port}/message`); console.error('Note: For full MCP functionality, use STDIO transport mode.'); resolve(); }); this.httpServer.on('error', (error) => { console.error('HTTP server error:', error); reject(error); }); } catch (error) { reject(error); } }); } /** * Stop the HTTP transport */ async stop() { return new Promise((resolve) => { if (this.httpServer) { console.error('SAP B1 MCP Server stopping HTTP transport...'); this.httpServer.close(() => { console.error('SAP B1 MCP Server HTTP transport stopped'); resolve(); }); } else { resolve(); } }); } /** * Get the server URL */ getUrl() { return `http://${this.options.host}:${this.options.port}`; } /** * Get the message endpoint URL for clients */ getMessageUrl() { return `${this.getUrl()}/message`; } } //# sourceMappingURL=http.js.map