📘 Introduction

Welcome back to another hands-on OpenAI tutorial! 🚀 In this guide, we’ll show you step-by-step how to add MCP Servers to your LLM using the OpenAI Python SDK. By the end of this tutorial, you’ll know how to extend your model’s abilities by connecting it to external MCP Servers— so your assistant can directly invoke cloud-hosted tools and services, not just generate text.

💡 What are MCP Servers?

MCP (Model Context Protocol) Servers let you connect external tools directly to your LLM. The OpenAI SDK automatically sets them up for you:

✅ The server label identifies your MCP Server.
✅ The server URL tells the model where to connect.
✅ Tools are automatically discovered from the MCP Server.
✅ Calls are routed securely from the model to the server.

Every MCP Server provides a set of tools — functions your LLM can call on demand. Think of it as a cloud-based toolbox: once connected, your model can access all the tools you’ve defined without extra setup. This lets your assistant interact with APIs, query databases, or run custom logic, bridging your LLM with real-world capabilities so it can take meaningful actions, not just generate text.

💡
If you want to learn how to build and deploy an MCP Server, check out our previous tutorial on creating MCP Servers with FastMCP.

✅ Prerequisites

Before starting, make sure you have the following:

🐍☑️ Python installed
📦☑️ Pip installed
🌐☑️ A virtual environment (venv) created and activated
🔑☑️ An OpenAI API key generated (get one fromOpenAI)
🔐☑️ OpenAI API key set as an environment variable (OPENAI_API_KEY="your_api_key_here")
🛠️☑️ An MCP Server created
☁️☑️ An MCP Server deployed (for this example: https://mcp-dlnerds.fastmcp.app/mcp)

📦1️⃣ Install Libraries

Install the following Python packages using pip:

pip install openai-agents

📥2️⃣ Import Libraries

Start by importing the required Python modules:

from openai import OpenAI

3️⃣ Initialize the OpenAI Client

Create an OpenAI client instance:

client = OpenAI()

⚙️4️⃣ Configure Your MCP Server

Let’s define the MCP Server we want the model to connect to:

mcp_server = {
    "type": "mcp",
    "server_label": "mcp-dlnerds",
    "server_url": "https://mcp-dlnerds.fastmcp.app/mcp",
    "require_approval": "never",
}

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In