Integrating MCP Servers to an OpenAI Agent
Introduction & Lesson Overview
Welcome! Now that you’ve built your MCP server and exposed your shopping list tools, it’s time to make them available to an OpenAI agent. In this lesson, you’ll learn how to connect your MCP server to an agent using both local (stdio) and remote (SSE) transports, how to provide the server to the agent, and how to test that the integration works as expected.
By the end of this lesson, you’ll be able to:
- Connect an OpenAI agent to your MCP server using both
stdioandSSEtransports. - Provide the MCP server to the agent so it can discover and use your tools.
- Run and test the integration, verifying that the agent can answer queries using your shopping list service.
Let’s walk through each step in detail.
Connecting the Agents SDK to an MCP Server via Stdio
The simplest way to connect your MCP server to an agent is by using the stdio transport. This is ideal for local development, where your server runs as a subprocess on the same machine as your agent. Communication happens over standard input and output, making it fast and easy to set up.
Here’s how you can launch your MCP server and connect to it via stdio using the OpenAI Agents SDK:
- The
commandandargsspecify how to launch your MCP server script. - The
MCPServerStdiocontext manager handles starting and stopping the server process for you.
This setup is perfect for development and testing on your own machine.
Connecting the Agents SDK to an MCP Server via SSE
If your MCP server is running remotely—perhaps on another machine or in the cloud—you’ll want to use the SSE (Server-Sent Events) transport. This allows the agent to communicate with your server over HTTP, making it suitable for distributed or production environments.
Here’s how to connect using SSE:
- Replace the URL with the address of your running MCP server.
- The
MCPServerSsecontext manager manages the HTTP connection for you.
This approach is great for connecting to servers that are not running on your local machine.
