多MCP协调代理

基于Strands Agents框架和模型上下文协议(MCP)的多MCP协调代理,支持多MCP服务器连接、OAuth 2.0认证、状态/无状态模式及交互式CLI。

  • by jief123
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 jief123/strands-mcp-hub

使用说明

Strands MCP 集线器

基于Strands代理框架并集成模型上下文协议(MCP)的多MCP编排代理。

🎯 功能

  • 多MCP支持同时连接到多个MCP服务器(HTTP/stdio)
  • OAuth 2.0 认证通用OAuth支持(Cognito、Auth0、Okta等)
  • 有状态/无状态模式选择保留对话历史或使用新查询
  • **交互式命令行界面(CLI)**具有上下文记忆的多轮对话
  • MCP 服务器模式将代理作为MCP工具暴露给其他代理
  • 灵活架构从多个MCP源中聚合工具

📋 架构

┌─────────────────────────────────────────────────────────────┐
│              External Agent / Application                    │
└─────────────────────────────────────────────────────────────┘
                           │ MCP Protocol
                           ▼
┌─────────────────────────────────────────────────────────────┐
│              my_mcp_server.py (MCP Server)                  │
│  Exposes Strands Agent as MCP tools:                        │
│  • chat_with_billing_agent                                  │
│  • chat_with_detailed_process                               │
└─────────────────────────────────────────────────────────────┘
                           │ Python Call
                           ▼
┌─────────────────────────────────────────────────────────────┐
│                    Strands MCP Hub                          │
│  ┌──────────────────────────────────────────────────────┐  │
│  │                  MCPHubAgent                         │  │
│  │  • Stateful/Stateless modes                          │  │
│  │  • Multi-MCP client management                       │  │
│  │  • Conversation history tracking                     │  │
│  └──────────────────────────────────────────────────────┘  │
│                           │                                 │
│         ┌─────────────────┼─────────────────┐              │
│         ▼                 ▼                 ▼              │
│  ┌──────────┐      ┌──────────┐      ┌──────────┐         │
│  │   MCP    │      │   MCP    │      │   MCP    │         │
│  │ Client 1 │      │ Client 2 │      │ Client 3 │         │
│  │ (OAuth)  │      │ (Bearer) │      │ (No Auth)│         │
│  └──────────┘      └──────────┘      └──────────┘         │
│         │                 │                 │              │
└─────────┼─────────────────┼─────────────────┼──────────────┘
          ▼                 ▼                 ▼
   ┌──────────┐      ┌──────────┐      ┌──────────┐
   │   MCP    │      │   MCP    │      │   MCP    │
   │ Server 1 │      │ Server 2 │      │ Server 3 │
   │(Gateway) │      │  (HTTP)  │      │ (stdio)  │
   └──────────┘      └──────────┘      └──────────┘

🚀 快速入门

1. 安装

pip install -r requirements.txt

2. 配置

创建 .env 文件:

# OAuth 2.0 Credentials
OAUTH_CLIENT_ID=your-client-id
OAUTH_CLIENT_SECRET=your-client-secret

# AWS Credentials (for Bedrock)
AWS_ACCESS_KEY_ID=your-access-key-id
AWS_SECRET_ACCESS_KEY=your-secret-access-key
AWS_DEFAULT_REGION=us-west-2

配置 config.yaml:

agent:
  model_id: "us.anthropic.claude-sonnet-4-20250514-v1:0"
  temperature: 0.1

mcp_server:
  name: "AWS Billing Analytics Agent"
  tools:
    - name: "chat_with_billing_agent"
      description: "Chat using natural language"
      method: "query"
  
mcp:
  servers:
    billing-gateway:
      type: "http"
      url: "https://your-gateway.com/mcp"
      oauth:
        client_id: "${OAUTH_CLIENT_ID}"
        client_secret: "${OAUTH_CLIENT_SECRET}"

3. 跑

交互模式 (附带对话历史):

python strands_mcp_hub.py
# or
python strands_mcp_hub.py interactive

单查询模式 (无状态)

# Simple answer
python strands_mcp_hub.py ask "Query September AWS billing"

# Detailed output with reasoning (JSON format)
python strands_mcp_hub.py ask-detailed "Analyze my cost trends"

作为MCP服务器 (无状态)

python my_mcp_server.py
# Starts MCP server at http://0.0.0.0:8000/mcp

📖 使用示例

有状态模式(交互式命令行界面)

from strands_mcp_hub import MCPHubAgent
import asyncio

async def main():
    # Create agent with conversation history
    agent = MCPHubAgent(stateless=False)
    
    # Multi-turn conversation
    response1 = await agent.query("What are my AWS costs?")
    response2 = await agent.query("How does it compare to last month?")
    # ✅ Agent remembers first query

asyncio.run(main())

无状态模式(MCP 服务器)

from strands_mcp_hub import MCPHubAgent
import asyncio

async def main():
    # Create agent without conversation history
    agent = MCPHubAgent(stateless=True)
    
    # Each query is independent
    response1 = await agent.query("What are my AWS costs?")
    response2 = await agent.query("How does it compare to last month?")
    # ❌ Agent doesn't remember first query

asyncio.run(main())

🔧 配置详情

看见 README_CN.md 翻译为中文是:“README中文版文件(Markdown格式)”。不过,通常我们不会直接翻译文件名,而是保持原名不变,仅在需要说明时提及其含义。所以,也可以简单地说这个文件是“README的中文版(Markdown格式)” 请参阅详细的配置文档。

🔑 核心组件

  • **MCPHubAgent(可译为“MCP中心代理”或根据具体上下文调整为更贴切的名称,如“MCP集线器代理”等)**主要代理类
  • OAuth令牌管理器通用OAuth 2.0令牌管理器
  • **AgentConfig(可译为“代理配置”)**带验证的配置加载器

🔐 安全

  • .env文件中的秘密永远不要提交(代码/更改) .env “to git”可以翻译为“去获取”或“去得到”,具体取决于上下文和语境。在编程或技术领域,有时“git”可能指的是使用Git版本控制系统,但在这里,“to git”更可能是一个简单的动词短语,表示去获取或得到某物
  • 令牌缓存缓存中的令牌 ~/.cache/strands-agent/
  • 环境变量使用 ${VAR} 在 config.yaml 中

📚 参考文献

📄 许可证

麻省理工学院许可证