MCP Clients
stdio
(Standard
Input/Output)
and SSE
(Server-Sent
Events).
MCP clients are responsible for interacting with these servers, sending
requests, and processing responses. By integrating MCP into your
application, you can build systems where LLMs can utilize external
computational tools in real-time.
In this guide, we will:
add
and
multiply
) and resources (get_server_file
)Note: If you have been usingautogen
orag2
, all you need to do is upgrade it using:orasautogen
, andag2
are aliases for the same PyPI package.
ClientSession
: Manages the client session for connecting to the
MCP server, allowing you to send requests and handle responses.StdioServerParameters
: Provides parameters to set up communication
with an MCP server over stdio
.stdio_client
: A utility that helps you connect to an MCP server
using the standard input/output protocol.sse_client
: A utility to connect to an MCP server using the SSE
(Server-Sent Events) protocol.create_toolkit
: A helper function that creates a toolkit, wrapping
the tools exposed by the MCP server for easier access.add
and multiply
The server listens for requests
using either the stdio or SSE transport protocols, depending
on the argument passed when starting the server.get_server_file
mcp_server.py
) with the following
content:
add
and multiply
) and resource endpoints
(like get_server_file
) are wrapped into a toolkit for easy use by
AG2.stdio
or SSE
) and other connection details.stdio_client
for Communicationstdio_client
requires:
StdioServerParameters
, where the command (python
) runs the
server (mcp_server.py
) with stdio
mode.stdio_client
establishes the
connection to the server, and the ClientSession
is used to
facilitate communication.create_toolkit_and_run(session)
is called to wrap the MCP tools
and perform the task asynchronously.sse_client
for CommunicationSSE
, follow these steps:
sse_client
:
sse_client
connects to the SSE
server
running locally at http://127.0.0.1:8000/sse
.sse_client
establishes an
SSE
connection, and the ClientSession
is used to manage the
communication with the server.create_toolkit_and_run(session)
is called to create the
toolkit from MCP tools and perform the task.