MCP服务器代理工具

一个轻量级的MCP服务器代理工具,提供统一的HTTP和STDIO接口来整合多个独立的MCP服务器。

  • by vtxf
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 vtxf/mcps-proxy

使用说明

⚠️ 项目已弃用

此项目已弃用,不再维护。请迁移到新项目:


mcps代理

一个最小化的MCP(模型上下文协议)服务器代理工具,将多个独立的MCP服务器整合到统一的HTTP和STDIO接口中。

语言: 英语 | 简体中文

📋 目录

✨ 特性

  • 🚀 极简设计 -轻量级代理专注于核心功能,依赖性最小
  • 🔌 多服务器支持 -同时连接到stdio、http和sse类型的MCP服务器
  • 📡 双界面模式 -通过HTTP API访问所有MCP功能 或 STDIO接口
  • 🌐 CORS支持 -跨源访问支持,便于web应用程序集成
  • 📝 完成日志记录 -支持文件和控制台输出的结构化日志记录
  • 🔧 零配置启动 -首次运行时自动创建默认配置
  • 🔄 模式管理 -具有模式级别启用/禁用控制的多环境配置
  • 🛡️ 错误处理 -全面的错误处理和重新连接机制
  • 📊 状态监测 -实时监控所有MCP服务器状态
  • ⚡ 性能优化 -STDIO模式提供更低的延迟和更少的资源使用

🚀 快速开始

安装

# Global installation
npm install -g mcps-proxy

# Or local installation
npm install mcps-proxy

启动服务

HTTP模式(默认)

# Start with default configuration
mcps-proxy

# Start with specific port
mcps-proxy --port 8080

# Use custom configuration file
mcps-proxy --config ./my-config.json

该服务将在 http://localhost:3095 启动后。

STDIO模式

# Start STDIO mode with default schema
mcps-proxy --stdio

# Start STDIO mode with specific schema
mcps-proxy --stdio --schema=workspace

# View help
mcps-proxy --help

STDIO模式使用JSON-RPC 2.0协议通过标准输入/输出进行通信,非常适合CLI工具集成和CI/CD管道。

📖 API使用

工具命名约定

所有工具都使用“服务器id工具名称”统一命名格式,例如:

  • filesystem-read_file
  • git-commit
  • web-search-webSearchPrime

HTTP API使用情况

获取工具列表

curl -X POST http://localhost:3095/api/default/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/list",
    "params": {},
    "id": 1
  }'

呼叫工具

curl -X POST http://localhost:3095/api/default/mcp \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "method": "tools/call",
    "params": {
      "name": "filesystem-read_file",
      "arguments": {"path": "./package.json"}
    },
    "id": 2
  }'

状态查询

curl http://localhost:3095/api/status

STDIO接口使用

STDIO模式使用JSON-RPC 2.0协议通过标准输入/输出进行通信。以下是如何使用它:

启动STDIO模式

mcps-proxy --stdio --schema=workspace

通过STDIN发送请求

{
  "jsonrpc": "2.0",
  "method": "tools/list",
  "params": {},
  "id": 1
}

通过STDOUT接收响应

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "tools": [
      {
        "name": "git-commit",
        "description": "Create a new commit",
        "inputSchema": {
          "type": "object",
          "properties": {
            "message": {"type": "string"},
            "files": {"type": "array", "items": {"type": "string"}}
          }
        }
      }
    ]
  }
}

工具调用示例

输入:

{
  "jsonrpc": "2.0",
  "method": "tools/call",
  "params": {
    "name": "filesystem-read_file",
    "arguments": {"path": "./package.json"}
  },
  "id": 2
}

输出:

{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "content": [
      {
        "type": "text",
        "text": "{\"name\": \"mcps-proxy\", \"version\": \"1.1.0\"}"
      }
    ]
  }
}

与Node.js集成

const { spawn } = require('child_process');

// Start STDIO mode
const proxy = spawn('mcps-proxy', ['--stdio', '--schema=workspace']);

// Send request
const request = {
  jsonrpc: "2.0",
  method: "tools/list",
  params: {},
  id: 1
};

proxy.stdin.write(JSON.stringify(request) + '\n');

// Receive response
proxy.stdout.on('data', (data) => {
  const response = JSON.parse(data.toString().trim());
  console.log('Tools:', response.result.tools);
});

⚙️ 配置

配置文件位置: ~/.mcps-proxy/config.json

基本配置示例

{
  "server": {
    "port": 3095,
    "host": "0.0.0.0"
  },
  "cli": {
    "stdio": {
      "encoding": "utf8",
      "delimiter": "\n",
      "timeout": 30000
    }
  },
  "schemas": {
    "default": {
      "enabled": true,
      "mcpServers": {
        "filesystem": {
          "command": "npx",
          "args": ["@modelcontextprotocol/server-filesystem", "."]
        }
      }
    },
    "workspace": {
      "enabled": true,
      "mcpServers": {
        "git": {
          "command": "npx",
          "args": ["@modelcontextprotocol/server-git", "."]
        }
      }
    }
  }
}

STDIO模式配置

这 cli.stdio 部分控制STDIO模式行为:

  • encoding -STDIO通信的字符编码(默认:“utf8”)
  • delimiter -消息分隔符(默认值:“\n”)
  • timeout -请求超时(毫秒)(默认值:30000)

支持的MCP服务器类型

  1. STDIO类型 -通过标准输入/输出进行本地流程通信
  2. HTTP类型 -通过HTTP API进行通信的远程服务器
  3. SSE类型 -服务器通过服务器发送的事件进行通信

有关详细的配置说明,请参阅 配置文档.

环境变量

配置文件支持环境变量替换:

{
  "mcpServers": {
    "web-search": {
      "type": "http",
      "headers": {
        "Authorization": "Bearer ${API_KEY}"
      }
    }
  }
}

🔧 发展

需求

  • Node.js 22+
  • TypeScript 5.0+

安装依赖项

npm install

发展模式

npm run dev

建筑

npm run build

测试

# Run tests
npm test

# Run tests with coverage report
npm run test:coverage

# Run tests in watch mode
npm run test:watch

代码质量

# Code linting
npm run lint

# Auto-fix code style
npm run lint:fix

# Code formatting
npm run format

📁 项目结构

src/
├── core/                    # Core modules
│   ├── JSONRPCHandler.ts    # JSON-RPC message handling
│   ├── HTTPServer.ts        # HTTP server
│   ├── HTTPRouter.ts        # Routing handling
│   ├── MCPConnectionManager.ts # MCP connection management
│   ├── StdioMCPServer.ts    # STDIO type MCP server
│   ├── HTTPMCPServer.ts     # HTTP type MCP server
│   ├── SSEMCPServer.ts      # SSE type MCP server
│   └── StdioProxyServer.ts  # STDIO proxy server (new)
├── types/                   # Type definitions
│   ├── MCPTypes.ts          # MCP protocol types
│   └── ConfigTypes.ts       # Configuration types
├── utils/                   # Utility functions
│   ├── Logger.ts            # Logging utilities
│   └── ConfigLoader.ts      # Configuration loader
├── interfaces/              # Interface definitions
│   ├── IMCPServer.ts        # MCP server interface
│   └── IHTTPRouter.ts       # HTTP router interface
├── applications/            # Application modes
│   ├── HTTPApplication.ts   # HTTP mode application (new)
│   └── STDIOApplication.ts  # STDIO mode application (new)
├── app.ts                   # Legacy application entry point
└── cli.ts                   # Command line interface (updated)

tests/                       # Test files
├── unit/                    # Unit tests
└── integration/             # Integration tests

docs/                        # Documentation
├── configuration.md         # Configuration documentation
├── api.md                   # API documentation
└── stdio-mode.md           # STDIO mode guide (new)

schema/                      # JSON Schema
└── config.schema.json       # Configuration file schema (updated)

openspec/                    # OpenSpec specifications
├── specs/                   # Active specifications
│   └── stdio-proxy-server/  # STDIO proxy server spec
└── changes/                 # Change proposals
    └── archive/             # Archived changes

🌐 API文档

有关API的详细文档,请参阅:

HTTP API终结点

  • GET /health -健康检查
  • GET /api/status -状态查询
  • POST /api/{schema}/mcp -MCP协议端点

STDIO接口

  • 协议:JSON-RPC 2.0
  • 输入:标准输入(stdin)
  • 输出:标准输出(stdout)
  • 沟通:以行分隔的JSON消息

支持的MCP方法

HTTP和STDIO模式都支持所有MCP方法:

  • tools/list -获取工具列表
  • tools/call -呼叫工具
  • resources/list -获取资源列表
  • resources/read -读取资源
  • prompts/list -获取提示列表
  • prompts/get -获取提示内容

🚀 部署

Docker部署

FROM node:22-alpine

WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production

COPY dist ./dist
EXPOSE 3095

CMD ["node", "dist/cli.js"]

系统服务

使用systemd创建系统服务:

[Unit]
Description=MCPS Proxy Service (DEPRECATED - Please use mcp-all-in-one)
After=network.target

[Service]
Type=simple
User=mcps-proxy
WorkingDirectory=/opt/mcps-proxy
ExecStart=/bin/sh -c "echo '⚠️ PROJECT DEPRECATED: This project has been deprecated and is no longer maintained.' && echo 'Please migrate to mcp-all-in-one instead: https://www.npmjs.com/package/mcp-all-in-one' && echo 'GitHub repository: https://github.com/vtxf/mcp-all-in-one' && echo '' && /usr/bin/node /opt/mcps-proxy/dist/cli.js"
Restart=always
RestartSec=10
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target

反向代理配置

Nginx配置示例:

server {
    listen 80;
    server_name your-domain.com;

    location / {
        proxy_pass http://localhost:3095;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

🔍 故障排除

常见问题

  1. 端口已在使用中

    # Check port usage
    netstat -tulpn | grep :3095
    # Use different port
    mcps-proxy --port 8080
    
  2. 配置文件错误

    # Check JSON format
    cat ~/.mcps-proxy/config.json | jq empty
    # Recreate default configuration
    rm ~/.mcps-proxy/config.json && mcps-proxy
    
  3. MCP服务器连接失败

    # View error logs
    tail -f ~/.mcps-proxy/logs/mcps-proxy.log
    # Check server status
    curl http://localhost:3095/api/status
    

日志文件

  • 主日志: ~/.mcps-proxy/logs/mcps-proxy.log
  • 错误日志:控制台输出

🤝 贡献

欢迎问题和拉取请求!

  1. 分叉项目
  2. 创建特征分支(git checkout -b feature/AmazingFeature)
  3. 提交更改(git commit -m 'Add some AmazingFeature')
  4. 推送到分支(git push origin feature/AmazingFeature)
  5. 打开拉取请求

📄 许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。

👨‍💻 作者

vtxf vtxf@qq.com

🙏 致谢


⭐ 如果这个项目对你有帮助,请给它一颗星!