Skip to content
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

[1/n] Add MCP types to the SDK #320

Merged
merged 1 commit into from
Mar 25, 2025
Merged

[1/n] Add MCP types to the SDK #320

merged 1 commit into from
Mar 25, 2025

Conversation

rm-openai
Copy link
Collaborator

@rm-openai rm-openai commented Mar 24, 2025

Summary:

  1. Add the MCP dep for python 3.10, since it doesn't support 3.9 and below
  2. Create MCPServer, which is the agents SDK representation of an MCP server
  3. Create implementations for HTTP-SSE and StdIO servers, directly copying the MCP SDK example
  4. Add a util to transform MCP tools into Agent SDK tools

Note: I added optional caching support to the servers. That way, if you happen to know a server's tools don't change, you can just cache them.

Test Plan:

Checks pass. I added tests at the end of the stack.


#324
#322
#321
-> #320
#319

@rm-openai rm-openai requested a review from stevenheidel March 24, 2025 19:09
@rm-openai rm-openai mentioned this pull request Mar 24, 2025
Base automatically changed from rm/pr319 to main March 25, 2025 03:19
rm-openai added a commit that referenced this pull request Mar 25, 2025
We don't really need mypy on 3.9 (unit tests would catch any real
issues), and it causes issues with the rest of this stack.


---
[//]: # (BEGIN SAPLING FOOTER)
* #324
* #322
* #321
* #320
* __->__ #319
from .server import MCPServer


class MCPUtil:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the same as the other MCPUtil class

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry deleted in the end of stack.

Comment on lines +37 to +45
@abc.abstractmethod
async def list_tools(self) -> list[MCPTool]:
"""List the tools available on the server."""
pass

@abc.abstractmethod
async def call_tool(self, tool_name: str, arguments: dict[str, Any] | None) -> CallToolResult:
"""Invoke a tool on the server."""
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about making these just return our Tool class? ie. the MCPServer is responsible for doing the conversion

this is an MCPServer class specifically for integrating with our SDK, so it can use our types

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good question. I considered that, but I actually think MCP servers should be shared between frameworks. I talked to @dsp-ant and plan to make a PR to the MCP repo to make this part of their SDK, so then this import would just be an alias to mcp.client.MCPServer.

stevenheidel
stevenheidel previously approved these changes Mar 25, 2025
### Summary:
1. Add the MCP dep for python 3.10, since it doesn't support 3.9 and below
2. Create MCPServer, which is the agents SDK representation of an MCP server
3. Create implementations for HTTP-SSE and StdIO servers, directly copying the [MCP SDK example](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/clients/simple-chatbot/mcp_simple_chatbot/main.py)
4. Add a util to transform MCP tools into Agent SDK tools

Note: I added optional caching support to the servers. That way, if you happen to know a server's tools don't change, you can just cache them.

### Test Plan:

Checks pass. I added tests at the end of the stack.
@rm-openai rm-openai merged commit 91feb7d into main Mar 25, 2025
12 of 15 checks passed
@rm-openai rm-openai deleted the rm/pr320 branch March 25, 2025 16:53
rm-openai added a commit that referenced this pull request Mar 25, 2025
### Summary:
This enables users to **use** MCP inside the SDK.
1. You add a list of MCP servers to `Agent`, via `mcp_server=[...]`
2. When an agent runs, we look up its MCP tools and add them to the list
of tools.
3. When a tool call occurs, we call the relevant MCP server.

Notes:
1. There's some refactoring to make sure we send the full list of tools
to the Runner/Model etc.
2. Right now, you could have a locally defined tool that conflicts with
an MCP defined tool. I didn't add errors for that, will do in a
followup.

### Test Plan:
See unit tests. Also has an end to end example next PR.

---
[//]: # (BEGIN SAPLING FOOTER)
* #324
* #322
* __->__ #321
* #320
rm-openai added a commit that referenced this pull request Mar 25, 2025
Just adding docs.
-
---
[//]: # (BEGIN SAPLING FOOTER)
* __->__ #324
* #322
* #321
* #320
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants