多服务器AI代理系统

一个基于模型上下文协议(MCP)的生产级多服务器AI代理系统,具备持久对话记忆和自主工具选择功能,支持多种传输协议。

  • by LeelaissakAttota
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 Leelaissakattaota/mcp-ai-agent

使用说明

🤖 具有模型上下文协议(MCP)的多服务器AI代理


📌 项目概述

该项目展示了 生产就绪的多服务器代理AI 系统 使用模型上下文协议(MCP)构建。

代理人充当 “通用接口” --就像用于AI的USB-C-- 同时连接到不同MCP服务器上的多个MCP服务器 传输协议,包括 持久对话记忆 和 自主工具选择 由GPT-5 Nano提供动力。

域名: Agent AI——多服务器MCP Agent
语言: Python 3.10+
LLM OpenAI GPT-5纳米
代理人: LangGraph与持久内存重新交互


🏗️ 建筑

┌──────────────────────────────────────────────────────┐
│           User (CLI Interface)                        │
│   "What paintings did Monet create?"                 │
│   "Show me LangChain documentation"                  │
└───────────────────────┬──────────────────────────────┘
                        │
┌───────────────────────▼──────────────────────────────┐
│         LangGraph ReAct Agent                         │
│   GPT-5 Nano — Reasoning + Acting                    │
│   InMemorySaver — thread_id conversation memory      │
│   Autonomously selects which MCP server to call      │
└──────────┬────────────────────────┬──────────────────┘
           │                        │
    HTTP Transport            STDIO Transport
           │                        │
┌──────────▼──────────┐   ┌─────────▼──────────────────┐
│   Context7 Server   │   │   MetMuseum-MCP Server      │
│   (Remote/Cloud)    │   │   (Local via npx)           │
│                     │   │                             │
│  LLM-optimized      │   │  400,000+ artworks          │
│  documentation      │   │  Metadata + images          │
│  search across      │   │  Metropolitan Museum        │
│  major frameworks   │   │  of Art collection          │
└─────────────────────┘   └─────────────────────────────┘

📂 项目结构

mcp-ai-agent/
│
├── main.py          # Complete multi-server MCP agent
└── README.md

🛠️ 技术栈

组件 技术
语言 Python 3.10+
MCP客户端 多服务器MCPClient(langchain MCP适配器)
代理框架 LangGraph ReAct
LLM OpenAI GPT-5纳米
内存 InMemorySaver(持久对话)
async 异步
MCP服务器1 Context7(HTTP——远程云)
MCP服务器2 MetMuseum MCP(STDIO--本地npx)

🔌 集成MCP服务器

服务器 传输 功能
背景7 HTTP(streamable_http) LLM优化的文档搜索跨主要软件框架和库
大都市博物馆-MCP 工作室(npx metmuseum-mcp) 访问400000多件大都会博物馆艺术品、元数据和图像

🌟 主要特点

🧠 LangGraph重构代理

agent = create_react_agent(
    model=openai_model,     # GPT-5 Nano reasoning engine
    tools=tools,            # All tools from both MCP servers
    checkpointer=checkpointer  # Persistent conversation memory
)

💾 持久对话记忆

checkpointer = InMemorySaver()
config = {"configurable": {"thread_id": "conversation_id"}}
# Agent remembers full conversation history across all turns

🔗 多服务器连接

client = MultiServerMCPClient({
    "context7": {
        "url": "https://mcp.context7.com/mcp",
        "transport": "streamable_http",    # Remote HTTP
    },
    "met-museum": {
        "command": "npx",
        "args": ["-y", "metmuseum-mcp"],
        "transport": "stdio",              # Local STDIO
    }
})
tools = await client.get_tools()  # All tools from both servers

🤖 自主工具选择

代理智能地决定调用哪个服务器:

User: "What is the LangChain documentation for agents?"
→ Agent calls Context7 tools → Returns LLM-optimized docs

User: "Show me Van Gogh paintings in the Met Museum"
→ Agent calls MetMuseum tools → Returns artwork metadata & images

⚙️ 如何跑步

步骤1——安装依赖项:

pip install langchain-mcp-adapters langgraph langchain-openai
npm install -g npx   # For MetMuseum STDIO server

步骤2-设置OpenAI API密钥:

export OPENAI_API_KEY="your-api-key-here"

步骤3--运行代理:

python main.py

第4步——互动:

Agent introduces itself and available tools...

Menu:
1. Ask the agent a question
2. Quit
Enter your choice (1 or 2): 1

Your question
> What LangGraph tools are available for building agents?
→ Agent queries Context7 → Returns documentation

> Show me Impressionist paintings from the Met Museum
→ Agent queries MetMuseum → Returns artwork data

🔄 代理工作流

User Query
    │
    ▼
GPT-5 Nano Reasoning
"Which server has this information?"
    │
    ├─── Library/Code docs? → Context7 (HTTP)
    │
    └─── Art/Museum data? → MetMuseum (STDIO)
    │
    ▼
Tool Execution → Result returned to LLM
    │
    ▼
LLM generates natural language response
    │
    ▼
Stored in InMemorySaver (thread_id)
    │
    ▼
User receives response + context preserved

🎓 技能展示

  • 多服务器MCP客户端架构
  • 同步STDIO+HTTP传输管理
  • 带有工具调用的LangGraph ReAct代理
  • 通过OpenAI API实现GPT-5 Nano集成
  • 使用InMemorySaver实现持久对话记忆
  • 基于线程的会话管理
  • 异步Python——异步+等待模式
  • 真正的外部MCP服务器集成(Context7+MetMuseum)
  • 基于LLM推理的自主刀具选择
  • 用于多域查询的自然语言接口
  • 通过npx集成Node.js MCP服务器

📜 认证

认证 发卡机构 平台
IBM数据科学专业证书 IBM Coursera
IBM生成人工智能专业证书 IBM Coursera
IBM RAG和代理人工智能专业证书 IBM Coursera

🤝 与我联系