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.
What is MCP?
Section titled “What is MCP?”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.
What can MCP do?
Section titled “What can MCP do?”- 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.
Why does MCP matter?
Section titled “Why does MCP matter?”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 Architecture (how it is built)
Section titled “MCP Architecture (how it is built)”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.
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
What is the OpenAI SDK?
Section titled “What is the OpenAI 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
Creating an Agent with the OpenAI SDK
Section titled “Creating an Agent with the OpenAI SDK”First, install the OpenAI Agents SDK:
uv add openai-agentsThen you can create an agent with this code:
# hello_agent.pyfrom agents import Agent, Runnerfrom 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)Adding Tools to an Agent
Section titled “Adding Tools to an Agent”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:
ComputerToolandApplyPatchToolalways run on your own machine, whileShellToolcan 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
Using Built-in (Hosted) Tools
Section titled “Using Built-in (Hosted) Tools”from agents import Agent, Runner, WebSearchToolfrom 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)Using Custom (Function) Tools
Section titled “Using Custom (Function) Tools”from agents import Agent, Runner, function_toolfrom dotenv import load_dotenvimport requests
load_dotenv()
@function_tooldef 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)Using Agents as Tools
Section titled “Using Agents as Tools”from agents import Agent, Runner, WebSearchTool, function_toolfrom dotenv import load_dotenvimport requests
load_dotenv()
@function_tooldef 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)