
eMCP

2025.03.09
10
TypeScript微服务通信认证处理中间件支持开发效率
eMCP是一个基于LiteMCP TS库的分支,扩展了内置认证处理和自定义中间件等功能。它旨在成为LiteMCP等工具的近乎直接替代品,所有新增功能均为可选。
View on GitHub
Overview
基本能力
产品定位
eMCP是一个用于构建和管理MCP(Microservice Communication Protocol)服务的工具库,特别适用于需要认证和中间件支持的微服务通信场景。
核心功能
- 支持所有当前LiteMCP的功能
- 内置认证处理器
- 自定义分层中间件支持
适用场景
- 需要认证的微服务通信
- 需要自定义中间件处理的微服务
- 需要与现有MCP工具兼容的项目
工具列表
addTool
: 添加自定义工具,支持参数验证和执行逻辑use
: 添加自定义中间件,支持请求和响应的预处理和后处理
常见问题解答
- 如何自定义认证逻辑?
在创建eMCP实例时,通过
authenticationHandler
参数传入自定义的认证函数。 - 中间件的执行顺序是怎样的? 中间件按注册顺序执行,请求处理完成后按相反顺序执行后处理逻辑。
使用教程
使用依赖
安装Node.js或Bun运行时环境。
安装教程
通过NPM或Bun安装eMCP:
npm i emcp
# 或使用Bun(推荐)
bun add emcp
调试方式
运行示例代码以测试基本功能:
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) => {
// 实现自定义认证逻辑
return true;
},
});
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;
},
});
自定义中间件
const server = new eMCP("mcp-server-with-middleware", "1.0.0", {
authenticationHandler: async (request) => {
// 实现自定义认证逻辑
return true;
},
});
server.use(async (request, next) => {
const startTime = Date.now();
server.logger.debug("Request started", { method: request.method });
const response = await next();
const endTime = Date.now();
server.logger.debug("Request completed", {
method: request.method,
duration: `${endTime - startTime}ms`,
});
return response;
});