Skip to content

Commit 362a9dc

Browse files
authored
[4/n] Add docs for MCP (#338)
Just adding docs. (Repeat of #324)
2 parents 8661849 + b98a6bd commit 362a9dc

File tree

6 files changed

+69
-103
lines changed

6 files changed

+69
-103
lines changed

Diff for: docs/mcp.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Model context protocol
2+
3+
The [Model context protocol](https://modelcontextprotocol.io/introduction) (aka MCP) is a way to provide tools and context to the LLM. From the MCP docs:
4+
5+
> MCP is an open protocol that standardizes how applications provide context to LLMs. Think of MCP like a USB-C port for AI applications. Just as USB-C provides a standardized way to connect your devices to various peripherals and accessories, MCP provides a standardized way to connect AI models to different data sources and tools.
6+
7+
The Agents SDK has support for MCP. This enables you to use a wide range of MCP servers to provide tools to your Agents.
8+
9+
## MCP servers
10+
11+
Currently, the MCP spec defines two kinds of servers, based on the transport mechanism they use:
12+
13+
1. **stdio** servers run as a subprocess of your application. You can think of them as running "locally".
14+
2. **HTTP over SSE** servers run remotely. You connect to them via a URL.
15+
16+
You can use the [`MCPServerStdio`][agents.mcp.server.MCPServerStdio] and [`MCPServerSse`][agents.mcp.server.MCPServerSse] classes to connect to these servers.
17+
18+
For example, this is how you'd use the [official MCP filesystem server](https://www.npmjs.com/package/@modelcontextprotocol/server-filesystem).
19+
20+
```python
21+
async with MCPServerStdio(
22+
params={
23+
"command": "npx",
24+
"args": ["-y", "@modelcontextprotocol/server-filesystem", samples_dir],
25+
}
26+
) as server:
27+
tools = await server.list_tools()
28+
```
29+
30+
## Using MCP servers
31+
32+
MCP servers can be added to Agents. The Agents SDK will call `list_tools()` on the MCP servers each time the Agent is run. This makes the LLM aware of the MCP server's tools. When the LLM calls a tool from an MCP server, the SDK calls `call_tool()` on that server.
33+
34+
```python
35+
36+
agent=Agent(
37+
name="Assistant",
38+
instructions="Use the tools to achieve the task",
39+
mcp_servers=[mcp_server_1, mcp_server_2]
40+
)
41+
```
42+
43+
## Caching
44+
45+
Every time an Agent runs, it calls `list_tools()` on the MCP server. This can be a latency hit, especially if the server is a remote server. To automatically cache the list of tools, you can pass `cache_tools_list=True` to both [`MCPServerStdio`][agents.mcp.server.MCPServerStdio] and [`MCPServerSse`][agents.mcp.server.MCPServerSse]. You should only do this if you're certain the tool list will not change.
46+
47+
If you want to invalidate the cache, you can call `invalidate_tools_cache()` on the servers.
48+
49+
## End-to-end example
50+
51+
View complete working examples at [examples/mcp](https://github.com/openai/openai-agents-python/tree/main/examples/mcp).

Diff for: docs/ref/mcp/server.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `MCP Servers`
2+
3+
::: agents.mcp.server

Diff for: docs/ref/mcp/util.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# `MCP Util`
2+
3+
::: agents.mcp.util

Diff for: mkdocs.yml

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ nav:
2828
- results.md
2929
- streaming.md
3030
- tools.md
31+
- mcp.md
3132
- handoffs.md
3233
- tracing.md
3334
- context.md
@@ -60,6 +61,8 @@ nav:
6061
- ref/models/interface.md
6162
- ref/models/openai_chatcompletions.md
6263
- ref/models/openai_responses.md
64+
- ref/mcp/server.md
65+
- ref/mcp/util.md
6366
- Tracing:
6467
- ref/tracing/index.md
6568
- ref/tracing/create.md
@@ -107,6 +110,8 @@ plugins:
107110
show_signature_annotations: true
108111
# Makes the font sizes nicer
109112
heading_level: 3
113+
# Show inherited members
114+
inherited_members: true
110115

111116
extra:
112117
# Remove material generation message in footer

Diff for: src/agents/mcp/mcp_util.py

-94
This file was deleted.

Diff for: src/agents/mcp/server.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ def __init__(self, params: MCPServerStdioParams, cache_tools_list: bool = False)
175175
"""Create a new MCP server based on the stdio transport.
176176
177177
Args:
178-
params: The params that configure the server. This includes:
179-
- The command (e.g. `python` or `node`) that starts the server.
180-
- The args to pass to the server command (e.g. `foo.py` or `server.js`).
181-
- The environment variables to set for the server.
178+
params: The params that configure the server. This includes the command to run to
179+
start the server, the args to pass to the command, the environment variables to
180+
set for the server, the working directory to use when spawning the process, and
181+
the text encoding used when sending/receiving messages to the server.
182182
cache_tools_list: Whether to cache the tools list. If `True`, the tools list will be
183183
cached and only fetched from the server once. If `False`, the tools list will be
184184
fetched from the server on each call to `list_tools()`. The cache can be
@@ -235,11 +235,9 @@ def __init__(self, params: MCPServerSseParams, cache_tools_list: bool = False):
235235
"""Create a new MCP server based on the HTTP with SSE transport.
236236
237237
Args:
238-
params: The params that configure the server. This includes:
239-
- The URL of the server.
240-
- The headers to send to the server.
241-
- The timeout for the HTTP request.
242-
- The timeout for the SSE connection.
238+
params: The params that configure the server. This includes the URL of the server,
239+
the headers to send to the server, the timeout for the HTTP request, and the
240+
timeout for the SSE connection.
243241
244242
cache_tools_list: Whether to cache the tools list. If `True`, the tools list will be
245243
cached and only fetched from the server once. If `False`, the tools list will be

0 commit comments

Comments
 (0)