MCP HubMCP Hub
joeymeere

emcp

by: joeymeere

A framework for building simple MCP servers with custom middleware

10created 02/03/2025
Visit
framework
middleware

📌Overview

Purpose: To provide an extended TypeScript library that serves as a near drop-in replacement for LiteMCP, featuring built-in authentication and custom middleware support.

Overview: eMCP is a fork of the LiteMCP library that enhances its functionalities while maintaining compatibility. It allows developers to implement custom authentication and middleware in their applications, offering flexibility and improved control over the request-handling process.

Key Features:

  • Comprehensive LiteMCP Functionality: Retains all existing features of LiteMCP, ensuring ease of transition for current users.

  • Built-in Authentication Handler: Simplifies the implementation of security measures by providing a default authentication mechanism that can be customized according to specific needs.

  • Custom Layered Middleware Support: Allows developers to define multiple middleware functions, enabling extensive customization of the request-response lifecycle.


eMCP

A fork of the LiteMCP TypeScript library with extended features like built-in authentication handling, and custom middleware.

Features

Designed to be a near drop-in replacement for tools like LiteMCP. All added features are currently optional.

  • All current LiteMCP features
  • Built-in authentication handler
  • Custom layered middleware support

Quickstart

Install via Bun or NPM:

npm i emcp
# or use Bun (preferred)
bun add emcp

Basic Usage

(Optional) Run the examples:

bun run example:basic
bun run example:auth
bun run example:middleware
bun run example:advanced
const server = new eMCP("mcp-server-with-auth", "1.0.0", {
  authenticationHandler: async (request) => {
    // implement your custom auth logic here
    return true;
  },
});

// Requests to this tool, or any other resource or prompt will
// require authentication governed by the handler
server.addTool({
  name: "add",
  description: "Add two numbers",
  parameters: z.object({
    a: z.number(),
    b: z.number(),
  }),
  execute: async (args) => {
    server.logger.debug("Adding two numbers", args);
    return args.a + args.b;
  },
});

Custom Middleware

const server = new eMCP("mcp-server-with-middleware", "1.0.0", {
  authenticationHandler: async (request) => {
    // implement your custom auth logic here
    return true;
  },
});

// This will time entire req -> res cycle, including middlewares
server.use(async (request, next) => {
  const startTime = Date.now();
  server.logger.debug("Request started", { method: request.method });

  // Wait for all inner middleware and the handler to complete
  const response = await next();

  const endTime = Date.now();
  server.logger.debug("Request completed", {
    method: request.method,
    duration: `${endTime - startTime}ms`,
  });

  return response;
});

How Middleware Works

Middleware in eMCP runs in order of registration. Once every middleware handler has hit its next() block, then the standard MCP procedure will occur. After the server finishes processing, middleware handlers run in reverse order for code after the next() block.

Simple flow:

<---- Request received ----
1. Middleware 1
2. Middleware 2
<---- Pre-processing done ---->
4. Server handler
<---- Post-processing start ---->
5. Middleware 2
6. Middleware 1
---- Response sent ---->

If you're familiar with frameworks like Hono, this will be familiar to you.

Roadmap

  • Ergonomic MCP<->MCP communication
  • Integration into frameworks

Why?

Because I felt like it