MCP ve LLM'ler: Araçlar

Model Context Protocol (MCP) Giriş

James Chapman

AI Curriculum Manager, DataCamp

MCP Araçlarından LLM Araçlarına

 

Araç ↔ LLM entegrasyonu LLM'e çok bağımlıdır

  • LLM'ler araçları farklı yollar ve sözdizimleriyle kabul eder
  • Claude'u Anthropic Messages API ile kullanın
  • Odak: süreçler ve dönüşümler

llm_mcp.png

Model Context Protocol (MCP) Giriş

Araç Çağırma İş Akışı

fct1.jpg

Model Context Protocol (MCP) Giriş

Araç Çağırma İş Akışı

fct2.jpg

Model Context Protocol (MCP) Giriş

Araç Çağırma İş Akışı

fct3.jpg

Model Context Protocol (MCP) Giriş

Araç Çağırma İş Akışı

fct4.jpg

Model Context Protocol (MCP) Giriş

Araç Çağırma İş Akışı

fct5.jpg

Model Context Protocol (MCP) Giriş

Araç Çağırma İş Akışı

fct6.jpg

Model Context Protocol (MCP) Giriş

MCP Sunucusu: 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")
Model Context Protocol (MCP) Giriş

İstemci Kodunu Genişletme

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):
Model Context Protocol (MCP) Giriş

Kurulum: Araçları LLM için Biçimlendirme

async def call_anthropic_llm(user_query: str):
    """Claude'u MCP araçlarıyla çağırın."""

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 JSON Schema biçimini kullanır }
anthropic_tools.append(anthropic_tool)
Model Context Protocol (MCP) Giriş

1. Sorguyu ve Araçları LLM'e Gönderme

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, )
Model Context Protocol (MCP) Giriş

2. Araç Çağrısını Kontrol Etme

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")
Model Context Protocol (MCP) Giriş

3. Aracı Çağırma | 4. Takip Mesajı

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, )
Model Context Protocol (MCP) Giriş

5. Son Yanıt

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

İşlevi Test Etme

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.
Model Context Protocol (MCP) Giriş

Araç Çağırma İş Akışı

fct6.jpg

Model Context Protocol (MCP) Giriş

Hadi pratik yapalım!

Model Context Protocol (MCP) Giriş

Preparing Video For Download...