Connecting Agents to External Tools with MCP Servers
Introduction & Overview
Welcome back! In the previous lessons, you learned how to make your OpenAI agents in TypeScript more powerful by integrating hosted tools, creating your own custom function tools, and even turning agents themselves into callable tools for modular workflows. Each of these steps has helped you build agents that are more flexible, maintainable, and capable of handling complex tasks.
Today, you'll take another important step: connecting your agent to external tools and data sources using the Model Context Protocol (MCP). This lesson will show you how to link your agent to an MCP server, discover its available tools, and use them in your workflows. By the end of this lesson, you'll know how to connect to both local and remote MCP servers using different transport mechanisms, and how to enable tool list caching for better performance. This will open up even more possibilities for your agents, allowing them to interact with a wide range of external systems.
What is MCP?
MCP stands for Model Context Protocol. It's an open standard designed to make it simple and efficient for AI agents — like the ones you're building — to connect with external tools, data sources, and services. Instead of having to write custom integration code for every new tool or service, MCP provides a universal way for agents to discover, access, and use any tool that's made available by an MCP-compatible server.
An MCP server acts as a hub that "advertises" the tools it offers — these could be anything from booking tickets, checking the weather, sending emails, searching databases, or even running custom business logic. When your agent connects to an MCP server, it can automatically retrieve a list of all available tools and interact with them as needed to fulfill user requests. This means you can easily extend your agent's capabilities by simply connecting it to different MCP servers.
MCP is designed to be flexible and secure, supporting both local and remote connections, and is already being adopted by major AI platforms and tool providers. By using MCP, you can build agents that are modular, maintainable, and ready to interact with a growing ecosystem of external services.
MCP Transport Mechanisms
MCP supports two main ways for your agent to connect to a server: Stdio and Streamable HTTP.
-
Stdio (Standard Input/Output) is used when your
MCPserver is running on the same machine as your agent. It's fast and simple, making it a great choice for local development or when you need low latency. In TypeScript, you can use theMCPServerStdioclass to connect to a localMCPserver by launching it as a subprocess. -
Streamable HTTP is used for connecting to remote
MCPservers over the network. It provides unified, bidirectional, and resumable communication over a single HTTP endpoint, making it suitable for cloud-based services or distributed systems. In TypeScript, you can use theMCPServerStreamableHttpclass to connect to a remoteMCPserver via HTTP.
In short, use MCPServerStdio for local, low-latency connections and MCPServerStreamableHttp for remote, real-time streaming over HTTP. The OpenAI Agents SDK makes it easy to use either method, depending on your needs.
