Custom Python MCP Client

Guide for creating a client to interact with a custom Python MCP server

Custom Python MCP Client

Diagram

Query

AI Agent

Python MCP Client

Python MCP Server

Answer

Overview

The Custom Python MCP Client demonstrates how to integrate a custom Python MCP server with a PraisonAI agent. This client connects to a stock price MCP server to retrieve real-time stock information.

Quick Start

  1. Install Dependencies

    Install the required packages:

    bash
    pip install praisonaiagents mcp
  2. Set Up the Server

    First, set up the Custom Python MCP Server.

  3. Create Custom Python Client

    Save the code above to a file named custom-python-client.py.

    python
    from praisonaiagents import Agent, MCP
    
    agent = Agent(
        instructions="""You are a helpful assistant that can check stock prices and perform other tasks.
        Use the available tools when relevant to answer user questions.""",
        llm="gpt-4o-mini",
        tools = MCP("/path/to/your/venv/bin/python /path/to/your/custom-python-server.py")
    )
    
    # NOTE: Python Path replace with yours: /path/to/your/venv/bin/python
    # NOTE: custom-python-server.py file path, replace it with yours: /path/to/your/custom-python-server.py
    
    agent.start("What is the stock price of Tesla?")
  4. Run the Client

    Execute the client script:

    bash
    python custom-python-client.py

Environment Variables

For better security and flexibility, you can modify the client to use environment variables:

python
import os
from praisonaiagents import Agent, MCP

# Get paths from environment variables or use defaults
python_path = os.getenv("PYTHON_PATH", "/path/to/python")
server_path = os.getenv("SERVER_PATH", "/path/to/server.py")

agent = Agent(
    instructions="""You are a helpful assistant that can check stock prices and perform other tasks.
    Use the available tools when relevant to answer user questions.""",
    llm="gpt-4o-mini",
    tools=MCP(f"{python_path} {server_path}")
)

agent.start("What is the stock price of Tesla?")

This approach allows you to set the paths using environment variables:

bash
export PYTHON_PATH=/path/to/your/venv/bin/python
export SERVER_PATH=/path/to/your/app.py