MCP Client-Server Communications

Introduction to Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

Client-Server Communications

client_server1.jpg

Introduction to Model Context Protocol (MCP)

Client-Server Communications

client_server2.jpg

Introduction to Model Context Protocol (MCP)

Transports: Standard I/O (stdio)

stdio2_v2.png

Introduction to Model Context Protocol (MCP)

Transports: Standard I/O (stdio)

stdio2_v2.png

 

  • Server runs as child process
Introduction to Model Context Protocol (MCP)

Transports: Standard I/O (stdio)

stdio3_v2.png

 

  • Server runs as child process
  • Communication via pipes (stdin/stdout)
Introduction to Model Context Protocol (MCP)

Transports: Standard I/O (stdio)

stdio7_v2.png

 

  • Server runs as child process
  • Communication via pipes (stdin/stdout)
Introduction to Model Context Protocol (MCP)

Transports: Standard I/O (stdio)

stdio7_v2.png

 

  • Server runs as child process
  • Communication via pipes (stdin/stdout)
  • Local only - same machine
Introduction to Model Context Protocol (MCP)

Transports: Standard I/O (stdio)

stdio7_v2.png

 

  • Server runs as child process
  • Communication via pipes (stdin/stdout)
  • Local only - same machine
  • Simple, no network configuration
Introduction to Model Context Protocol (MCP)

Transports: Standard I/O (stdio)

stdio7_v2.png

 

  • Server runs as child process
  • Communication via pipes (stdin/stdout)
  • Local only - same machine
  • Simple, no network configuration
  • Server dies when client exits
Introduction to Model Context Protocol (MCP)

Transports: Streamable HTTP

 

streamable_http.png

Introduction to Model Context Protocol (MCP)

MCP Server: timezone_server.py

import mcp
from mcp.server.fastmcp import FastMCP
import requests

mcp = FastMCP("Timezone Converter")

@mcp.tool()
def convert_timezone(date_time: str, from_timezone: str, to_timezone: str) -> str:
    ...

if __name__ == "__main__": mcp.run(transport="stdio")
Introduction to Model Context Protocol (MCP)

MCP Client: Listing Server Tools

from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client

async def get_tools_from_mcp():
# Define the server parameters params = StdioServerParameters(command=sys.executable, args=["timezone_server.py"])
# Connect to the MCP server and open a session async with stdio_client(params) as (reader, writer):
async with ClientSession(reader, writer) as session:
# Initialize the session await session.initialize()
# Ask the server what tools it provides response = await session.list_tools()
Introduction to Model Context Protocol (MCP)

MCP Client: Listing Server Tools

async def get_tools_from_mcp():
            # ...

            # Ask the server what tools it provides
            response = await session.list_tools()

print("Connected to MCP server!") print("Available tools:") for tool in response.tools: print(f" - {tool.name}: {tool.description}") return response.tools
asyncio.run(get_tools_from_mcp())
Introduction to Model Context Protocol (MCP)

MCP Client: Listing Server Tools

Connected to MCP server!
Available tools:
 - convert_timezone: 
    Convert a datetime from one timezone to another.

    Args:
        date_time: The datetime string in ISO format (e.g., '2025-01-20T14:30:00')
        from_timezone: Source timezone (e.g., 'America/New_York')
        to_timezone: Target timezone (e.g., 'Europe/London')

    Returns:
        A string with the converted datetime and timezone information
Introduction to Model Context Protocol (MCP)

MCP Client: Calling Server Tools

async def call_mcp_tool(tool_name: str, arguments: dict) -> str:
    params = StdioServerParameters(command=sys.executable, args=["timezone_server.py"])

    async with stdio_client(params) as (reader, writer):
        async with ClientSession(reader, writer) as session:

            await session.initialize()

result = await session.call_tool(tool_name, arguments)
text_content = result.content[0].text print(f"Conversion Result: {text_content}") return str(text_content)
Introduction to Model Context Protocol (MCP)

MCP Client: Calling Server Tools

asyncio.run(
    call_mcp_tool(
        "convert_timezone",
        {"date_time": "2025-01-20T14:30:00",
         "from_timezone": "America/New_York",
         "to_timezone": "Asia/Tokyo"}
    )
)
'2025-01-21T04:30:00+09:00'
Introduction to Model Context Protocol (MCP)

Let's practice!

Introduction to Model Context Protocol (MCP)

Preparing Video For Download...