API网关服务

Kong API网关服务,提供MCP特定插件的速率限制和遥测功能,适用于API管理和监控场景。

  • by 100rbh
  • Lua
  • 本地部署
  • 更新于
在 GitHub 查看 100rbh/kong-gateway-mcp-plugins

使用说明

MCP上的Kong网关设置

此目录包含一个完整的Kong API网关设置,其中集成了MCP特定的限流和遥测插件,与Apisix设置中使用的相同MCP服务器并行运行。

🏗️ 建筑学

Client → Kong Gateway → MCP Rate Limiter Plugin → MCP Telemetry Plugin → MCP Server
              ↓
        Redis (Rate Limiting) + OpenTelemetry (Telemetry)

🚀 快速入门

  1. 启动 Kong Gateway 堆栈:

    cd kong-gateway-mcp-plugins
    docker-compose up -d
    
  2. 等孔(Kong)准备好然后配置服务和路由:

    ./setup-kong.sh
    
  3. 测试设置:

    ./test-kong.sh
    

📂 目录结构

kong-gateway-mcp-plugins/
├── docker-compose.yml          # Kong stack with PostgreSQL, Redis, monitoring
├── config/
│   └── kong.conf              # Kong configuration
├── plugins/
│   ├── mcp-rate-limiter/      # Rate limiting plugin for Kong
│   │   ├── handler.lua        # Plugin logic
│   │   └── schema.lua         # Configuration schema
│   └── mcp-telemetry/         # Telemetry plugin for Kong
│       ├── handler.lua        # Plugin logic
│       └── schema.lua         # Configuration schema
├── setup-kong.sh              # Service/route/plugin configuration script
├── test-kong.sh               # End-to-end test script
└── README.md                  # This file

🔧 配置

服务和路线

安装脚本配置:

  • 服务: mcp-http-server → http://mcp-http-server:8002
  • 路线: /mcp/http/* → mcp-http-server
  • 服务: mcp-sse-server → http://mcp-sse-server:8001
  • 路线: /mcp/sse/* → mcp-sse-server

已应用插件

  1. MCP 速率限制器:

    • 每分钟3次请求的基本限制
    • 1次突发配额(总计:4次请求)
    • 基于Redis的滑动窗口
  2. MCP遥测:

    • OpenTelemetry 追踪和指标
    • 基于会话的追踪
    • 特定工具的指标
  3. CORS(跨源资源共享):

    • 支持跨域的Web客户端

🧪 测试

自动化测试

./test-kong.sh

预期输出:

Request 1: SUCCESS - Current time: 2025-01-01 12:00:00
Request 2: SUCCESS - Current time: 2025-01-01 12:00:00
Request 3: SUCCESS - Current time: 2025-01-01 12:00:00
Request 4: SUCCESS - Current time: 2025-01-01 12:00:00
Request 5: RATE LIMITED (HTTP 429) - Retry after: 60s
Request 6: RATE LIMITED (HTTP 429) - Retry after: 60s
...

手动测试

  1. 初始化MCP会话:

    curl -X POST "http://localhost:8000/mcp/http/mcp/" \
      -H "Content-Type: application/json" \
      -H "mcp-session-id: test-123" \
      -d '{
        "jsonrpc": "2.0",
        "id": 1,
        "method": "initialize",
        "params": {
          "protocolVersion": "2024-11-05",
          "capabilities": {},
          "clientInfo": {"name": "test-client", "version": "1.0.0"}
        }
      }'
    
  2. 调用MCP工具:

    curl -X POST "http://localhost:8000/mcp/http/mcp/" \
      -H "Content-Type: application/json" \
      -H "mcp-session-id: test-123" \
      -d '{
        "jsonrpc": "2.0",
        "id": 2,
        "method": "tools/call",
        "params": {
          "name": "get_current_time",
          "arguments": {}
        }
      }'
    

📊 监控

Kong 管理API

  • 管理APIhttp://localhost:8001
  • 查看服务: curl http://localhost:8001/services
  • 查看路线: curl http://localhost:8001/routes
  • 查看插件: curl http://localhost:8001/plugins

可观测性堆栈

  • **Jaeger UI(界面)**http://localhost:16686 (分布式追踪)
  • 普罗米修斯http://localhost:9090(指标)
  • **Grafana(中文可译为“格拉夫纳”或直接保留原名,根据上下文选择是否翻译)**http://localhost:3000(仪表板,管理员账号:admin/admin)

日志

# Kong logs
docker logs kong-gateway-mcp-plugins_kong_1

# Redis logs
docker logs kong-gateway-mcp-plugins_redis_1

# MCP server logs
docker logs kong-gateway-mcp-plugins_mcp-http-server_1

🔍 插件详情

MCP限速插件

  • 位置: plugins/mcp-rate-limiter/
  • 优先级2000(在请求生命周期早期运行)
  • 特点/功能:
    • 特定工具的速率限制
    • 基于Redis的滑动窗口
    • 原子操作防止竞态条件
    • 可配置的突发限制

MCP遥测插件

  • 位置: plugins/mcp-telemetry/
  • 优先权1500(限流后运行)
  • 特点/特性:
    • 基于会话的追踪
    • 特定工具的指标
    • OpenTelemetry标准合规性
    • 异步遥测导出

🆚 Kong 与 Apisix 对比

功能 Kong Apisix
数据库 PostgreSQL etcd
插件语言 Lua Lua
配置 管理员API + 数据库 YAML + etcd
演出 高 高
生态系统 企业 + 开源软件(OSS) Apache 基金会
速率限制 Redis + Lua 脚本 Redis + Lua 脚本
遥测 OpenTelemetry OpenTelemetry

两种实现都提供了相同的功能,包括基于Redis的速率限制和OpenTelemetry遥测功能。

🐛 故障排除

Kong 未启动

# Check database connection
docker logs kong-gateway-mcp-plugins_kong-migrations_1

# Check Kong logs
docker logs kong-gateway-mcp-plugins_kong_1

# Verify Kong config
docker exec kong-gateway-mcp-plugins_kong_1 kong config -c /etc/kong/kong.conf

插件未加载

# Check if plugins are installed
docker exec kong-gateway-mcp-plugins_kong_1 ls -la /opt/kong/plugins/

# Verify plugin configuration
curl http://localhost:8001/plugins

# Check Kong error logs
docker logs kong-gateway-mcp-plugins_kong_1 | grep -i error

速率限制问题

# Check Redis connectivity
docker exec kong-gateway-mcp-plugins_kong_1 nc -zv redis 6379

# Monitor Redis keys
docker exec kong-gateway-mcp-plugins_redis_1 redis-cli KEYS "mcp_rate_limit:*"

# Check plugin logs
docker logs kong-gateway-mcp-plugins_kong_1 | grep "mcp-rate-limiter"

🔄 配置更新

修改速率限制

  1. 使用管理API:

    # Get plugin ID
    PLUGIN_ID=$(curl -s http://localhost:8001/plugins | jq -r '.data[] | select(.name=="mcp-rate-limiter") | .id')
    
    # Update configuration
    curl -X PATCH "http://localhost:8001/plugins/${PLUGIN_ID}" \
      -d 'config.rate_limits.default.per_minute.requests=5'
    
  2. 重新运行设置 (将更新现有配置):

    ./setup-kong.sh
    

添加新的工具限制

更新设置脚本或使用管理员API:

curl -X PATCH "http://localhost:8001/plugins/${PLUGIN_ID}" \
  -d 'config.rate_limits.tools.new_tool.per_minute.requests=10' \
  -d 'config.rate_limits.tools.new_tool.per_minute.window=60' \
  -d 'config.rate_limits.tools.new_tool.per_minute.burst=2'

🔐 安全

  • docker-compose 中的数据库凭据(生产环境需更改)
  • 管理API已暴露在8001端口(生产环境中请限制访问)
  • MCP端点无需认证(根据需要添加Kong认证插件)
  • 未启用认证的 Redis(生产环境请添加密码)

📚 额外资源