Integrasi API MCP

Pengantar Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

mcp_code_flow_v2.png

Pengantar Model Context Protocol (MCP)

Peningkatan API

Pengantar Model Context Protocol (MCP)

Peningkatan API

apis_improvs2.png

Pengantar Model Context Protocol (MCP)

Peningkatan API

apis_improvs3.png

Pengantar Model Context Protocol (MCP)

Timeout dan Penanganan Error

import requests

@mcp.tool()
def convert_timezone(date_time: str, from_timezone: str, to_timezone: str) -> str:
    payload = {"dateTime": date_time, "fromTimezone": from_timezone, "toTimezone": to_timezone}

try: r = requests.post("https://api.opentimezone.com/convert", json=payload, timeout=10)
r.raise_for_status()
data = r.json() converted_time = data.get("dateTime", "N/A") return f"Time in {to_timezone}: {converted_time}"
except requests.exceptions.RequestException as e: return f"Error converting timezone: {e}"
Pengantar Model Context Protocol (MCP)

Autentikasi API: Yang Boleh dan Tidak

 

  • Kunci API → selalu diakses di sisi server

 

local_api1.png

Pengantar Model Context Protocol (MCP)

Autentikasi API: Yang Boleh dan Tidak

 

  • Kunci API → selalu diakses di sisi server
    • Host Lokal: Variabel lingkungan, .env

 

local_api2.png

Pengantar Model Context Protocol (MCP)

Autentikasi API: Yang Boleh dan Tidak

 

  • Kunci API → selalu diakses di sisi server
    • Host Lokal: Variabel lingkungan, .env
    • Host Jarak Jauh: Variabel lingkungan, secret terkelola

 

remote_api1.png

Pengantar Model Context Protocol (MCP)

Autentikasi API: Yang Boleh dan Tidak

 

  • Kunci API → selalu diakses di sisi server
    • Host Lokal: Variabel lingkungan, .env
    • Host Jarak Jauh: Variabel lingkungan, secret terkelola

Klien tidak pernah mengirim, menerima, atau mengakses kredensial

 

remote_api2.png

Pengantar Model Context Protocol (MCP)

Autentikasi API: Yang Boleh dan Tidak

 

  • Kunci API → selalu diakses di sisi server
    • Host Lokal: Variabel lingkungan, .env
    • Host Jarak Jauh: Variabel lingkungan, secret terkelola

Klien tidak pernah mengirim, menerima, atau mengakses kredensial

❌ Hard-coded, dimasukkan ke URL, dicatat log

❌ Argumen tool atau hasil tool

 

remote_api3.png

Pengantar Model Context Protocol (MCP)
import os
import requests

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

headers = {"Content-Type": "application/json"}
api_key = os.environ.get("TIMEZONE_API_KEY") if api_key: headers["Authorization"] = f"Bearer {api_key}"
payload = {"dateTime": date_time, "fromTimezone": from_timezone, "toTimezone": to_timezone} try: r = requests.post("https://api.opentimezone.com/convert", json=payload, headers=headers, timeout=10) r.raise_for_status() data = r.json() converted_time = data.get("dateTime", "N/A") return f"Time in {to_timezone}: {converted_time}" except requests.exceptions.RequestException as e: return f"Error converting timezone: {e}"
Pengantar Model Context Protocol (MCP)

Ayo berlatih!

Pengantar Model Context Protocol (MCP)

Preparing Video For Download...