Communications client-serveur

Introduction au Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

Communications client-serveur

client_server1.jpg

Introduction au Model Context Protocol (MCP)

Communications client-serveur

client_server2.jpg

Introduction au Model Context Protocol (MCP)

Transports : Entrées/sorties standard (stdio)

stdio2_v2.png

Introduction au Model Context Protocol (MCP)

Transports : Entrées/sorties standard (stdio)

stdio2_v2.png

 

  • Serveur lancé comme processus enfant
Introduction au Model Context Protocol (MCP)

Transports : Entrées/sorties standard (stdio)

stdio3_v2.png

 

  • Serveur lancé comme processus enfant
  • Communication via des tuyaux (stdin/stdout)
Introduction au Model Context Protocol (MCP)

Transports : Entrées/sorties standard (stdio)

stdio7_v2.png

 

  • Serveur lancé comme processus enfant
  • Communication via des tuyaux (stdin/stdout)
Introduction au Model Context Protocol (MCP)

Transports : Entrées/sorties standard (stdio)

stdio7_v2.png

 

  • Serveur lancé comme processus enfant
  • Communication via des tuyaux (stdin/stdout)
  • Local seulement — même machine
Introduction au Model Context Protocol (MCP)

Transports : Entrées/sorties standard (stdio)

stdio7_v2.png

 

  • Serveur lancé comme processus enfant
  • Communication via des tuyaux (stdin/stdout)
  • Local seulement — même machine
  • Simple, aucune configuration réseau
Introduction au Model Context Protocol (MCP)

Transports : Entrées/sorties standard (stdio)

stdio7_v2.png

 

  • Serveur lancé comme processus enfant
  • Communication via des tuyaux (stdin/stdout)
  • Local seulement — même machine
  • Simple, aucune configuration réseau
  • Le serveur s'arrête quand le client quitte
Introduction au Model Context Protocol (MCP)

Transports : HTTP en flux (streamable)

 

streamable_http.png

Introduction au Model Context Protocol (MCP)

Serveur MCP : 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 au Model Context Protocol (MCP)

Client MCP : lister les outils du serveur

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 au Model Context Protocol (MCP)

Client MCP : lister les outils du serveur

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 au Model Context Protocol (MCP)

Client MCP : lister les outils du serveur

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 au Model Context Protocol (MCP)

Client MCP : appeler les outils du serveur

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 au Model Context Protocol (MCP)

Client MCP : appeler les outils du serveur

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 au Model Context Protocol (MCP)

Passons à la pratique !

Introduction au Model Context Protocol (MCP)

Preparing Video For Download...