การผสานรวม API กับ MCP

Model Context Protocol (MCP) เบื้องต้น

James Chapman

AI Curriculum Manager, DataCamp

mcp_code_flow_v2.png

Model Context Protocol (MCP) เบื้องต้น

การปรับปรุง API

Model Context Protocol (MCP) เบื้องต้น

การปรับปรุง API

apis_improvs2.png

Model Context Protocol (MCP) เบื้องต้น

การปรับปรุง API

apis_improvs3.png

Model Context Protocol (MCP) เบื้องต้น

การจัดการ Timeout และข้อผิดพลาด

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}"
Model Context Protocol (MCP) เบื้องต้น

การยืนยันตัวตน API: สิ่งที่ควรและไม่ควรทำ

 

  • API key → เข้าถึงเสมอผ่าน ฝั่งเซิร์ฟเวอร์

 

local_api1.png

Model Context Protocol (MCP) เบื้องต้น

การยืนยันตัวตน API: สิ่งที่ควรและไม่ควรทำ

 

  • API key → เข้าถึงเสมอผ่าน ฝั่งเซิร์ฟเวอร์
    • Local Host: ตัวแปรสภาพแวดล้อม, .env

 

local_api2.png

Model Context Protocol (MCP) เบื้องต้น

การยืนยันตัวตน API: สิ่งที่ควรและไม่ควรทำ

 

  • API key → เข้าถึงเสมอผ่าน ฝั่งเซิร์ฟเวอร์
    • Local Host: ตัวแปรสภาพแวดล้อม, .env
    • Remote Host: ตัวแปรสภาพแวดล้อม, managed secrets

 

remote_api1.png

Model Context Protocol (MCP) เบื้องต้น

การยืนยันตัวตน API: สิ่งที่ควรและไม่ควรทำ

 

  • API key → เข้าถึงเสมอผ่าน ฝั่งเซิร์ฟเวอร์
    • Local Host: ตัวแปรสภาพแวดล้อม, .env
    • Remote Host: ตัวแปรสภาพแวดล้อม, managed secrets

ไคลเอนต์จะไม่ส่ง รับ หรือเข้าถึงข้อมูลประจำตัวเลย

 

remote_api2.png

Model Context Protocol (MCP) เบื้องต้น

การยืนยันตัวตน API: สิ่งที่ควรและไม่ควรทำ

 

  • API key → เข้าถึงเสมอผ่าน ฝั่งเซิร์ฟเวอร์
    • Local Host: ตัวแปรสภาพแวดล้อม, .env
    • Remote Host: ตัวแปรสภาพแวดล้อม, managed secrets

ไคลเอนต์จะไม่ส่ง รับ หรือเข้าถึงข้อมูลประจำตัวเลย

❌ ฮาร์ดโค้ด, แทรกใน URL, บันทึกใน log

❌ อาร์กิวเมนต์หรือผลลัพธ์ของเครื่องมือ

 

remote_api3.png

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}"
Model Context Protocol (MCP) เบื้องต้น

ลองฝึกกันเลย!

Model Context Protocol (MCP) เบื้องต้น

Preparing Video For Download...