- MCPClient Python Application
MCPClient Python Application
MCPClient Python Application
This is a Python client application designed to interact with an MCP (Model Context Protocol) server.
Features
- Asynchronous Communication: Uses
asynciofor non-blocking communication between the client and server. - Customizable Server Scripts: The client can connect to both Python and JavaScript-based server scripts.
- Tool Management: Dynamically fetches and interacts with tools available on the connected server.
- Chat Interface: Provides a simple command-line interface to interact with the server in a conversational format.
- Tool Integration: Supports extracting JSON-formatted tool calls from server responses and executing them.
- Environment Variable Loading: Supports loading environment variables from a
.envfile using thedotenvpackage.
Requirements
- Python 3.7 or higher
asynciolibrary (included with Python)requestsfor HTTP requests to the servermcp(custom library for handling MCP communication)dotenvfor environment variable management
Setup
-
Clone the repository (or download the script files) to your local machine.
-
Install required dependencies:
pip install -r requirements.txt -
Create a
.envfile in the root directory to load necessary environment variables. For example:BASE_URL=http://localhost:11434 MODEL=llama3.2 -
Run the client with the path to the server script:
python client.py <server_script_path>The server script can be a Python
.pyor JavaScript.jsfile.
How It Works
- Connecting to the MCP Server: The client connects to the server via standard input/output channels, using the provided script (
.pyor.js). - Processing Queries: The client sends user queries to the server and receives responses. Available tools are listed and can be called directly from the assistant’s replies.
- Tool Execution: If a response contains a valid tool call (in JSON format), the client extracts the call and triggers the respective tool on the server.
- Interaction: The client interacts with the server in a conversational format, displaying results from server tools and continuing the conversation.
Example Workflow
-
The user enters a query like:
Question: What is the weather today? -
The client sends the query to the server, which responds with available tools and information.
-
If the server suggests using a weather tool, the client executes the tool with the necessary parameters and shows the result.
-
The client continues the conversation based on the new information returned by the tool. https://github.com/furey/mongodb-lens