Token导航 LogoToken导航TokenDH.com
MCP Vs Rest Comparison logo
AI代理未说明官方级别未说明来源级核验

MCP Vs Rest Comparison

MCP Server

提供REST和MCP两种API实现的完整可运行代码示例,包括性能测试和架构比较。

工具数

0

提示词数

0

GitHub Stars

0

资源数

0
代码示例JavaScriptAI代理

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

作者 / 组织

Amogh-2404

提供方

Amogh-2404

最后核验

2026/5/17 20:19

快速接入

先看主来源和安装命令,再打开仓库或文档;下面只保留这个条目的关键接入事实。

详细介绍

REST与MCP实现-工作代码

该目录包含Medium文章“我在3周内将我的Side项目的REST API迁移到MCP”中的完整、可运行的代码示例

目录结构

code/
├── shared/           # Shared mock database
│   └── db.js
├── rest/             # REST implementation
│   ├── server.js     # Express REST API (85 lines)
│   └── client.js     # REST client (217 lines)
├── mcp/              # MCP implementation
│   ├── server.js     # MCP server (120 lines)
│   ├── client.js     # MCP client (23 lines)
│   └── package.json  # MCP dependencies
├── benchmarks/       # Performance tests
│   └── compare.js    # REST vs MCP comparison
├── package.json      # Root dependencies
└── README.md         # This file

快速开始

先决条件

  • 已安装Node.js 18+
  • npm或纱线

安装

# Install root dependencies
npm install

# Install MCP dependencies
cd mcp && npm install && cd ..

运行示例

1.REST实现

启动REST服务器:

npm run rest:server

# Server starts on http://localhost:3000
# Get a test token: curl http://localhost:3000/auth/token

运行REST客户端(在另一个终端中):

npm run rest:client

# Output shows all CRUD operations with full error handling

API手动测试:

# Get auth token
TOKEN=$(curl -s http://localhost:3000/auth/token | jq -r .token)

# List all tasks
curl -H "Authorization: Bearer $TOKEN" http://localhost:3000/tasks

# Get specific task
curl -H "Authorization: Bearer $TOKEN" http://localhost:3000/tasks/1

# Create task
curl -X POST -H "Authorization: Bearer $TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"title":"New task","status":"todo"}' \
     http://localhost:3000/tasks

# Update task
curl -X PUT -H "Authorization: Bearer $TOKEN" \
     -H "Content-Type: application/json" \
     -d '{"status":"done"}' \
     http://localhost:3000/tasks/1

# Delete task
curl -X DELETE -H "Authorization: Bearer $TOKEN" \
     http://localhost:3000/tasks/1

2.MCP实施

运行MCP客户端(自动启动服务器):

npm run mcp:client

# Output shows:
# 1. Tool discovery
# 2. All CRUD operations
# 3. Automatic tool schema

独立运行MCP服务器:

npm run mcp:server

# Server runs on stdio (for MCP protocol)

3.绩效基准

运行比较基准:

npm run benchmark

# Output:
# - Single request latency (REST vs MCP)
# - Multi-step workflow comparison
# - Throughput analysis
# - Summary with recommendations

展示的主要差异

行数比较

组件RESTMCP缩减
服务器85行120行-41%
客户端217行23行-89%
总计302行143行-53%

代码显示了什么

REST客户端(217行)包括:

  • 具有指数回退的手动重试逻辑
  • 限速检测和处理
  • 超时管理
  • 解析和格式化时出错
  • 请求/响应序列化
  • HTTP标头管理
  • 查询字符串构建

MCP客户端(23行)得到:

  • 以上所有内容均由协议处理
  • 自动工具发现
  • 自文档化API
  • 有状态的连接(更快的工作流程)

性能结果

基于此代码中的基准:

单一请求:

  • 休息时间:~3.5ms
  • MCP:~5.2ms
  • 获胜者:REST(速度提高48%)

5步工作流程:

  • 休息时间:~18.7ms
  • MCP:约11.2ms
  • 获胜者:MCP(速度提高40%)

管理费用:

  • REST:629字节/请求
  • MCP:162字节/请求
  • 获胜者:MCP(减少74%)

体系结构比较

REST架构

Client → HTTP Request → Express Middleware → JWT Auth → Handler → Database → Response
  • 无状态(每个请求独立)
  • 基于HTTP的传输
  • 根据请求进行手动身份验证
  • 客户端处理重试/错误

MCP架构

Client → MCP Tool Call → MCP Server → Database → MCP Response
  • 有状态(持久连接)
  • JSON-RPC传输
  • 进程级身份验证
  • 协议处理重试/错误

何时使用每个

在以下情况下使用REST:

  • ✅ 构建面向公众的API
  • ✅ 需要HTTP缓存(CDN支持)
  • ✅ 简单、无状态的请求
  • ✅ 移动应用程序或浏览器(普遍支持)
  • ✅ 一次性API调用

在以下情况下使用MCP:

  • ✅ 构建具有工具访问权限的AI代理
  • ✅ 内部集成(多种服务)
  • ✅ 长时间运行的对话式工作流程
  • ✅ 安全关键(将凭据保留在本地)
  • ✅ 想要最少的客户端代码

代码质量说明

REST实现

  • ✅ 生产就绪错误处理
  • ✅ JWT身份验证
  • ✅ 使用指数回退重试逻辑
  • ✅ 速率限制支持
  • ✅ 综合录井
  • ⚠️ 很多样板

MCP实施

  • ✅ 自我记录工具
  • ✅ 自动发现
  • ✅ 干净、最少的代码
  • ✅ 协议级错误处理
  • ⚠️ 需要MCP兼容客户端
  • ⚠️ 不适合公共API

扩展代码

添加新端点(REST)

// In rest/server.js
app.get('/tasks/:id/comments', authenticateToken, (req, res) => {
  // Your logic here
});

// In rest/client.js (add 20+ lines)
async getTaskComments(taskId) {
  return this.request('GET', `/tasks/${taskId}/comments`);
}

添加新工具(MCP)

// In mcp/server.js tools list
{
  name: "get_task_comments",
  description: "Get comments for a task",
  inputSchema: { /* schema */ }
}

// In mcp/server.js tools/call handler
case "get_task_comments": {
  // Your logic here
}

// Client usage (no changes needed - auto-discovery!)
await client.callTool("get_task_comments", { taskId: 1 });

故障排除

REST服务器无法启动

# Check if port 3000 is in use
lsof -i :3000

# Kill existing process
kill -9 

# Or use a different port
PORT=3001 npm run rest:server

MCP客户端连接失败

# Make sure you're in the mcp directory or use full path
cd mcp && npm run client

# Check Node version (requires 18+)
node --version

基准误差

# Ensure REST server is NOT running before benchmarks
# The benchmark starts its own server instance

测试

该代码包括演示以下内容的工作示例:

  1. ✅ 所有CRUD操作
  2. ✅ 错误处理
  3. ✅ 身份验证(REST)
  4. ✅ 工具发现(MCP)
  5. ✅ 性能比较
  6. ✅ 实际用例

许可证

MIT许可证-免费用于您的项目

问题?

有关以下内容的详细说明,请参阅完整的Medium文章:

  • 为什么做出这些架构决策
  • Block’s Goose案例研究(企业MCP使用)
  • 安全影响
  • 何时不使用MCP
  • 迁移策略

______________________________________________________________________

文章: “我在3周内将我的副项目的REST API迁移到MCP” 作者 R.阿莫 代码库: 此目录包含文章中所有可运行的示例

目录标签

目录标签

代码示例JavaScriptAI代理API实现本地部署性能测试架构比较RESTMCP

接入字段

传输方式(transport,传输协议)

未说明

鉴权方式(authType,认证方式)

token

工具数量(toolCount,工具数)

0

资源数量(resourceCount,资源数)

0

提示词数量(promptCount,提示词数)

0

权限和风险

未说明token部署方式未说明

接入前请确认传输方式、认证方式和部署位置,并根据实际工具能力限制访问范围。

安装前确认

不要直接授予不必要的文件、网络或账号权限;先核对安装命令和配置内容。

仍需确认:installCommand

来源信息

继续浏览同类 MCP