MCP API Integrations

Introduction to Model Context Protocol (MCP)

James Chapman

AI Curriculum Manager, DataCamp

mcp_code_flow_v2.png

Introduction to Model Context Protocol (MCP)

API Improvements

Introduction to Model Context Protocol (MCP)

API Improvements

apis_improvs2.png

Introduction to Model Context Protocol (MCP)

API Improvements

apis_improvs3.png

Introduction to Model Context Protocol (MCP)

Timeout and Error Handling

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

API Authentication: The Do's and Don'ts

 

  • API keys → always accessed server-side

 

local_api1.png

Introduction to Model Context Protocol (MCP)

API Authentication: The Do's and Don'ts

 

  • API keys → always accessed server-side
    • Local Host: Environment variables, .env

 

local_api2.png

Introduction to Model Context Protocol (MCP)

API Authentication: The Do's and Don'ts

 

  • API keys → always accessed server-side
    • Local Host: Environment variables, .env
    • Remote Host: Environment variables, managed secrets

 

remote_api1.png

Introduction to Model Context Protocol (MCP)

API Authentication: The Do's and Don'ts

 

  • API keys → always accessed server-side
    • Local Host: Environment variables, .env
    • Remote Host: Environment variables, managed secrets

The client never sends, receives, or accesses the credentials

 

remote_api2.png

Introduction to Model Context Protocol (MCP)

API Authentication: The Do's and Don'ts

 

  • API keys → always accessed server-side
    • Local Host: Environment variables, .env
    • Remote Host: Environment variables, managed secrets

The client never sends, receives, or accesses the credentials

❌ Hard-coded, inserted into URLs, logged

❌ Tools argument or tool results

 

remote_api3.png

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

Let's practice!

Introduction to Model Context Protocol (MCP)

Preparing Video For Download...