📘 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 AI Agent using the OpenAI Agents SDK. By the end of this tutorial, you’ll know how to extend your agent’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 AI Agent. With the OpenAI Agents SDK, you can attach MCP servers when creating or running your agent.

Each MCP Server exposes a toolbox of functions (tools) that your agent can call in real time. Once connected, your agent can:

  • 🔗 Interact with APIs
  • 📊 Query databases
  • ⚙️ Run custom business logic

This turns your agent into more than a text generator — it becomes action-oriented, capable of invoking real-world services and workflows.

💡
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 from OpenAI)
🔐☑️ OpenAI API key set as environment variable OPENAI_API_KEY
🛠️☑️ An MCP Server created
☁️☑️ An MCP Server deployed

📦1️⃣ Install Libraries

Install the following Python packages using pip:

pip install openai-agents

📥2️⃣ Import Libraries

Start by importing the required Python modules:

import asyncio
from agents import Agent, Runner
from agents.mcp import MCPServer, MCPServerStreamableHttp

🤖3️⃣ Define the AI Agent

Let’s create an agent that will use our MCP Server:

async def run(mcp_server: MCPServer):
    agent = Agent(
        name="Assistant",
        instructions="You are a helpful assistant that can greet users.",
        mcp_servers=[mcp_server],
    )

    result = await Runner.run(
        starting_agent=agent,
        input="Please greet Alice"
    )
    print(result.final_output)

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