PraisonAI allows you to expose your Python functions as MCP (Model Context Protocol) tools that can be consumed by any MCP client, including Claude Desktop, Cursor, and other AI assistants.
Define Python functions with type hints and docstrings:
Copy
def search_web(query: str, max_results: int = 5) -> dict: """Search the web for information. Args: query: The search query string max_results: Maximum number of results to return """ return {"results": [f"Result for {query}"]}
3
Create and Run MCP Server
Copy
from praisonaiagents.mcp import ToolsMCPServerserver = ToolsMCPServer(name="my-tools")server.register_tool(search_web)server.run() # Starts stdio server
from praisonaiagents.mcp import ToolsMCPServer# Define your tools as regular Python functionsdef search_web(query: str, max_results: int = 5) -> dict: """Search the web for information.""" return { "query": query, "results": [ {"title": f"Result {i+1}", "url": f"https://example.com/{i}"} for i in range(max_results) ] }def calculate(expression: str) -> dict: """Evaluate a mathematical expression.""" try: result = eval(expression) # Use safer parser in production return {"expression": expression, "result": result} except Exception as e: return {"error": str(e)}def get_weather(city: str, units: str = "celsius") -> dict: """Get current weather for a city.""" return { "city": city, "temperature": 22 if units == "celsius" else 72, "condition": "Sunny" }# Create and run MCP serverserver = ToolsMCPServer(name="my-tools")server.register_tools([search_web, calculate, get_weather])server.run() # Starts stdio server (for Claude Desktop)
PraisonAI automatically generates MCP-compatible schemas from your Python functions:
Copy
from praisonaiagents.mcp import function_to_mcp_schemadef search(query: str, max_results: int = 10, include_images: bool = False) -> dict: """Search the web for information. Args: query: The search query max_results: Maximum results to return include_images: Whether to include images """ return {}schema = function_to_mcp_schema(search)print(schema)
Output:
Copy
{ "name": "search", "description": "Search the web for information.", "inputSchema": { "type": "object", "properties": { "query": {"type": "string"}, "max_results": {"type": "integer"}, "include_images": {"type": "boolean"} }, "required": ["query"] }}
launch_tools_mcp_server( tools=None, # List of tool functions tool_names=None, # List of built-in tool names name="praisonai-tools", transport="stdio", # "stdio" or "sse" host="0.0.0.0", port=8080, debug=False)