Express-mcp-handler is a middleware library designed to integrate the Model Context Protocol (MCP) into Express.js applications. It facilitates seamless communication between large language models (LLMs) and external tools or data sources by providing standardized handling for MCP requests. The package is currently in version 0.3.0, indicating it is in early development and its API may evolve rapidly, though it appears actively maintained. Key differentiators include its ability to manage stateful, stateless, and Server-Sent Events (SSE) based MCP interactions, offering a flexible solution for various integration patterns. Built with TypeScript, it provides a type-safe API, enhancing reliability and developer experience. It also offers customizable options for error handling, session management, and lifecycle hooks, making it adaptable to complex application requirements, and directly plugs into existing Express routes.
npm install express-mcp-handlerVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up a basic Express server and integrate the `statelessHandler` to process Model Context Protocol (MCP) requests. It uses a server factory to create a new `McpServer` instance for each incoming request, ensuring isolated handling.
Refer to the GitHub repository and changelog for updates before upgrading. Pin exact versions for stability in production.
Run `npm install express @modelcontextprotocol/sdk zod` to satisfy peer dependency requirements.
Ensure your project's Node.js version is 18.0.0 or greater. Update your Node.js environment if necessary.
Carefully review the documentation for each handler (`statelessHandler`, `statefulHandler`, `sseHandler`) to understand whether it expects a `McpServer` instance or a function that creates one per request. The quickstart demonstrates a factory for `statelessHandler`.
Install the required peer dependencies: `npm install express @modelcontextprotocol/sdk zod`.
Ensure your project is configured for ESM (e.g., `"type": "module"` in `package.json`) and use `import` statements. If using TypeScript, ensure your `tsconfig.json` targets a modern `module` resolution (e.g., `"ES2022"` or `"Node16"`).
Convert your calling code to use ES Modules with `import` statements. If this is not feasible, consider a build step like Babel or Webpack to handle module transformations, or ensure your `package.json` specifies `"type": "module"` for relevant files.