mirror of
https://github.com/MervinPraison/PraisonAI.git
synced 2026-11-08 17:31:00 -10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc23d3a104 |
No files matched your search
+1
-1
@@ -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.8" \
|
||||
"praisonai>=2.3.9" \
|
||||
"praisonai[api]" \
|
||||
gunicorn \
|
||||
markdown
|
||||
|
||||
@@ -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.8" \
|
||||
"praisonai>=2.3.9" \
|
||||
"praisonai[chat]" \
|
||||
"embedchain[github,youtube]"
|
||||
|
||||
|
||||
@@ -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.8" \
|
||||
"praisonai>=2.3.9" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[chat]" \
|
||||
"praisonai[realtime]" \
|
||||
|
||||
@@ -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.8" \
|
||||
"praisonai>=2.3.9" \
|
||||
"praisonai[ui]" \
|
||||
"praisonai[crewai]"
|
||||
|
||||
|
||||
@@ -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]
|
||||
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.187"
|
||||
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"])
|
||||
Generated
+1
-1
@@ -2422,7 +2422,7 @@ wheels = [
|
||||
|
||||
[[package]]
|
||||
name = "praisonaiagents"
|
||||
version = "0.0.187"
|
||||
version = "0.0.188"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "aiohttp" },
|
||||
|
||||
@@ -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.8.tar.gz"
|
||||
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.8.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]"
|
||||
|
||||
@@ -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.8 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 @@
|
||||
__version__ = "2.3.8"
|
||||
__version__ = "2.3.9"
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
"rich>=13.7",
|
||||
"markdown>=3.5",
|
||||
"pyparsing>=3.0.0",
|
||||
"praisonaiagents>=0.0.187",
|
||||
"praisonaiagents>=0.0.188",
|
||||
"python-dotenv>=0.19.0",
|
||||
"instructor>=1.3.3",
|
||||
"PyYAML>=6.0",
|
||||
|
||||
Generated
+4
-4
@@ -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.187" },
|
||||
{ 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.187"
|
||||
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/d9/81/64f01ac3755126842de60144fe5092d645b21a9536b944533ab602b10ec6/praisonaiagents-0.0.187.tar.gz", hash = "sha256:26c8dcb40d24a56c01db40e737dda53ba0175f58f9cef9327103afff96a88e39", size = 429535, upload-time = "2025-12-16T07:53:14.206Z" }
|
||||
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/02/bb/eaa82f06c0cf1503bb478ec20e2fc85a0a632dceeeacae03b10d7ff29bd8/praisonaiagents-0.0.187-py3-none-any.whl", hash = "sha256:6776997544673e06a55860c558270b43c4b73fce72f105ca1ad6b0f3724b49f2", size = 415005, upload-time = "2025-12-16T07:53:12.564Z" },
|
||||
{ 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]]
|
||||
|
||||
Reference in new issue
Block a user