Cover image
Try Now
2025-03-09

使用自定义中间件构建简单MCP服务器的框架

3 years

Works with Finder

2

Github Watches

1

Github Forks

10

Github Stars

eMCP


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

Features

This is designed to be a near drop-in replacement for tools like LiteMCP. Because of this, 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;
  },
});

// Request 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 it's next() block, then the standard MCP procedure will occur. Once the server is finished processing, then the order will run in reverse for middleware handlers with code after the next() block.

To put it simply, it looks something like this:

<---- 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, then this will be familiar to you.

Roadmap

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

Why?

Because I felt like it

相关推荐

  • NiKole Maxwell
  • I craft unique cereal names, stories, and ridiculously cute Cereal Baby images.

  • Bora Yalcin
  • Evaluator for marketplace product descriptions, checks for relevancy and keyword stuffing.

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • https://suefel.com
  • Latest advice and best practices for custom GPT development.

  • Callycode Limited
  • A geek-themed horoscope generator blending Bitcoin prices, tech jargon, and astrological whimsy.

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Khalid kalib
  • Write professional emails

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • Beniyam Berhanu
  • Therapist adept at identifying core issues and offering practical advice with images.

  • Lists Tailwind CSS classes in monospaced font

  • apappascs
  • 发现市场上最全面,最新的MCP服务器集合。该存储库充当集中式枢纽,提供了广泛的开源和专有MCP服务器目录,并提供功能,文档链接和贡献者。

  • ShrimpingIt
  • MCP系列GPIO Expander的基于Micropython I2C的操作,源自ADAFRUIT_MCP230XX

  • OffchainLabs
  • 进行以太坊的实施

  • huahuayu
  • 统一的API网关,用于将多个Etherscan样区块链Explorer API与对AI助手的模型上下文协议(MCP)支持。

  • deemkeen
  • 用电源组合控制您的MBOT2:MQTT+MCP+LLM

    Reviews

    3 (1)
    Avatar
    user_6CcYfUPZ
    2025-04-15

    I've been using Zionfhe_mcp_server_test by joyecai and it has significantly improved my experience. The product is efficient, user-friendly, and seamlessly integrates with my workflow. Highly recommend for anyone in need of a reliable MCP application. Check it out at the given link!