You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: update MCPClient for persistent session requirement
Updates MCPClient documentation to reflect changes from pipecat PR #4034:
- Added async context manager requirement to Overview
- Updated all usage examples to use async with MCPClient(...) as mcp:
- Added documentation for start() and close() methods
- Updated method descriptions to reflect persistent session behavior
Copy file name to clipboardExpand all lines: api-reference/server/utilities/mcp/mcp.mdx
+91-70Lines changed: 91 additions & 70 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,13 @@ description: "Service to connect to MCP (Model Context Protocol) servers"
7
7
8
8
MCP is an open standard for enabling AI agents to interact with external data and tools. `MCPClient` provides a way to access and call tools via MCP. For example, instead of writing bespoke function call implementations for an external API, you may use an MCP server that provides a bridge to the API. _Be aware there may be security implications._ See [MCP documenation](https://github.com/modelcontextprotocol) for more details.
9
9
10
+
The client maintains a persistent connection to the MCP server and must be used as an async context manager or explicitly started and closed:
11
+
12
+
```python
13
+
asyncwith MCPClient(server_params=...) as mcp:
14
+
tools =await mcp.register_tools(llm)
15
+
```
16
+
10
17
## Installation
11
18
12
19
To use `MCPClient`, install the required dependencies:
@@ -105,27 +112,26 @@ from pipecat.services.mcp_service import MCPClient
105
112
llm =...
106
113
107
114
# Initialize and configure MCPClient with server parameters
108
-
mcp = MCPClient(
115
+
asyncwith MCPClient(
109
116
server_params=StdioServerParameters(
110
117
command=shutil.which("npx"),
111
118
args=["-y", "@name/mcp-server-name@latest"],
112
119
env={"ENV_API_KEY": "<env_api_key>"},
113
120
)
114
-
)
115
-
116
-
# Create tools schema from the MCP server and register them with llm
117
-
tools =await mcp.register_tools(llm)
118
-
119
-
# Create context with system message and tools
120
-
context = LLMContext(
121
-
messages=[
122
-
{
123
-
"role": "system",
124
-
"content": "You are a helpful assistant in a voice conversation. You have access to MCP tools. Keep responses concise."
125
-
}
126
-
],
127
-
tools=tools
128
-
)
121
+
) as mcp:
122
+
# Create tools schema from the MCP server and register them with llm
123
+
tools =await mcp.register_tools(llm)
124
+
125
+
# Create context with system message and tools
126
+
context = LLMContext(
127
+
messages=[
128
+
{
129
+
"role": "system",
130
+
"content": "You are a helpful assistant in a voice conversation. You have access to MCP tools. Keep responses concise."
131
+
}
132
+
],
133
+
tools=tools
134
+
)
129
135
```
130
136
131
137
### MCP SSE Transport
@@ -138,25 +144,24 @@ from pipecat.services.mcp_service import MCPClient
138
144
llm =...
139
145
140
146
# Initialize and configure MCPClient with SSE server parameters
141
-
mcp = MCPClient(
147
+
asyncwith MCPClient(
142
148
server_params=SseServerParameters(
143
149
url="https://your.mcp.server/sse",
144
150
)
145
-
)
146
-
147
-
# Create tools schema from the MCP server and register them with llm
148
-
tools =await mcp.register_tools(llm)
149
-
150
-
# Create context with system message and tools
151
-
context = LLMContext(
152
-
messages=[
153
-
{
154
-
"role": "system",
155
-
"content": "You are a helpful assistant in a voice conversation. You have access to MCP tools. Keep responses concise."
156
-
}
157
-
],
158
-
tools=tools
159
-
)
151
+
) as mcp:
152
+
# Create tools schema from the MCP server and register them with llm
153
+
tools =await mcp.register_tools(llm)
154
+
155
+
# Create context with system message and tools
156
+
context = LLMContext(
157
+
messages=[
158
+
{
159
+
"role": "system",
160
+
"content": "You are a helpful assistant in a voice conversation. You have access to MCP tools. Keep responses concise."
161
+
}
162
+
],
163
+
tools=tools
164
+
)
160
165
```
161
166
162
167
### MCP Streamable HTTP Transport
@@ -171,18 +176,17 @@ from pipecat.services.mcp_service import MCPClient
171
176
llm =...
172
177
173
178
# Initialize and configure MCPClient with Streamable HTTP parameters
0 commit comments