使用说明
EXT-MCP
🧩 可扩展MCP框架
ext-mcp 介绍了两个核心概念: middleware 和 mods.
- 中间件:承载共享功能,遵循洋葱式执行模型。
- Mods:扩展功能;mod是MCP特征的集合-- 这里的功能是指MCP协议中的工具/提示/资源.
┌─────────────────────────┐
│ LLM Request │
└─────────────────────────┘
↓
╔═════════════╗
║ middleware1 ║
╚═════════════╝
↓
╔═════════════╗
║ middleware2 ║
╚═════════════╝
↓
╔═══════════════════════════╗
║ mods ║
║ ┌─────┐ ┌─────┐ ┌─────┐ ║
║ │mod1 │ │mod2 │ │mod3 │ ║
║ └─────┘ └─────┘ └─────┘ ║
╚═══════════════════════════╝
↓
╔═════════════╗
║ middleware2 ║
╚═════════════╝
↓
╔═════════════╗
║ middleware1 ║
╚═════════════╝
快速开始
🌰 请参阅 演示 目录
安装
npm i ext-mcp
应用程序入口
import path from "path";
import XMCP from "ext-mcp";
import errorHandler from "./middlewares/error-handler";
import sayGoodbye from "./mods/say-goodbye";
const app = new XMCP({
name: "my-mcp",
version: "0.0.1",
});
// 🖇️ Middleware
app.use(path.join(__dirname, "./middlewares/logger")); // file path
app.use(errorHandler); // function
app.use(require.resolve('@foo/mcp-middleware-logger')); // npm/workspace package
// 🧩 Mods
app.installMod(path.join(__dirname, "./mods/say-hello")); // file path
app.installMod(sayGoodbye); // function
app.installMod(require.resolve('@foo/mcp-mod-demo')); // npm/workspace package
// Start the service. Currently only stdio mode is supported
app.start();
中间件示例
示例:实现一个日志中间件,用于注入
logId和logger结合上下文
import { type Middleware } from "ext-mcp";
export interface LoggerContext {
logId: string;
logger: {
info: (message: string) => void;
error: (message: string) => void;
};
}
const middleware: Middleware<LoggerContext> = async (context, next) => {
context.logId = `foo-log-id`;
context.logger = createLogger(context.logId);
context.logger.info(`mcp started: ${context.actionName}`);
const res = await next();
context.logger.info(`mcp finished: ${context.actionName}`);
return res;
};
export default middleware;
Mod示例
import type { Mod, Tool } from "ext-mcp";
import { z } from "zod/v3";
const sayHello: Tool<{ name: z.ZodString }> = {
name: "say-hello",
config: {
title: "Say hello",
description: "Used for MCP mod demo",
inputSchema: { name: z.string().describe("user's name") },
},
handler: (context) => {
const { name } = context.args;
return {
content: [
{
type: "text",
text: `Hello, ${name}!`,
},
],
};
},
};
const demoMod: Mod = {
name: "demo-mod",
version: "0.0.1",
description: "Demo mod",
tools: [sayHello],
};
export default demoMod;
贡献
构建与测试
# Install dependencies
npm install
# Build
npm run build
# Test
npm test
调试演示服务
# Start the inspector. @see https://github.com/modelcontextprotocol/inspector
npx @modelcontextprotocol/inspector
检查器启动后,将绝对路径粘贴到 demo/run.sh 进入 Command 字段开始调试。
演示服务的IDE配置
此存储库包括几个IDE的配置,可以直接查看演示: