jhgaylor

express-mcp-handler

Community jhgaylor
Updated

Effortlessly integrate the Model Context Protocol (MCP) into Express with lightweight stateful, stateless, and SSE middleware.

express-mcp-handler

A utility for integrating Model Context Protocol (MCP) into your Express applications with both stateful and stateless modes.

Features

  • Stateful Handler: Maintains long-lived sessions with session IDs and Server-Sent Events (SSE).
  • Stateless Handler: Handles each request in complete isolation for simple, one-off interactions.
  • Flexible and easy-to-use API that plugs directly into Express routes.

Installation

Install via npm:

npm install express-mcp-handler

Or yarn:

yarn add express-mcp-handler

Peer Dependencies

  • express >= 4.x
  • @modelcontextprotocol/sdk

Usage

Import the handlers from the package and mount them on your Express app:

import express from 'express';
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { statefulHandler, statelessHandler } from 'express-mcp-handler';

const app = express();
app.use(express.json());

Stateful Mode

Use statefulHandler to establish reusable sessions between client and server.

import { randomUUID } from 'node:crypto';

const server = new McpServer({
  name: 'my-server',
  version: '1.0.0',
});

const handlerOptions = {
  sessionIdGenerator: randomUUID,
  onSessionInitialized: (sessionId: string) => {
    console.log(`Session initialized: ${sessionId}`);
  },
  onSessionClosed: (sessionId: string) => {
    console.log(`Session closed: ${sessionId}`);
    // perform cleanup logic here
  },
  onError: (error: Error, sessionId?: string) => {
    console.error(`Error in session ${sessionId}:`, error);
  }
};

app.post('/mcp', statefulHandler(server, handlerOptions));
app.get('/mcp', statefulHandler(server, handlerOptions));
app.delete('/mcp', statefulHandler(server, handlerOptions));

app.listen(3000, () => {
  console.log('Express MCP server running on port 3000');
});

The handler will:

  • Initialize a new session on the first request (with no mcp-session-id header).
  • Return a mcp-session-id header that clients must include in subsequent requests.
  • Manage Server-Sent Events (SSE) to push messages from the server to the client.
  • Automatically clean up sessions when closed.

Stateless Mode

Use statelessHandler for one-off request handling with no session management:

app.post('/mcp', statelessHandler());

app.listen(3000, () => {
  console.log('Express Stateless MCP server running on port 3000');
});

Each request:

  • Creates a fresh transport and server instance.
  • Ensures isolation and no session tracking.
  • Suitable for simple or serverless environments.

API Reference

statefulHandler

function statefulHandler(
  server: McpServer,
  options: {
    sessionIdGenerator: () => string;
    onSessionInitialized?: (sessionId: string) => void;
    onSessionClosed?: (sessionId: string) => void;
    onError?: (error: Error, sessionId?: string) => void;
  }
): express.RequestHandler;
  • server: Instance of McpServer to handle protocol logic.
  • options.sessionIdGenerator: Function that returns a unique session ID.
  • options.onSessionInitialized (optional): Callback invoked with the new session ID.
  • options.onSessionClosed (optional): Callback invoked when a session is closed.
  • options.onError (optional): Callback invoked on errors.

statelessHandler

function statelessHandler(
  serverFactory: () => McpServer,
  options?: {
    sessionIdGenerator?: () => string;
    onClose?: (req: express.Request, res: express.Response) => void;
    onError?: (error: Error) => void;
  }
): express.RequestHandler;
  • serverFactory: Function that returns a new server instance.
  • options.sessionIdGenerator (optional): Override transport session ID generation.
  • options.onClose (optional): Callback fired when the request/response cycle ends.
  • options.onError (optional): Callback fired on errors during handling.

Development

git clone https://github.com/your-org/express-mcp-handler.git
cd express-mcp-handler
npm install
npm run build
npm test

License

MIT License

Publishing to npm

Log in to npm if you haven't already:

npm login

Publish the package to npm (will run your prepublishOnly build):

npm publish

To bump, tag, and push a new version:

npm version patch    # or minor, major
git push origin main --tags

MCP Server · Populars

MCP Server · New

    WW-AI-Lab

    Any2Markdown MCP 服务器

    一个高性能的文档转换服务器,同时支持 Model Context Protocol (MCP) 和 RESTful API 接口。将 PDF、Word 和 Excel 文档转换为 Markdown 格式,具备图片提取、页眉页脚移除和批量处理等高级功能

    Community WW-AI-Lab
    bartholomej

    CSFD API 🎬 + CSFD Export 💾 + CSFD MCP 🤖

    ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz. Now with MCP server

    Community bartholomej
    okx

    OKX Agent Trade Kit

    OKX trading MCP server — connect AI agents to spot, swap, futures, options & grid bots via the Model Context Protocol.

    Community okx
    ashwwwin

    🤖 Automation MCP

    Control your Mac with detailed mouse, keyboard, screen, and window management capabilities.

    Community ashwwwin
    RobThePCGuy

    Claude Patent Creator

    USPTO patent creation system with MCP server + Claude Code plugin. Hybrid RAG search over MPEP/USC/CFR, BigQuery access to 76M+ patents, automated 35 USC 112 compliance checks, prior art search, diagram generation. GPU-accelerated with skills and autonomous agents.

    Community RobThePCGuy