使用说明
🌐 MCP HTTP代理
一个强大的 模型上下文协议(MCP) 为AI应用程序提供HTTP请求代理的服务器。所有响应都是为AI消费而构建的,具有一致的状态、数据和错误字段。
📋 存储库信息
| 字段 | 值 |
|---|---|
| 🔗 GitHub | akhsyganesh/mcp http代理 |
| 👤 作者 | Akhshy Ganesh(akhshy.balakannan@gmail.com) |
| 📄 许可证 | MIT许可证 |
| 🔖 版本 | 1.0.0 |
| 📦 包裹 | mcp-http-proxy |
🤖 What is MCP?
这 模型上下文协议(MCP) 是一个开放标准,可实现人工智能应用程序和外部数据源之间的安全连接。该服务器实现了MCP,为AI助手提供了通过标准化接口对外部API执行HTTP操作的能力。
✨ 特性
- 🌐 HTTP请求代理:支持外部API的GET、POST、PUT、DELETE方法
- 🖥️ 卷发代理:使用系统curl命令与目标服务器通信
- 🤖 AI友好响应:带状态、HTTP代码、数据和错误字段的结构化JSON响应
- 🛡️ 智能错误处理:为常见的HTTP错误(401、403、429等)提供有用的指导
- ✅ 架构验证:使用Zod模式进行输入验证
- 🔒 安全:内置超时保护和输入净化,以防止命令注入
- 📘 TypeScript:使用MCP SDK的全类型实现
🏗️ 建筑
graph LR
A[AI Assistant] <--> B[MCP Client]
B <--> C[This MCP Server]
C <--> D[Curl]
D <--> E[Target API Server]
服务器接收MCP请求,验证输入,构造curl命令,执行它们,并返回结构化响应。
🚀 安装和设置
📦 面向最终用户(推荐)
选项1:从NPM安装(最简单)
# Install globally to use anywhere
npm install -g mcp-http-proxy
# Start the server
mcp-http-proxy
选项2:从GitHub安装
# Install directly from the repository
npm install -g git+https://github.com/akhshyganesh/mcp-http-proxy.git
# Start the server
mcp-http-proxy
选项3:与npx一起使用(无需安装)
# Run directly without installing
npx mcp-http-proxy
🛠️ 对于开发者
1.️⃣ 克隆并安装依赖关系
git clone https://github.com/akhshyganesh/mcp-http-proxy.git
cd mcp-http-proxy
npm install
2.️⃣ 构建项目
npm run build
3.️⃣ 启动服务器
npm start
# or for development with auto-reload
npm run dev
🔧 MCP客户端配置
安装后,您可以将此服务器与任何兼容MCP的客户端一起使用。以下是示例配置:
Claude桌面配置
添加到您的 claude_desktop_config.json:
{
"mcpServers": {
"mcp-http-proxy": {
"command": "mcp-http-proxy"
}
}
}
VS代码MCP扩展
添加到您的 .vscode/mcp.json:
{
"mcpServers": {
"mcp-http-proxy": {
"command": "mcp-http-proxy"
}
}
}
🛠️ 发展
📜 可用脚本
| 命令 | 描述 |
|---|---|
npm run build |
🔨 将TypeScript编译为JavaScript |
npm start |
▶️ 启动已编译的服务器 |
npm run dev |
👀 开发监视模式(更改时自动重新编译) |
npm run clean |
🧹 删除已编译的文件 |
npm test |
🧪 运行测试套件 |
npm run test:watch |
🔍 在监视模式下运行测试 |
npm run test:coverage |
📊 使用覆盖率报告运行测试 |
npm run lint |
🔍 检查代码样式和质量 |
npm run lint:fix |
🔧 自动修复掉毛问题 |
📁 项目结构
├── 📂 src/
│ ├── 📄 index.ts # Main MCP server implementation
│ └── 📂 @types/ # Custom type declarations
│ └── 📄 modelcontextprotocol__sdk.d.ts
├── 📂 tests/ # Test files
│ ├── 📄 server.test.ts # Server functionality tests
│ └── 📄 setup.ts # Test setup configuration
├── 📂 dist/ # Compiled JavaScript (generated, not tracked)
├── 📂 .vscode/
│ ├── 📄 mcp.json # VS Code MCP integration config
│ └── 📄 tasks.json # VS Code build tasks
├── 📂 .github/
│ └── 📂 workflows/
│ └── 📄 ci.yml # Continuous Integration pipeline
├── 📄 jest.config.js # Jest testing configuration
├── 📄 .eslintrc.js # ESLint configuration
├── 📄 .npmignore # NPM package exclusions
│ └── 📄 copilot-instructions.md # GitHub Copilot workspace instructions
├── 📄 test-requests.json # Sample test requests
├── 📄 test-requests.md # Test requests documentation
├── 📄 package.json # Project dependencies and scripts
├── 📄 tsconfig.json # TypeScript configuration
├── 📄 tsconfig.build.json # TypeScript build configuration
├── 📄 .gitignore # Git ignore patterns
├── 📄 LICENSE # MIT license file
└── 📄 README.md # This file
📖 用法
📥 输入架构
服务器接受具有以下结构的请求:
interface Request {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
url: string; // Valid URL
data?: Record<string, any>; // Request body (for POST/PUT)
headers?: Record<string, string>; // HTTP headers
}
📤 响应架构
所有回复均遵循以下结构:
interface Response {
status: 'ok' | 'error';
code: number; // HTTP status code
data: any; // Response body (parsed JSON if possible)
error?: string; // Error message (if status is 'error')
}
💡 例子
📋 获取请求
{
"method": "GET",
"url": "https://api.example.com/users/123",
"headers": {
"Authorization": "Bearer token123"
}
}
➕ 发布请求
{
"method": "POST",
"url": "https://api.example.com/users",
"headers": {
"Content-Type": "application/json"
},
"data": {
"name": "John Doe",
"email": "john@example.com"
}
}
✅ 成功响应
{
"status": "ok",
"code": 200,
"data": {
"id": 123,
"name": "John Doe",
"email": "john@example.com"
}
}
❌ 错误响应
{
"status": "error",
"code": 401,
"data": {
"message": "Unauthorized"
},
"error": "Authentication required (HTTP 401). The API endpoint requires authentication. Please provide an Authorization header (e.g., \"Authorization\": \"Bearer YOUR_TOKEN\" or \"Authorization\": \"Basic YOUR_CREDENTIALS\"). Check the API documentation for the correct authentication method."
}
🚨 智能错误处理
服务器为常见的HTTP错误提供了有用的指导:
| 状态 | 错误 | 指导 |
|---|---|---|
| 401 | 🔒 未经授权 | 建议添加带有示例的身份验证标头 |
| 403 | ⛔ Forbidden | 解释权限问题和故障排除步骤 |
| 429 | ⏰ 费率限制 | 建议等待并实施回退策略 |
| 400 | ❌ 错误请求 | 修复请求格式和验证问题的指南 |
| 404 | 🔍 未找到 | 帮助验证URL和资源是否存在 |
| 5xx | 🛠️ 服务器错误 | 就重试策略和临时问题提供建议 |
每个错误响应都包括具体的、可操作的指导,以帮助快速解决问题。
💻 VS代码集成
🔧 在VS代码中设置MCP
1.️⃣ 安装MCP扩展
在VS代码中安装以下MCP扩展之一:
📦 automatalabs.copilot-mcp
📦 buildwithlayer.mcp-integration-expert-eligr
📦 semanticworkbenchteam.mcp-server-vscode
2.️⃣ 配置MCP设置
这 .vscode/mcp.json 文件已配置:
{
"servers": {
"curl": {
"type": "stdio",
"command": "node",
"args": ["dist/index.js"]
}
}
}
3.️⃣ 构建和启动
npm run build
npm start
4.️⃣ 在VS代码中使用
打开VS Code的命令面板(Cmd+Shift+P)并查找与MCP相关的命令以与您的服务器交互。
🧪 使用虚拟端点进行测试
以下是一些可以用于测试的免费JSON API端点:
🏷️ JSON占位符(伪造REST API)
- 🌐 基本URL:
https://jsonplaceholder.typicode.com - 📋 特性:用户、帖子、评论、相册、照片、待办事项
🔬 测试MCP服务器的请求示例
1️⃣ GET Users
{
"method": "GET",
"url": "https://jsonplaceholder.typicode.com/users"
}
2️⃣ GET Single User
{
"method": "GET",
"url": "https://jsonplaceholder.typicode.com/users/1"
}
3️⃣ CREATE Post
{
"method": "POST",
"url": "https://jsonplaceholder.typicode.com/posts",
"headers": {
"Content-Type": "application/json"
},
"data": {
"title": "My New Post",
"body": "This is the content of my post",
"userId": 1
}
}
4️⃣ UPDATE Post
{
"method": "PUT",
"url": "https://jsonplaceholder.typicode.com/posts/1",
"headers": {
"Content-Type": "application/json"
},
"data": {
"id": 1,
"title": "Updated Post Title",
"body": "Updated post content",
"userId": 1
}
}
5️⃣ DELETE Post
{
"method": "DELETE",
"url": "https://jsonplaceholder.typicode.com/posts/1"
}
🌐 其他测试API
| API | 描述 | 示例端点 |
|---|---|---|
| HTTPBin | HTTP测试服务 | https://httpbin.org/get, https://httpbin.org/post |
| 需求 | 假冒用户API | https://reqres.in/api/users, https://reqres.in/api/users/2 |
🔄 VS代码使用工作流
graph TD
A[🚀 Start MCP Server] --> B[📖 Open VS Code]
B --> C[⌨️ Access MCP Commands]
C --> D[📨 Send HTTP Requests]
D --> E[👀 View JSON Responses]
A1[npm start] --> A
C1[Cmd+Shift+P → MCP] --> C
- 🚀 启动MCP服务器:运行
npm start在您的终端 - 📖 打开VS代码:打开任何文件或工作区
- ⌨️ 访问MCP命令:使用
Cmd+Shift+P→ 搜索“MCP” - 📨 发送请求:使用MCP接口通过服务器发送HTTP请求
- 👀 查看回复:请参阅VS Code中的结构化JSON响应
🧪 测试命令
该存储库包括一个全面的 test-requests.json 包含测试所有HTTP操作的示例请求的文件。有关每个测试请求的详细信息,请参阅 测试请求.md.
📋 测试覆盖范围包括:
- JSON占位符API:用户和帖子HTTP操作
- API HTTPBin:使用自定义标头进行HTTP测试
- ReqRes API:带有分页的用户API
您可以直接将这些测试请求与MCP客户端一起使用,以验证服务器功能。
🔒 安全考虑
| 安全功能 | 描述 |
|---|---|
| ✅ 输入验证 | 所有输入都使用Zod模式进行验证 |
| 🛡️ 命令注入预防 | 输入净化可防止curl命令注入攻击 |
| ⏰ 超时保护 | 内置超时(最多30秒,连接10秒)可防止挂起请求 |
| 🌐 网络接入 | 此服务器可以发出任意HTTP请求-确保正确的网络策略 |
| 🔑 标头 | 小心API键等敏感头 |
| 📊 速率限制 | 服务器包括适当处理API速率限制的指南 |
🔧 故障排除
⚠️ 常见问题
🚫 "Cannot find module dist/index.js"
解决方案:
- 跑
npm run build将TypeScript编译为JavaScript - 这
dist/目录将在构建过程中自动创建 - 注:
dist/git中没有跟踪该文件夹,因为它包含已编译的输出 - 检查一下
tsconfig.json有"outDir": "./dist"和"rootDir": "./src"
🔴 TypeScript Compilation Errors
解决方案:
确保安装了所有依赖项(npm install)
📘 MCP SDK Types
解决方案:
自定义类型声明在 src/@types/
🖥️ Curl Not Found
解决方案: 确保您的系统上安装了curl
🌐 Network Errors
解决方案: 检查目标服务器的可访问性和网络策略
🔒 Authentication Errors (401/403)
解决:
- 验证API密钥和令牌是否正确
- 检查标题格式是否正确
- 确保您的帐户具有所需的权限
⏰ Rate Limiting (429)
解决:
- 实现请求之间的延迟
- 使用指数退避策略
- 查看API文档了解费率限制政策
⏱️ Timeout Errors
解决:
- 出于安全考虑,服务器请求在30秒后超时
- 检查目标API是否响应缓慢
- 考虑端点是否需要不同的超时设置
💡 开发技巧
- 👀 使用
npm run dev开发过程中的手表模式 - 🔍 检查VS代码问题面板中的TypeScript错误
- 🚀 首先使用简单的GET请求进行测试
- ✅ 验证来自目标服务器的JSON响应
🤝 贡献
我们欢迎为改进MCP HTTP代理服务器做出贡献!以下是您如何做出贡献:
🚀 快速开始
- 🍴 分叉存储库 在GitHub上: akhsyganesh/mcp http代理
- 🌿 创建要素分支:
git checkout -b feature/your-feature-name - ✨ 进行更改 并确保它们遵循现有的代码风格
- 🧪 添加测试 如适用
- 💾 提交您的更改:
git commit -am 'Add some feature' - 📤 推到分支:
git push origin feature/your-feature-name - 🔄 提交拉取请求 通过GitHub
📋 开发指南
- ✅ 遵循TypeScript的最佳实践
- 📝 使用有意义的提交消息
- 📚 更新新功能的文档
- 🏗️ 在提交PR之前,确保所有构建都通过
📚 参考文献
- 📖 模型上下文协议文档
- 🛠️ MCP SDK参考
- 📘 TypeScript文档
- ✅ Zod模式验证
🧪 测试
运行测试
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Run tests with coverage
npm run test:coverage
测试覆盖率
测试套件包括:
- ✅ 服务器启动测试 -确保服务器启动时不会崩溃
- ✅ 可执行文件验证 -验证CLI二进制文件是否具有正确的shebang
- ✅ 模块导入测试 -确认模块可以导入
- ✅ 构建验证 -验证TypeScript编译
持续集成
GitHub自动操作:
- ✅ 在多个Node.js版本(18、20、22)上运行测试
- ✅ 在多个平台(Ubuntu、Windows、macOS)上进行测试
- ✅ 执行安全审计
- ✅ 使用ESLint验证代码质量
🔒 安全
安全功能
- ✅ 输入消毒 -所有用户输入都经过验证和消毒
- ✅ 命令注入保护 -安全卷曲指令构造
- ✅ 超时保护 -防止挂起请求
- ✅ 错误处理 -无敏感数据的安全错误消息
- ✅ 依赖关系扫描 -定期对依赖关系进行安全审计
安全审计
# Run security audit
npm audit
# Fix security issues
npm audit fix
# Check for high-severity vulnerabilities
npm audit --audit-level=high
报告安全问题
如果您发现安全问题,请:
- 不要 公开GitHub问题
- 电子邮件安全问题: akhshy.balakannan@gmail.com
- 包括有关漏洞的详细信息
- 在公开披露之前,留出时间解决问题
🤝 贡献
开发工作流程
- 分叉 存储库
- 克隆 你的叉子在当地
- 安装 依赖关系:
npm install - 构建 项目:
npm run build - 运行测试:
npm test - 进行更改 并添加测试
- Lint代码:
npm run lint:fix - 创建 pull请求
代码质量标准
- ✅ 所有代码都必须通过TypeScript编译
- ✅ 所有测试必须通过
- ✅ 代码覆盖率不应降低
- ✅ 必须遵守ESLint规则
- ✅ 安全审计必须通过
📄 许可证
该项目根据 MIT许可证 -看看 许可证 文件以获取详细信息。
🌟 由以下材料制成❤️ 靠近 Akhshy Ganesh
⭐ 如果你觉得这个repo有用,请将其标记为星号!
