Understanding MCP Architecture: Single-Server vs Multi-Server Clients

Understanding MCP Architecture: Single-Server vs Multi-Server Clients

MCP architecture demo with single-server & multi-server clients using LangGraph, AI-driven tool calls, and async communication.

commitbyrajat

研究与数据
访问服务器

README

Understanding MCP Architecture: Single-Server vs Multi-Server Clients

Modern AI-driven applications require efficient and scalable architectures to interact with multiple external services. The Multi-Client Protocol (MCP) provides a robust framework to communicate with various tool servers, enabling seamless interaction between AI models and external computations. In this blog, we'll explore the advantages of MCP architecture by comparing single-server and multi-server clients.

Math Server (math_server.py)

The Math Server acts as an MCP tool server that provides basic mathematical operations like addition and multiplication. It listens for incoming requests, processes tool invocations, and responds with results.

Code Breakdown

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Math")

@mcp.tool()
def add(a: int, b: int) -> int:
    """Add two numbers"""
    return a + b

@mcp.tool()
def multiply(a: int, b: int) -> int:
    """Multiply two numbers"""
    return a * b

if __name__ == "__main__":
    mcp.run(transport="stdio")
  • Defines an MCP server named Math.
  • Registers add(a, b) and multiply(a, b) as available tools.
  • Runs with stdio transport, allowing communication via standard input/output.

Single-Server Client

A single-server client connects to one MCP server, which exposes a set of tools to be used in computations. Let's analyze the single_server_client.py script, which interacts with math_server.py to perform mathematical operations.

How It Works

  1. Start the Math Server: The math_server.py runs as an MCP server exposing two tools: add(a, b) and multiply(a, b).
  2. Initialize a Stdio Connection: The client creates a subprocess to communicate with the math server via stdio transport.
  3. Load Available Tools: The client fetches the available tools (add, multiply) from the server.
  4. Invoke AI Model: The AI model, GPT-4o, uses LangGraph’s ReAct (Reasoning + Acting) agent to decide how to solve the given mathematical expression.
  5. Perform Computation: The model generates tool calls, invoking add(3,5) first, then multiply(8,12), and finally returns the computed answer.

Code Breakdown (single_server_client.py)

server_params = StdioServerParameters(
    command="python",
    args=["server/math_server.py"],
)

async with stdio_client(server_params) as (read, write):
    async with ClientSession(read, write) as session:
        await session.initialize()
        tools = await load_mcp_tools(session)
        agent = create_react_agent(model, tools)
        events = await agent.ainvoke({"messages": "what's (3 + 5) x 12?"})

The output prints:

----------------------------------------------------------------------------
The result is 96

MCP Communication Flow (Sequence Diagram)

sequenceDiagram
    participant Client as Client Script
    participant LangGraph as LangGraph React Agent
    participant ClientSession as MCP ClientSession
    participant stdio as stdio Transport Layer
    participant MathServer as Math Server Process
    participant Tools as Math Tools (add/multiply)
    participant LLM as GPT-4o Model

    Note over Client: Starts execution with asyncio.run()
    Client->>MathServer: Launch as subprocess with stdio parameters
    
    Client->>stdio: Create stdio client connection
    stdio-->>Client: Return read/write streams
    
    Client->>ClientSession: Initialize session
    ClientSession->>stdio: Send initialize message
    stdio->>MathServer: Forward initialize request
    MathServer-->>stdio: Return server capabilities
    stdio-->>ClientSession: Return initialization response
    ClientSession-->>Client: Session initialized
    
    Client->>ClientSession: load_mcp_tools()
    ClientSession->>stdio: Request available tools
    stdio->>MathServer: Forward tools request
    MathServer->>Tools: Discover registered tools (add, multiply)
    Tools-->>MathServer: Return tool descriptions
    MathServer-->>stdio: Return tool descriptions
    stdio-->>ClientSession: Return tool descriptions
    ClientSession-->>Client: Return langchain-compatible tools
    
    Client->>LangGraph: Create React agent with model and tools
    LangGraph-->>Client: Return agent instance
    
    Client->>LangGraph: agent.ainvoke("what's (3 + 5) x 12?")
    LangGraph->>LLM: Generate reasoning and tool calls
    
    Note over LLM: Decides to use add(3, 5) first
    LLM-->>LangGraph: Return reasoning and add(3, 5) tool call
    
    LangGraph->>ClientSession: Call add(3, 5)
    ClientSession->>stdio: Send tool execution request
    stdio->>MathServer: Forward tool execution request
    MathServer->>Tools: Execute add(3, 5)
    Tools-->>MathServer: Return result (8)
    MathServer-->>stdio: Return tool execution result
    stdio-->>ClientSession: Return tool execution result
    ClientSession-->>LangGraph: Return add result (8)
    
    LangGraph->>LLM: Generate next reasoning step with add result
    
    Note over LLM: Decides to use multiply(8, 12) next
    LLM-->>LangGraph: Return reasoning and multiply(8, 12) tool call
    
    LangGraph->>ClientSession: Call multiply(8, 12)
    ClientSession->>stdio: Send tool execution request
    stdio->>MathServer: Forward tool execution request
    MathServer->>Tools: Execute multiply(8, 12)
    Tools-->>MathServer: Return result (96)
    MathServer-->>stdio: Return tool execution result
    stdio-->>ClientSession: Return tool execution result
    ClientSession-->>LangGraph: Return multiply result (96)
    
    LangGraph->>LLM: Generate final answer with all results
    LLM-->>LangGraph: Return final answer "The result is 96"
    LangGraph-->>Client: Return complete event trace and answer
    
    Client->>Client: Print final answer

Weather Server (weather_server.py)

The Weather Server is another MCP tool server that provides real-time weather information. It listens for incoming weather queries and responds with the latest weather data.

Command to start the server:

python server/weather_server.py

Code Breakdown

from mcp.server.fastmcp import FastMCP

mcp = FastMCP("Weather")

@mcp.tool()
async def get_weather(location: str) -> str:
    """Get weather for location."""
    return "It's always sunny in New York"

if __name__ == "__main__":
    mcp.run(transport="sse")
  • Defines an MCP server named Weather.
  • Registers get_weather(location) as an available tool.
  • Runs with SSE (Server-Sent Events) transport, allowing real-time event streaming.

Multi-Server Client

A multi-server client can interact with multiple MCP servers simultaneously. The multi_server_client.py script demonstrates this by communicating with both math_server.py and weather_server.py.

How It Works

  1. Initialize Multiple Servers: The client connects to the Math server using stdio and the Weather server via SSE (Server-Sent Events).
  2. Load Tools from Multiple Servers: The AI model gains access to tools from both servers.
  3. Parallel Processing: The client can now invoke tools from different servers within the same execution.
  4. AI-driven Responses: The model processes both math expressions and weather queries using multiple MCP servers.

Code Breakdown (multi_server_client.py)

async with MultiServerMCPClient(
    {
        "math": {
            "command": "python",
            "args": ["server/math_server.py"],
            "transport": "stdio",
        },
        "weather": {
            "url": "http://localhost:8000/sse",
            "transport": "sse",
        },
    }
) as client:
    agent = create_react_agent(model, client.get_tools())
    math_response = await agent.ainvoke({"messages": "what's (3 + 5) x 12?"})
    weather_response = await agent.ainvoke({"messages": "what is the weather in nyc?"})

Multi-Server Client Response

The output from the multi-server client looks like this:

{
  "math_response": "The result of (3 + 5) x 12 is 96.",
  "weather_response": "The weather in NYC is currently sunny."
}

Benefits of MCP Architecture

  • Scalability: Easily extendable to multiple services.
  • Parallel Execution: Allows concurrent queries to different servers.
  • Flexible Communication: Supports multiple transport layers (stdio, sse, etc.).
  • AI Integration: Seamlessly connects AI models with external computation services.

Code Setup and Execution

To clone and execute the code, follow these steps:

git clone https://github.com/commitbyrajat/langgraph-mcp-integration.git
cd langgraph-mcp-integration
rye sync  # Sync dependencies
rye run python server/weather_server.py &
rye run python client/single_server_client.py
rye run python client/multi_server_client.py

MCP architecture enables developers to build powerful AI-driven applications that interact with multiple services efficiently. 🚀


推荐服务器

Crypto Price & Market Analysis MCP Server

Crypto Price & Market Analysis MCP Server

一个模型上下文协议 (MCP) 服务器,它使用 CoinCap API 提供全面的加密货币分析。该服务器通过一个易于使用的界面提供实时价格数据、市场分析和历史趋势。 (Alternative, slightly more formal and technical translation): 一个模型上下文协议 (MCP) 服务器,利用 CoinCap API 提供全面的加密货币分析服务。该服务器通过用户友好的界面,提供实时价格数据、市场分析以及历史趋势数据。

精选
TypeScript
MCP PubMed Search

MCP PubMed Search

用于搜索 PubMed 的服务器(PubMed 是一个免费的在线数据库,用户可以在其中搜索生物医学和生命科学文献)。 我是在 MCP 发布当天创建的,但当时正在度假。 我看到有人在您的数据库中发布了类似的服务器,但还是决定发布我的。

精选
Python
mixpanel

mixpanel

连接到您的 Mixpanel 数据。从 Mixpanel 分析查询事件、留存和漏斗数据。

精选
TypeScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

精选
Python
Nefino MCP Server

Nefino MCP Server

为大型语言模型提供访问德国可再生能源项目新闻和信息的能力,允许按地点、主题(太阳能、风能、氢能)和日期范围进行筛选。

官方
Python
Vectorize

Vectorize

将 MCP 服务器向量化以实现高级检索、私有深度研究、Anything-to-Markdown 文件提取和文本分块。

官方
JavaScript
Mathematica Documentation MCP server

Mathematica Documentation MCP server

一个服务器,通过 FastMCP 提供对 Mathematica 文档的访问,使用户能够从 Wolfram Mathematica 检索函数文档和列出软件包符号。

本地
Python
kb-mcp-server

kb-mcp-server

一个 MCP 服务器,旨在实现便携性、本地化、简易性和便利性,以支持对 txtai “all in one” 嵌入数据库进行基于语义/图的检索。任何 tar.gz 格式的 txtai 嵌入数据库都可以被加载。

本地
Python
Research MCP Server

Research MCP Server

这个服务器用作 MCP 服务器,与 Notion 交互以检索和创建调查数据,并与 Claude Desktop Client 集成以进行和审查调查。

本地
Python
Cryo MCP Server

Cryo MCP Server

一个API服务器,实现了模型补全协议(MCP),用于Cryo区块链数据提取。它允许用户通过任何兼容MCP的客户端查询以太坊区块链数据。

本地
Python