Custom Python MCP Server

Guide for creating and using custom Python MCP servers with PraisonAI agents

Custom Python MCP Server

Diagram

In

AI Agent

Custom Python MCP

Out

Quick Start

  1. Create MCP Server

    Create a new file app.py with your custom MCP server implementation:

    python
    import yfinance as yf
    from mcp.server.fastmcp import FastMCP
    
    mcp = FastMCP("stock_prices")
    
    @mcp.tool()
    async def get_stock_price(ticker: str) -> str:
        """Get the current stock price for a given ticker symbol.
    
        Args:
            ticker: Stock ticker symbol (e.g., AAPL, MSFT, GOOG)
    
        Returns:
            Current stock price as a string
        """
        if not ticker:
            return "No ticker provided"
        try:
            stock = yf.Ticker(ticker)
            info = stock.info
            current_price = info.get('currentPrice') or info.get('regularMarketPrice')
            if not current_price:
                return f"Could not retrieve price for {ticker}"
            return f"${current_price:.2f}"
    
        except Exception as e:
            return f"Error: {str(e)}"
    
    if __name__ == "__main__":
        mcp.run(transport='stdio')
  2. Install Dependencies

    Install the required dependencies in a conda environment:

    bash
    pip install yfinance mcp
  3. Create Agent Integration

    Create a new file stock_agent.py with the following code:

    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/python /path/to/app.py")
    )
    
    # NOTE: Replace with your actual Python path and app.py file path
    
    agent.start("What is the stock price of Tesla?")
  4. Run the Agent

    Execute your script:

    bash
    python stock_agent.py

Features

Implementation Details

FastMCP Class

The FastMCP class from the mcp-python-sdk package provides a simple way to create MCP servers in Python:

python
from mcp.server.fastmcp import FastMCP

# Create an MCP server with a name
mcp = FastMCP("my_tools")

# Define a tool using the @mcp.tool decorator
@mcp.tool()
async def my_tool(param1: str, param2: int) -> str:
    """Tool description with clear documentation.

    Args:
        param1: Description of param1
        param2: Description of param2

    Returns:
        Description of the return value
    """
    # Tool implementation
    return f"Processed {param1} with {param2}"

# Run the server with stdio transport
if __name__ == "__main__":
    mcp.run(transport='stdio')

Agent Integration

To use your custom MCP server with PraisonAI agents, use the MCP class to specify the command to run your Python script:

python
from praisonaiagents import Agent, MCP

agent = Agent(
    instructions="Agent instructions",
    llm="gpt-4o-mini",
    tools=MCP(
        command="python",  # Or full path to Python
        args=["path/to/your/mcp_server.py"]  # Path to your MCP server script
    )
)