-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add MCP support to Agent SDK #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
awesome work! @saqadri |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR. I'm unable to accept it as is for a few reasons:
- We don't want to change the min python version required.
- We want to keep this library light and with very few dependencies, in both the API and the implementation.
I'm supportive of the idea of MCP here - but I think this might not be the ideal approach. I'd recommend extracting this into an extension repository that users can use to extend their agents, rather than baking it into the SDK this way.
@rm-openai thanks for your review!
The Python dep can be reversed back to 3.9 with a minor update to MCP-agent, so that shouldn't be a problem.
This is reasonable. Would you be open to having an extension package in this repository itself, or would you prefer a separate repo altogether (with perhaps a comment in docs or readme about the MCP extension)? Happy to address both these points today. |
Quick update -- I'm working on updating this to be an extension package. I'll try to complete the work by today, and will keep the |
Thank you. For now let's leave it as a separate repo. We're gonna circle up internally on the best way to support MCP and get back to you. Closing this PR as a result, but if there are any follow ups, feel free to reopen or create an issue. |
This PR has been really helpful to me, but I ran into an issue during testing: openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for function 'filesystem-set_permissions': In context=('properties', 'permissions'), 'additionalProperties' is required to be supplied and to be false.", 'type': 'invalid_request_error', 'param': 'tools[22].parameters', 'code': 'invalid_function_parameters'}} After some debugging, I found that the problem was caused by nested sub-objects not having additionalProperties set. I fixed it with the following changes:
Hope this helps! |
@liurenhao thanks! I'll add this fix into the openai-agents-mcp package. I'm working to turn that into an extension module (will publish an update to pypi tomorrow). Can you please add an issue here https://github.com/lastmile-ai/openai-agents-mcp |
@saqadri Have you published this yet? |
@nikhilmaddirala I just published this extension package: https://github.com/lastmile-ai/openai-agents-mcp Please try it out and let me know how it goes: Installationuv add openai-agents-mcp UsageIn order to use Agents SDK with MCP, simply replace the following import: - from agents import Agent
+ from agents_mcp import Agent With that you can instantiate an Agent with from agents_mcp import Agent
# Create an agent with specific MCP servers you want to use
# These must be defined in your mcp_agent.config.yaml file
agent = Agent(
name="MCP Agent",
instructions="""You are a helpful assistant with access to both local/OpenAI tools and tools from MCP servers. Use these tools to help the user.""",
# Local/OpenAI tools
tools=[get_current_weather],
# Specify which MCP servers to use
# These must be defined in your mcp_agent config
mcp_servers=["fetch", "filesystem"],
) Then define an mcp:
servers:
fetch:
command: npx
args: ["-y", "@modelcontextprotocol/server-fetch"]
filesystem:
command: npx
args: ["-y", "@modelcontextprotocol/server-filesystem", "."] The rest of the Agents SDK works exactly as before. |
@saqadri Thank you! Can you clarify what exactly you mean by "extension package"? Your repo appears to be a fork. |
@nikhilmaddirala the readme goes into detail. It started as a fork but now is just extending the Agent class from the base openai-agents package to add mcp support. |
Thanks, I did read it, and I understand how your repo works, but I'm still not understanding what "extension package" means and how it differs from a fork. |
@nikhilmaddirala by extension package I was just trying to say that it can be used alongside the openai-agents package, instead of in lieu of it. When I first implemented this feature, I had forked/cloned the entire agents SDK codebase, and made changes to Hope this helps! |
This explanation is very helpful, thank you! :) |
https://github.com/yanmxa/litemcp This one is a lightweight implementation to support the Agent SDK with MCP in a non-invasive way. |
This change adds support for Model Context Protocol servers to Agent SDK using the mcp-agent library.
The support works for both streaming and non-streaming use-cases, and doesn't affect existing functionality for users who are using regular tools. In addition, the MCP support is additive -- it works seamlessly with regular tool use (a developer can specify a local function tool, as well as MCP servers, with the tools list extended with the MCP servers' tools).
The core implementation has been added to
mcp.py
, with surgical updates to Agent and Runner.I have added several examples to
examples/mcp
to show the various usage patterns.Detailed change overview:
Model Context Protocol (MCP) in OpenAI Agents SDK
This directory contains examples demonstrating how to use the Model Context Protocol (MCP) with the OpenAI Agents SDK. The integration allows agents to leverage tools from MCP servers alongside native OpenAI Agent SDK tools.
What is MCP?
Model Context Protocol (MCP) gives models access to tools, resources and prompts in a standardized way. It defines a standardized way, enabling interoperability between different AI systems and tool providers.
Using MCP servers in Agents SDK
mcp_servers
property on AgentYou can specify the names of MCP servers to give an Agent access to by
setting its
mcp_servers
property.The Agent will then automatically aggregate tools from the servers, as well as
any
tools
specified, and create a single extended list of tools. This means you can seamlesslyuse local tools, MCP servers, and other kinds of Agent SDK tools through a single unified syntax.
MCP Configuration File
Configure MCP servers by creating an
mcp_agent.config.yaml
file. You can place this file in your project directory or any parent directory.Here's an example configuration file that defines three MCP servers:
For servers that require sensitive information like API keys, you can:
mcp_agent.secrets.yaml
file (more secure)Methods for Configuring MCP
The OpenAI Agents SDK supports several ways to configure MCP servers:
1. Automatic Discovery (Recommended)
The simplest approach lets the SDK automatically find your configuration file if it's named
mcp_agent.config.yaml
andmcp_agent.secrets.yaml
:2. Explicit Config Path
You can explicitly specify the path to your config file:
3. Programmatic Configuration
You can programmatically define your MCP settings:
4. Custom Server Registry
You can create and configure your own MCP server registry:
Examples
Basic Hello World
A simple example demonstrating how to create an agent that uses MCP tools:
See hello_world.py for the complete example.
Streaming Responses
To stream responses instead of waiting for the complete result:
See hello_world_streamed.py for the complete example.
Slack Integration
An example showing how to use MCP for Slack integration:
See slack.py for the complete example.