|
| 1 | +--- |
| 2 | +title: "Custom Python MCP Server" |
| 3 | +sidebarTitle: "Custom Python MCP" |
| 4 | +description: "Guide for creating and using custom Python MCP servers with PraisonAI agents" |
| 5 | +icon: "python" |
| 6 | +--- |
| 7 | + |
| 8 | +# Custom Python MCP Server |
| 9 | + |
| 10 | +```mermaid |
| 11 | +flowchart LR |
| 12 | + In[In] --> Agent[AI Agent] |
| 13 | + Agent --> Tool[Custom Python MCP] |
| 14 | + Tool --> Agent |
| 15 | + Agent --> Out[Out] |
| 16 | + |
| 17 | + style In fill:#8B0000,color:#fff |
| 18 | + style Agent fill:#2E8B57,color:#fff |
| 19 | + style Tool fill:#3776AB,color:#fff |
| 20 | + style Out fill:#8B0000,color:#fff |
| 21 | +``` |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +<Steps> |
| 26 | + <Step title="Create MCP Server"> |
| 27 | + Create a new file `app.py` with your custom MCP server implementation: |
| 28 | + ```python |
| 29 | + import yfinance as yf |
| 30 | + from mcp.server.fastmcp import FastMCP |
| 31 | + |
| 32 | + mcp = FastMCP("stock_prices") |
| 33 | + |
| 34 | + @mcp.tool() |
| 35 | + async def get_stock_price(ticker: str) -> str: |
| 36 | + """Get the current stock price for a given ticker symbol. |
| 37 | + |
| 38 | + Args: |
| 39 | + ticker: Stock ticker symbol (e.g., AAPL, MSFT, GOOG) |
| 40 | + |
| 41 | + Returns: |
| 42 | + Current stock price as a string |
| 43 | + """ |
| 44 | + if not ticker: |
| 45 | + return "No ticker provided" |
| 46 | + try: |
| 47 | + stock = yf.Ticker(ticker) |
| 48 | + info = stock.info |
| 49 | + current_price = info.get('currentPrice') or info.get('regularMarketPrice') |
| 50 | + if not current_price: |
| 51 | + return f"Could not retrieve price for {ticker}" |
| 52 | + return f"${current_price:.2f}" |
| 53 | + |
| 54 | + except Exception as e: |
| 55 | + return f"Error: {str(e)}" |
| 56 | + |
| 57 | + if __name__ == "__main__": |
| 58 | + mcp.run(transport='stdio') |
| 59 | + ``` |
| 60 | + </Step> |
| 61 | + |
| 62 | + <Step title="Install Dependencies"> |
| 63 | + Install the required dependencies in a conda environment: |
| 64 | + ```bash |
| 65 | + conda create -n mcp python=3.10 |
| 66 | + conda activate mcp |
| 67 | + pip install yfinance mcp-python-sdk |
| 68 | + ``` |
| 69 | + </Step> |
| 70 | + |
| 71 | + <Step title="Create Agent Integration"> |
| 72 | + Create a new file `stock_agent.py` with the following code: |
| 73 | + ```python |
| 74 | + from praisonaiagents import Agent, MCP |
| 75 | + |
| 76 | + agent = Agent( |
| 77 | + instructions="""You are a helpful assistant that can check stock prices and perform other tasks. |
| 78 | + Use the available tools when relevant to answer user questions.""", |
| 79 | + llm="gpt-4o-mini", |
| 80 | + tools = MCP("/path/to/python /path/to/app.py") |
| 81 | + ) |
| 82 | + |
| 83 | + # NOTE: Replace with your actual Python path and app.py file path |
| 84 | + |
| 85 | + agent.start("What is the stock price of Tesla?") |
| 86 | + ``` |
| 87 | + </Step> |
| 88 | + |
| 89 | + <Step title="Run the Agent"> |
| 90 | + Execute your script: |
| 91 | + ```bash |
| 92 | + zsh -c "source $(conda info --base)/etc/profile.d/conda.sh && conda activate windsurf && python stock_agent.py" |
| 93 | + ``` |
| 94 | + </Step> |
| 95 | +</Steps> |
| 96 | + |
| 97 | +<Note> |
| 98 | + **Requirements** |
| 99 | + - Python 3.10 or higher |
| 100 | + - Conda for environment management |
| 101 | + - yfinance package for stock data |
| 102 | + - mcp-python-sdk for MCP server implementation |
| 103 | + - OpenAI API key (for the agent's LLM) |
| 104 | +</Note> |
| 105 | + |
| 106 | +## Features |
| 107 | + |
| 108 | +<CardGroup cols={2}> |
| 109 | + <Card title="Custom Tools" icon="wrench"> |
| 110 | + Create your own custom tools with Python. |
| 111 | + </Card> |
| 112 | + <Card title="MCP Integration" icon="plug"> |
| 113 | + Seamless integration with Model Context Protocol. |
| 114 | + </Card> |
| 115 | + <Card title="Type Hints" icon="code"> |
| 116 | + Strong typing for better code reliability. |
| 117 | + </Card> |
| 118 | + <Card title="Async Support" icon="bolt"> |
| 119 | + Built-in support for asynchronous functions. |
| 120 | + </Card> |
| 121 | +</CardGroup> |
| 122 | + |
| 123 | +## Implementation Details |
| 124 | + |
| 125 | +### FastMCP Class |
| 126 | + |
| 127 | +The `FastMCP` class from the `mcp-python-sdk` package provides a simple way to create MCP servers in Python: |
| 128 | + |
| 129 | +```python |
| 130 | +from mcp.server.fastmcp import FastMCP |
| 131 | + |
| 132 | +# Create an MCP server with a name |
| 133 | +mcp = FastMCP("my_tools") |
| 134 | + |
| 135 | +# Define a tool using the @mcp.tool decorator |
| 136 | +@mcp.tool() |
| 137 | +async def my_tool(param1: str, param2: int) -> str: |
| 138 | + """Tool description with clear documentation. |
| 139 | + |
| 140 | + Args: |
| 141 | + param1: Description of param1 |
| 142 | + param2: Description of param2 |
| 143 | + |
| 144 | + Returns: |
| 145 | + Description of the return value |
| 146 | + """ |
| 147 | + # Tool implementation |
| 148 | + return f"Processed {param1} with {param2}" |
| 149 | + |
| 150 | +# Run the server with stdio transport |
| 151 | +if __name__ == "__main__": |
| 152 | + mcp.run(transport='stdio') |
| 153 | +``` |
| 154 | + |
| 155 | +### Agent Integration |
| 156 | + |
| 157 | +To use your custom MCP server with PraisonAI agents, use the `MCP` class to specify the command to run your Python script: |
| 158 | + |
| 159 | +```python |
| 160 | +from praisonaiagents import Agent, MCP |
| 161 | + |
| 162 | +agent = Agent( |
| 163 | + instructions="Agent instructions", |
| 164 | + llm="gpt-4o-mini", |
| 165 | + tools=MCP( |
| 166 | + command="python", # Or full path to Python |
| 167 | + args=["path/to/your/mcp_server.py"] # Path to your MCP server script |
| 168 | + ) |
| 169 | +) |
| 170 | +``` |
0 commit comments