🚀 MCP代理包装器
在不更改代码的情况下,将钩子和插件添加到任何MCP服务器
一种代理包装器,在不修改原始代码的情况下,向现有的MCP服务器添加钩子、插件和自定义逻辑。
🚀 快速开始
安装
npm install mcp-proxy-wrapper适用于克劳德代码和克劳德桌面
选项1:直接服务器配置
创建一个包装好的MCP服务器,并在Claude中对其进行配置:
// server.js
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { wrapWithProxy, LLMSummarizationPlugin } from 'mcp-proxy-wrapper';
const server = new McpServer({ name: 'Enhanced Server', version: '1.0.0' });
// Add your tools
server.tool('getData', schema, getData);
// Add proxy wrapper with plugins
const plugin = new LLMSummarizationPlugin();
plugin.updateConfig({
options: {
provider: 'openai',
openaiApiKey: process.env.OPENAI_API_KEY
}
});
const enhanced = await wrapWithProxy(server, {
plugins: [plugin],
hooks: {
beforeToolCall: async (context) => {
console.log(`🔧 ${context.toolName}`);
}
}
});
// Start the server
enhanced.start(process.stdin, process.stdout);然后添加到克劳德代码中:
# Add to local project
claude mcp add enhanced-server node /path/to/server.js
# Add to all projects (user scope)
claude mcp add enhanced-server --scope user node /path/to/server.js
# Add to team projects (project scope)
claude mcp add enhanced-server --scope project node /path/to/server.js选项2:代理现有服务器
无需修改即可增强现有的MCP服务器:
// proxy-server.js
import { createStdioServerProxy, LLMSummarizationPlugin } from 'mcp-proxy-wrapper';
const plugin = new LLMSummarizationPlugin();
plugin.updateConfig({
options: { openaiApiKey: process.env.OPENAI_API_KEY }
});
// Proxy an existing server and add features
const proxy = await createStdioServerProxy({
command: 'npx',
args: ['@modelcontextprotocol/server-filesystem', '/tmp'],
plugins: [plugin],
hooks: {
beforeToolCall: async (context) => {
// Add authentication, logging, etc.
console.log(`Tool called: ${context.toolName}`);
}
}
});
proxy.start(process.stdin, process.stdout);在Claude代码中配置:
claude mcp add filesystem-enhanced node /path/to/proxy-server.js基本用法
import { wrapWithProxy, LLMSummarizationPlugin } from 'mcp-proxy-wrapper';
// Your existing server - no changes needed
const server = new McpServer({ name: 'My Server', version: '1.0.0' });
server.tool('getData', schema, getData);
// Add plugins and hooks
const plugin = new LLMSummarizationPlugin();
plugin.updateConfig({
options: {
provider: 'openai',
openaiApiKey: process.env.OPENAI_API_KEY
}
});
const enhanced = await wrapWithProxy(server, {
plugins: [plugin],
hooks: {
beforeToolCall: async (context) => {
console.log(`🔧 ${context.toolName}`);
// Add auth, rate limiting, etc.
}
}
});结果:您的服务器现在具有AI摘要、日志记录和自定义挂钩,无需任何代码更改。
✨ 主要特点
- 🔧 零代码更改 -立即包装现有服务器
- 🤖 人工智能集成 -基于OpenAI的响应摘要
- 🪝 钉钩系统 -完全控制ToolCall之前/之后
- 🔌 插件架构 -可重用、可组合的功能
- 🌐 远程服务器 -通过HTTP/WebSocket代理外部MCP服务器
- 🛡️ 身份验证和安全 -身份验证、速率限制、访问控制模式
- 📊 综合测试 -273项测试,涵盖MCP协议兼容性
📖 例子
身份验证和速率限制
const secure = await wrapWithProxy(server, {
hooks: {
beforeToolCall: async (context) => {
if (!await validateApiKey(context.args.apiKey)) {
return { result: { content: [{ type: 'text', text: 'Unauthorized' }], isError: true }};
}
}
}
});AI驱动的总结
const plugin = new LLMSummarizationPlugin();
plugin.updateConfig({
options: {
provider: 'openai',
openaiApiKey: process.env.OPENAI_API_KEY,
summarizeTools: ['research', 'analyze']
}
});
const intelligent = await wrapWithProxy(server, {
plugins: [plugin]
});远程服务器代理
// Proxy external servers and add features
const plugin = new LLMSummarizationPlugin();
plugin.updateConfig({
options: {
provider: 'openai',
openaiApiKey: process.env.OPENAI_API_KEY
}
});
const proxy = await createHttpServerProxy('https://api.example.com/mcp', {
plugins: [plugin],
hooks: { /* your custom logic */ }
});🧪 技术细节
- 273项测试通过 具有真正的MCP客户端-服务器通信
- 全面的错误处理 具有回退和适当的错误传播
- TypeScript原生 具有完全类型安全和IntelliSense
- MCP SDK v1.6.0+ 与任何现有服务器兼容
📚 文档
🤝 贡献
欢迎投稿!看 贡献指南 了解详情。
git clone https://github.com/mcp-plugins/mcp-proxy-wrapper.git
cd mcp-proxy-wrapper && npm install && npm test📄 许可证
MIT许可证-请参阅 许可证 文件以获取详细信息。
______________________________________________________________________
Built for the MCP ecosystem • Dennison Bertram
