adding-mcp-to-an-ai-agent

Adding MCP to an AI Agent

In the previous articles, we gradually increased the capabilities of our AI Agent.

Our Agent can now:

  • Understand user requests
  • Use an AI model
  • Use tools
  • Maintain conversation context
  • Use memory
  • Retrieve information using RAG

Now we will introduce another important technology:

MCP – Model Context Protocol.

MCP provides a standardized way for AI applications to connect to external tools, data sources, and services. The OpenAI Agents SDK currently supports MCP servers through several connection methods, including local stdio servers and Streamable HTTP servers. (OpenAI GitHub)

What Is MCP?

MCP stands for:

Model Context Protocol

MCP is an open protocol designed to standardize how applications provide tools and context to AI models.

A simple way to understand MCP is to think of it as a standard connection between an AI application and external capabilities.

The official MCP documentation uses a useful analogy: MCP is similar to USB-C for AI applications — one standardized connection can be used to connect an application to different types of tools and data sources. (OpenAI GitHub)

Why Do We Need MCP?

Imagine that we build an AI Agent that needs to work with several external systems:

AI Agent
   |
   +---- File System
   |
   +---- Database
   |
   +---- GitHub
   |
   +---- Calendar
   |
   +---- Company Documents
   |
   +---- External APIs

Without a standard protocol, each connection could require a different implementation.

MCP provides a standardized way for AI applications to discover and use capabilities exposed by MCP servers.

A Simple MCP Architecture

A simplified architecture looks like this:

                     AI Agent
                         |
                         v
                     MCP Client
                         |
                         v
                    MCP Server
                         |
          +--------------+--------------+
          |              |              |
          v              v              v
      File System     Database      External API

The MCP client is the part of the AI application that connects to an MCP server.

The MCP server exposes tools or other context that the client can use.

MCP Client and MCP Server

It is important to understand these two terms.

MCP Client

The client is part of the AI application or Agent that connects to an MCP server.

In our case, the Agent application can act as an MCP client.

MCP Server

The MCP server provides capabilities to the client.

For example, an MCP server might expose tools for:

  • Reading files
  • Searching information
  • Accessing a database
  • Working with Git repositories
  • Calling an external service

The server does not necessarily have to be a large cloud application. It can also be a local process running on your computer.

MCP Is Not an AI Model

MCP is sometimes misunderstood as an AI model.

It is not.

MCP is a protocol.

The relationship is approximately:

LLM
 |
 v
AI Agent
 |
 v
MCP Client
 |
 v
MCP Server
 |
 v
External System

The LLM provides language understanding and reasoning.

The Agent manages the workflow.

MCP provides a standardized connection to external capabilities.

MCP and Tools

There is a strong relationship between MCP and tools.

An MCP server can expose tools that an Agent can use.

For example:

MCP Server
     |
     +---- search_files
     |
     +---- read_file
     |
     +---- write_file

The Agent can discover these tools and use them when appropriate.

The OpenAI Agents SDK documentation describes MCP server tool calling as an integrated part of the SDK, with MCP-backed tools attached to an Agent similarly to other tools. (OpenAI GitHub)

MCP vs a Normal Python Function

In our earlier article, we created a calculator tool directly in Python.

Conceptually:

AI Agent
   |
   v
Python Function
   |
   v
Calculator

The function belongs directly to our application.

With MCP, the architecture can instead be:

AI Agent
   |
   v
MCP Client
   |
   v
MCP Server
   |
   v
Calculator / External System

The capability can therefore be provided by a separate MCP server.

Why Is This Useful?

Imagine that you have an MCP server providing access to a company database.

You could potentially use the same MCP server from different AI applications.

For example:

             MCP Server
                  |
        +---------+---------+
        |                   |
        v                   v
    AI Agent A          AI Agent B

Both Agents can use the capabilities exposed by the MCP server.

This separation can make systems easier to reuse and maintain.

MCP Servers Can Be Local or Remote

MCP does not require everything to run in the cloud.

An MCP server can run locally on your computer.

For example:

Your Computer
|
+-- AI Agent
|
+-- MCP Client
|
+-- MCP Server
|
+-- Local Files

An MCP server can also be accessed remotely.

For example:

Your Computer
|
+-- AI Agent
      |
      v
   Internet
      |
      v
Remote MCP Server
      |
      v
External Service

The OpenAI Agents SDK currently supports local stdio MCP servers as well as Streamable HTTP MCP servers, among other integrations. (OpenAI GitHub)

MCP Transports

The word transport describes how the MCP client communicates with the MCP server.

For our purposes, two important approaches are:

stdio

The Agent starts or communicates with a local MCP server process using standard input and output.

AI Agent
   |
 stdin/stdout
   |
MCP Server

This is useful for local MCP servers.

Streamable HTTP

The Agent communicates with an MCP server through HTTP.

AI Agent
   |
 HTTP
   |
MCP Server

The current OpenAI Agents SDK documentation recommends Streamable HTTP or stdio for new integrations, while older SSE-based MCP connections are considered legacy. (OpenAI GitHub)

Our First MCP Example

For our practical tutorial, we will keep the example simple.

Instead of immediately connecting to a complicated enterprise system, we will first understand the basic architecture.

Our Agent will connect to an MCP server.

The MCP server will expose a tool.

The Agent will then be able to use that tool.

The architecture will be:

User
 |
 v
AI Agent
 |
 v
MCP Client
 |
 v
MCP Server
 |
 v
MCP Tool
 |
 v
Result
 |
 v
AI Agent
 |
 v
User

MCP in the OpenAI Agents SDK

The OpenAI Agents SDK provides built-in support for MCP servers.

For example, the Python SDK includes MCP server classes for different connection methods. (OpenAI GitHub)

Conceptually, the Agent can be configured like this:

from agents import Agent

agent = Agent(
    name="MCP Assistant",
    instructions="Use the MCP tools when they are useful.",
    mcp_servers=[mcp_server],
)

The important part is:

mcp_servers=[mcp_server]

This tells the Agent that an MCP server is available.

The actual server configuration depends on whether we are using a local or remote MCP server.

A Local MCP Server

One of the simplest approaches for learning MCP is a local server using stdio.

The architecture is:

VS Code
 |
 v
Python Agent
 |
 v
MCP Client
 |
 v
Local MCP Server
 |
 v
Local Resource

The MCP server can expose tools that operate on an authorized set of resources.

For example, a filesystem MCP server could expose tools for working with files.

MCP and the File System

Imagine an MCP server provides these tools:

read_file
write_file
list_files

Our Agent could then potentially perform tasks such as:

List the files in my project.

or:

Read the project documentation.

or:

Create a new text file.

The important point is that the Agent does not need to implement every file operation itself.

The MCP server provides those capabilities.

MCP Tool Discovery

One of the useful aspects of MCP is that an Agent can discover the tools provided by an MCP server.

Conceptually:

Agent
 |
 | "What tools are available?"
 |
 v
MCP Server
 |
 +---- read_file
 |
 +---- list_files
 |
 +---- search_files

The Agent can then use the appropriate tool when needed.

The OpenAI Agents SDK supports MCP tool discovery and also provides mechanisms for filtering which tools are exposed to an Agent. (OpenAI GitHub)

Tool Filtering

This is especially important for security.

Suppose an MCP server exposes:

read_file
write_file
delete_file

Perhaps our Agent only needs to read files.

We can expose only:

read_file

The current Agents SDK supports tool filtering for MCP servers, allowing developers to restrict which tools are exposed to an Agent. (OpenAI GitHub)

This gives us an important security principle:

An Agent should receive only the tools and permissions it actually needs.

MCP and Security

MCP can make Agents much more powerful.

But greater capability also means greater responsibility.

Imagine giving an Agent access to:

Email
Database
File System
Git
Cloud Infrastructure

The Agent could potentially perform significant actions.

Therefore, we need to consider:

  • Authentication
  • Authorization
  • Tool permissions
  • Access control
  • Human approval
  • Sensitive information
  • Logging
  • Monitoring

For example, an Agent might be allowed to:

Read files       ✓
Search documents ✓
Delete files     ✗
Send email       ✗

until a human explicitly approves the more sensitive actions.

MCP and Human Approval

Some actions should require human confirmation.

For example:

User:
Send this email to 5,000 customers.

The Agent may prepare the email, but the application could require a human to approve the action before it is sent.

The workflow becomes:

User
 |
 v
AI Agent
 |
 v
Prepare Action
 |
 v
Human Approval
 |
 +---- No ----> Stop
 |
 +---- Yes ---> Execute Tool

This is an important design pattern for production AI Agents.

MCP vs RAG

MCP and RAG can work together, but they are not the same thing.

RAG focuses on retrieving relevant information from a knowledge source.

Question
   |
   v
Search Knowledge
   |
   v
Relevant Documents
   |
   v
LLM

MCP provides a standardized way for an application to connect to external tools and context.

AI Agent
   |
   v
MCP
   |
   +---- Tool
   +---- Data Source
   +---- Service

An MCP server could even expose a search capability that an Agent uses as part of a broader RAG workflow.

MCP vs Tools

Tools are capabilities that an Agent can call.

MCP provides a standardized protocol for exposing tools and context to AI applications.

So:

Tool
 |
 +-- A capability an Agent can use

MCP
 |
 +-- A standardized way to expose
     tools and context

MCP is therefore not a replacement for tools.

It is a standardized way of connecting applications and Agents to tools and context.

Our Agent Architecture So Far

We have now built our conceptual Agent step by step.

At the beginning:

Agent
 |
 +-- LLM

Then:

Agent
 |
 +-- LLM
 |
 +-- Tools

Then:

Agent
 |
 +-- LLM
 |
 +-- Tools
 |
 +-- Memory

Then:

Agent
 |
 +-- LLM
 |
 +-- Tools
 |
 +-- Memory
 |
 +-- RAG

Now:

Agent
 |
 +-- LLM
 |
 +-- Tools
 |
 +-- Memory
 |
 +-- RAG
 |
 +-- MCP
       |
       +-- External Tools
       +-- External Data
       +-- External Services

This is starting to look like a real-world Agent architecture.

MCP in Enterprise Applications

MCP can be particularly interesting in enterprise environments.

Imagine a company has:

Customer Database
       |
Document System
       |
Git Repository
       |
Internal APIs
       |
Calendar
       |
Ticket System

MCP can provide standardized connections between AI applications and these external capabilities.

A possible architecture is:

                         AI Agent
                            |
                         MCP Client
                            |
          +-----------------+-----------------+
          |                 |                 |
          v                 v                 v
      MCP Server        MCP Server        MCP Server
       Documents         Database          Git
          |                 |                 |
          v                 v                 v
      Documents          Data            Repository

This makes MCP particularly interesting when building larger Agent systems.

What We Have Learned

In this article, we introduced Model Context Protocol (MCP).

We learned that:

  • MCP stands for Model Context Protocol.
  • MCP is an open protocol.
  • MCP standardizes connections between AI applications and external capabilities.
  • An MCP server can expose tools and context.
  • An Agent can connect to MCP servers.
  • MCP servers can be local or remote.
  • MCP can work with tools, memory, and RAG.
  • Tool filtering and permissions are important.
  • Sensitive actions may require human approval.

The basic idea is:

AI Agent
   |
   v
MCP Client
   |
   v
MCP Server
   |
   +---- Tools
   +---- Data
   +---- Services

Conclusion

MCP provides an important building block for modern AI Agent systems.

Instead of developing a completely different integration for every external service, developers can use a standardized protocol for connecting AI applications to tools and context.

When combined with the capabilities we have already explored, our Agent is becoming significantly more powerful:

                         AI Agent
                            |
        +-------------------+-------------------+
        |                   |                   |
        v                   v                   v
       LLM               Memory               Tools
                            |
                            v
                           RAG
                            |
                            v
                           MCP
                            |
              +-------------+-------------+
              |             |             |
              v             v             v
           Data          Services       Tools

We are now moving from a simple AI Agent toward a more complete Agent architecture.

In the next article, we will explore another important capability:

Planning and Reasoning in AI Agents.

We will see how an Agent can break a complex goal into smaller steps, decide what actions are needed, use tools, and work toward a final result.

Planning and Reasoning in AI Agents

← Back to AI Agents – Step-by-Step

← Back to Home Page

Note for your practical example

For this article, I deliberately did not make you install a complicated MCP server yet. MCP has several integration methods, and the current OpenAI Agents SDK supports multiple transports and hosted MCP integrations. (OpenAI GitHub)

For your blog series, I think it is better to first explain the architecture clearly and then, in a later practical article, connect our existing Python Agent to a real MCP server and demonstrate it in VS Code. That will give your readers a much more useful step-by-step example.