Skip to main content
Glama
server.ts2.38 kB
/** * Example 04: Separate MCP Server * * This example shows how to run the MCP server as a standalone HTTP gateway, * separate from the main Express application. This is useful for: * - Microservices architecture * - Different scaling requirements * - Security isolation */ import { ExpressMCP } from "../../src/index"; import { app } from "../shared/app"; // Start the main Express app on its own port async function startMainApp() { const PORT = process.env.APP_PORT || 3004; app.listen(PORT, () => { console.log(`✅ Main Express app running on http://localhost:${PORT}`); console.log(` - GET http://localhost:${PORT}/items`); console.log(` - POST http://localhost:${PORT}/items`); console.log(` - GET http://localhost:${PORT}/health`); }); } // Start the MCP server as a standalone HTTP gateway async function startMCPServer() { const mcp = new ExpressMCP(app, { logging: { info: (...args) => console.log("[MCP Gateway]", ...args), error: (...args) => console.error("[MCP Gateway ERROR]", ...args), }, }); // Initialize MCP await mcp.init(); // Start standalone MCP server on a different port const MCP_PORT = process.env.MCP_PORT || 7878; await mcp.startStandalone({ port: Number(MCP_PORT) }); console.log( `\n📡 MCP Gateway running separately on http://localhost:${MCP_PORT}`, ); console.log(` - GET http://localhost:${MCP_PORT}/mcp/tools`); console.log(` - POST http://localhost:${MCP_PORT}/mcp/invoke`); // Display available tools const tools = mcp.listTools(); console.log(`\n🔧 MCP Gateway serving ${tools.length} tools`); } // Start both servers async function start() { console.log("Starting servers...\n"); // Start main app await startMainApp(); // Start MCP gateway await startMCPServer(); console.log("\n🎯 Architecture:"); console.log( " Main App (port 3004) ← MCP Gateway (port 7878) ← MCP Clients", ); console.log("\n📝 Notes:"); console.log(" - Main app handles regular HTTP traffic"); console.log(" - MCP gateway handles MCP protocol traffic"); console.log(" - MCP gateway internally dispatches to main app handlers"); console.log(" - No network calls between servers (in-memory dispatch)"); } // Handle graceful shutdown process.on("SIGINT", () => { console.log("\n\nShutting down servers..."); process.exit(0); }); start().catch(console.error);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/bowen31337/expressjs_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server