Member-only story
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)
andmultiply(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
- Start the Math Server: The
math_server.py
runs as an MCP server exposing two tools:add(a, b)
andmultiply(a, b)
. - Initialize a Stdio Connection: The client creates a subprocess to communicate with the math server via stdio transport.
- Load Available Tools: The client fetches the available tools (
add
,multiply
) from the server. - Invoke AI Model: The AI model,
GPT-4o
, uses LangGraph’s ReAct (Reasoning + Acting) agent to decide how to solve the given mathematical expression. - Perform Computation: The model generates tool calls…