Compare commits

...
2 Commits
Author SHA1 Message Date
Mervin Praison bc23d3a104 Release v2.3.9 2025-12-16 08:18:09 +00:00
Mervin Praison bb4e1d8b9e Release v2.3.8 2025-12-16 07:55:55 +00:00
20 changed files with 1984 additions and 22 deletions

No files matched your search

+1 -1
View File
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
# Install Python packages (using latest versions)
RUN pip install --no-cache-dir \
flask \
"praisonai>=2.3.7" \
"praisonai>=2.3.9" \
"praisonai[api]" \
gunicorn \
markdown
+1 -1
View File
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
# Install Python packages (using latest versions)
RUN pip install --no-cache-dir \
praisonai_tools \
"praisonai>=2.3.7" \
"praisonai>=2.3.9" \
"praisonai[chat]" \
"embedchain[github,youtube]"
+1 -1
View File
@@ -20,7 +20,7 @@ RUN mkdir -p /root/.praison
# Install Python packages (using latest versions)
RUN pip install --no-cache-dir \
praisonai_tools \
"praisonai>=2.3.7" \
"praisonai>=2.3.9" \
"praisonai[ui]" \
"praisonai[chat]" \
"praisonai[realtime]" \
+1 -1
View File
@@ -16,7 +16,7 @@ RUN mkdir -p /root/.praison
# Install Python packages (using latest versions)
RUN pip install --no-cache-dir \
praisonai_tools \
"praisonai>=2.3.7" \
"praisonai>=2.3.9" \
"praisonai[ui]" \
"praisonai[crewai]"
+27
View File
@@ -657,6 +657,8 @@ agent = Agent(tools=[my_tool])
Connect to MCP servers for extended capabilities.
### MCP Client (Consume MCP Servers)
```python
from praisonaiagents import Agent, MCP
@@ -683,6 +685,31 @@ agent = Agent(
)
```
### MCP Server (Expose Tools as MCP Server)
Expose your Python functions as MCP tools for external clients (Claude Desktop, Cursor, etc.).
```python
from praisonaiagents.mcp import ToolsMCPServer
# Define your tools as regular Python functions
def search_web(query: str, max_results: int = 5) -> dict:
"""Search the web for information."""
return {"results": [f"Result for {query}"]}
def calculate(expression: str) -> dict:
"""Evaluate a mathematical expression."""
return {"result": eval(expression)}
# Create and run MCP server
server = ToolsMCPServer(name="my-tools")
server.register_tools([search_web, calculate])
server.run() # Starts stdio server (for Claude Desktop)
# Or run as SSE server for web clients
# server.run_sse(host="0.0.0.0", port=8080)
```
---
## Native Web Search
@@ -0,0 +1,144 @@
"""Example: Expose custom tools as MCP server.
This example demonstrates how to create an MCP server that exposes
custom Python functions as MCP tools. These tools can then be used
by any MCP client (Claude Desktop, Cursor, etc.).
Usage:
# Run as stdio server (for Claude Desktop, etc.)
python mcp_server_example.py
# Run as SSE server (for web clients)
python mcp_server_example.py --sse --port 8080
"""
import argparse
from typing import List, Dict, Any
# Define your custom tools as regular Python functions
# Type hints and docstrings are used to generate MCP schemas
def search_web(query: str, max_results: int = 5) -> Dict[str, Any]:
"""Search the web for information.
Args:
query: The search query string
max_results: Maximum number of results to return (default: 5)
Returns:
Search results with titles and URLs
"""
# This is a mock implementation - replace with actual search logic
return {
"query": query,
"results": [
{"title": f"Result {i+1} for '{query}'", "url": f"https://example.com/{i+1}"}
for i in range(max_results)
]
}
def calculate(expression: str) -> Dict[str, Any]:
"""Evaluate a mathematical expression.
Args:
expression: A mathematical expression to evaluate (e.g., "2 + 2 * 3")
Returns:
The result of the calculation
"""
try:
# Safe evaluation of mathematical expressions
# In production, use a proper math parser
allowed_chars = set("0123456789+-*/.() ")
if not all(c in allowed_chars for c in expression):
return {"error": "Invalid characters in expression"}
result = eval(expression) # Note: Use safer alternative in production
return {"expression": expression, "result": result}
except Exception as e:
return {"error": str(e)}
def get_weather(city: str, units: str = "celsius") -> Dict[str, Any]:
"""Get current weather for a city.
Args:
city: Name of the city
units: Temperature units - "celsius" or "fahrenheit" (default: celsius)
Returns:
Weather information for the city
"""
# Mock implementation - replace with actual weather API
return {
"city": city,
"temperature": 22 if units == "celsius" else 72,
"units": units,
"condition": "Sunny",
"humidity": 45
}
def list_files(directory: str = ".", pattern: str = "*") -> List[str]:
"""List files in a directory.
Args:
directory: Directory path to list (default: current directory)
pattern: Glob pattern to filter files (default: *)
Returns:
List of file names matching the pattern
"""
import glob
import os
try:
full_pattern = os.path.join(directory, pattern)
files = glob.glob(full_pattern)
return [os.path.basename(f) for f in files]
except Exception as e:
return [f"Error: {str(e)}"]
def main():
parser = argparse.ArgumentParser(description="Run PraisonAI Tools as MCP Server")
parser.add_argument("--sse", action="store_true", help="Use SSE transport instead of stdio")
parser.add_argument("--port", type=int, default=8080, help="Port for SSE server (default: 8080)")
parser.add_argument("--host", type=str, default="0.0.0.0", help="Host for SSE server")
parser.add_argument("--debug", action="store_true", help="Enable debug logging")
args = parser.parse_args()
# Import MCP server components
from praisonaiagents.mcp import ToolsMCPServer
# Create server with custom name
server = ToolsMCPServer(
name="praisonai-custom-tools",
debug=args.debug
)
# Register all tools
server.register_tools([
search_web,
calculate,
get_weather,
list_files
])
# Print registered tools
print(f"📦 Registered {len(server.tools)} tools:")
for name in server.get_tool_names():
print(f" - {name}")
print()
# Run the server
if args.sse:
server.run_sse(host=args.host, port=args.port)
else:
server.run_stdio()
if __name__ == "__main__":
main()
@@ -1,9 +1,40 @@
"""
Model Context Protocol (MCP) integration for PraisonAI Agents.
This package provides classes and utilities for connecting to MCP servers
using different transport methods (stdio, SSE, etc.).
This package provides classes and utilities for:
- Connecting to MCP servers (as client) using different transports (stdio, SSE, etc.)
- Exposing Python functions as MCP servers for external clients
Client Usage:
from praisonaiagents.mcp import MCP
agent = Agent(
tools=MCP("npx -y @modelcontextprotocol/server-weather")
)
Server Usage:
from praisonaiagents.mcp import ToolsMCPServer
def my_tool(query: str) -> str:
'''Search for information.'''
return f"Results for {query}"
server = ToolsMCPServer(name="my-tools")
server.register_tool(my_tool)
server.run() # Starts MCP server
"""
from .mcp import MCP
from .mcp_server import ToolsMCPServer, launch_tools_mcp_server
from .mcp_utils import function_to_mcp_schema, get_tool_metadata, python_type_to_json_schema
__all__ = ["MCP"]
__all__ = [
# Client
"MCP",
# Server
"ToolsMCPServer",
"launch_tools_mcp_server",
# Utilities
"function_to_mcp_schema",
"get_tool_metadata",
"python_type_to_json_schema",
]
@@ -0,0 +1,356 @@
"""MCP Server wrapper for exposing PraisonAI tools as MCP servers.
This module provides a simple way to expose Python functions as MCP tools
that can be consumed by any MCP client (Claude Desktop, Cursor, etc.).
Usage:
from praisonaiagents.mcp import ToolsMCPServer
def search(query: str) -> str:
'''Search the web.'''
return f"Results for {query}"
server = ToolsMCPServer(name="my-tools")
server.register_tool(search)
server.run() # Starts MCP server (stdio by default)
"""
import asyncio
import inspect
import logging
from typing import Any, Callable, Dict, List, Optional
from .mcp_utils import function_to_mcp_schema
logger = logging.getLogger(__name__)
class ToolsMCPServer:
"""MCP Server that exposes Python functions as MCP tools.
This class wraps Python functions and exposes them as MCP tools
using the FastMCP library. It supports both sync and async functions.
Example:
server = ToolsMCPServer(name="my-tools")
def search(query: str) -> str:
'''Search the web.'''
return f"Results for {query}"
server.register_tool(search)
server.run() # Starts stdio server
"""
def __init__(
self,
name: str = "praisonai-tools",
tools: Optional[List[Callable]] = None,
debug: bool = False
):
"""Initialize the MCP server.
Args:
name: Name of the MCP server
tools: Optional list of tools to register on init
debug: Enable debug logging
"""
self.name = name
self.tools: List[Callable] = []
self._tool_map: Dict[str, Callable] = {}
self._fastmcp = None
self._debug = debug
if debug:
logger.setLevel(logging.DEBUG)
# Register initial tools if provided
if tools:
self.register_tools(tools)
def register_tool(self, func: Callable) -> None:
"""Register a single tool function.
Args:
func: The function to register as an MCP tool
"""
if not callable(func):
raise TypeError(f"Expected callable, got {type(func)}")
self.tools.append(func)
tool_name = getattr(func, '__mcp_name__', None) or func.__name__
self._tool_map[tool_name] = func
logger.debug(f"Registered tool: {tool_name}")
def register_tools(self, funcs: List[Callable]) -> None:
"""Register multiple tool functions.
Args:
funcs: List of functions to register as MCP tools
"""
for func in funcs:
self.register_tool(func)
def get_tool_schemas(self) -> List[Dict[str, Any]]:
"""Get MCP schemas for all registered tools.
Returns:
List of MCP tool schema dictionaries
"""
return [function_to_mcp_schema(tool) for tool in self.tools]
def get_tool_names(self) -> List[str]:
"""Get names of all registered tools.
Returns:
List of tool names
"""
return list(self._tool_map.keys())
def execute_tool(self, tool_name: str, arguments: Dict[str, Any]) -> Any:
"""Execute a registered tool synchronously.
Args:
tool_name: Name of the tool to execute
arguments: Arguments to pass to the tool
Returns:
Result from the tool execution
Raises:
ValueError: If tool is not found
"""
if tool_name not in self._tool_map:
raise ValueError(f"Tool '{tool_name}' not found")
func = self._tool_map[tool_name]
# Handle async functions
if asyncio.iscoroutinefunction(func):
loop = asyncio.new_event_loop()
try:
return loop.run_until_complete(func(**arguments))
finally:
loop.close()
return func(**arguments)
async def execute_tool_async(self, tool_name: str, arguments: Dict[str, Any]) -> Any:
"""Execute a registered tool asynchronously.
Args:
tool_name: Name of the tool to execute
arguments: Arguments to pass to the tool
Returns:
Result from the tool execution
Raises:
ValueError: If tool is not found
"""
if tool_name not in self._tool_map:
raise ValueError(f"Tool '{tool_name}' not found")
func = self._tool_map[tool_name]
if asyncio.iscoroutinefunction(func):
return await func(**arguments)
# Run sync function in executor
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, lambda: func(**arguments))
def get_fastmcp(self):
"""Get or create FastMCP instance with registered tools.
Returns:
FastMCP instance with all tools registered
"""
if self._fastmcp is None:
self._fastmcp = self._create_fastmcp()
return self._fastmcp
def _create_fastmcp(self):
"""Create FastMCP instance and register all tools.
Returns:
Configured FastMCP instance
"""
try:
from mcp.server.fastmcp import FastMCP
except ImportError:
raise ImportError(
"FastMCP not available. Install with: pip install mcp"
)
mcp = FastMCP(self.name)
# Register each tool with FastMCP
for func in self.tools:
self._register_tool_with_fastmcp(mcp, func)
return mcp
def _register_tool_with_fastmcp(self, mcp, func: Callable) -> None:
"""Register a single function with FastMCP.
Args:
mcp: FastMCP instance
func: Function to register
"""
# Get tool metadata
tool_name = getattr(func, '__mcp_name__', None) or func.__name__
description = getattr(func, '__mcp_description__', None)
if description is None:
docstring = inspect.getdoc(func)
description = docstring.split('\n')[0] if docstring else tool_name
# Create wrapper that preserves signature
if asyncio.iscoroutinefunction(func):
@mcp.tool(name=tool_name, description=description)
async def tool_wrapper(**kwargs):
return await func(**kwargs)
else:
@mcp.tool(name=tool_name, description=description)
def tool_wrapper(**kwargs):
return func(**kwargs)
# Copy signature for proper schema generation
tool_wrapper.__signature__ = inspect.signature(func)
tool_wrapper.__annotations__ = getattr(func, '__annotations__', {})
def run(self, transport: str = "stdio") -> None:
"""Run the MCP server.
Args:
transport: Transport type - "stdio" or "sse"
"""
if transport == "stdio":
self.run_stdio()
elif transport == "sse":
self.run_sse()
else:
raise ValueError(f"Unknown transport: {transport}. Use 'stdio' or 'sse'")
def run_stdio(self) -> None:
"""Run the MCP server using stdio transport."""
mcp = self.get_fastmcp()
print(f"🚀 Starting MCP server '{self.name}' with stdio transport")
print(f"🛠️ Available tools: {', '.join(self.get_tool_names())}")
mcp.run()
def run_sse(self, host: str = "0.0.0.0", port: int = 8080) -> None:
"""Run the MCP server using SSE transport.
Args:
host: Host to bind to
port: Port to listen on
"""
try:
import uvicorn
from mcp.server.sse import SseServerTransport
from starlette.applications import Starlette
from starlette.requests import Request
from starlette.routing import Mount, Route
except ImportError as e:
raise ImportError(
f"SSE dependencies not available: {e}. "
"Install with: pip install uvicorn starlette"
)
mcp = self.get_fastmcp()
# Set up SSE transport
sse_path = "/sse"
messages_path = "/messages/"
sse_transport = SseServerTransport(messages_path)
async def handle_sse(request: Request):
async with sse_transport.connect_sse(
request.scope, request.receive, request._send
) as (read_stream, write_stream):
await mcp._mcp_server.run(
read_stream,
write_stream,
mcp._mcp_server.create_initialization_options()
)
app = Starlette(
debug=self._debug,
routes=[
Route(sse_path, endpoint=handle_sse),
Mount(messages_path, app=sse_transport.handle_post_message),
]
)
print(f"🚀 Starting MCP server '{self.name}' with SSE transport")
print(f"📡 SSE endpoint: http://{host}:{port}{sse_path}")
print(f"🛠️ Available tools: {', '.join(self.get_tool_names())}")
uvicorn.run(app, host=host, port=port)
def launch_tools_mcp_server(
tools: Optional[List[Callable]] = None,
tool_names: Optional[List[str]] = None,
name: str = "praisonai-tools",
transport: str = "stdio",
host: str = "0.0.0.0",
port: int = 8080,
debug: bool = False
) -> None:
"""Convenience function to launch an MCP server with tools.
Args:
tools: List of tool functions to expose
tool_names: List of tool names to load from praisonaiagents.tools
name: Name of the MCP server
transport: Transport type - "stdio" or "sse"
host: Host for SSE transport
port: Port for SSE transport
debug: Enable debug logging
Example:
# With custom tools
def my_search(query: str) -> str:
return f"Results for {query}"
launch_tools_mcp_server(tools=[my_search])
# With built-in tools
launch_tools_mcp_server(tool_names=["tavily_search", "exa_search"])
"""
server = ToolsMCPServer(name=name, debug=debug)
# Register custom tools
if tools:
server.register_tools(tools)
# Load and register tools by name
if tool_names:
from praisonaiagents import tools as praison_tools
for tool_name in tool_names:
if hasattr(praison_tools, tool_name):
tool_func = getattr(praison_tools, tool_name)
if callable(tool_func):
server.register_tool(tool_func)
else:
logger.warning(f"Tool '{tool_name}' is not callable")
else:
logger.warning(f"Tool '{tool_name}' not found in praisonaiagents.tools")
if not server.tools:
raise ValueError("No tools registered. Provide 'tools' or 'tool_names'.")
# Run the server
if transport == "stdio":
server.run_stdio()
elif transport == "sse":
server.run_sse(host=host, port=port)
else:
raise ValueError(f"Unknown transport: {transport}")
@@ -0,0 +1,238 @@
"""MCP utility functions for converting Python functions to MCP tool schemas.
This module provides utilities to convert Python functions with type hints
into MCP-compatible tool schemas for exposing as MCP server tools.
Usage:
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def my_tool(query: str, max_results: int = 10) -> str:
'''Search for information.'''
return "results"
schema = function_to_mcp_schema(my_tool)
# Returns MCP-compatible tool schema
"""
import inspect
from typing import Any, Callable, Dict, List, Union, get_type_hints, get_origin, get_args
def python_type_to_json_schema(python_type: Any) -> Dict[str, Any]:
"""Convert a Python type annotation to JSON Schema.
Args:
python_type: A Python type annotation (e.g., str, int, List[str], Optional[int])
Returns:
JSON Schema dictionary representing the type
"""
# Handle None type
if python_type is None or python_type is type(None):
return {"type": "null"}
# Handle Any type
if python_type is Any:
return {}
# Handle basic types
type_mapping = {
str: {"type": "string"},
int: {"type": "integer"},
float: {"type": "number"},
bool: {"type": "boolean"},
bytes: {"type": "string"},
}
if python_type in type_mapping:
return type_mapping[python_type]
# Handle generic types (List, Dict, Optional, Union)
origin = get_origin(python_type)
args = get_args(python_type)
# Handle List[T]
if origin is list:
if args:
return {
"type": "array",
"items": python_type_to_json_schema(args[0])
}
return {"type": "array", "items": {}}
# Handle Dict[K, V]
if origin is dict:
return {"type": "object"}
# Handle Optional[T] (Union[T, None])
if origin is Union:
# Filter out None type
non_none_args = [arg for arg in args if arg is not type(None)]
if len(non_none_args) == 1:
# Optional[T] case
return python_type_to_json_schema(non_none_args[0])
elif len(non_none_args) > 1:
# Union of multiple types
return {"oneOf": [python_type_to_json_schema(arg) for arg in non_none_args]}
return {}
# Handle tuple
if origin is tuple:
if args:
return {
"type": "array",
"items": [python_type_to_json_schema(arg) for arg in args]
}
return {"type": "array"}
# Handle set
if origin is set:
if args:
return {
"type": "array",
"items": python_type_to_json_schema(args[0]),
"uniqueItems": True
}
return {"type": "array", "uniqueItems": True}
# Default to object for unknown types
return {"type": "object"}
def get_tool_metadata(func: Callable) -> Dict[str, str]:
"""Extract metadata from a function for MCP tool registration.
Checks for custom attributes (__mcp_name__, __mcp_description__) first,
then falls back to function name and docstring.
Args:
func: The function to extract metadata from
Returns:
Dictionary with 'name' and 'description' keys
"""
# Get name - check for custom attribute first
name = getattr(func, '__mcp_name__', None) or func.__name__
# Get description - check for custom attribute first
description = getattr(func, '__mcp_description__', None)
if description is None:
# Extract from docstring
docstring = inspect.getdoc(func)
if docstring:
# Use first line of docstring as description
description = docstring.split('\n')[0].strip()
else:
# Fall back to function name
description = name
return {
"name": name,
"description": description
}
def function_to_mcp_schema(func: Callable) -> Dict[str, Any]:
"""Convert a Python function to an MCP tool schema.
This function inspects the function's signature, type hints, and docstring
to generate a complete MCP-compatible tool schema.
Args:
func: The function to convert
Returns:
MCP tool schema dictionary with:
- name: Tool name
- description: Tool description
- inputSchema: JSON Schema for parameters
Example:
def search(query: str, max_results: int = 10) -> str:
'''Search the web for information.'''
return "results"
schema = function_to_mcp_schema(search)
# {
# "name": "search",
# "description": "Search the web for information.",
# "inputSchema": {
# "type": "object",
# "properties": {
# "query": {"type": "string"},
# "max_results": {"type": "integer"}
# },
# "required": ["query"]
# }
# }
"""
# Get metadata
metadata = get_tool_metadata(func)
# Get function signature
sig = inspect.signature(func)
# Try to get type hints
try:
type_hints = get_type_hints(func)
except Exception:
type_hints = {}
# Build properties and required list
properties = {}
required = []
for param_name, param in sig.parameters.items():
# Skip self, cls, and special parameters
if param_name in ('self', 'cls'):
continue
# Skip *args and **kwargs
if param.kind in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD):
continue
# Get type annotation
param_type = type_hints.get(param_name, param.annotation)
if param_type is inspect.Parameter.empty:
# No type annotation - default to string
properties[param_name] = {"type": "string"}
else:
properties[param_name] = python_type_to_json_schema(param_type)
# Check if parameter is required (no default value)
if param.default is inspect.Parameter.empty:
# Check if it's Optional type
origin = get_origin(param_type) if param_type is not inspect.Parameter.empty else None
args = get_args(param_type) if param_type is not inspect.Parameter.empty else ()
is_optional = origin is Union and type(None) in args
if not is_optional:
required.append(param_name)
# Build the schema
schema = {
"name": metadata["name"],
"description": metadata["description"],
"inputSchema": {
"type": "object",
"properties": properties,
"required": required
}
}
return schema
def functions_to_mcp_tools(functions: List[Callable]) -> List[Dict[str, Any]]:
"""Convert multiple functions to MCP tool schemas.
Args:
functions: List of functions to convert
Returns:
List of MCP tool schemas
"""
return [function_to_mcp_schema(func) for func in functions]
@@ -208,6 +208,16 @@ TOOL_MAPPINGS = {
'exa_answer_async': ('.exa_tools', None),
'ExaTools': ('.exa_tools', 'ExaTools'),
'exa_tools': ('.exa_tools', None),
# Crawl4AI Tools (async web crawling)
'crawl4ai': ('.crawl4ai_tools', None),
'crawl4ai_many': ('.crawl4ai_tools', None),
'crawl4ai_extract': ('.crawl4ai_tools', None),
'crawl4ai_llm_extract': ('.crawl4ai_tools', None),
'crawl4ai_sync': ('.crawl4ai_tools', None),
'crawl4ai_extract_sync': ('.crawl4ai_tools', None),
'Crawl4AITools': ('.crawl4ai_tools', 'Crawl4AITools'),
'crawl4ai_tools': ('.crawl4ai_tools', None),
}
_instances = {} # Cache for class instances
@@ -219,8 +229,8 @@ def __getattr__(name: str) -> Any:
module_path, class_name = TOOL_MAPPINGS[name]
# Return class itself (not instance) for ClaudeMemoryTool, TavilyTools, YouTools, ExaTools
if name in ('ClaudeMemoryTool', 'TavilyTools', 'YouTools', 'ExaTools'):
# Return class itself (not instance) for ClaudeMemoryTool, TavilyTools, YouTools, ExaTools, Crawl4AITools
if name in ('ClaudeMemoryTool', 'TavilyTools', 'YouTools', 'ExaTools', 'Crawl4AITools'):
module = import_module(module_path, __package__)
return getattr(module, class_name)
@@ -242,11 +252,13 @@ def __getattr__(name: str) -> Any:
'tavily_search_async', 'tavily_extract_async',
'ydc', 'ydc_search', 'ydc_contents', 'ydc_news', 'ydc_images',
'exa', 'exa_search', 'exa_search_contents', 'exa_find_similar', 'exa_answer',
'exa_search_async', 'exa_search_contents_async', 'exa_answer_async'
'exa_search_async', 'exa_search_contents_async', 'exa_answer_async',
'crawl4ai', 'crawl4ai_many', 'crawl4ai_extract', 'crawl4ai_llm_extract',
'crawl4ai_sync', 'crawl4ai_extract_sync'
]:
return getattr(module, name)
if name in ['file_tools', 'pandas_tools', 'wikipedia_tools',
'newspaper_tools', 'arxiv_tools', 'spider_tools', 'duckdb_tools', 'mongodb_tools', 'csv_tools', 'json_tools', 'excel_tools', 'xml_tools', 'yaml_tools', 'calculator_tools', 'python_tools', 'shell_tools', 'cot_tools', 'tavily_tools', 'youdotcom_tools', 'exa_tools']:
'newspaper_tools', 'arxiv_tools', 'spider_tools', 'duckdb_tools', 'mongodb_tools', 'csv_tools', 'json_tools', 'excel_tools', 'xml_tools', 'yaml_tools', 'calculator_tools', 'python_tools', 'shell_tools', 'cot_tools', 'tavily_tools', 'youdotcom_tools', 'exa_tools', 'crawl4ai_tools']:
return module # Returns the callable module
return getattr(module, name)
else:
@@ -0,0 +1,630 @@
"""Crawl4AI web crawling and extraction tools.
Crawl4AI provides async web crawling with JavaScript rendering, content extraction,
and LLM-based data extraction capabilities.
Usage:
from praisonaiagents.tools import crawl4ai, crawl4ai_extract
# Simple crawl
result = await crawl4ai("https://example.com")
print(result["markdown"])
# Extract structured data
result = await crawl4ai_extract(
"https://example.com/products",
schema={"name": "Products", "baseSelector": ".product", ...}
)
# Or use the class directly
from praisonaiagents.tools import Crawl4AITools
tools = Crawl4AITools()
result = await tools.crawl("https://example.com")
"""
from typing import List, Dict, Any, Optional, Union
import logging
import asyncio
from importlib import util
def _check_crawl4ai_available() -> tuple[bool, Optional[str]]:
"""Check if Crawl4AI is available.
Returns:
Tuple of (is_available, error_message)
"""
if util.find_spec("crawl4ai") is None:
return False, "crawl4ai package is not installed. Install it with: pip install crawl4ai && crawl4ai-setup"
return True, None
class Crawl4AITools:
"""Crawl4AI web crawling and extraction tools.
Provides methods for:
- crawl: Basic web crawling with markdown output
- crawl_many: Crawl multiple URLs concurrently
- extract_css: Extract data using CSS selectors
- extract_llm: Extract data using LLM
Example:
from praisonaiagents.tools import Crawl4AITools
tools = Crawl4AITools()
result = await tools.crawl("https://example.com")
print(result["markdown"])
"""
def __init__(
self,
headless: bool = True,
verbose: bool = False,
browser_type: str = "chromium"
):
"""Initialize Crawl4AITools.
Args:
headless: Run browser in headless mode (default True)
verbose: Enable verbose logging (default False)
browser_type: Browser type - "chromium", "firefox", or "webkit"
"""
self.headless = headless
self.verbose = verbose
self.browser_type = browser_type
self.logger = logging.getLogger(__name__)
self._crawler = None
async def _get_crawler(self):
"""Get or create AsyncWebCrawler instance."""
is_available, error = _check_crawl4ai_available()
if not is_available:
raise ImportError(error)
from crawl4ai import AsyncWebCrawler, BrowserConfig
if self._crawler is None:
browser_config = BrowserConfig(
headless=self.headless,
verbose=self.verbose,
browser_type=self.browser_type
)
self._crawler = AsyncWebCrawler(config=browser_config)
await self._crawler.start()
return self._crawler
async def close(self):
"""Close the crawler and release resources."""
if self._crawler is not None:
await self._crawler.close()
self._crawler = None
async def crawl(
self,
url: str,
word_count_threshold: int = 10,
css_selector: Optional[str] = None,
excluded_tags: Optional[List[str]] = None,
exclude_external_links: bool = False,
js_code: Optional[Union[str, List[str]]] = None,
wait_for: Optional[str] = None,
screenshot: bool = False,
pdf: bool = False,
bypass_cache: bool = True
) -> Dict[str, Any]:
"""Crawl a single URL and return content.
Args:
url: The URL to crawl
word_count_threshold: Minimum words per text block (default 10)
css_selector: CSS selector to focus on specific content
excluded_tags: HTML tags to exclude (e.g., ["nav", "footer"])
exclude_external_links: Remove external links from content
js_code: JavaScript code to execute before crawling
wait_for: Wait condition - "css:selector" or "js:() => boolean"
screenshot: Capture screenshot (base64)
pdf: Generate PDF
bypass_cache: Skip cache and fetch fresh content (default True)
Returns:
Dict with 'markdown', 'html', 'cleaned_html', 'links', 'media', etc.
"""
try:
crawler = await self._get_crawler()
from crawl4ai import CrawlerRunConfig, CacheMode
config = CrawlerRunConfig(
word_count_threshold=word_count_threshold,
css_selector=css_selector,
excluded_tags=excluded_tags or [],
exclude_external_links=exclude_external_links,
js_code=js_code,
wait_for=wait_for,
screenshot=screenshot,
pdf=pdf,
cache_mode=CacheMode.BYPASS if bypass_cache else CacheMode.ENABLED
)
result = await crawler.arun(url=url, config=config)
if not result.success:
return {"error": f"Crawl failed: {result.error_message}", "success": False}
response = {
"success": True,
"url": result.url,
"markdown": result.markdown.raw_markdown if hasattr(result.markdown, 'raw_markdown') else str(result.markdown),
"html": result.html,
"cleaned_html": result.cleaned_html,
"links": result.links,
"media": result.media,
"metadata": result.metadata
}
if hasattr(result.markdown, 'fit_markdown') and result.markdown.fit_markdown:
response["fit_markdown"] = result.markdown.fit_markdown
if screenshot and result.screenshot:
response["screenshot"] = result.screenshot
if pdf and result.pdf:
response["pdf"] = result.pdf
return response
except Exception as e:
self.logger.error(f"Crawl4AI crawl error: {e}")
return {"error": f"Crawl4AI crawl error: {str(e)}", "success": False}
async def crawl_many(
self,
urls: List[str],
word_count_threshold: int = 10,
css_selector: Optional[str] = None,
bypass_cache: bool = True,
stream: bool = False
) -> List[Dict[str, Any]]:
"""Crawl multiple URLs concurrently.
Args:
urls: List of URLs to crawl
word_count_threshold: Minimum words per text block
css_selector: CSS selector to focus on specific content
bypass_cache: Skip cache and fetch fresh content
stream: If True, returns async generator
Returns:
List of crawl results
"""
try:
crawler = await self._get_crawler()
from crawl4ai import CrawlerRunConfig, CacheMode
config = CrawlerRunConfig(
word_count_threshold=word_count_threshold,
css_selector=css_selector,
cache_mode=CacheMode.BYPASS if bypass_cache else CacheMode.ENABLED,
stream=stream
)
results = []
if stream:
async for result in await crawler.arun_many(urls, config=config):
if result.success:
results.append({
"success": True,
"url": result.url,
"markdown": result.markdown.raw_markdown if hasattr(result.markdown, 'raw_markdown') else str(result.markdown),
"html": result.html
})
else:
results.append({
"success": False,
"url": result.url,
"error": result.error_message
})
else:
raw_results = await crawler.arun_many(urls, config=config)
for result in raw_results:
if result.success:
results.append({
"success": True,
"url": result.url,
"markdown": result.markdown.raw_markdown if hasattr(result.markdown, 'raw_markdown') else str(result.markdown),
"html": result.html
})
else:
results.append({
"success": False,
"url": result.url,
"error": result.error_message
})
return results
except Exception as e:
self.logger.error(f"Crawl4AI crawl_many error: {e}")
return [{"error": f"Crawl4AI crawl_many error: {str(e)}", "success": False}]
async def extract_css(
self,
url: str,
schema: Dict[str, Any],
js_code: Optional[Union[str, List[str]]] = None,
wait_for: Optional[str] = None,
bypass_cache: bool = True
) -> Dict[str, Any]:
"""Extract structured data using CSS selectors.
Args:
url: The URL to crawl (or "raw://<html>" for raw HTML)
schema: Extraction schema with baseSelector and fields
js_code: JavaScript to execute before extraction
wait_for: Wait condition before extraction
bypass_cache: Skip cache
Returns:
Dict with 'data' containing extracted items
Example schema:
{
"name": "Products",
"baseSelector": "div.product",
"fields": [
{"name": "title", "selector": "h2", "type": "text"},
{"name": "price", "selector": ".price", "type": "text"},
{"name": "link", "selector": "a", "type": "attribute", "attribute": "href"}
]
}
"""
try:
crawler = await self._get_crawler()
from crawl4ai import CrawlerRunConfig, CacheMode
from crawl4ai.extraction_strategy import JsonCssExtractionStrategy
extraction_strategy = JsonCssExtractionStrategy(schema, verbose=self.verbose)
config = CrawlerRunConfig(
extraction_strategy=extraction_strategy,
js_code=js_code,
wait_for=wait_for,
cache_mode=CacheMode.BYPASS if bypass_cache else CacheMode.ENABLED
)
result = await crawler.arun(url=url, config=config)
if not result.success:
return {"error": f"Extraction failed: {result.error_message}", "success": False}
import json
try:
data = json.loads(result.extracted_content) if result.extracted_content else []
except json.JSONDecodeError:
data = result.extracted_content
return {
"success": True,
"url": result.url,
"data": data,
"count": len(data) if isinstance(data, list) else 1
}
except Exception as e:
self.logger.error(f"Crawl4AI extract_css error: {e}")
return {"error": f"Crawl4AI extract_css error: {str(e)}", "success": False}
async def extract_llm(
self,
url: str,
instruction: str,
schema: Optional[Dict[str, Any]] = None,
provider: str = "openai/gpt-4o-mini",
api_token: Optional[str] = None,
js_code: Optional[Union[str, List[str]]] = None,
wait_for: Optional[str] = None,
bypass_cache: bool = True
) -> Dict[str, Any]:
"""Extract structured data using LLM.
Args:
url: The URL to crawl
instruction: Instructions for the LLM on what to extract
schema: Optional Pydantic-style JSON schema for structured output
provider: LLM provider string (e.g., "openai/gpt-4o-mini", "ollama/llama3.3")
api_token: API token for the provider (uses env var if not provided)
js_code: JavaScript to execute before extraction
wait_for: Wait condition before extraction
bypass_cache: Skip cache
Returns:
Dict with 'data' containing extracted content
"""
try:
crawler = await self._get_crawler()
import os
from crawl4ai import CrawlerRunConfig, CacheMode, LLMConfig
from crawl4ai.extraction_strategy import LLMExtractionStrategy
# Get API token from env if not provided
if api_token is None and "openai" in provider.lower():
api_token = os.environ.get("OPENAI_API_KEY")
llm_config = LLMConfig(provider=provider, api_token=api_token)
extraction_strategy = LLMExtractionStrategy(
llm_config=llm_config,
schema=schema,
extraction_type="schema" if schema else "block",
instruction=instruction,
verbose=self.verbose
)
config = CrawlerRunConfig(
extraction_strategy=extraction_strategy,
js_code=js_code,
wait_for=wait_for,
cache_mode=CacheMode.BYPASS if bypass_cache else CacheMode.ENABLED
)
result = await crawler.arun(url=url, config=config)
if not result.success:
return {"error": f"LLM extraction failed: {result.error_message}", "success": False}
import json
try:
data = json.loads(result.extracted_content) if result.extracted_content else None
except json.JSONDecodeError:
data = result.extracted_content
return {
"success": True,
"url": result.url,
"data": data
}
except Exception as e:
self.logger.error(f"Crawl4AI extract_llm error: {e}")
return {"error": f"Crawl4AI extract_llm error: {str(e)}", "success": False}
# Standalone async functions for easy access
async def crawl4ai(
url: str,
css_selector: Optional[str] = None,
js_code: Optional[str] = None,
wait_for: Optional[str] = None,
screenshot: bool = False
) -> Dict[str, Any]:
"""Crawl a URL and return markdown content.
Args:
url: The URL to crawl
css_selector: CSS selector to focus on specific content
js_code: JavaScript to execute before crawling
wait_for: Wait condition - "css:selector" or "js:() => boolean"
screenshot: Capture screenshot
Returns:
Dict with 'markdown', 'html', 'links', 'media', etc.
Example:
result = await crawl4ai("https://example.com")
print(result["markdown"])
"""
is_available, error = _check_crawl4ai_available()
if not is_available:
logging.error(error)
return {"error": error, "success": False}
tools = Crawl4AITools()
try:
result = await tools.crawl(
url=url,
css_selector=css_selector,
js_code=js_code,
wait_for=wait_for,
screenshot=screenshot
)
return result
finally:
await tools.close()
async def crawl4ai_many(
urls: List[str],
css_selector: Optional[str] = None
) -> List[Dict[str, Any]]:
"""Crawl multiple URLs concurrently.
Args:
urls: List of URLs to crawl
css_selector: CSS selector to focus on specific content
Returns:
List of crawl results
Example:
results = await crawl4ai_many(["https://example.com", "https://example.org"])
"""
is_available, error = _check_crawl4ai_available()
if not is_available:
logging.error(error)
return [{"error": error, "success": False}]
tools = Crawl4AITools()
try:
return await tools.crawl_many(urls=urls, css_selector=css_selector)
finally:
await tools.close()
async def crawl4ai_extract(
url: str,
schema: Dict[str, Any],
js_code: Optional[str] = None,
wait_for: Optional[str] = None
) -> Dict[str, Any]:
"""Extract structured data using CSS selectors.
Args:
url: The URL to crawl
schema: Extraction schema with baseSelector and fields
js_code: JavaScript to execute before extraction
wait_for: Wait condition before extraction
Returns:
Dict with 'data' containing extracted items
Example:
schema = {
"name": "Products",
"baseSelector": "div.product",
"fields": [
{"name": "title", "selector": "h2", "type": "text"},
{"name": "price", "selector": ".price", "type": "text"}
]
}
result = await crawl4ai_extract("https://example.com/products", schema)
"""
is_available, error = _check_crawl4ai_available()
if not is_available:
logging.error(error)
return {"error": error, "success": False}
tools = Crawl4AITools()
try:
return await tools.extract_css(
url=url,
schema=schema,
js_code=js_code,
wait_for=wait_for
)
finally:
await tools.close()
async def crawl4ai_llm_extract(
url: str,
instruction: str,
schema: Optional[Dict[str, Any]] = None,
provider: str = "openai/gpt-4o-mini"
) -> Dict[str, Any]:
"""Extract data using LLM.
Args:
url: The URL to crawl
instruction: Instructions for the LLM
schema: Optional JSON schema for structured output
provider: LLM provider (default "openai/gpt-4o-mini")
Returns:
Dict with 'data' containing extracted content
Example:
result = await crawl4ai_llm_extract(
"https://example.com/about",
instruction="Extract company name, founding year, and description"
)
"""
is_available, error = _check_crawl4ai_available()
if not is_available:
logging.error(error)
return {"error": error, "success": False}
tools = Crawl4AITools()
try:
return await tools.extract_llm(
url=url,
instruction=instruction,
schema=schema,
provider=provider
)
finally:
await tools.close()
# Synchronous wrappers for convenience
def crawl4ai_sync(
url: str,
css_selector: Optional[str] = None,
js_code: Optional[str] = None,
wait_for: Optional[str] = None
) -> Dict[str, Any]:
"""Synchronous version of crawl4ai.
Args:
url: The URL to crawl
css_selector: CSS selector to focus on specific content
js_code: JavaScript to execute
wait_for: Wait condition
Returns:
Dict with crawl results
"""
return asyncio.get_event_loop().run_until_complete(
crawl4ai(url, css_selector, js_code, wait_for)
)
def crawl4ai_extract_sync(
url: str,
schema: Dict[str, Any],
js_code: Optional[str] = None
) -> Dict[str, Any]:
"""Synchronous version of crawl4ai_extract.
Args:
url: The URL to crawl
schema: Extraction schema
js_code: JavaScript to execute
Returns:
Dict with extracted data
"""
return asyncio.get_event_loop().run_until_complete(
crawl4ai_extract(url, schema, js_code)
)
if __name__ == "__main__":
# Example usage
async def main():
print("=" * 60)
print("Crawl4AI Tools - Example Usage")
print("=" * 60)
is_available, error = _check_crawl4ai_available()
if not is_available:
print(f"\nCrawl4AI is not available: {error}")
print("\nTo use Crawl4AI tools:")
print("1. Install the package: pip install crawl4ai")
print("2. Run setup: crawl4ai-setup")
else:
print("Crawl4AI is available!")
# Example crawl
print("\n1. Basic Crawl")
print("-" * 30)
result = await crawl4ai("https://example.com")
if result.get("success"):
print(f"URL: {result['url']}")
print(f"Markdown length: {len(result.get('markdown', ''))}")
print(f"Links found: {len(result.get('links', {}).get('internal', []))}")
else:
print(f"Error: {result.get('error')}")
print("\n" + "=" * 60)
print("Demonstration Complete")
print("=" * 60)
asyncio.run(main())
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "praisonaiagents"
version = "0.0.185"
version = "0.0.188"
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
requires-python = ">=3.10"
authors = [
@@ -0,0 +1,299 @@
"""Tests for MCP server wrapper - TDD approach.
These tests define the expected behavior of ToolsMCPServer class.
"""
import pytest
from unittest.mock import patch
class TestToolsMCPServerInit:
"""Test ToolsMCPServer initialization."""
def test_init_with_name(self):
"""Test server initializes with custom name."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer(name="my-tools")
assert server.name == "my-tools"
def test_init_with_default_name(self):
"""Test server initializes with default name."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
assert server.name == "praisonai-tools"
def test_init_with_tools_list(self):
"""Test server initializes with list of tools."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
def my_tool(x: str) -> str:
"""A test tool."""
return x
server = ToolsMCPServer(tools=[my_tool])
assert len(server.tools) == 1
def test_init_empty_tools(self):
"""Test server initializes with empty tools list."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
assert server.tools == []
class TestToolsMCPServerRegister:
"""Test tool registration."""
def test_register_single_tool(self):
"""Test registering a single tool."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
def search(query: str) -> str:
"""Search for information."""
return f"Results for {query}"
server.register_tool(search)
assert len(server.tools) == 1
assert server.tools[0].__name__ == "search"
def test_register_multiple_tools(self):
"""Test registering multiple tools."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
def tool1(x: str) -> str:
"""Tool 1."""
return x
def tool2(y: int) -> int:
"""Tool 2."""
return y
server.register_tool(tool1)
server.register_tool(tool2)
assert len(server.tools) == 2
def test_register_tools_batch(self):
"""Test registering multiple tools at once."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
def tool1(x: str) -> str:
"""Tool 1."""
return x
def tool2(y: int) -> int:
"""Tool 2."""
return y
server.register_tools([tool1, tool2])
assert len(server.tools) == 2
def test_register_async_tool(self):
"""Test registering an async tool."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
async def async_search(query: str) -> str:
"""Async search."""
return f"Results for {query}"
server.register_tool(async_search)
assert len(server.tools) == 1
class TestToolsMCPServerSchemas:
"""Test schema generation for registered tools."""
def test_get_tool_schemas(self):
"""Test getting schemas for all registered tools."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
def search(query: str, max_results: int = 10) -> str:
"""Search the web."""
return ""
server.register_tool(search)
schemas = server.get_tool_schemas()
assert len(schemas) == 1
assert schemas[0]["name"] == "search"
assert schemas[0]["description"] == "Search the web."
assert "query" in schemas[0]["inputSchema"]["properties"]
def test_get_tool_names(self):
"""Test getting list of tool names."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
def tool1(x: str) -> str:
"""Tool 1."""
return x
def tool2(y: int) -> int:
"""Tool 2."""
return y
server.register_tools([tool1, tool2])
names = server.get_tool_names()
assert set(names) == {"tool1", "tool2"}
class TestToolsMCPServerExecution:
"""Test tool execution through MCP server."""
def test_execute_sync_tool(self):
"""Test executing a synchronous tool."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
server.register_tool(add)
result = server.execute_tool("add", {"a": 5, "b": 3})
assert result == 8
@pytest.mark.asyncio
async def test_execute_async_tool(self):
"""Test executing an async tool."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
async def async_add(a: int, b: int) -> int:
"""Add two numbers asynchronously."""
return a + b
server.register_tool(async_add)
result = await server.execute_tool_async("async_add", {"a": 5, "b": 3})
assert result == 8
def test_execute_nonexistent_tool(self):
"""Test executing a tool that doesn't exist."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
with pytest.raises(ValueError, match="Tool 'nonexistent' not found"):
server.execute_tool("nonexistent", {})
class TestToolsMCPServerFastMCP:
"""Test FastMCP integration."""
def test_get_fastmcp_instance(self):
"""Test getting FastMCP instance with registered tools."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer(name="test-server")
def my_tool(x: str) -> str:
"""A test tool."""
return x
server.register_tool(my_tool)
# Get FastMCP instance
mcp = server.get_fastmcp()
assert mcp is not None
assert mcp.name == "test-server"
def test_fastmcp_has_registered_tools(self):
"""Test that FastMCP instance has all registered tools."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
def search(query: str) -> str:
"""Search tool."""
return query
def calculate(x: int, y: int) -> int:
"""Calculate tool."""
return x + y
server.register_tools([search, calculate])
mcp = server.get_fastmcp()
# FastMCP should have both tools registered
# We can check by looking at the internal tool registry
assert mcp is not None
class TestLaunchToolsMCPServer:
"""Test the convenience function for launching MCP server."""
def test_launch_function_exists(self):
"""Test that launch_tools_mcp_server function exists."""
from praisonaiagents.mcp.mcp_server import launch_tools_mcp_server
assert callable(launch_tools_mcp_server)
def test_launch_with_tool_names(self):
"""Test launching server with tool names from registry."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
# This should create a server with specified tools
# We'll mock the actual launch to avoid blocking
with patch.object(ToolsMCPServer, 'run', return_value=None):
# Just verify it doesn't raise
pass # Actual launch test will be in integration tests
def test_launch_with_custom_tools(self):
"""Test launching server with custom tool functions."""
from praisonaiagents.mcp.mcp_server import launch_tools_mcp_server
def my_custom_tool(x: str) -> str:
"""Custom tool."""
return x
# Verify function accepts tools parameter
# Actual launch test will be in integration tests
assert callable(launch_tools_mcp_server)
class TestToolsMCPServerTransports:
"""Test different transport options."""
def test_stdio_transport_config(self):
"""Test stdio transport configuration."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
# Server should support stdio transport
assert hasattr(server, 'run_stdio') or hasattr(server, 'run')
def test_sse_transport_config(self):
"""Test SSE transport configuration."""
from praisonaiagents.mcp.mcp_server import ToolsMCPServer
server = ToolsMCPServer()
# Server should support SSE transport
assert hasattr(server, 'run_sse') or hasattr(server, 'run')
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -0,0 +1,225 @@
"""Tests for MCP utility functions - TDD approach.
These tests define the expected behavior of mcp_utils module.
"""
import pytest
from typing import Optional, List, Dict, Any
class TestFunctionToMCPSchema:
"""Test function_to_mcp_schema() converts Python functions to MCP tool schemas."""
def test_simple_function_with_string_param(self):
"""Test conversion of simple function with string parameter."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def search(query: str) -> str:
"""Search the web for information."""
return f"Results for {query}"
schema = function_to_mcp_schema(search)
assert schema["name"] == "search"
assert schema["description"] == "Search the web for information."
assert schema["inputSchema"]["type"] == "object"
assert "query" in schema["inputSchema"]["properties"]
assert schema["inputSchema"]["properties"]["query"]["type"] == "string"
assert "query" in schema["inputSchema"]["required"]
def test_function_with_multiple_params(self):
"""Test function with multiple parameters of different types."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def search(query: str, max_results: int, include_images: bool) -> Dict[str, Any]:
"""Search with options."""
return {}
schema = function_to_mcp_schema(search)
assert schema["name"] == "search"
props = schema["inputSchema"]["properties"]
assert props["query"]["type"] == "string"
assert props["max_results"]["type"] == "integer"
assert props["include_images"]["type"] == "boolean"
assert set(schema["inputSchema"]["required"]) == {"query", "max_results", "include_images"}
def test_function_with_optional_params(self):
"""Test function with optional parameters (defaults)."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def search(query: str, max_results: int = 10, include_images: bool = False) -> str:
"""Search with optional params."""
return ""
schema = function_to_mcp_schema(search)
# Only required params should be in required list
assert schema["inputSchema"]["required"] == ["query"]
# All params should still be in properties
assert "max_results" in schema["inputSchema"]["properties"]
assert "include_images" in schema["inputSchema"]["properties"]
def test_function_with_list_param(self):
"""Test function with list parameter."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def process(items: List[str]) -> str:
"""Process a list of items."""
return ""
schema = function_to_mcp_schema(process)
assert schema["inputSchema"]["properties"]["items"]["type"] == "array"
assert schema["inputSchema"]["properties"]["items"]["items"]["type"] == "string"
def test_function_with_dict_param(self):
"""Test function with dict parameter."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def process(data: Dict[str, Any]) -> str:
"""Process data dictionary."""
return ""
schema = function_to_mcp_schema(process)
assert schema["inputSchema"]["properties"]["data"]["type"] == "object"
def test_function_with_optional_type(self):
"""Test function with Optional type hint."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def search(query: str, category: Optional[str] = None) -> str:
"""Search with optional category."""
return ""
schema = function_to_mcp_schema(search)
assert "category" in schema["inputSchema"]["properties"]
assert "category" not in schema["inputSchema"]["required"]
def test_function_without_docstring(self):
"""Test function without docstring uses function name as description."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
def my_tool(x: str) -> str:
return x
schema = function_to_mcp_schema(my_tool)
assert schema["name"] == "my_tool"
assert schema["description"] == "my_tool" # Falls back to name
def test_async_function(self):
"""Test async function is handled correctly."""
from praisonaiagents.mcp.mcp_utils import function_to_mcp_schema
async def async_search(query: str) -> str:
"""Async search function."""
return ""
schema = function_to_mcp_schema(async_search)
assert schema["name"] == "async_search"
assert schema["description"] == "Async search function."
class TestGetToolMetadata:
"""Test get_tool_metadata() extracts metadata from functions."""
def test_extracts_name(self):
"""Test name extraction."""
from praisonaiagents.mcp.mcp_utils import get_tool_metadata
def my_tool(x: str) -> str:
"""Tool description."""
return x
metadata = get_tool_metadata(my_tool)
assert metadata["name"] == "my_tool"
def test_extracts_description_from_docstring(self):
"""Test description extraction from docstring."""
from praisonaiagents.mcp.mcp_utils import get_tool_metadata
def my_tool(x: str) -> str:
"""This is the tool description.
More details here.
"""
return x
metadata = get_tool_metadata(my_tool)
assert metadata["description"] == "This is the tool description."
def test_custom_name_attribute(self):
"""Test custom __mcp_name__ attribute is used."""
from praisonaiagents.mcp.mcp_utils import get_tool_metadata
def my_tool(x: str) -> str:
"""Description."""
return x
my_tool.__mcp_name__ = "custom_name"
metadata = get_tool_metadata(my_tool)
assert metadata["name"] == "custom_name"
def test_custom_description_attribute(self):
"""Test custom __mcp_description__ attribute is used."""
from praisonaiagents.mcp.mcp_utils import get_tool_metadata
def my_tool(x: str) -> str:
"""Original docstring."""
return x
my_tool.__mcp_description__ = "Custom description"
metadata = get_tool_metadata(my_tool)
assert metadata["description"] == "Custom description"
class TestPythonTypeToJsonSchema:
"""Test Python type to JSON Schema conversion."""
def test_basic_types(self):
"""Test basic Python types convert correctly."""
from praisonaiagents.mcp.mcp_utils import python_type_to_json_schema
assert python_type_to_json_schema(str) == {"type": "string"}
assert python_type_to_json_schema(int) == {"type": "integer"}
assert python_type_to_json_schema(float) == {"type": "number"}
assert python_type_to_json_schema(bool) == {"type": "boolean"}
def test_list_type(self):
"""Test List type converts to array."""
from praisonaiagents.mcp.mcp_utils import python_type_to_json_schema
result = python_type_to_json_schema(List[str])
assert result["type"] == "array"
assert result["items"]["type"] == "string"
def test_dict_type(self):
"""Test Dict type converts to object."""
from praisonaiagents.mcp.mcp_utils import python_type_to_json_schema
result = python_type_to_json_schema(Dict[str, Any])
assert result["type"] == "object"
def test_optional_type(self):
"""Test Optional type is handled."""
from praisonaiagents.mcp.mcp_utils import python_type_to_json_schema
result = python_type_to_json_schema(Optional[str])
assert result["type"] == "string" # Optional just means nullable
def test_any_type(self):
"""Test Any type converts to empty schema."""
from praisonaiagents.mcp.mcp_utils import python_type_to_json_schema
result = python_type_to_json_schema(Any)
assert result == {} # Any type = no constraints
if __name__ == "__main__":
pytest.main([__file__, "-v"])
+1 -1
View File
@@ -2422,7 +2422,7 @@ wheels = [
[[package]]
name = "praisonaiagents"
version = "0.0.185"
version = "0.0.188"
source = { editable = "." }
dependencies = [
{ name = "aiohttp" },
+2 -2
View File
@@ -3,8 +3,8 @@ class Praisonai < Formula
desc "AI tools for various AI applications"
homepage "https://github.com/MervinPraison/PraisonAI"
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.7.tar.gz"
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.7.tar.gz | shasum -a 256`.split.first
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.9.tar.gz"
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.9.tar.gz | shasum -a 256`.split.first
license "MIT"
depends_on "[email protected]"
+1 -1
View File
@@ -57,7 +57,7 @@ class CloudDeployer:
file.write("FROM python:3.11-slim\n")
file.write("WORKDIR /app\n")
file.write("COPY . .\n")
file.write("RUN pip install flask praisonai==2.3.7 gunicorn markdown\n")
file.write("RUN pip install flask praisonai==2.3.9 gunicorn markdown\n")
file.write("EXPOSE 8080\n")
file.write('CMD ["gunicorn", "-b", "0.0.0.0:8080", "api:app"]\n')
+1 -1
View File
@@ -1 +1 @@
__version__ = "2.3.7"
__version__ = "2.3.9"
+1 -1
View File
@@ -12,7 +12,7 @@ dependencies = [
"rich>=13.7",
"markdown>=3.5",
"pyparsing>=3.0.0",
"praisonaiagents>=0.0.185",
"praisonaiagents>=0.0.188",
"python-dotenv>=0.19.0",
"instructor>=1.3.3",
"PyYAML>=6.0",
+4 -4
View File
@@ -4197,7 +4197,7 @@ requires-dist = [
{ name = "praisonai-tools", marker = "extra == 'autogen'", specifier = ">=0.0.22" },
{ name = "praisonai-tools", marker = "extra == 'autogen-v4'", specifier = ">=0.0.22" },
{ name = "praisonai-tools", marker = "extra == 'crewai'", specifier = ">=0.0.22" },
{ name = "praisonaiagents", specifier = ">=0.0.185" },
{ name = "praisonaiagents", specifier = ">=0.0.188" },
{ name = "pyautogen", marker = "extra == 'autogen'", specifier = "==0.2.29" },
{ name = "pydantic", marker = "extra == 'chat'", specifier = "<=2.10.1" },
{ name = "pydantic", marker = "extra == 'code'", specifier = "<=2.10.1" },
@@ -4252,7 +4252,7 @@ wheels = [
[[package]]
name = "praisonaiagents"
version = "0.0.185"
version = "0.0.188"
source = { registry = "https://pypi.org/simple" }
dependencies = [
{ name = "aiohttp" },
@@ -4261,9 +4261,9 @@ dependencies = [
{ name = "pydantic" },
{ name = "rich" },
]
sdist = { url = "https://files.pythonhosted.org/packages/eb/b9/e04ca0b88ccd779820f895c773afdb124a01242fe3e4f4831f24eea951de/praisonaiagents-0.0.185.tar.gz", hash = "sha256:1213893e3a690e64c5ef3f0991cd230ec94b052114340ebcf0b84a6c3797a41a", size = 425649, upload-time = "2025-12-16T07:44:28.169Z" }
sdist = { url = "https://files.pythonhosted.org/packages/76/b1/4ea337476bdb07ef504af6023e378c513a9c35414ff080a777221e70add5/praisonaiagents-0.0.188.tar.gz", hash = "sha256:ba498e6328e3a6184e3d99d2ae7a73473bd0aef747abe71e9a2c99d201e791f5", size = 438151, upload-time = "2025-12-16T08:17:22.931Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/ca/04/9f60d36df1b3340847fd7b930c99f101678d7b8a0c301024d7940e771aa8/praisonaiagents-0.0.185-py3-none-any.whl", hash = "sha256:bad408a451b8e26957fa444caa305ff8a784461a67dd8a71e0507af533fa7972", size = 410732, upload-time = "2025-12-16T07:44:26.525Z" },
{ url = "https://files.pythonhosted.org/packages/0d/3c/122ca9485b531a7307a44c1149f7b6e72b5a962b2244f8a8756f459f50e8/praisonaiagents-0.0.188-py3-none-any.whl", hash = "sha256:3ba651ca4e52de2b2f63101f265411a676f918ac308d4dbb95f2124cdc42ca6e", size = 420694, upload-time = "2025-12-16T08:17:20.78Z" },
]
[[package]]