MCP API-integrationer

Introduktion till Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

mcp_code_flow_v2.png

Introduktion till Model Context Protocol (MCP)

API-förbättringar

Introduktion till Model Context Protocol (MCP)

API-förbättringar

apis_improvs2.png

Introduktion till Model Context Protocol (MCP)

API-förbättringar

apis_improvs3.png

Introduktion till Model Context Protocol (MCP)

Timeout och felhantering

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}"
Introduktion till Model Context Protocol (MCP)

API-autentisering: Rätt och fel

 

  • API-nycklar → alltid åtkomst server-side

 

local_api1.png

Introduktion till Model Context Protocol (MCP)

API-autentisering: Rätt och fel

 

  • API-nycklar → alltid åtkomst server-side
    • Lokal värd: Miljövariabler, .env

 

local_api2.png

Introduktion till Model Context Protocol (MCP)

API-autentisering: Rätt och fel

 

  • API-nycklar → alltid åtkomst server-side
    • Lokal värd: Miljövariabler, .env
    • Fjärrvärd: Miljövariabler, hanterade hemligheter

 

remote_api1.png

Introduktion till Model Context Protocol (MCP)

API-autentisering: Rätt och fel

 

  • API-nycklar → alltid åtkomst server-side
    • Lokal värd: Miljövariabler, .env
    • Fjärrvärd: Miljövariabler, hanterade hemligheter

Klienten skickar, tar emot eller öppnar aldrig autentiseringsuppgifterna

 

remote_api2.png

Introduktion till Model Context Protocol (MCP)

API-autentisering: Rätt och fel

 

  • API-nycklar → alltid åtkomst server-side
    • Lokal värd: Miljövariabler, .env
    • Fjärrvärd: Miljövariabler, hanterade hemligheter

Klienten skickar, tar emot eller öppnar aldrig autentiseringsuppgifterna

❌ Hårdkodade, infogade i URL:er, loggade

❌ Verktygsargument eller verktygsresultat

 

remote_api3.png

Introduktion till 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}"
Introduktion till Model Context Protocol (MCP)

Lass uns üben!

Introduktion till Model Context Protocol (MCP)

Preparing Video For Download...