MCP Clients
The Model Context Protocol (MCP) is an open protocol that defines a standardized way to provide context to large language models (LLMs). By using MCP, applications can seamlessly interact with LLMs in a structured and efficient manner, making it easier to exchange information, use external tools, and enhance the capabilities of language models in a consistent and reusable way.
This guide will walk you through how to create MCP clients that connect to MCP servers, enabling you to utilize external tools and processes efficiently.
Introduction to MCP
MCP aims to standardize how applications provide contextual information to LLMs and how LLMs can access external tools. MCP servers expose specific tools that are accessible via different communication protocols, such as 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:
- Set up an MCP server that exposes basic MCP tools (
add
andmultiply
) and resources (get_server_file
) - Connect the AG2 framework to this MCP server.
- Communicate using two different transport protocols: stdio and SSE.
Installation
To integrate MCP tools into the AG2 framework, install the required dependencies:
Note: If you have been using
autogen
orpyautogen
, all you need to do is upgrade it using:or
as
pyautogen
,autogen
, andag2
are aliases for the same PyPI package.
Imports
Before diving into the code, let’s go over the main imports used in this guide:
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 overstdio
.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 theSSE
(Server-Sent Events) protocol.create_toolkit
: A helper function that creates a toolkit, wrapping the tools exposed by the MCP server for easier access.
Setting Up an MCP Server
We will create a simple MCP server that exposes:
- two tools:
add
andmultiply
The server listens for requests using either the stdio or SSE transport protocols, depending on the argument passed when starting the server. - one resource endpoint:
get_server_file
The server listens for requests using either the stdio or SSE transport protocols, depending on the argument passed when starting the server.
For more details on creating MCP servers, visit the MCP Python SDK documentation.
Let’s create a Python script (mcp_server.py
) with the following content:
Creating a Toolkit from MCP Tools
To allow AG2 to interact with the MCP server, we need to establish a connection, create a toolkit from the tools exposed by the server, and then register this toolkit with an AG2 agent. The toolkit will provide the necessary functionality for AG2 to call the MCP tools, such as performing mathematical operations and getting resource content.
Steps to Create a Toolkit from MCP Tools
- Wrap MCP tools and resources into a toolkit: The tools exposed by the MCP server (like
add
andmultiply
) and resource endpoints (likeget_server_file
) are wrapped into a toolkit for easy use by AG2. - Register the toolkit with an AG2 agent: This step allows AG2 to use the toolkit’s tools when processing requests.
- Start a conversation: AG2 can now send a message to the MCP server requesting a task to be performed (e.g., adding two numbers) and receive the result.
Setting Up the Client Session and Communicating with the MCP Server
Once the toolkit is created, the next step is to connect AG2 to the MCP server and manage the communication between them. This involves setting up connection parameters and starting a client session, which will facilitate the entire interaction. After establishing the connection, AG2 can send requests and receive responses from the server.
Steps to Set Up the Client Session and Communicate with the MCP Server
- Set up connection parameters: Define how AG2 will communicate with the MCP server. This includes specifying the transport protocol (
stdio
orSSE
) and other connection details. - Start a client session: This session facilitates communication between AG2 and the MCP server. The session is responsible for sending requests, receiving responses, and maintaining the connection state.
Using stdio_client
for Communication
Establish the connection in the client code by using the stdio_client
requires:
- Server Parameters: The connection is defined using the
StdioServerParameters
, where the command (python
) runs the server (mcp_server.py
) withstdio
mode. - Connecting to the Server: The
stdio_client
establishes the connection to the server, and theClientSession
is used to facilitate communication. - Running the Task: Once the connection is established,
create_toolkit_and_run(session)
is called to wrap the MCP tools and perform the task asynchronously.
Expected Output
Using sse_client
for Communication
To interact with the MCP server via SSE
, follow these steps:
-
Open new terminal and start the server using the sse transport mode:
-
Once the server is running, rstablish the connection in the client code by using the
sse_client
:- Server URL: The
sse_client
connects to theSSE
server running locally athttp://127.0.0.1:8000/sse
. - Connecting to the Server: The
sse_client
establishes anSSE
connection, and theClientSession
is used to manage the communication with the server. - Running the Task: Once the session is initialized,
create_toolkit_and_run(session)
is called to create the toolkit from MCP tools and perform the task.
- Server URL: The