MCP et LLM : Outils

Introduction au Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

Des outils MCP aux outils LLM

 

L'intégration Outil ↔ LLM dépend fortement du LLM

  • Les LLM acceptent les outils de façons et syntaxes différentes
  • Utiliser Claude via l'API Messages d'Anthropic
  • Focus : processus et transformations

llm_mcp.png

Introduction au Model Context Protocol (MCP)

Flux d'appel d'outil

fct1.jpg

Introduction au Model Context Protocol (MCP)

Flux d'appel d'outil

fct2.jpg

Introduction au Model Context Protocol (MCP)

Flux d'appel d'outil

fct3.jpg

Introduction au Model Context Protocol (MCP)

Flux d'appel d'outil

fct4.jpg

Introduction au Model Context Protocol (MCP)

Flux d'appel d'outil

fct5.jpg

Introduction au Model Context Protocol (MCP)

Flux d'appel d'outil

fct6.jpg

Introduction au Model Context Protocol (MCP)

Serveur MCP : timezone_server.py

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)

Étendre le code client

async def get_tools_from_mcp():
            # ... 
            return response.tools

async def call_mcp_tool(tool_name: str, arguments: dict) -> str:        
            # ...
            result = await session.call_tool(tool_name, arguments)
            return str(result.content[0].text)

async def call_anthropic_llm(user_query: str):
Introduction au Model Context Protocol (MCP)

Préparation : formater les outils pour le LLM

async def call_anthropic_llm(user_query: str):
    """Appeler Claude avec des outils MCP."""

mcp_tools = await get_tools_from_mcp()
anthropic_tools = [] for tool in mcp_tools:
anthropic_tool = { "name": tool.name, "description": tool.description or "", "input_schema": tool.inputSchema, # MCP utilise le format JSON Schema }
anthropic_tools.append(anthropic_tool)
Introduction au Model Context Protocol (MCP)

1. Envoyer la requête et les outils au LLM

from anthropic import AsyncAnthropic
async def call_anthropic_llm(user_query: str):
    # ...

client = AsyncAnthropic(api_key="<ANTHROPIC_API_TOKEN>")
response = await client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, messages=[{"role": "user", "content": user_query}], tools=anthropic_tools, )
Introduction au Model Context Protocol (MCP)

2. Vérifier un appel d'outil

async def call_anthropic_llm(user_query: str):
    # ...

    if response.stop_reason == "tool_use":

tool_use = next(b for b in response.content if b.type == "tool_use") name = tool_use.name args = tool_use.input print(f"Model decided to call: {name}") print(f"Arguments: {args}\n")
Introduction au Model Context Protocol (MCP)

3. Appeler l'outil | 4. Message de suivi

async def call_anthropic_llm(user_query: str):
    # ...

    if response.stop_reason == "tool_use":
        # ...

result = await call_mcp_tool(name, args)
tool_result = {"type": "tool_result", "tool_use_id": tool_use.id, "content": result} followup = await client.messages.create( model="claude-sonnet-4-6", max_tokens=1024, messages=[ {"role": "user", "content": user_query}, {"role": "assistant", "content": response.content}, {"role": "user", "content": [tool_result]}, ], tools=anthropic_tools, )
Introduction au Model Context Protocol (MCP)

5. Réponse finale

async def call_anthropic_llm(user_query: str):
    # ...

    if response.stop_reason == "tool_use":
        # ...

final = next((b.text for b in followup.content if b.type == "text"), None) if final: print(f"\nAssistant: {final}") else: print("No follow-up message from model.")
else: text = next((b.text for b in response.content if b.type == "text"), "") print(f"\nAssistant: {text}") return str(text)
Introduction au Model Context Protocol (MCP)

Tester la fonction

if __name__ == "__main__":
    asyncio.run(call_anthropic_llm("It is 9:50 AM in the UK in January. What time 
    is it in Lisbon, Portugal?"))
Assistant: It's 9:50 AM in Lisbon as well.
Introduction au Model Context Protocol (MCP)

Flux d'appel d'outil

fct6.jpg

Introduction au Model Context Protocol (MCP)

Passons à la pratique !

Introduction au Model Context Protocol (MCP)

Preparing Video For Download...