Create a new file app.py with your custom MCP server implementation:
import yfinance as yffrom mcp.server.fastmcp import FastMCPmcp = 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:
pip install yfinance mcp
3
Create Agent Integration
Create a new file stock_agent.py with the following code:
from praisonaiagents import Agent, MCPagent = 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 pathagent.start("What is the stock price of Tesla?")
The FastMCP class from the mcp-python-sdk package provides a simple way to create MCP servers in Python:
from mcp.server.fastmcp import FastMCP# Create an MCP server with a namemcp = 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 transportif __name__ == "__main__": mcp.run(transport='stdio')
To use your custom MCP server with PraisonAI agents, use the MCP class to specify the command to run your Python script:
from praisonaiagents import Agent, MCPagent = 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 ))