Skip to content

MCP and OpenAI SDK

In this part, we will look at MCP and the OpenAI SDK in simple words - what they are, how they work, and how you can use them yourself.

MCP (Model Context Protocol) is an open-source standard that connects AI applications to outside systems.

With MCP, AI apps like Claude or ChatGPT can connect to data sources (like local files or databases), tools (like search engines or calculators), and workflows (like ready-made prompts). This lets them get useful information and do tasks for you.

Think of MCP like a USB-C port for AI apps. Just like USB-C gives one standard way to connect electronic devices, MCP gives one standard way to connect AI apps to outside systems.

flowchart LR MCP[MCP<br/>Standardized Protocol] Chat["💬 Chat Interface<br/>Claude Desktop<br/>LibreChat"] IDE["💻 IDEs & Code Editors<br/>Claude Code<br/>Goose"] AI["🤖 Other AI Applications<br/>5ire<br/>Superinterface"] Data["🗄️ Data & File Systems<br/>PostgreSQL<br/>SQLite<br/>Google Drive"] Dev["🛠️ Development Tools<br/>Git<br/>Sentry"] Prod["📋 Productivity Tools<br/>Slack<br/>Google Maps"] Chat <--> MCP IDE <--> MCP AI <--> MCP MCP <--> Data MCP <--> Dev MCP <--> Prod
  • Agents can connect to your Google Calendar and Notion, so they act like a more personal AI assistant.
  • Claude Code can build a whole web app just from a Figma design.
  • Company chatbots can connect to many databases at once, so people can ask questions about data using normal chat.
  • AI models can create 3D designs in Blender and then print them using a 3D printer.

MCP is useful in different ways depending on who you are.

  • Developers: MCP makes it faster and easier to build or connect an AI app or agent to other systems.
  • AI apps or agents: MCP gives access to many data sources, tools, and apps, which makes the AI more useful for the end user.
  • End-users: MCP means you get AI apps or agents that are more capable - they can reach your data and do tasks for you when needed.

MCP works using a client-server setup. In this setup, one AI app (called the MCP Host) manages several MCP Clients. Each client keeps one dedicated connection to one specific MCP Server. These servers can run locally (like on your own computer, for example a filesystem or database) or remotely (like Sentry or some other external API). Each client only talks to one server, but the same server can talk to many clients at the same time. This keeps every connection separate, easy to manage, and able to grow, so the host can work with many tools and data sources together.

flowchart TB subgraph Host["MCP Host (AI Application)"] direction LR C1["MCP Client 1"] C2["MCP Client 2"] C3["MCP Client 3"] C4["MCP Client 4"] end S1["MCP Server A - Local<br/>(Filesystem)"] S2["MCP Server B - Local<br/>(Database)"] S3["MCP Server C - Remote<br/>(Sentry)"] C1 <-->|Dedicated Connection| S1 C2 <-->|Dedicated Connection| S2 C3 <-->|Dedicated Connection| S3 C4 <-->|Dedicated Connection| S3

The main parts in MCP are:

  • MCP Host: The AI app that manages one or more MCP clients.
  • MCP Client: A part that keeps a connection to an MCP server and gets information from it, so the host app can use it.
  • MCP Server: A program that gives information to MCP clients.

Official Docs: MCP Docs

MCP SDK: MCP SDK

The OpenAI Agents SDK helps you build agent-based AI apps in a simple, lightweight way, with very few extra layers to learn. It is a production-ready upgrade of an earlier experiment called Swarm. The Agents SDK has just a few basic building blocks:

  • Agents: These are LLMs (language models) given instructions and tools to use.
  • Agents as tools / Handoffs: These let one agent pass work to another agent for a specific task.
  • Guardrails: These check and validate what goes into and comes out of an agent.

When you combine these building blocks with Python, you can build complex relationships between tools and agents, and create real-world apps without a hard learning curve. The SDK also comes with built-in tracing, so you can see and debug how your agents are working, test them, and even fine-tune models for your app.

Official Docs: OpenAI Agents SDK

First, install the OpenAI Agents SDK:

Terminal window
uv add openai-agents

Then you can create an agent with this code:

# hello_agent.py
from agents import Agent, Runner
from dotenv import load_dotenv
load_dotenv()
hello_agent = Agent(
name="HelloAgent",
instructions="You are a helpful assistant that greets users.",
# tools=[], # optional: you can add tools here if needed (covered in the next section)
)
query = input("Enter your query: ")
result = Runner.run_sync(hello_agent, query)
print(result.raw_responses)
print("\n\n")
print(result.final_output)

Tools let agents do actions, such as fetching data, running code, calling external APIs, and even using a computer. The SDK supports these types:

  • Local/runtime execution tools: ComputerTool and ApplyPatchTool always run on your own machine, while ShellTool can run locally or inside a hosted container.
  • Function calling: You can turn any Python function into a tool.
  • Agents as tools: You can turn an agent into a callable tool, without doing a full handoff.

Documentation: OpenAI Agents SDK - Tools

from agents import Agent, Runner, WebSearchTool
from dotenv import load_dotenv
load_dotenv()
search_agent = Agent(
name="SearchAgent",
instructions="You are a helpful assistant that can search the web.",
tools=[
WebSearchTool(),
]
)
query = input("Enter your query: ")
result = Runner.run_sync(search_agent, query)
print(result.raw_responses)
print("\n\n")
print(result.final_output)
from agents import Agent, Runner, function_tool
from dotenv import load_dotenv
import requests
load_dotenv()
@function_tool
def get_weather(city: str):
"""
Get the current weather for a given city using the wttr.in API.
Args:
city (str): The name of the city to get the weather for.
Returns:
str: A string describing the current weather in the specified city.
"""
try:
url = f"https://wttr.in/{city.lower()}?format=%C+%t"
response = requests.get(url, timeout=10)
if response.status_code == 200:
return f"The weather in {city} is {response.text}"
return "Something went wrong, try again or respond accordingly if you have tried multiple times"
except Exception:
return "Something went wrong, try again or respond accordingly if you have tried multiple times"
@function_tool(name="stdwrite")
def stdwrite(message: str):
"""
Write a message to the standard output.
Args:
message (str): The message to write.
Returns:
str: A confirmation message indicating that the message was written.
"""
print(message)
return "Message written to standard output."
weather_agent = Agent(
name="WeatherAgent",
instructions="You are a helpful assistant that provides weather information.",
tools=[
get_weather,
stdwrite
]
)
query = input("Enter your query: ")
result = Runner.run_sync(weather_agent, query)
print(result.raw_responses)
print("\n\n")
print(result.final_output)
from agents import Agent, Runner, WebSearchTool, function_tool
from dotenv import load_dotenv
import requests
load_dotenv()
@function_tool
def get_weather(city: str):
"""
Get the current weather for a given city using the wttr.in API.
Args:
city (str): The name of the city to get the weather for.
Returns:
str: A string describing the current weather in the specified city.
"""
try:
url = f"https://wttr.in/{city.lower()}?format=%C+%t"
response = requests.get(url, timeout=10)
if response.status_code == 200:
return f"The weather in {city} is {response.text}"
return "Something went wrong, try again or respond accordingly if you have tried multiple times"
except Exception:
return "Something went wrong, try again or respond accordingly if you have tried multiple times"
search_agent = Agent(
name="SearchAgent",
instructions="You are a helpful assistant that can search the web.",
tools=[
WebSearchTool(),
]
)
weather_agent = Agent(
name="WeatherAgent",
instructions="You are a helpful assistant that provides weather information.",
tools=[
get_weather
]
)
orchestrator_agent = Agent(
name="OrchestratorAgent",
instructions="You are a helpful assistant that can orchestrate tasks by delegating to other agents if needed.",
tools=[
search_agent.as_tool(
tool_name="search_agent",
tool_description="Use this tool to search the web for information."
),
weather_agent.as_tool(
tool_name="weather_agent",
tool_description="Use this tool to get the current weather for a given city."
),
]
)
query = input("Enter your query: ")
result = Runner.run_sync(orchestrator_agent, query)
print(result.raw_responses)
print("\n\n")
print(result.final_output)