Compare commits

...
9 Commits
Author SHA1 Message Date
Mervin Praison 67dbd1007a Release v2.3.10 2025-12-16 09:54:57 +00:00
Mervin Praison cf2a5a135f workflow 2025-12-16 09:23:49 +00:00
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
Mervin Praison 69740baa7c Release v2.3.7 2025-12-16 07:46:40 +00:00
Mervin Praison 5035978a29 fix: resolve Mintlify parsing errors
- Fix corrupted/duplicated database service in docker-compose.dev.yml
- Fix MDX parsing error in tests/README.md (<1s -> under 1s)
2025-12-15 23:11:04 +00:00
Mervin Praison 4ddb043324 Release v2.3.7 2025-12-15 22:49:12 +00:00
Mervin Praison 337014f4db planning example 2025-12-15 22:25:09 +00:00
Mervin Praison 35c371b25e update 2025-12-15 22:22:42 +00:00
58 changed files with 9105 additions and 98 deletions

No files matched your search

+38 -22
View File
@@ -86,13 +86,29 @@ runs:
- name: Extract GitHub Context and Create Prompt
shell: bash
id: prepare_context
env:
# Pass user-controlled inputs via environment variables to prevent script injection (GHSL-2025-093)
EVENT_COMMENT_BODY: ${{ github.event.comment.body }}
EVENT_ISSUE_TITLE: ${{ github.event.issue.title }}
EVENT_ISSUE_BODY: ${{ github.event.issue.body }}
EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}
EVENT_PR_TITLE: ${{ github.event.pull_request.title }}
EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
EVENT_REVIEW_BODY: ${{ github.event.review.body }}
EVENT_ASSIGNEE_LOGIN: ${{ github.event.assignee.login }}
INPUT_TRIGGER_PHRASE: ${{ inputs.trigger_phrase }}
INPUT_ASSIGNEE_TRIGGER: ${{ inputs.assignee_trigger }}
INPUT_DIRECT_PROMPT: ${{ inputs.direct_prompt }}
EVENT_ACTION: ${{ github.event.action }}
EVENT_NAME: ${{ github.event_name }}
REPO_NAME: ${{ github.repository }}
run: |
echo "🔍 Extracting GitHub context from event: ${{ github.event_name }}"
echo "🔍 Extracting GitHub context from event: $EVENT_NAME"
# Function to check for trigger phrase
check_trigger() {
local text="$1"
local trigger="${{ inputs.trigger_phrase }}"
local trigger="$INPUT_TRIGGER_PHRASE"
if [[ "$text" == *"$trigger"* ]]; then
return 0
fi
@@ -104,11 +120,11 @@ runs:
USER_REQUEST=""
CONTEXT_INFO=""
case "${{ github.event_name }}" in
case "$EVENT_NAME" in
"issue_comment")
COMMENT_BODY="${{ github.event.comment.body }}"
ISSUE_TITLE="${{ github.event.issue.title }}"
ISSUE_NUMBER="${{ github.event.issue.number }}"
COMMENT_BODY="$EVENT_COMMENT_BODY"
ISSUE_TITLE="$EVENT_ISSUE_TITLE"
ISSUE_NUMBER="$EVENT_ISSUE_NUMBER"
if check_trigger "$COMMENT_BODY"; then
TRIGGER_FOUND="true"
@@ -118,9 +134,9 @@ runs:
;;
"pull_request_review_comment")
COMMENT_BODY="${{ github.event.comment.body }}"
PR_TITLE="${{ github.event.pull_request.title }}"
PR_NUMBER="${{ github.event.pull_request.number }}"
COMMENT_BODY="$EVENT_COMMENT_BODY"
PR_TITLE="$EVENT_PR_TITLE"
PR_NUMBER="$EVENT_PR_NUMBER"
if check_trigger "$COMMENT_BODY"; then
TRIGGER_FOUND="true"
@@ -130,9 +146,9 @@ runs:
;;
"pull_request_review")
REVIEW_BODY="${{ github.event.review.body }}"
PR_TITLE="${{ github.event.pull_request.title }}"
PR_NUMBER="${{ github.event.pull_request.number }}"
REVIEW_BODY="$EVENT_REVIEW_BODY"
PR_TITLE="$EVENT_PR_TITLE"
PR_NUMBER="$EVENT_PR_NUMBER"
if check_trigger "$REVIEW_BODY"; then
TRIGGER_FOUND="true"
@@ -142,17 +158,17 @@ runs:
;;
"issues")
ISSUE_BODY="${{ github.event.issue.body }}"
ISSUE_TITLE="${{ github.event.issue.title }}"
ISSUE_NUMBER="${{ github.event.issue.number }}"
ISSUE_BODY="$EVENT_ISSUE_BODY"
ISSUE_TITLE="$EVENT_ISSUE_TITLE"
ISSUE_NUMBER="$EVENT_ISSUE_NUMBER"
if check_trigger "$ISSUE_TITLE" || check_trigger "$ISSUE_BODY"; then
TRIGGER_FOUND="true"
USER_REQUEST="$ISSUE_BODY"
CONTEXT_INFO="Issue #$ISSUE_NUMBER: $ISSUE_TITLE"
elif [[ "${{ github.event.action }}" == "assigned" && -n "${{ inputs.assignee_trigger }}" ]]; then
ASSIGNEE="${{ github.event.assignee.login }}"
if [[ "$ASSIGNEE" == "${{ inputs.assignee_trigger }}" ]]; then
elif [[ "$EVENT_ACTION" == "assigned" && -n "$INPUT_ASSIGNEE_TRIGGER" ]]; then
ASSIGNEE="$EVENT_ASSIGNEE_LOGIN"
if [[ "$ASSIGNEE" == "$INPUT_ASSIGNEE_TRIGGER" ]]; then
TRIGGER_FOUND="true"
USER_REQUEST="$ISSUE_BODY"
CONTEXT_INFO="Issue #$ISSUE_NUMBER assigned to $ASSIGNEE: $ISSUE_TITLE"
@@ -162,9 +178,9 @@ runs:
esac
# Check for direct prompt override
if [[ -n "${{ inputs.direct_prompt }}" ]]; then
if [[ -n "$INPUT_DIRECT_PROMPT" ]]; then
TRIGGER_FOUND="true"
USER_REQUEST="${{ inputs.direct_prompt }}"
USER_REQUEST="$INPUT_DIRECT_PROMPT"
CONTEXT_INFO="Automated GitHub workflow"
fi
@@ -181,9 +197,9 @@ runs:
cat > /tmp/claude-action/github-context-prompt.txt << EOF
You are Claude Code, an AI assistant helping with GitHub workflows and code.
Repository: ${{ github.repository }}
Repository: $REPO_NAME
Context: $CONTEXT_INFO
Event: ${{ github.event_name }}
Event: $EVENT_NAME
User Request:
$USER_REQUEST
+10 -6
View File
@@ -46,12 +46,16 @@ jobs:
id: issue_data
env:
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
# Pass user-controlled inputs via environment variables to prevent script injection (GHSL-2025-093)
EVENT_NAME: ${{ github.event_name }}
EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}
EVENT_COMMENT_BODY: ${{ github.event.comment.body }}
run: |
# Get issue number from event or context
if [ "${{ github.event_name }}" = "issue_comment" ]; then
ISSUE_NUMBER="${{ github.event.issue.number }}"
elif [ "${{ github.event_name }}" = "issues" ]; then
ISSUE_NUMBER="${{ github.event.issue.number }}"
if [ "$EVENT_NAME" = "issue_comment" ]; then
ISSUE_NUMBER="$EVENT_ISSUE_NUMBER"
elif [ "$EVENT_NAME" = "issues" ]; then
ISSUE_NUMBER="$EVENT_ISSUE_NUMBER"
else
echo "Unable to determine issue number"
exit 1
@@ -62,8 +66,8 @@ jobs:
# Extract additional instructions from comment if triggered by comment
ADDITIONAL_INSTRUCTIONS=""
if [ "${{ github.event_name }}" = "issue_comment" ]; then
COMMENT_BODY="${{ github.event.comment.body }}"
if [ "$EVENT_NAME" = "issue_comment" ]; then
COMMENT_BODY="$EVENT_COMMENT_BODY"
ADDITIONAL_INSTRUCTIONS=$(echo "$COMMENT_BODY" | sed -n 's/.*@gemini[[:space:]]*\(.*\)/\1/p' | head -1)
fi
+13 -7
View File
@@ -55,21 +55,27 @@ jobs:
id: get_pr
env:
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
# Pass user-controlled inputs via environment variables to prevent script injection (GHSL-2025-093)
EVENT_NAME: ${{ github.event_name }}
EVENT_INPUTS_PR_NUMBER: ${{ github.event.inputs.pr_number }}
EVENT_ISSUE_NUMBER: ${{ github.event.issue.number }}
EVENT_PR_NUMBER: ${{ github.event.pull_request.number }}
EVENT_COMMENT_BODY: ${{ github.event.comment.body }}
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
PR_NUMBER=${{ github.event.inputs.pr_number }}
elif [ "${{ github.event_name }}" = "issue_comment" ]; then
PR_NUMBER=${{ github.event.issue.number }}
if [ "$EVENT_NAME" = "workflow_dispatch" ]; then
PR_NUMBER=$EVENT_INPUTS_PR_NUMBER
elif [ "$EVENT_NAME" = "issue_comment" ]; then
PR_NUMBER=$EVENT_ISSUE_NUMBER
else
PR_NUMBER=${{ github.event.pull_request.number }}
PR_NUMBER=$EVENT_PR_NUMBER
fi
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
# Extract additional instructions from comment (if triggered by comment)
ADDITIONAL_INSTRUCTIONS=""
if [ "${{ github.event_name }}" = "issue_comment" ]; then
COMMENT_BODY="${{ github.event.comment.body }}"
if [ "$EVENT_NAME" = "issue_comment" ]; then
COMMENT_BODY="$EVENT_COMMENT_BODY"
ADDITIONAL_INSTRUCTIONS=$(echo "$COMMENT_BODY" | sed 's/.*@gemini//' | xargs)
fi
echo "additional_instructions=$ADDITIONAL_INSTRUCTIONS" >> "$GITHUB_OUTPUT"
+101 -1
View File
@@ -60,9 +60,14 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
- 💾 **Prompt Caching** (Reduce costs & latency - OpenAI, Anthropic, Bedrock, Deepseek)
- 🧠 **Claude Memory Tool** (Persistent cross-conversation memory - Anthropic Beta)
- 💾 **File-Based Memory** (Zero-dependency persistent memory for all agents)
- 🔍 **Built-in Search Tools** (Tavily, You.com, Exa - web search, news, content extraction)
- 📋 **Planning Mode** (Plan before execution - Agent & Multi-Agent)
- 🔧 **Planning Tools** (Research with tools during planning)
- 🧠 **Planning Reasoning** (Chain-of-thought planning)
- 🔌 **MCP Transports** (stdio, Streamable HTTP, WebSocket, SSE - Protocol 2025-11-25)
- 🌐 **WebSocket MCP** (Real-time bidirectional connections with auto-reconnect)
- 🔐 **MCP Security** (Origin validation, DNS rebinding prevention, secure sessions)
- 🔄 **MCP Resumability** (SSE stream recovery via Last-Event-ID)
## Using Python Code
@@ -414,6 +419,101 @@ praisonai research --query-rewrite --tools tools.py --save "Your research query"
praisonai research -v "Your research query"
```
### Planning Mode CLI:
```bash
# Enable planning mode - agent creates a plan before execution
praisonai "Research AI trends and write a summary" --planning
# Planning with tools for research
praisonai "Analyze market trends" --planning --planning-tools tools.py
# Planning with chain-of-thought reasoning
praisonai "Complex analysis task" --planning --planning-reasoning
# Auto-approve plans without confirmation
praisonai "Task" --planning --auto-approve-plan
```
### Memory CLI:
```bash
# Enable memory for agent (persists across sessions)
praisonai "My name is John" --memory
# Memory with user isolation
praisonai "Remember my preferences" --memory --user-id user123
# Memory management commands
praisonai memory show # Show memory statistics
praisonai memory add "User prefers Python" # Add to long-term memory
praisonai memory search "Python" # Search memories
praisonai memory clear # Clear short-term memory
praisonai memory clear all # Clear all memory
praisonai memory save my_session # Save session
praisonai memory resume my_session # Resume session
praisonai memory sessions # List saved sessions
praisonai memory checkpoint # Create checkpoint
praisonai memory restore <checkpoint_id> # Restore checkpoint
praisonai memory checkpoints # List checkpoints
praisonai memory help # Show all commands
```
### Rules CLI:
```bash
# List all loaded rules (from PRAISON.md, CLAUDE.md, etc.)
praisonai rules list
# Show specific rule details
praisonai rules show <rule_name>
# Create a new rule
praisonai rules create my_rule "Always use type hints"
# Delete a rule
praisonai rules delete my_rule
# Show rules statistics
praisonai rules stats
# Include manual rules with prompts
praisonai "Task" --include-rules security,testing
```
### Workflow CLI:
```bash
# List available workflows
praisonai workflow list
# Execute a workflow
praisonai workflow run deploy
# Execute with variables
praisonai workflow run deploy --workflow-var environment=staging --workflow-var branch=main
# Show workflow details
praisonai workflow show deploy
# Create a new workflow template
praisonai workflow create my_workflow
```
### Hooks CLI:
```bash
# List configured hooks
praisonai hooks list
# Show hooks statistics
praisonai hooks stats
# Create hooks.json template
praisonai hooks init
```
### Claude Memory Tool CLI:
```bash
# Enable Claude Memory Tool (Anthropic models only)
praisonai "Research and remember findings" --claude-memory --llm anthropic/claude-sonnet-4-20250514
```
## Using JavaScript Code
```bash
@@ -1045,7 +1145,7 @@ cd src/praisonai && uv publish
- 🎨 Interactive UIs
- 📄 YAML-based Configuration
- 🛠️ Custom Tool Integration
- 🔍 Internet Search Capability (using Crawl4AI and Tavily)
- 🔍 Internet Search Capability (Tavily, You.com, Exa, DuckDuckGo, Crawl4AI)
- 🖼️ Vision Language Model (VLM) Support
- 🎙️ Real-time Voice Interaction
+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.6" \
"praisonai>=2.3.10" \
"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.6" \
"praisonai>=2.3.10" \
"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.6" \
"praisonai>=2.3.10" \
"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.6" \
"praisonai>=2.3.10" \
"praisonai[ui]" \
"praisonai[crewai]"
+4 -15
View File
@@ -137,22 +137,11 @@ services:
POSTGRES_USER: praisonai
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-praisonai_dev_pass}
volumes:
database:
image: postgres:15-alpine
environment:
POSTGRES_DB: praisonai_dev
POSTGRES_USER: praisonai
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-praisonai_dev_pass}
volumes:
- postgres_data:/var/lib/postgresql/data
read_only: true
tmpfs:
- /tmp
- /var/tmp
ports:
- "5432:5432"
networks:
- postgres_data:/var/lib/postgresql/data
read_only: true
tmpfs:
- /tmp
- /var/tmp
ports:
- "5432:5432"
networks:
@@ -0,0 +1,83 @@
"""
MCP Backward Compatibility Example
Demonstrates backward compatibility with older MCP servers
using the deprecated HTTP+SSE transport (protocol version 2024-11-05).
Features:
- Legacy SSE endpoint detection (/sse suffix)
- Automatic transport negotiation
- Protocol version support (2024-11-05 to 2025-11-25)
Protocol: MCP 2025-11-25 with 2024-11-05 compatibility
"""
# Example code (requires running MCP servers):
#
# from praisonaiagents import Agent, MCP
#
# # Legacy SSE server (auto-detected via /sse suffix)
# agent_legacy = Agent(
# name="Legacy SSE Assistant",
# tools=MCP("http://localhost:8080/sse")
# )
#
# # Modern Streamable HTTP server
# agent_modern = Agent(
# name="Modern HTTP Assistant",
# tools=MCP("http://localhost:8080/mcp")
# )
if __name__ == "__main__":
from praisonaiagents.mcp.mcp_transport import get_transport_type
from praisonaiagents.mcp.mcp_compat import (
detect_transport_support,
is_legacy_sse_url,
TransportNegotiator
)
from praisonaiagents.mcp.mcp_session import VALID_PROTOCOL_VERSIONS
print("MCP Backward Compatibility Example")
print("=" * 50)
# URL detection
print("\n1. URL-based Transport Detection:")
urls = [
"http://localhost:8080/sse",
"https://api.example.com/sse",
"http://localhost:8080/mcp",
"https://api.example.com/mcp",
]
for url in urls:
transport = get_transport_type(url)
legacy = is_legacy_sse_url(url)
print(f" {url}")
print(f" Transport: {transport}, Legacy: {legacy}")
# Transport negotiation
print("\n2. Transport Negotiation (HTTP response based):")
responses = [
(200, "application/json", "Streamable HTTP"),
(200, "text/event-stream", "SSE stream"),
(400, None, "Fallback to SSE"),
(404, None, "Fallback to SSE"),
(405, None, "Fallback to SSE"),
]
for status, content_type, desc in responses:
detected = detect_transport_support(status, content_type)
print(f" HTTP {status} ({desc}): {detected}")
# Protocol versions
print("\n3. Supported Protocol Versions:")
for version in sorted(VALID_PROTOCOL_VERSIONS):
print(f" - {version}")
# Negotiator
print("\n4. Transport Negotiator:")
negotiator = TransportNegotiator("https://api.example.com/mcp")
print(f" Base URL: {negotiator.base_url}")
print(f" Try Streamable first: {negotiator.should_try_streamable_first()}")
negotiator_sse = TransportNegotiator("https://api.example.com/sse")
print(f" SSE URL: {negotiator_sse.base_url}")
print(f" Try Streamable first: {negotiator_sse.should_try_streamable_first()}")
+110
View File
@@ -0,0 +1,110 @@
"""
MCP Custom Transport Example
Demonstrates how to create custom MCP transports by extending
the BaseTransport class.
Features:
- BaseTransport abstract class
- TransportRegistry for registration
- TransportConfig for configuration
- Pluggable transport mechanism
Protocol: MCP 2025-11-25
"""
from praisonaiagents.mcp.mcp_transport import (
BaseTransport,
TransportConfig,
TransportRegistry,
get_default_registry
)
# Custom transport implementation
class MyCustomTransport(BaseTransport):
"""Example custom transport implementation."""
def __init__(self, url: str, config: TransportConfig = None):
self.url = url
self.config = config or TransportConfig()
self._connected = False
async def connect(self):
"""Establish connection."""
print(f"Connecting to {self.url}...")
self._connected = True
print("Connected!")
async def send(self, message: dict):
"""Send JSON-RPC message."""
if not self._connected:
raise RuntimeError("Not connected")
print(f"Sending: {message}")
async def receive(self) -> dict:
"""Receive JSON-RPC message."""
if not self._connected:
raise RuntimeError("Not connected")
return {"jsonrpc": "2.0", "result": "ok", "id": 1}
async def close(self):
"""Close connection."""
print("Closing connection...")
self._connected = False
@property
def is_connected(self) -> bool:
return self._connected
if __name__ == "__main__":
import asyncio
print("MCP Custom Transport Example")
print("=" * 50)
# 1. Transport configuration
print("\n1. Transport Configuration:")
config = TransportConfig(
timeout=60,
debug=True,
retry_count=3,
retry_delay=1.0,
auth_token="my-token"
)
print(f" Timeout: {config.timeout}s")
print(f" Debug: {config.debug}")
print(f" Retry count: {config.retry_count}")
# 2. Default registry
print("\n2. Default Transport Registry:")
registry = get_default_registry()
print(f" Registered transports: {registry.list_transports()}")
# 3. Register custom transport
print("\n3. Registering Custom Transport:")
custom_registry = TransportRegistry()
custom_registry.register("my_custom", MyCustomTransport)
print(f" Registered: {custom_registry.list_transports()}")
# 4. Use custom transport
print("\n4. Using Custom Transport:")
async def demo():
transport = MyCustomTransport("custom://localhost:8080", config)
async with transport:
await transport.send({"jsonrpc": "2.0", "method": "test", "id": 1})
response = await transport.receive()
print(f" Response: {response}")
asyncio.run(demo())
# 5. BaseTransport interface
print("\n5. BaseTransport Interface:")
print(" Required methods:")
print(" - async connect()")
print(" - async send(message: dict)")
print(" - async receive() -> dict")
print(" - async close()")
print(" - @property is_connected -> bool")
+47
View File
@@ -0,0 +1,47 @@
"""
MCP Resumability Example
Demonstrates SSE stream resumability for MCP transports.
Resumability allows recovering from disconnections without losing messages.
Features:
- Event ID tracking per stream
- Last-Event-ID header on reconnection
- Retry delay handling from server
- Message replay on reconnection
Protocol: MCP 2025-11-25
"""
from praisonaiagents import Agent, MCP
# Agent with resumability support (automatic)
agent = Agent(
name="Resumable Assistant",
instructions="You are a helpful assistant with resumable connections.",
tools=MCP("https://api.example.com/mcp")
)
# Resumability is automatic - the MCP client:
# 1. Tracks event IDs from SSE streams (id: field)
# 2. Stores last event ID per stream
# 3. Includes Last-Event-ID header on reconnection
# 4. Respects retry delay from server (retry: field)
if __name__ == "__main__":
print("MCP Resumability Example")
print("=" * 40)
print()
print("SSE Event Format:")
print(" id: event-123")
print(" retry: 3000")
print(" data: {\"jsonrpc\": \"2.0\", ...}")
print()
print("Resumability Flow:")
print("1. Client receives SSE events with id: field")
print("2. Client stores last event ID")
print("3. On disconnection, client reconnects with Last-Event-ID header")
print("4. Server replays messages after that event ID")
print("5. Client respects retry: field for reconnection delay")
print()
print("This prevents message loss during network interruptions.")
+79
View File
@@ -0,0 +1,79 @@
"""
MCP Security Example
Demonstrates security features for MCP transports.
These features help prevent attacks like DNS rebinding.
Features:
- Origin header validation
- DNS rebinding prevention
- Localhost binding for local servers
- Authentication header support
- Secure session ID generation
Protocol: MCP 2025-11-25
"""
# Example code (requires running MCP servers):
#
# from praisonaiagents import Agent, MCP
#
# # Agent with authentication
# agent = Agent(
# name="Secure Assistant",
# tools=MCP(
# "https://api.example.com/mcp",
# headers={"Authorization": "Bearer your-token"}
# )
# )
#
# # WebSocket with auth token
# agent_ws = Agent(
# name="Secure WebSocket Assistant",
# tools=MCP(
# "wss://api.example.com/mcp",
# auth_token="Bearer your-secret-token"
# )
# )
# Using security utilities directly (no server required)
if __name__ == "__main__":
from praisonaiagents.mcp.mcp_security import (
is_valid_origin,
create_auth_header,
generate_secure_session_id,
SecurityConfig
)
print("MCP Security Example")
print("=" * 40)
# Origin validation (DNS rebinding prevention)
print("\n1. Origin Validation:")
allowed = ["localhost", "127.0.0.1", "example.com"]
print(f" Allowed origins: {allowed}")
print(f" 'http://localhost:8080' valid: {is_valid_origin('http://localhost:8080', allowed)}")
print(f" 'https://evil.com' valid: {is_valid_origin('https://evil.com', allowed)}")
# Authentication headers
print("\n2. Authentication Headers:")
bearer = create_auth_header("my-token", auth_type="bearer")
print(f" Bearer: {bearer}")
basic = create_auth_header("user:pass", auth_type="basic")
print(f" Basic: {basic}")
# Secure session IDs
print("\n3. Secure Session IDs:")
session_id = generate_secure_session_id()
print(f" Generated: {session_id}")
print(f" Length: {len(session_id)} chars")
# Security config
print("\n4. Security Configuration:")
config = SecurityConfig(
allowed_origins=["localhost", "example.com"],
require_auth=True
)
print(f" Validate origin: {config.validate_origin}")
print(f" Require auth: {config.require_auth}")
print(f" Bind address: {config.get_bind_address()}")
@@ -0,0 +1,44 @@
"""
MCP Session Management Example
Demonstrates session management features for MCP transports.
Sessions allow stateful interactions with MCP servers.
Features:
- Automatic session ID handling
- Session expiration detection (HTTP 404)
- Session termination via HTTP DELETE
- Protocol version negotiation
Protocol: MCP 2025-11-25
"""
from praisonaiagents import Agent, MCP
# Basic agent with automatic session management
agent = Agent(
name="Session Assistant",
instructions="You are a helpful assistant with session support.",
tools=MCP("https://api.example.com/mcp")
)
# Session management is automatic - the MCP client:
# 1. Receives Mcp-Session-Id from server during initialization
# 2. Includes session ID in all subsequent requests
# 3. Handles HTTP 404 (session expired) by reinitializing
# 4. Can terminate session explicitly when done
if __name__ == "__main__":
print("MCP Session Management Example")
print("=" * 40)
print()
print("Session Lifecycle:")
print("1. Client sends InitializeRequest")
print("2. Server returns InitializeResult with Mcp-Session-Id header")
print("3. Client includes Mcp-Session-Id in all subsequent requests")
print("4. If HTTP 404 received, client reinitializes session")
print("5. Client can send HTTP DELETE to terminate session")
print()
print("Headers used:")
print("- Mcp-Session-Id: Session identifier")
print("- Mcp-Protocol-Version: Protocol version (e.g., 2025-03-26)")
@@ -0,0 +1,91 @@
"""
MCP Transports Overview Example
Demonstrates all MCP transport types and auto-detection.
The MCP class automatically selects the appropriate transport
based on the URL pattern.
Transport Types:
- stdio: Command strings (local subprocess)
- Streamable HTTP: http:// or https:// URLs
- WebSocket: ws:// or wss:// URLs
- SSE (Legacy): URLs ending in /sse
Protocol: MCP 2025-11-25
"""
# Example code (requires running MCP servers):
#
# from praisonaiagents import Agent, MCP
#
# # 1. stdio Transport - Local subprocess
# agent_stdio = Agent(
# name="Stdio Assistant",
# tools=MCP("npx @modelcontextprotocol/server-memory")
# )
#
# # 2. Streamable HTTP Transport
# agent_http = Agent(
# name="HTTP Assistant",
# tools=MCP("https://api.example.com/mcp")
# )
#
# # 3. WebSocket Transport
# agent_ws = Agent(
# name="WebSocket Assistant",
# tools=MCP("wss://api.example.com/mcp")
# )
#
# # 4. SSE Transport (Legacy)
# agent_sse = Agent(
# name="SSE Assistant",
# tools=MCP("http://localhost:8080/sse")
# )
if __name__ == "__main__":
from praisonaiagents.mcp.mcp_transport import get_transport_type
print("MCP Transports Overview")
print("=" * 50)
print()
print("Transport Auto-Detection:")
print("-" * 50)
examples = [
("npx @mcp/server-memory", "stdio"),
("python3 server.py", "stdio"),
("https://api.example.com/mcp", "http_stream"),
("http://localhost:8080/mcp", "http_stream"),
("ws://localhost:8080/mcp", "websocket"),
("wss://api.example.com/mcp", "websocket"),
("http://localhost:8080/sse", "sse"),
("https://api.example.com/sse", "sse"),
]
for url, expected in examples:
detected = get_transport_type(url)
status = "✓" if detected == expected else "✗"
print(f" {status} '{url}'")
print(f" → {detected}")
print()
print("Transport Features:")
print("-" * 50)
print(" stdio:")
print(" - Local subprocess communication")
print(" - Newline-delimited JSON-RPC")
print(" - Environment variable support")
print()
print(" Streamable HTTP:")
print(" - Session management (Mcp-Session-Id)")
print(" - Protocol versioning (Mcp-Protocol-Version)")
print(" - SSE streaming with resumability")
print()
print(" WebSocket:")
print(" - Bidirectional real-time")
print(" - Auto-reconnect with exponential backoff")
print(" - Authentication token support")
print()
print(" SSE (Legacy):")
print(" - Backward compatibility")
print(" - Protocol version 2024-11-05")
@@ -0,0 +1,56 @@
"""
MCP Streamable HTTP Transport Example
Demonstrates connecting to MCP servers via Streamable HTTP transport.
This is the current standard transport (Protocol Revision 2025-11-25).
Features:
- Single MCP endpoint for all communication
- Session management via Mcp-Session-Id header
- Protocol versioning via Mcp-Protocol-Version header
- SSE streaming with resumability
- Session termination via HTTP DELETE
Protocol: MCP 2025-11-25
"""
from praisonaiagents import Agent, MCP
# Basic Streamable HTTP connection
agent = Agent(
name="HTTP Stream Assistant",
instructions="You are a helpful assistant connected via Streamable HTTP.",
tools=MCP("https://api.example.com/mcp")
)
# With authentication headers
agent_auth = Agent(
name="Authenticated Assistant",
instructions="You are a helpful assistant with authenticated connection.",
tools=MCP(
"https://api.example.com/mcp",
headers={"Authorization": "Bearer your-token"}
)
)
# With timeout and debug
agent_debug = Agent(
name="Debug Assistant",
tools=MCP(
"https://api.example.com/mcp",
timeout=120,
debug=True
)
)
if __name__ == "__main__":
print("Streamable HTTP MCP Transport Example")
print("=" * 40)
print("This example requires a Streamable HTTP MCP server running.")
print("URL pattern: https://host:port/mcp")
print()
print("Features enabled:")
print("- Mcp-Protocol-Version header (default: 2025-03-26)")
print("- Mcp-Session-Id header (automatic)")
print("- SSE streaming support")
print("- Resumability via Last-Event-ID")
+55
View File
@@ -0,0 +1,55 @@
"""
MCP WebSocket Transport Example
Demonstrates connecting to MCP servers via WebSocket (ws:// or wss://).
WebSocket transport provides bidirectional real-time communication,
ideal for cloud deployments and long-lived connections.
Features:
- Auto-detection of ws:// and wss:// URLs
- Authentication token support
- Auto-reconnect with exponential backoff
- Ping/pong keepalive
Protocol: SEP-1288 (WebSocket Transport)
"""
from praisonaiagents import Agent, MCP
# Basic WebSocket connection
agent = Agent(
name="WebSocket Assistant",
instructions="You are a helpful assistant connected via WebSocket.",
tools=MCP("ws://localhost:8080/mcp")
)
# Secure WebSocket with authentication
agent_secure = Agent(
name="Secure WebSocket Assistant",
instructions="You are a helpful assistant with secure WebSocket connection.",
tools=MCP(
"wss://api.example.com/mcp",
auth_token="Bearer your-secret-token",
timeout=60
)
)
# WebSocket with custom options
agent_custom = Agent(
name="Custom WebSocket Assistant",
tools=MCP(
"wss://api.example.com/mcp",
auth_token="your-token",
timeout=120,
debug=True
)
)
if __name__ == "__main__":
# Note: Requires a running WebSocket MCP server
# Example server: https://github.com/modelcontextprotocol/servers
print("WebSocket MCP Transport Example")
print("=" * 40)
print("This example requires a WebSocket MCP server running.")
print("URL patterns: ws://host:port/path or wss://host:port/path")
+1
View File
@@ -86,3 +86,4 @@ build
graph.py
chroma_db/
.qodo
*_COMPARISON_ANALYSIS.md
+58 -4
View File
@@ -655,12 +655,23 @@ agent = Agent(tools=[my_tool])
## MCP (Model Context Protocol)
Connect to MCP servers for extended capabilities.
Connect to MCP servers for extended capabilities. Supports **Protocol Revision 2025-11-25**.
### MCP Transports
| Transport | URL Pattern | Use Case |
|-----------|-------------|----------|
| **stdio** | Command string | Local NPX/Python servers |
| **Streamable HTTP** | `http(s)://...` | Production deployments |
| **WebSocket** | `ws://` or `wss://` | Real-time bidirectional |
| **SSE (Legacy)** | `http(s)://.../sse` | Backward compatibility |
### MCP Client (Consume MCP Servers)
```python
from praisonaiagents import Agent, MCP
# Local MCP server
# stdio - Local MCP server
agent = Agent(
tools=MCP(
command="npx",
@@ -668,9 +679,19 @@ agent = Agent(
)
)
# Remote MCP server (SSE)
# Streamable HTTP - Production server
agent = Agent(
tools=MCP(url="http://localhost:8080/sse")
tools=MCP("https://api.example.com/mcp")
)
# WebSocket - Real-time bidirectional (NEW)
agent = Agent(
tools=MCP("wss://api.example.com/mcp", auth_token="your-token")
)
# SSE (Legacy) - Backward compatibility
agent = Agent(
tools=MCP("http://localhost:8080/sse")
)
# With environment variables
@@ -683,6 +704,39 @@ agent = Agent(
)
```
### MCP Features (Protocol 2025-11-25)
- **Session Management**: Automatic `Mcp-Session-Id` handling
- **Protocol Versioning**: `Mcp-Protocol-Version` header on all requests
- **Resumability**: SSE stream recovery via `Last-Event-ID`
- **Security**: Origin validation, DNS rebinding prevention
- **WebSocket**: Auto-reconnect with exponential backoff
### 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()
@@ -2,8 +2,65 @@
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.).
using different transport methods:
- stdio: Standard input/output (subprocess)
- sse: Server-Sent Events (legacy HTTP+SSE)
- http_stream: Streamable HTTP (current standard)
- websocket: WebSocket (SEP-1288)
Protocol Revision: 2025-11-25
"""
from .mcp import MCP
__all__ = ["MCP"]
# Lazy imports for optional components
def __getattr__(name):
"""Lazy import for optional modules."""
if name == "WebSocketMCPClient":
from .mcp_websocket import WebSocketMCPClient
return WebSocketMCPClient
elif name == "SessionManager":
from .mcp_session import SessionManager
return SessionManager
elif name == "SecurityConfig":
from .mcp_security import SecurityConfig
return SecurityConfig
elif name == "BaseTransport":
from .mcp_transport import BaseTransport
return BaseTransport
elif name == "TransportConfig":
from .mcp_transport import TransportConfig
return TransportConfig
elif name == "ToolsMCPServer":
from .mcp_server import ToolsMCPServer
return ToolsMCPServer
elif name == "launch_tools_mcp_server":
from .mcp_server import launch_tools_mcp_server
return launch_tools_mcp_server
elif name == "function_to_mcp_schema":
from .mcp_utils import function_to_mcp_schema
return function_to_mcp_schema
elif name == "get_tool_metadata":
from .mcp_utils import get_tool_metadata
return get_tool_metadata
elif name == "python_type_to_json_schema":
from .mcp_utils import python_type_to_json_schema
return python_type_to_json_schema
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
__all__ = [
# Client
"MCP",
# Server (lazy loaded)
"ToolsMCPServer",
"launch_tools_mcp_server",
# Utilities (lazy loaded)
"function_to_mcp_schema",
"get_tool_metadata",
"python_type_to_json_schema",
# Transport components (lazy loaded)
"WebSocketMCPClient",
"SessionManager",
"SecurityConfig",
"BaseTransport",
"TransportConfig",
]
@@ -227,6 +227,26 @@ class MCP:
self.timeout = timeout
self.debug = debug
# Check if this is a WebSocket URL (ws:// or wss://)
if isinstance(command_or_string, str) and re.match(r'^wss?://', command_or_string):
from .mcp_websocket import WebSocketMCPClient
# Extract auth token if provided
auth_token = kwargs.pop('auth_token', None)
self.websocket_client = WebSocketMCPClient(
command_or_string,
debug=debug,
timeout=timeout,
auth_token=auth_token,
options=kwargs
)
self._tools = list(self.websocket_client.tools)
self.is_sse = False
self.is_http_stream = False
self.is_websocket = True
self.is_npx = False
return
# Check if this is an HTTP URL
if isinstance(command_or_string, str) and re.match(r'^https?://', command_or_string):
# Determine transport type based on URL or kwargs
@@ -0,0 +1,150 @@
"""
MCP Backward Compatibility Module.
This module provides utilities for maintaining backward compatibility
with older MCP servers and the deprecated HTTP+SSE transport from
protocol version 2024-11-05.
Per MCP specification, clients wanting to support older servers should:
1. Attempt POST to server URL with InitializeRequest
2. If it fails with 400, 404, or 405, fall back to legacy SSE
3. Issue GET to server URL expecting SSE stream with endpoint event
"""
from typing import Optional
def detect_transport_support(
response_status: int,
content_type: Optional[str] = None
) -> str:
"""
Detect which transport a server supports based on response.
Per MCP spec backward compatibility guide:
- Success (200) with JSON indicates Streamable HTTP
- 400, 404, 405 indicates need for legacy SSE fallback
Args:
response_status: HTTP response status code
content_type: Response Content-Type header
Returns:
Transport type: "http_stream" or "sse"
"""
# Check for fallback indicators
if response_status in (400, 404, 405):
return "sse"
# Successful response indicates Streamable HTTP support
if response_status == 200:
return "http_stream"
# Default to SSE for unknown cases
return "sse"
def is_legacy_sse_url(url: str) -> bool:
"""
Check if URL indicates legacy SSE transport.
Legacy SSE URLs typically end with /sse.
Args:
url: Server URL
Returns:
True if URL indicates legacy SSE
"""
return url.rstrip('/').endswith('/sse')
def get_legacy_sse_endpoint(base_url: str) -> str:
"""
Get the legacy SSE endpoint URL.
Args:
base_url: Base server URL
Returns:
SSE endpoint URL
"""
base = base_url.rstrip('/')
if not base.endswith('/sse'):
return f"{base}/sse"
return base
def get_streamable_http_endpoint(base_url: str) -> str:
"""
Get the Streamable HTTP endpoint URL.
Args:
base_url: Base server URL
Returns:
MCP endpoint URL
"""
base = base_url.rstrip('/')
# Remove /sse suffix if present
if base.endswith('/sse'):
base = base[:-4]
if not base.endswith('/mcp'):
return f"{base}/mcp"
return base
class TransportNegotiator:
"""
Handles automatic transport negotiation with MCP servers.
This class implements the backward compatibility algorithm
from the MCP specification for detecting server capabilities.
"""
def __init__(self, base_url: str):
"""
Initialize negotiator.
Args:
base_url: Server base URL
"""
self.base_url = base_url
self._detected_transport: Optional[str] = None
@property
def detected_transport(self) -> Optional[str]:
"""Get the detected transport type."""
return self._detected_transport
def set_detected_transport(self, transport: str) -> None:
"""
Set the detected transport type.
Args:
transport: Transport type ("http_stream" or "sse")
"""
self._detected_transport = transport
def get_endpoint_url(self) -> str:
"""
Get the appropriate endpoint URL based on detected transport.
Returns:
Endpoint URL for the detected transport
"""
if self._detected_transport == "sse":
return get_legacy_sse_endpoint(self.base_url)
return get_streamable_http_endpoint(self.base_url)
def should_try_streamable_first(self) -> bool:
"""
Determine if Streamable HTTP should be tried first.
Per MCP spec, clients should try Streamable HTTP first
unless the URL explicitly indicates legacy SSE.
Returns:
True if should try Streamable HTTP first
"""
return not is_legacy_sse_url(self.base_url)
@@ -200,16 +200,31 @@ class HTTPStreamTransport:
This transport provides a single endpoint for all MCP communication,
supporting both batch (JSON) and streaming (SSE) response modes.
Per MCP Protocol Revision 2025-11-25:
- Includes MCP-Protocol-Version header on all requests
- Handles session management via Mcp-Session-Id header
- Supports SSE resumability via Last-Event-ID
"""
# Default protocol version for backward compatibility
DEFAULT_PROTOCOL_VERSION = '2025-03-26'
def __init__(self, base_url: str, session_id: Optional[str] = None, options: Optional[Dict[str, Any]] = None):
self.base_url = base_url
self.session_id = session_id
self.options = options or {}
self.response_mode = self.options.get('responseMode', 'batch')
self.protocol_version = self.options.get('protocol_version', self.DEFAULT_PROTOCOL_VERSION)
# Track last event ID for resumability
self.last_event_id: Optional[str] = None
self._retry_delay_ms: int = 3000 # Default retry delay
self.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json, text/event-stream'
'Accept': 'application/json, text/event-stream',
'Mcp-Protocol-Version': self.protocol_version # Required per spec
}
if session_id:
self.headers['Mcp-Session-Id'] = session_id
@@ -281,7 +296,7 @@ class HTTPStreamTransport:
raise
async def _process_sse_response(self, response):
"""Process SSE response stream."""
"""Process SSE response stream with resumability support."""
buffer = ""
async for chunk in response.content:
buffer += chunk.decode('utf-8')
@@ -291,12 +306,34 @@ class HTTPStreamTransport:
event, buffer = buffer.split("\n\n", 1)
lines = event.strip().split("\n")
# Parse SSE event
data = None
for line in lines:
if line.startswith("data: "):
data = line[6:] # Remove "data: " prefix
# Parse SSE event fields per spec
event_id = None
data_lines = []
retry_value = None
for line in lines:
if line.startswith("id:"):
# Track event ID for resumability
event_id = line[3:].strip()
elif line.startswith("data:"):
data_lines.append(line[5:].strip())
elif line.startswith("retry:"):
# Handle retry field per SSE spec
try:
retry_value = int(line[6:].strip())
except ValueError:
pass
# Update last event ID for resumability
if event_id:
self.last_event_id = event_id
# Update retry delay if provided
if retry_value is not None:
self._retry_delay_ms = retry_value
# Process data if present
data = '\n'.join(data_lines) if data_lines else None
if data:
try:
message = json.loads(data)
@@ -310,13 +347,49 @@ class HTTPStreamTransport:
except json.JSONDecodeError:
logger.error(f"Failed to parse SSE event: {data}")
async def terminate_session(self) -> bool:
"""
Terminate the session via HTTP DELETE.
Per MCP spec: Clients that no longer need a particular session
SHOULD send an HTTP DELETE to the MCP endpoint with the
Mcp-Session-Id header to explicitly terminate the session.
Returns:
True if session was terminated, False if server doesn't support it
"""
if not self._session or not self.session_id:
return False
try:
headers = {
'Mcp-Session-Id': self.session_id,
'Mcp-Protocol-Version': self.protocol_version
}
async with self._session.delete(self.base_url, headers=headers) as response:
if response.status == 405:
# Server doesn't allow client-initiated session termination
logger.debug("Server does not allow session termination via DELETE")
return False
elif response.status in (200, 202, 204):
# Session terminated successfully
self.session_id = None
self.headers.pop('Mcp-Session-Id', None)
return True
else:
logger.warning(f"Unexpected response to DELETE: {response.status}")
return False
except Exception as e:
logger.error(f"Error terminating session: {e}")
return False
async def start_sse_listener(self):
"""Start listening for SSE events from the server."""
if self._sse_task is None or self._sse_task.done():
self._sse_task = asyncio.create_task(self._sse_listener())
async def _sse_listener(self):
"""Background task to listen for SSE events."""
"""Background task to listen for SSE events with resumability support."""
while True:
try:
# Check if we should stop
@@ -324,18 +397,28 @@ class HTTPStreamTransport:
break
url = self.base_url
if self.session_id:
# Add session as query parameter for SSE connection
url = f"{url}?session={self.session_id}"
# Build headers per MCP spec
headers = {
'Accept': 'text/event-stream',
'Cache-Control': 'no-cache'
'Cache-Control': 'no-cache',
'Mcp-Protocol-Version': self.protocol_version # Required per spec
}
if self.session_id:
headers['Mcp-Session-Id'] = self.session_id
# Include Last-Event-ID for resumability per spec
if self.last_event_id:
headers['Last-Event-ID'] = self.last_event_id
async with self._session.get(url, headers=headers) as response:
# Handle session expiration (HTTP 404)
if response.status == 404:
logger.warning("Session expired (HTTP 404), need to reinitialize")
self.session_id = None
self.headers.pop('Mcp-Session-Id', None)
break
buffer = ""
async for chunk in response.content:
# Check if we should stop
@@ -349,12 +432,32 @@ class HTTPStreamTransport:
event, buffer = buffer.split("\n\n", 1)
lines = event.strip().split("\n")
# Parse SSE event
data = None
for line in lines:
if line.startswith("data: "):
data = line[6:] # Remove "data: " prefix
# Parse SSE event fields
event_id = None
data_lines = []
retry_value = None
for line in lines:
if line.startswith("id:"):
event_id = line[3:].strip()
elif line.startswith("data:"):
data_lines.append(line[5:].strip())
elif line.startswith("retry:"):
try:
retry_value = int(line[6:].strip())
except ValueError:
pass
# Update last event ID for resumability
if event_id:
self.last_event_id = event_id
# Update retry delay if provided
if retry_value is not None:
self._retry_delay_ms = retry_value
# Process data
data = '\n'.join(data_lines) if data_lines else None
if data:
try:
message = json.loads(data)
@@ -368,7 +471,8 @@ class HTTPStreamTransport:
except Exception as e:
if not (hasattr(self, '_closing') and self._closing):
logger.error(f"SSE listener error: {e}")
await asyncio.sleep(1) # Reconnect after 1 second
# Use retry delay from server or default
await asyncio.sleep(self._retry_delay_ms / 1000.0)
else:
break
@@ -0,0 +1,248 @@
"""
MCP Security Module.
This module provides security utilities for MCP transports,
implementing security best practices from MCP Protocol
Revision 2025-11-25.
Security features:
- Origin header validation (DNS rebinding prevention)
- Localhost binding recommendations
- Authentication header support
- Secure session ID generation
"""
import secrets
import base64
from typing import Optional, Dict, List, Set
from urllib.parse import urlparse
from dataclasses import dataclass, field
# Localhost addresses
LOCALHOST_ADDRESSES: Set[str] = {
'localhost',
'127.0.0.1',
'::1', # IPv6 localhost
}
def is_valid_origin(
origin: Optional[str],
allowed_origins: List[str],
allow_missing: bool = False
) -> bool:
"""
Validate Origin header against allowed origins.
Per MCP spec, servers MUST validate the Origin header on all
incoming connections to prevent DNS rebinding attacks.
Args:
origin: The Origin header value
allowed_origins: List of allowed origin hosts
allow_missing: Whether to allow missing Origin header
Returns:
True if origin is valid, False otherwise
"""
if not origin:
return allow_missing
# Extract host from origin
host = extract_origin_host(origin)
if not host:
return False
# Check against allowed origins
return host in allowed_origins
def extract_origin_host(origin: str) -> Optional[str]:
"""
Extract the host from an Origin header value.
Args:
origin: Origin header value (e.g., "https://example.com:443")
Returns:
Host portion of the origin, or None if invalid
"""
if not origin:
return None
try:
parsed = urlparse(origin)
return parsed.hostname
except Exception:
return None
def is_localhost_address(address: str) -> bool:
"""
Check if an address is a localhost address.
Args:
address: IP address or hostname
Returns:
True if localhost, False otherwise
"""
return address.lower() in LOCALHOST_ADDRESSES
def should_bind_localhost_only(is_local: bool = True) -> bool:
"""
Determine if server should bind to localhost only.
Per MCP spec, when running locally, servers SHOULD bind only
to localhost (127.0.0.1) rather than all network interfaces.
Args:
is_local: Whether this is a local development server
Returns:
True if should bind to localhost only
"""
return is_local
def is_potential_dns_rebinding(origin: str, server_host: str) -> bool:
"""
Detect potential DNS rebinding attack.
DNS rebinding occurs when an external origin tries to access
a local server through DNS manipulation.
Args:
origin: The Origin header value
server_host: The host the server is bound to
Returns:
True if potential DNS rebinding attempt
"""
origin_host = extract_origin_host(origin)
if not origin_host:
return True # Treat invalid origin as suspicious
# If server is on localhost, only localhost origins are safe
if is_localhost_address(server_host):
return not is_localhost_address(origin_host)
# For non-localhost servers, check if origin matches server
return origin_host != server_host
def create_auth_header(
credentials: str,
auth_type: str = "bearer",
header_name: str = "Authorization"
) -> Dict[str, str]:
"""
Create an authentication header.
Args:
credentials: The credentials/token
auth_type: Type of auth ("bearer", "basic", "custom")
header_name: Custom header name (for auth_type="custom")
Returns:
Dictionary with authentication header
"""
if auth_type.lower() == "bearer":
return {"Authorization": f"Bearer {credentials}"}
elif auth_type.lower() == "basic":
# Base64 encode for Basic auth
encoded = base64.b64encode(credentials.encode()).decode()
return {"Authorization": f"Basic {encoded}"}
elif auth_type.lower() == "custom":
return {header_name: credentials}
else:
raise ValueError(f"Unknown auth_type: {auth_type}")
def validate_auth_header(headers: Dict[str, str]) -> bool:
"""
Validate that authentication header is present.
Args:
headers: Request headers dictionary
Returns:
True if Authorization header is present
"""
# Check for Authorization header (case-insensitive)
for key in headers:
if key.lower() == 'authorization':
return bool(headers[key])
return False
def generate_secure_session_id(length: int = 32) -> str:
"""
Generate a cryptographically secure session ID.
Per MCP spec, session IDs SHOULD be globally unique and
cryptographically secure (e.g., securely generated UUID,
JWT, or cryptographic hash).
The ID MUST only contain visible ASCII characters (0x21-0x7E).
Args:
length: Desired length of session ID (minimum 32)
Returns:
Secure session ID string
"""
# Ensure minimum length for security
length = max(length, 32)
# Use URL-safe base64 which only uses visible ASCII
# Generate extra bytes to account for base64 expansion
num_bytes = (length * 3) // 4 + 1
random_bytes = secrets.token_bytes(num_bytes)
# Encode to URL-safe base64 (uses A-Z, a-z, 0-9, -, _)
session_id = base64.urlsafe_b64encode(random_bytes).decode()
# Trim to desired length
return session_id[:length]
@dataclass
class SecurityConfig:
"""
Security configuration for MCP transports.
This class holds security settings that can be applied
to MCP server implementations.
Attributes:
validate_origin: Whether to validate Origin header
allow_missing_origin: Whether to allow missing Origin
allowed_origins: List of allowed origin hosts
require_auth: Whether authentication is required
bind_localhost_only: Whether to bind to localhost only
"""
validate_origin: bool = True
allow_missing_origin: bool = False
allowed_origins: List[str] = field(default_factory=lambda: ["localhost", "127.0.0.1"])
require_auth: bool = False
bind_localhost_only: bool = True
def is_origin_allowed(self, origin: Optional[str]) -> bool:
"""Check if an origin is allowed by this config."""
if not self.validate_origin:
return True
return is_valid_origin(
origin,
self.allowed_origins,
self.allow_missing_origin
)
def get_bind_address(self) -> str:
"""Get recommended bind address based on config."""
return "127.0.0.1" if self.bind_localhost_only else "0.0.0.0"
@@ -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,457 @@
"""
MCP Session Management Module.
This module provides session management utilities for MCP transports,
implementing the session management requirements from MCP Protocol
Revision 2025-11-25.
Features:
- Session ID validation (visible ASCII 0x21-0x7E)
- Session lifecycle management
- Protocol version header handling
- Session expiration detection
"""
import re
from typing import Optional, Dict, Any
# Valid protocol versions
VALID_PROTOCOL_VERSIONS = {
'2024-11-05', # Original HTTP+SSE
'2025-03-26', # Streamable HTTP introduction
'2025-06-18', # Updates
'2025-11-25', # Current revision
}
# Default protocol version for backward compatibility
DEFAULT_PROTOCOL_VERSION = '2025-03-26'
def is_valid_session_id(session_id: Optional[str]) -> bool:
"""
Validate MCP session ID per specification.
Session IDs MUST only contain visible ASCII characters (0x21 to 0x7E).
This excludes space (0x20), control characters (0x00-0x1F), and DEL (0x7F).
Args:
session_id: The session ID to validate
Returns:
True if valid, False otherwise
"""
if session_id is None or session_id == "":
return False
# Check each character is in visible ASCII range (0x21-0x7E)
for char in session_id:
code = ord(char)
if code < 0x21 or code > 0x7E:
return False
return True
def is_valid_protocol_version(version: Optional[str]) -> bool:
"""
Validate MCP protocol version string.
Args:
version: Protocol version string (e.g., '2025-11-25')
Returns:
True if valid, False otherwise
"""
if not version:
return False
# Check format: YYYY-MM-DD
if not re.match(r'^\d{4}-\d{2}-\d{2}$', version):
return False
return True
def is_session_expired(status_code: int) -> bool:
"""
Check if HTTP status code indicates session expiration.
Per MCP spec, HTTP 404 indicates the session has been terminated.
Args:
status_code: HTTP response status code
Returns:
True if session is expired, False otherwise
"""
return status_code == 404
def extract_session_id(headers: Dict[str, str]) -> Optional[str]:
"""
Extract session ID from HTTP response headers.
The header name is case-insensitive per HTTP spec.
Args:
headers: HTTP response headers dictionary
Returns:
Session ID if present, None otherwise
"""
# Check various case combinations
for key in headers:
if key.lower() == 'mcp-session-id':
return headers[key]
return None
class SessionManager:
"""
Manages MCP session state for HTTP transports.
This class handles:
- Session ID storage and validation
- Protocol version negotiation
- Session lifecycle (active, expired, terminated)
- Header generation for requests
Example:
```python
manager = SessionManager()
# After receiving InitializeResult with session ID
manager.set_session_id("abc123")
# Get headers for subsequent requests
headers = manager.get_headers()
# {'Mcp-Session-Id': 'abc123', 'Mcp-Protocol-Version': '2025-03-26'}
```
"""
def __init__(self, protocol_version: Optional[str] = None):
"""
Initialize session manager.
Args:
protocol_version: Initial protocol version (defaults to 2025-03-26)
"""
self._session_id: Optional[str] = None
self._protocol_version: str = protocol_version or DEFAULT_PROTOCOL_VERSION
self._is_expired: bool = False
self._metadata: Dict[str, Any] = {}
@property
def session_id(self) -> Optional[str]:
"""Get current session ID."""
return self._session_id
@property
def protocol_version(self) -> str:
"""Get current protocol version."""
return self._protocol_version
@property
def is_active(self) -> bool:
"""Check if session is active (has ID and not expired)."""
return self._session_id is not None and not self._is_expired
@property
def is_expired(self) -> bool:
"""Check if session has expired."""
return self._is_expired
def set_session_id(self, session_id: str) -> None:
"""
Set the session ID.
Args:
session_id: The session ID from server
Raises:
ValueError: If session ID is invalid
"""
if not is_valid_session_id(session_id):
raise ValueError("Invalid session ID: must contain only visible ASCII (0x21-0x7E)")
self._session_id = session_id
self._is_expired = False
def set_protocol_version(self, version: str) -> None:
"""
Set the negotiated protocol version.
Args:
version: Protocol version string
Raises:
ValueError: If version format is invalid
"""
if not is_valid_protocol_version(version):
raise ValueError(f"Invalid protocol version format: {version}")
self._protocol_version = version
def clear(self) -> None:
"""Clear the session (logout/disconnect)."""
self._session_id = None
self._is_expired = False
self._metadata = {}
def mark_expired(self) -> None:
"""Mark the session as expired (received HTTP 404)."""
self._is_expired = True
def get_headers(self) -> Dict[str, str]:
"""
Get HTTP headers for MCP requests.
Returns:
Dictionary of headers to include in requests
"""
headers = {
'Mcp-Protocol-Version': self._protocol_version
}
if self._session_id:
headers['Mcp-Session-Id'] = self._session_id
return headers
def update_from_response(self, headers: Dict[str, str], status_code: int = 200) -> None:
"""
Update session state from HTTP response.
Args:
headers: Response headers
status_code: HTTP status code
"""
# Check for session expiration
if is_session_expired(status_code):
self.mark_expired()
return
# Extract and update session ID if present
new_session_id = extract_session_id(headers)
if new_session_id:
self.set_session_id(new_session_id)
def set_metadata(self, key: str, value: Any) -> None:
"""Store arbitrary metadata with the session."""
self._metadata[key] = value
def get_metadata(self, key: str, default: Any = None) -> Any:
"""Retrieve metadata from the session."""
return self._metadata.get(key, default)
class ResumabilityManager:
"""
Manages SSE stream resumability for MCP transports.
This class handles:
- Event ID tracking per stream
- Last-Event-ID header generation
- Retry delay handling
Per MCP spec, event IDs should be assigned per-stream to act as
a cursor within that particular stream.
"""
def __init__(self):
"""Initialize resumability manager."""
self._last_event_ids: Dict[str, str] = {} # stream_id -> last_event_id
self._retry_delay: int = 3000 # Default retry delay in ms
def set_last_event_id(self, stream_id: str, event_id: str) -> None:
"""
Store the last received event ID for a stream.
Args:
stream_id: Identifier for the SSE stream
event_id: The event ID from the SSE event
"""
self._last_event_ids[stream_id] = event_id
def get_last_event_id(self, stream_id: str) -> Optional[str]:
"""
Get the last received event ID for a stream.
Args:
stream_id: Identifier for the SSE stream
Returns:
Last event ID if available, None otherwise
"""
return self._last_event_ids.get(stream_id)
def set_retry_delay(self, delay_ms: int) -> None:
"""
Set the retry delay from SSE retry field.
Args:
delay_ms: Delay in milliseconds
"""
self._retry_delay = delay_ms
def get_retry_delay(self) -> int:
"""Get current retry delay in milliseconds."""
return self._retry_delay
def get_resume_headers(self, stream_id: str) -> Dict[str, str]:
"""
Get headers for resuming a stream.
Args:
stream_id: Identifier for the SSE stream
Returns:
Headers dict with Last-Event-ID if available
"""
headers = {}
last_event_id = self.get_last_event_id(stream_id)
if last_event_id:
headers['Last-Event-ID'] = last_event_id
return headers
def clear_stream(self, stream_id: str) -> None:
"""Clear tracking for a specific stream."""
self._last_event_ids.pop(stream_id, None)
def clear_all(self) -> None:
"""Clear all stream tracking."""
self._last_event_ids.clear()
def parse_sse_event(event_text: str) -> Dict[str, Any]:
"""
Parse an SSE event into its components.
SSE events have the format:
id: <event-id>
retry: <milliseconds>
data: <data-line>
data: <data-line-2>
(blank line ends event)
Args:
event_text: Raw SSE event text
Returns:
Dictionary with 'id', 'retry', 'data', 'event' fields as available
"""
result: Dict[str, Any] = {}
data_lines = []
for line in event_text.strip().split('\n'):
if line.startswith('id:'):
result['id'] = line[3:].strip()
elif line.startswith('retry:'):
try:
result['retry'] = int(line[6:].strip())
except ValueError:
pass
elif line.startswith('data:'):
data_lines.append(line[5:].strip())
elif line.startswith('event:'):
result['event'] = line[6:].strip()
# Join multiple data lines with newlines
if data_lines:
result['data'] = '\n'.join(data_lines)
return result
class EventStore:
"""
Optional event store for message redelivery support.
This class stores events per stream to enable replay when
clients reconnect with Last-Event-ID header.
Note: This is an in-memory store suitable for single-server
deployments. For multi-node deployments, use an external
store (Redis, database, etc.).
"""
def __init__(self, max_events: int = 1000):
"""
Initialize event store.
Args:
max_events: Maximum events to store per stream
"""
self.max_events = max_events
self._streams: Dict[str, list] = {} # stream_id -> [(event_id, data), ...]
def store(self, stream_id: str, event_id: str, data: Any) -> None:
"""
Store an event for potential replay.
Args:
stream_id: Identifier for the SSE stream
event_id: The event ID
data: The event data
"""
if stream_id not in self._streams:
self._streams[stream_id] = []
self._streams[stream_id].append((event_id, data))
# Trim to max_events
if len(self._streams[stream_id]) > self.max_events:
self._streams[stream_id] = self._streams[stream_id][-self.max_events:]
def get_events_after(self, stream_id: str, after_event_id: str) -> list:
"""
Get events after a specific event ID.
Args:
stream_id: Identifier for the SSE stream
after_event_id: The Last-Event-ID from client
Returns:
List of event data after the specified event
"""
if stream_id not in self._streams:
return []
events = self._streams[stream_id]
# Find the index of the after_event_id
start_index = None
for i, (event_id, _) in enumerate(events):
if event_id == after_event_id:
start_index = i + 1
break
if start_index is None:
return []
return [data for _, data in events[start_index:]]
def get_all_events(self, stream_id: str) -> list:
"""
Get all stored events for a stream.
Args:
stream_id: Identifier for the SSE stream
Returns:
List of all event data
"""
if stream_id not in self._streams:
return []
return [data for _, data in self._streams[stream_id]]
def clear_stream(self, stream_id: str) -> None:
"""Clear events for a specific stream."""
self._streams.pop(stream_id, None)
def clear_all(self) -> None:
"""Clear all stored events."""
self._streams.clear()
@@ -0,0 +1,298 @@
"""
MCP Transport Abstraction Layer.
This module provides a common interface for all MCP transports,
enabling easy switching between transport types and custom
transport implementations.
Transport types:
- stdio: Standard input/output (subprocess)
- sse: Server-Sent Events (legacy HTTP+SSE)
- http_stream: Streamable HTTP (current standard)
- websocket: WebSocket (SEP-1288)
"""
import re
from abc import ABC, abstractmethod
from typing import Any, Dict, Optional, List, Type
from dataclasses import dataclass, field
@dataclass
class TransportConfig:
"""
Configuration for MCP transports.
Attributes:
timeout: Connection/operation timeout in seconds
debug: Enable debug logging
retry_count: Number of retry attempts
retry_delay: Base delay between retries in seconds
auth_token: Optional authentication token
headers: Additional headers for HTTP transports
"""
timeout: int = 60
debug: bool = False
retry_count: int = 3
retry_delay: float = 1.0
auth_token: Optional[str] = None
headers: Dict[str, str] = field(default_factory=dict)
class BaseTransport(ABC):
"""
Abstract base class for MCP transports.
All MCP transports must implement this interface to ensure
consistent behavior across transport types.
The transport is responsible for:
- Establishing and maintaining connections
- Sending JSON-RPC messages
- Receiving JSON-RPC messages
- Proper cleanup on close
"""
@abstractmethod
async def connect(self) -> None:
"""
Establish connection to the MCP server.
Raises:
ConnectionError: If connection fails
"""
pass
@abstractmethod
async def send(self, message: Dict[str, Any]) -> None:
"""
Send a JSON-RPC message to the server.
Args:
message: JSON-RPC message dictionary
Raises:
RuntimeError: If not connected
"""
pass
@abstractmethod
async def receive(self) -> Dict[str, Any]:
"""
Receive a JSON-RPC message from the server.
Returns:
JSON-RPC message dictionary
Raises:
RuntimeError: If not connected
"""
pass
@abstractmethod
async def close(self) -> None:
"""
Close the connection and cleanup resources.
"""
pass
@property
@abstractmethod
def is_connected(self) -> bool:
"""Check if transport is currently connected."""
pass
async def __aenter__(self):
"""Async context manager entry."""
await self.connect()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit."""
await self.close()
def get_transport_type(target: str) -> str:
"""
Determine the appropriate transport type for a target.
Args:
target: URL or command string
Returns:
Transport type string: "stdio", "sse", "http_stream", or "websocket"
"""
if not isinstance(target, str):
return "stdio"
# WebSocket URLs
if re.match(r'^wss?://', target, re.IGNORECASE):
return "websocket"
# HTTP URLs
if re.match(r'^https?://', target, re.IGNORECASE):
# Legacy SSE endpoint
if target.endswith('/sse'):
return "sse"
# Default to Streamable HTTP
return "http_stream"
# Everything else is stdio (command)
return "stdio"
class TransportRegistry:
"""
Registry for MCP transport implementations.
This allows registration of custom transports and provides
a central place to look up transport classes by name.
"""
def __init__(self):
"""Initialize empty registry."""
self._transports: Dict[str, Type[BaseTransport]] = {}
def register(self, name: str, transport_class: Type[BaseTransport]) -> None:
"""
Register a transport class.
Args:
name: Transport name (e.g., "websocket")
transport_class: Transport class (must extend BaseTransport)
"""
if not issubclass(transport_class, BaseTransport):
raise TypeError(f"{transport_class} must extend BaseTransport")
self._transports[name] = transport_class
def get(self, name: str) -> Optional[Type[BaseTransport]]:
"""
Get a registered transport class.
Args:
name: Transport name
Returns:
Transport class or None if not found
"""
return self._transports.get(name)
def list_transports(self) -> List[str]:
"""
List all registered transport names.
Returns:
List of transport names
"""
return list(self._transports.keys())
def unregister(self, name: str) -> None:
"""
Unregister a transport.
Args:
name: Transport name to remove
"""
self._transports.pop(name, None)
# Placeholder transport classes for registry
# These are minimal implementations to satisfy the registry
class StdioTransportPlaceholder(BaseTransport):
"""Placeholder for stdio transport."""
async def connect(self): pass
async def send(self, message): pass
async def receive(self): return {}
async def close(self): pass
@property
def is_connected(self): return False
class SSETransportPlaceholder(BaseTransport):
"""Placeholder for SSE transport."""
async def connect(self): pass
async def send(self, message): pass
async def receive(self): return {}
async def close(self): pass
@property
def is_connected(self): return False
class HTTPStreamTransportPlaceholder(BaseTransport):
"""Placeholder for HTTP Stream transport."""
async def connect(self): pass
async def send(self, message): pass
async def receive(self): return {}
async def close(self): pass
@property
def is_connected(self): return False
class WebSocketTransportPlaceholder(BaseTransport):
"""Placeholder for WebSocket transport."""
async def connect(self): pass
async def send(self, message): pass
async def receive(self): return {}
async def close(self): pass
@property
def is_connected(self): return False
# Default registry instance
_default_registry: Optional[TransportRegistry] = None
def get_default_registry() -> TransportRegistry:
"""
Get the default transport registry with built-in transports.
Returns:
TransportRegistry with default transports registered
"""
global _default_registry
if _default_registry is None:
_default_registry = TransportRegistry()
# Register default transports
_default_registry.register("stdio", StdioTransportPlaceholder)
_default_registry.register("sse", SSETransportPlaceholder)
_default_registry.register("http_stream", HTTPStreamTransportPlaceholder)
_default_registry.register("websocket", WebSocketTransportPlaceholder)
return _default_registry
def create_transport(
target: str,
config: Optional[TransportConfig] = None,
registry: Optional[TransportRegistry] = None
) -> BaseTransport:
"""
Create a transport instance for the given target.
Args:
target: URL or command string
config: Optional transport configuration
registry: Optional custom registry (uses default if None)
Returns:
Transport instance
Raises:
ValueError: If no suitable transport found
"""
if registry is None:
registry = get_default_registry()
if config is None:
config = TransportConfig()
transport_type = get_transport_type(target)
transport_class = registry.get(transport_type)
if transport_class is None:
raise ValueError(f"No transport registered for type: {transport_type}")
return transport_class()
@@ -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]
@@ -0,0 +1,596 @@
"""
WebSocket Transport for MCP (Model Context Protocol).
This module provides WebSocket-based transport for connecting to MCP servers.
WebSocket transport offers:
- Long-lived bidirectional communication
- Better cloud provider support (hibernatable connections)
- Lower protocol overhead compared to HTTP
- Session persistence across network interruptions
This is an optional transport - the 'websockets' package is only imported
when ws:// or wss:// URLs are used, ensuring no performance impact on
existing stdio/SSE/HTTP transports.
Based on SEP-1288: WebSocket Transport for MCP (in review).
Protocol Revision: 2025-11-25
"""
import asyncio
import json
import logging
import threading
import inspect
import re
from typing import Any, Dict, Optional, List
logger = logging.getLogger("mcp-websocket")
def is_websocket_url(url: str) -> bool:
"""
Check if a URL is a WebSocket URL.
Args:
url: The URL string to check
Returns:
True if the URL starts with ws:// or wss://, False otherwise
"""
if not isinstance(url, str):
return False
return bool(re.match(r'^wss?://', url, re.IGNORECASE))
def calculate_backoff(attempt: int, base_delay: float = 1.0, max_delay: float = 60.0) -> float:
"""
Calculate exponential backoff delay for reconnection.
Args:
attempt: The retry attempt number (0-indexed)
base_delay: Base delay in seconds
max_delay: Maximum delay cap in seconds
Returns:
Delay in seconds for this attempt
"""
delay = base_delay * (2 ** attempt)
return min(delay, max_delay)
# Global event loop for WebSocket operations (similar to other MCP transports)
_event_loop = None
_loop_lock = threading.Lock()
def get_event_loop():
"""Get or create a global event loop for WebSocket operations."""
global _event_loop
with _loop_lock:
if _event_loop is None or _event_loop.is_closed():
_event_loop = asyncio.new_event_loop()
return _event_loop
class WebSocketTransport:
"""
Low-level WebSocket transport for MCP communication.
This class handles the WebSocket connection and JSON-RPC message framing.
It supports:
- Secure (wss://) and insecure (ws://) connections
- Session ID management
- Authentication via subprotocol or headers
- Automatic reconnection with exponential backoff
"""
def __init__(
self,
url: str,
session_id: Optional[str] = None,
auth_token: Optional[str] = None,
max_retries: int = 3,
retry_delay: float = 1.0,
timeout: float = 30.0,
ping_interval: float = 30.0,
ping_timeout: float = 10.0
):
"""
Initialize WebSocket transport.
Args:
url: WebSocket URL (ws:// or wss://)
session_id: Optional MCP session ID
auth_token: Optional authentication token
max_retries: Maximum reconnection attempts
retry_delay: Base delay between retries (exponential backoff)
timeout: Connection timeout in seconds
ping_interval: Interval between ping messages (keepalive)
ping_timeout: Timeout for ping response
"""
self.url = url
self.session_id = session_id
self.auth_token = auth_token
self.max_retries = max_retries
self.retry_delay = retry_delay
self.timeout = timeout
self.ping_interval = ping_interval
self.ping_timeout = ping_timeout
self._ws = None
self._closed = False
self._message_queue: asyncio.Queue = asyncio.Queue()
self._receive_task = None
async def __aenter__(self):
"""Async context manager entry - establish connection."""
await self.connect()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Async context manager exit - close connection."""
await self.close()
async def connect(self):
"""Establish WebSocket connection."""
try:
# Lazy import websockets
import websockets
except ImportError:
raise ImportError(
"websockets package is required for WebSocket transport. "
"Install it with: pip install websockets"
)
# Build connection options
extra_headers = {}
subprotocols = []
# Add session ID if present
if self.session_id:
extra_headers['Mcp-Session-Id'] = self.session_id
# Add auth token via subprotocol (SEP-1288 recommendation)
if self.auth_token:
# Smuggle auth token in subprotocol list
# Format: "mcp-auth-<base64-encoded-token>"
import base64
encoded_token = base64.b64encode(self.auth_token.encode()).decode()
subprotocols.append(f"mcp-auth-{encoded_token}")
# Always include MCP subprotocol
subprotocols.append("mcp")
# Connect with retry logic
last_error = None
for attempt in range(self.max_retries + 1):
try:
self._ws = await asyncio.wait_for(
websockets.connect(
self.url,
extra_headers=extra_headers if extra_headers else None,
subprotocols=subprotocols if subprotocols else None,
ping_interval=self.ping_interval,
ping_timeout=self.ping_timeout,
close_timeout=10
),
timeout=self.timeout
)
logger.debug(f"WebSocket connected to {self.url}")
# Start background receive task
self._receive_task = asyncio.create_task(self._receive_loop())
return
except Exception as e:
last_error = e
if attempt < self.max_retries:
delay = calculate_backoff(attempt, self.retry_delay)
logger.warning(f"WebSocket connection failed, retrying in {delay}s: {e}")
await asyncio.sleep(delay)
else:
raise ConnectionError(f"Failed to connect after {self.max_retries + 1} attempts: {last_error}")
async def _receive_loop(self):
"""Background task to receive messages and queue them."""
try:
async for message in self._ws:
try:
data = json.loads(message)
await self._message_queue.put(data)
except json.JSONDecodeError as e:
logger.error(f"Failed to parse WebSocket message: {e}")
except Exception as e:
if not self._closed:
logger.error(f"WebSocket receive error: {e}")
await self._message_queue.put({"error": str(e)})
async def send(self, message: Dict[str, Any]):
"""
Send a JSON-RPC message over WebSocket.
Args:
message: JSON-RPC message dictionary
"""
if self._ws is None:
raise RuntimeError("WebSocket not connected")
data = json.dumps(message)
await self._ws.send(data)
logger.debug(f"Sent: {message.get('method', message.get('id', 'response'))}")
async def receive(self) -> Dict[str, Any]:
"""
Receive a JSON-RPC message from WebSocket.
Returns:
Parsed JSON-RPC message dictionary
"""
message = await self._message_queue.get()
if "error" in message and len(message) == 1:
raise ConnectionError(message["error"])
return message
async def close(self):
"""Close WebSocket connection."""
self._closed = True
if self._receive_task:
self._receive_task.cancel()
try:
await self._receive_task
except asyncio.CancelledError:
pass
if self._ws:
await self._ws.close()
self._ws = None
logger.debug("WebSocket connection closed")
class WebSocketMCPTool:
"""
A wrapper for an MCP tool that can be used with praisonaiagents.
This class wraps a tool from a WebSocket MCP server and makes it
compatible with the Agent class tool interface.
"""
def __init__(
self,
name: str,
description: str,
session: Any, # ClientSession from mcp package
input_schema: Optional[Dict[str, Any]] = None,
timeout: int = 60
):
"""
Initialize WebSocket MCP tool wrapper.
Args:
name: Tool name
description: Tool description
session: MCP ClientSession for making calls
input_schema: JSON Schema for tool input
timeout: Timeout for tool calls in seconds
"""
self.name = name
self.__name__ = name
self.__qualname__ = name
self.__doc__ = description
self.description = description
self.session = session
self.input_schema = input_schema or {}
self.timeout = timeout
# Build function signature from input schema
self.__signature__ = self._build_signature()
def _build_signature(self) -> inspect.Signature:
"""Build function signature from input schema."""
params = []
if self.input_schema and 'properties' in self.input_schema:
required = self.input_schema.get('required', [])
for param_name, prop_schema in self.input_schema['properties'].items():
# Determine type annotation
prop_type = prop_schema.get('type', 'string') if isinstance(prop_schema, dict) else 'string'
annotation = self._json_type_to_python(prop_type)
# Determine default value
default = inspect.Parameter.empty if param_name in required else None
params.append(
inspect.Parameter(
name=param_name,
kind=inspect.Parameter.POSITIONAL_OR_KEYWORD,
default=default,
annotation=annotation
)
)
return inspect.Signature(params)
def _json_type_to_python(self, json_type: str) -> type:
"""Convert JSON Schema type to Python type."""
type_map = {
'string': str,
'integer': int,
'number': float,
'boolean': bool,
'array': list,
'object': dict
}
return type_map.get(json_type, Any)
def __call__(self, **kwargs):
"""
Call the tool synchronously.
This wraps the async call for synchronous usage.
"""
logger.debug(f"Tool {self.name} called with args: {kwargs}")
loop = get_event_loop()
future = asyncio.run_coroutine_threadsafe(self._async_call(**kwargs), loop)
try:
return future.result(timeout=self.timeout)
except Exception as e:
logger.error(f"Error calling tool {self.name}: {e}")
return f"Error: {str(e)}"
async def _async_call(self, **kwargs):
"""Call the tool asynchronously."""
logger.debug(f"Async calling tool {self.name} with args: {kwargs}")
try:
result = await self.session.call_tool(self.name, kwargs)
# Extract text from result
if hasattr(result, 'content') and result.content:
if hasattr(result.content[0], 'text'):
return result.content[0].text
return str(result.content[0])
return str(result)
except Exception as e:
logger.error(f"Error in _async_call for {self.name}: {e}")
raise
def _fix_array_schemas(self, schema: Dict[str, Any]) -> Dict[str, Any]:
"""
Fix array schemas by adding missing 'items' attribute.
OpenAI requires array types to specify item types.
"""
if not isinstance(schema, dict):
return schema
fixed_schema = schema.copy()
# Fix array types at current level
if fixed_schema.get("type") == "array" and "items" not in fixed_schema:
fixed_schema["items"] = {"type": "string"}
# Recursively fix nested schemas
if "properties" in fixed_schema:
fixed_properties = {}
for prop_name, prop_schema in fixed_schema["properties"].items():
fixed_properties[prop_name] = self._fix_array_schemas(prop_schema)
fixed_schema["properties"] = fixed_properties
if "items" in fixed_schema:
fixed_schema["items"] = self._fix_array_schemas(fixed_schema["items"])
return fixed_schema
def to_openai_tool(self) -> Dict[str, Any]:
"""Convert the tool to OpenAI function calling format."""
fixed_schema = self._fix_array_schemas(self.input_schema)
return {
"type": "function",
"function": {
"name": self.name,
"description": self.description,
"parameters": fixed_schema
}
}
class WebSocketMCPClient:
"""
High-level client for connecting to MCP servers over WebSocket.
This client handles:
- Connection establishment and lifecycle
- Tool discovery and wrapping
- Session management
- Automatic reconnection
Example:
```python
from praisonaiagents.mcp.mcp_websocket import WebSocketMCPClient
client = WebSocketMCPClient("ws://localhost:8080/mcp")
for tool in client.tools:
print(tool.name)
```
"""
def __init__(
self,
server_url: str,
debug: bool = False,
timeout: int = 60,
auth_token: Optional[str] = None,
options: Optional[Dict[str, Any]] = None
):
"""
Initialize WebSocket MCP client.
Args:
server_url: WebSocket URL (ws:// or wss://)
debug: Enable debug logging
timeout: Timeout for operations in seconds
auth_token: Optional authentication token
options: Additional configuration options
"""
self.server_url = server_url
self.debug = debug
self.timeout = timeout
self.auth_token = auth_token
self.options = options or {}
self.session = None
self.transport = None
self.tools: List[WebSocketMCPTool] = []
self._closed = False
# Configure logging
if debug:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.WARNING)
# Initialize connection
self._initialize()
def _initialize(self):
"""Initialize the connection and discover tools."""
loop = get_event_loop()
# Start event loop in background thread if not running
def run_event_loop():
asyncio.set_event_loop(loop)
loop.run_forever()
self.loop_thread = threading.Thread(target=run_event_loop, daemon=True)
self.loop_thread.start()
# Run initialization
future = asyncio.run_coroutine_threadsafe(self._async_initialize(), loop)
self.tools = future.result(timeout=self.timeout)
async def _async_initialize(self):
"""Asynchronously initialize connection and discover tools."""
logger.debug(f"Connecting to MCP server at {self.server_url}")
# Lazy import mcp package
try:
from mcp import ClientSession
except ImportError:
raise ImportError(
"mcp package is required. Install it with: pip install mcp"
)
# Create transport
self.transport = WebSocketTransport(
self.server_url,
auth_token=self.auth_token,
timeout=self.timeout
)
await self.transport.connect()
# Create read/write streams for ClientSession
# The ClientSession expects async iterators for read/write
read_stream = self._create_read_stream()
write_stream = self._create_write_stream()
# Create and initialize session
self._session_context = ClientSession(read_stream, write_stream)
self.session = await self._session_context.__aenter__()
await self.session.initialize()
# Discover tools
logger.debug("Listing tools...")
response = await self.session.list_tools()
tools_data = response.tools
logger.debug(f"Found {len(tools_data)} tools: {[t.name for t in tools_data]}")
# Create tool wrappers
tools = []
for tool in tools_data:
input_schema = tool.inputSchema if hasattr(tool, 'inputSchema') else None
wrapper = WebSocketMCPTool(
name=tool.name,
description=tool.description if hasattr(tool, 'description') else f"Call the {tool.name} tool",
session=self.session,
input_schema=input_schema,
timeout=self.timeout
)
tools.append(wrapper)
return tools
def _create_read_stream(self):
"""Create async iterator for reading from transport."""
async def read():
while not self._closed:
try:
message = await self.transport.receive()
yield message
except Exception as e:
if not self._closed:
logger.error(f"Read error: {e}")
break
return read()
def _create_write_stream(self):
"""Create write function for transport."""
async def write(message):
if hasattr(message, 'to_dict'):
message = message.to_dict()
await self.transport.send(message)
return write
def __iter__(self):
"""Return iterator over tools."""
return iter(self.tools)
def to_openai_tools(self) -> List[Dict[str, Any]]:
"""Convert all tools to OpenAI format."""
return [tool.to_openai_tool() for tool in self.tools]
async def aclose(self):
"""Async cleanup."""
if self._closed:
return
self._closed = True
try:
if hasattr(self, '_session_context') and self._session_context:
await self._session_context.__aexit__(None, None, None)
except Exception:
pass
try:
if self.transport:
await self.transport.close()
except Exception:
pass
def close(self):
"""Synchronous cleanup."""
if self._closed:
return
try:
loop = get_event_loop()
if not loop.is_closed():
future = asyncio.run_coroutine_threadsafe(self.aclose(), loop)
future.result(timeout=5)
except Exception:
self._closed = True
def __del__(self):
"""Cleanup on garbage collection."""
try:
if not self._closed:
self.close()
except Exception:
pass
@@ -176,6 +176,48 @@ TOOL_MAPPINGS = {
# Claude Memory Tool (Anthropic Beta)
'ClaudeMemoryTool': ('.claude_memory_tool', 'ClaudeMemoryTool'),
# Tavily Tools (requires TAVILY_API_KEY)
'tavily': ('.tavily_tools', None),
'tavily_search': ('.tavily_tools', None),
'tavily_extract': ('.tavily_tools', None),
'tavily_crawl': ('.tavily_tools', None),
'tavily_map': ('.tavily_tools', None),
'tavily_search_async': ('.tavily_tools', None),
'tavily_extract_async': ('.tavily_tools', None),
'TavilyTools': ('.tavily_tools', 'TavilyTools'),
'tavily_tools': ('.tavily_tools', None),
# You.com Tools (requires YDC_API_KEY)
'ydc': ('.youdotcom_tools', None),
'ydc_search': ('.youdotcom_tools', None),
'ydc_contents': ('.youdotcom_tools', None),
'ydc_news': ('.youdotcom_tools', None),
'ydc_images': ('.youdotcom_tools', None),
'YouTools': ('.youdotcom_tools', 'YouTools'),
'youdotcom_tools': ('.youdotcom_tools', None),
# Exa Tools (requires EXA_API_KEY)
'exa': ('.exa_tools', None),
'exa_search': ('.exa_tools', None),
'exa_search_contents': ('.exa_tools', None),
'exa_find_similar': ('.exa_tools', None),
'exa_answer': ('.exa_tools', None),
'exa_search_async': ('.exa_tools', None),
'exa_search_contents_async': ('.exa_tools', None),
'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
@@ -187,8 +229,8 @@ def __getattr__(name: str) -> Any:
module_path, class_name = TOOL_MAPPINGS[name]
# Return class itself (not instance) for ClaudeMemoryTool
if name == 'ClaudeMemoryTool':
# 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)
@@ -205,11 +247,18 @@ def __getattr__(name: str) -> Any:
'insert_document', 'insert_documents', 'find_documents', 'update_document', 'delete_document',
'create_vector_index', 'vector_search', 'store_with_embedding', 'text_search', 'get_stats', 'connect_mongodb',
'execute_command', 'list_processes', 'kill_process', 'get_system_info',
'evaluate', 'solve_equation', 'convert_units', 'calculate_statistics', 'calculate_financial'
'evaluate', 'solve_equation', 'convert_units', 'calculate_statistics', 'calculate_financial',
'tavily', 'tavily_search', 'tavily_extract', 'tavily_crawl', 'tavily_map',
'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',
'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']:
'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())
@@ -0,0 +1,741 @@
"""Exa search and research tools.
Exa provides AI-powered web search, content retrieval, and research capabilities.
This module requires the EXA_API_KEY environment variable to be set.
Usage:
from praisonaiagents.tools import exa_search, exa_search_contents
# Search the web
results = exa_search("AI startups")
# Search with contents
results = exa_search_contents("AI in healthcare", text=True)
# Or use the class directly
from praisonaiagents.tools import ExaTools
exa = ExaTools()
results = exa.search("AI news")
"""
from typing import List, Dict, Any, Optional, Union
import logging
import os
from importlib import util
def _check_exa_available() -> tuple[bool, Optional[str]]:
"""Check if Exa is available and API key is set.
Returns:
Tuple of (is_available, error_message)
"""
# Check if exa_py package is installed
if util.find_spec("exa_py") is None:
return False, "exa_py package is not installed. Install it with: pip install exa_py"
# Check if API key is set
if not os.environ.get("EXA_API_KEY"):
return False, "EXA_API_KEY environment variable is not set. Please set it to use Exa tools."
return True, None
class ExaTools:
"""Exa search and research tools.
Provides methods for:
- search: Basic web search
- search_and_contents: Search with full text/highlights
- find_similar: Find similar pages to a URL
- find_similar_and_contents: Find similar with content
- answer: Get AI-generated answers with citations
Example:
from praisonaiagents.tools import ExaTools
exa = ExaTools()
results = exa.search("AI startups", num_results=5)
"""
def __init__(self, api_key: Optional[str] = None):
"""Initialize ExaTools.
Args:
api_key: Exa API key. If not provided, uses EXA_API_KEY env var.
"""
self.api_key = api_key or os.environ.get("EXA_API_KEY")
self._client = None
self.logger = logging.getLogger(__name__)
def _get_client(self):
"""Get or create Exa client."""
if self._client is None:
is_available, error = _check_exa_available()
if not is_available:
raise ImportError(error)
from exa_py import Exa
self._client = Exa(self.api_key)
return self._client
def search(
self,
query: str,
num_results: int = 10,
type: Optional[str] = None,
category: Optional[str] = None,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
start_crawl_date: Optional[str] = None,
end_crawl_date: Optional[str] = None,
start_published_date: Optional[str] = None,
end_published_date: Optional[str] = None,
include_text: Optional[List[str]] = None,
exclude_text: Optional[List[str]] = None,
additional_queries: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Perform an Exa search.
Args:
query: The search query
num_results: Number of results to return (default 10, max 100)
type: Search type - "auto", "neural", "fast", or "deep"
category: Data category - company, research paper, news, linkedin profile,
github, tweet, movie, song, personal site, pdf, financial report
include_domains: List of domains to include
exclude_domains: List of domains to exclude
start_crawl_date: Only include links crawled after this date
end_crawl_date: Only include links crawled before this date
start_published_date: Only include links published after this date
end_published_date: Only include links published before this date
include_text: Strings that must be present in results
exclude_text: Strings that must not be present in results
additional_queries: Additional query variations for deep search
Returns:
Dict with 'results' list and optional 'autopromptString'
"""
try:
client = self._get_client()
kwargs = {
"query": query,
"num_results": num_results
}
if type:
kwargs["type"] = type
if category:
kwargs["category"] = category
if include_domains:
kwargs["include_domains"] = include_domains
if exclude_domains:
kwargs["exclude_domains"] = exclude_domains
if start_crawl_date:
kwargs["start_crawl_date"] = start_crawl_date
if end_crawl_date:
kwargs["end_crawl_date"] = end_crawl_date
if start_published_date:
kwargs["start_published_date"] = start_published_date
if end_published_date:
kwargs["end_published_date"] = end_published_date
if include_text:
kwargs["include_text"] = include_text
if exclude_text:
kwargs["exclude_text"] = exclude_text
if additional_queries:
kwargs["additional_queries"] = additional_queries
response = client.search(**kwargs)
return {
"results": [
{
"url": r.url,
"id": r.id,
"title": getattr(r, "title", None),
"published_date": getattr(r, "published_date", None),
"author": getattr(r, "author", None)
}
for r in response.results
],
"autopromptString": getattr(response, "autoprompt_string", None),
"requestId": getattr(response, "request_id", None)
}
except Exception as e:
self.logger.error(f"Exa search error: {e}")
return {"error": f"Exa search error: {str(e)}"}
def search_and_contents(
self,
query: str,
text: Union[bool, Dict] = True,
highlights: Union[bool, Dict] = False,
summary: Optional[Dict] = None,
num_results: int = 10,
type: Optional[str] = None,
category: Optional[str] = None,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
start_crawl_date: Optional[str] = None,
end_crawl_date: Optional[str] = None,
start_published_date: Optional[str] = None,
end_published_date: Optional[str] = None,
include_text: Optional[List[str]] = None,
exclude_text: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Search and retrieve content from results.
Args:
query: The search query
text: Include full text content (True or TextContentsOptions dict)
highlights: Include highlights (True or HighlightsContentsOptions dict)
summary: Summary options with optional schema for structured output
num_results: Number of results to return
type: Search type - "auto", "neural", "fast", or "deep"
category: Data category filter
include_domains: List of domains to include
exclude_domains: List of domains to exclude
start_crawl_date: Only include links crawled after this date
end_crawl_date: Only include links crawled before this date
start_published_date: Only include links published after this date
end_published_date: Only include links published before this date
include_text: Strings that must be present in results
exclude_text: Strings that must not be present in results
Returns:
Dict with 'results' list containing text/highlights
"""
try:
client = self._get_client()
kwargs = {
"query": query,
"num_results": num_results
}
if text:
kwargs["text"] = text
if highlights:
kwargs["highlights"] = highlights
if summary:
kwargs["summary"] = summary
if type:
kwargs["type"] = type
if category:
kwargs["category"] = category
if include_domains:
kwargs["include_domains"] = include_domains
if exclude_domains:
kwargs["exclude_domains"] = exclude_domains
if start_crawl_date:
kwargs["start_crawl_date"] = start_crawl_date
if end_crawl_date:
kwargs["end_crawl_date"] = end_crawl_date
if start_published_date:
kwargs["start_published_date"] = start_published_date
if end_published_date:
kwargs["end_published_date"] = end_published_date
if include_text:
kwargs["include_text"] = include_text
if exclude_text:
kwargs["exclude_text"] = exclude_text
response = client.search_and_contents(**kwargs)
results = []
for r in response.results:
result = {
"url": r.url,
"id": r.id,
"title": getattr(r, "title", None),
"published_date": getattr(r, "published_date", None),
"author": getattr(r, "author", None)
}
if hasattr(r, "text") and r.text:
result["text"] = r.text
if hasattr(r, "highlights") and r.highlights:
result["highlights"] = r.highlights
if hasattr(r, "highlight_scores") and r.highlight_scores:
result["highlight_scores"] = r.highlight_scores
if hasattr(r, "summary") and r.summary:
result["summary"] = r.summary
results.append(result)
return {
"results": results,
"requestId": getattr(response, "request_id", None)
}
except Exception as e:
self.logger.error(f"Exa search_and_contents error: {e}")
return {"error": f"Exa search_and_contents error: {str(e)}"}
def find_similar(
self,
url: str,
num_results: int = 10,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
start_crawl_date: Optional[str] = None,
end_crawl_date: Optional[str] = None,
start_published_date: Optional[str] = None,
end_published_date: Optional[str] = None,
exclude_source_domain: bool = False,
category: Optional[str] = None
) -> Dict[str, Any]:
"""Find similar pages to a given URL.
Args:
url: The URL to find similar pages for
num_results: Number of results to return
include_domains: List of domains to include
exclude_domains: List of domains to exclude
start_crawl_date: Only include links crawled after this date
end_crawl_date: Only include links crawled before this date
start_published_date: Only include links published after this date
end_published_date: Only include links published before this date
exclude_source_domain: Exclude results from the same domain
category: Data category filter
Returns:
Dict with 'results' list of similar pages
"""
try:
client = self._get_client()
kwargs = {
"url": url,
"num_results": num_results
}
if include_domains:
kwargs["include_domains"] = include_domains
if exclude_domains:
kwargs["exclude_domains"] = exclude_domains
if start_crawl_date:
kwargs["start_crawl_date"] = start_crawl_date
if end_crawl_date:
kwargs["end_crawl_date"] = end_crawl_date
if start_published_date:
kwargs["start_published_date"] = start_published_date
if end_published_date:
kwargs["end_published_date"] = end_published_date
if exclude_source_domain:
kwargs["exclude_source_domain"] = exclude_source_domain
if category:
kwargs["category"] = category
response = client.find_similar(**kwargs)
return {
"results": [
{
"url": r.url,
"id": r.id,
"title": getattr(r, "title", None),
"published_date": getattr(r, "published_date", None),
"author": getattr(r, "author", None)
}
for r in response.results
],
"requestId": getattr(response, "request_id", None)
}
except Exception as e:
self.logger.error(f"Exa find_similar error: {e}")
return {"error": f"Exa find_similar error: {str(e)}"}
def find_similar_and_contents(
self,
url: str,
text: Union[bool, Dict] = True,
highlights: Union[bool, Dict] = False,
num_results: int = 10,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
start_crawl_date: Optional[str] = None,
end_crawl_date: Optional[str] = None,
start_published_date: Optional[str] = None,
end_published_date: Optional[str] = None,
exclude_source_domain: bool = False,
category: Optional[str] = None
) -> Dict[str, Any]:
"""Find similar pages with content.
Args:
url: The URL to find similar pages for
text: Include full text content
highlights: Include highlights
num_results: Number of results to return
include_domains: List of domains to include
exclude_domains: List of domains to exclude
start_crawl_date: Only include links crawled after this date
end_crawl_date: Only include links crawled before this date
start_published_date: Only include links published after this date
end_published_date: Only include links published before this date
exclude_source_domain: Exclude results from the same domain
category: Data category filter
Returns:
Dict with 'results' list containing text/highlights
"""
try:
client = self._get_client()
kwargs = {
"url": url,
"num_results": num_results
}
if text:
kwargs["text"] = text
if highlights:
kwargs["highlights"] = highlights
if include_domains:
kwargs["include_domains"] = include_domains
if exclude_domains:
kwargs["exclude_domains"] = exclude_domains
if start_crawl_date:
kwargs["start_crawl_date"] = start_crawl_date
if end_crawl_date:
kwargs["end_crawl_date"] = end_crawl_date
if start_published_date:
kwargs["start_published_date"] = start_published_date
if end_published_date:
kwargs["end_published_date"] = end_published_date
if exclude_source_domain:
kwargs["exclude_source_domain"] = exclude_source_domain
if category:
kwargs["category"] = category
response = client.find_similar_and_contents(**kwargs)
results = []
for r in response.results:
result = {
"url": r.url,
"id": r.id,
"title": getattr(r, "title", None),
"published_date": getattr(r, "published_date", None),
"author": getattr(r, "author", None)
}
if hasattr(r, "text") and r.text:
result["text"] = r.text
if hasattr(r, "highlights") and r.highlights:
result["highlights"] = r.highlights
if hasattr(r, "highlight_scores") and r.highlight_scores:
result["highlight_scores"] = r.highlight_scores
results.append(result)
return {
"results": results,
"requestId": getattr(response, "request_id", None)
}
except Exception as e:
self.logger.error(f"Exa find_similar_and_contents error: {e}")
return {"error": f"Exa find_similar_and_contents error: {str(e)}"}
def answer(
self,
query: str,
text: bool = False
) -> Dict[str, Any]:
"""Generate an answer using Exa's search and LLM capabilities.
Args:
query: The question to answer
text: If True, include full text of citations
Returns:
Dict with 'answer' and 'citations'
"""
try:
client = self._get_client()
response = client.answer(query=query, text=text)
citations = []
for c in response.citations:
citation = {
"id": c.id,
"url": c.url,
"title": getattr(c, "title", None),
"published_date": getattr(c, "published_date", None),
"author": getattr(c, "author", None)
}
if text and hasattr(c, "text") and c.text:
citation["text"] = c.text
citations.append(citation)
return {
"answer": response.answer,
"citations": citations
}
except Exception as e:
self.logger.error(f"Exa answer error: {e}")
return {"error": f"Exa answer error: {str(e)}"}
# Standalone functions for easy access
def exa_search(
query: str,
num_results: int = 10,
type: Optional[str] = None,
category: Optional[str] = None,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
start_published_date: Optional[str] = None,
end_published_date: Optional[str] = None
) -> Dict[str, Any]:
"""Search the web using Exa.
Args:
query: The search query
num_results: Number of results (default 10)
type: Search type - "auto", "neural", "fast", or "deep"
category: Data category filter
include_domains: Domains to include
exclude_domains: Domains to exclude
start_published_date: Only include links published after this date
end_published_date: Only include links published before this date
Returns:
Dict with search results or error
Example:
results = exa_search("AI startups", num_results=5)
"""
is_available, error = _check_exa_available()
if not is_available:
logging.error(error)
return {"error": error}
tools = ExaTools()
return tools.search(
query=query,
num_results=num_results,
type=type,
category=category,
include_domains=include_domains,
exclude_domains=exclude_domains,
start_published_date=start_published_date,
end_published_date=end_published_date
)
def exa_search_contents(
query: str,
text: bool = True,
highlights: bool = False,
num_results: int = 10,
type: Optional[str] = None,
category: Optional[str] = None,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None
) -> Dict[str, Any]:
"""Search and get content from results.
Args:
query: The search query
text: Include full text content
highlights: Include highlights
num_results: Number of results
type: Search type
category: Data category filter
include_domains: Domains to include
exclude_domains: Domains to exclude
Returns:
Dict with results containing text/highlights
Example:
results = exa_search_contents("AI in healthcare", text=True, highlights=True)
"""
is_available, error = _check_exa_available()
if not is_available:
logging.error(error)
return {"error": error}
tools = ExaTools()
return tools.search_and_contents(
query=query,
text=text,
highlights=highlights,
num_results=num_results,
type=type,
category=category,
include_domains=include_domains,
exclude_domains=exclude_domains
)
def exa_find_similar(
url: str,
num_results: int = 10,
exclude_source_domain: bool = True,
category: Optional[str] = None
) -> Dict[str, Any]:
"""Find pages similar to a given URL.
Args:
url: The URL to find similar pages for
num_results: Number of results
exclude_source_domain: Exclude same domain results
category: Data category filter
Returns:
Dict with similar results
Example:
results = exa_find_similar("https://openai.com", num_results=5)
"""
is_available, error = _check_exa_available()
if not is_available:
logging.error(error)
return {"error": error}
tools = ExaTools()
return tools.find_similar(
url=url,
num_results=num_results,
exclude_source_domain=exclude_source_domain,
category=category
)
def exa_answer(
query: str,
text: bool = False
) -> Dict[str, Any]:
"""Get an AI-generated answer with citations.
Args:
query: The question to answer
text: Include full text of citations
Returns:
Dict with 'answer' and 'citations'
Example:
result = exa_answer("What is the capital of France?")
print(result["answer"])
"""
is_available, error = _check_exa_available()
if not is_available:
logging.error(error)
return {"error": error}
tools = ExaTools()
return tools.answer(query=query, text=text)
# Async versions
async def exa_search_async(
query: str,
num_results: int = 10,
type: Optional[str] = None,
category: Optional[str] = None
) -> Dict[str, Any]:
"""Async version of exa_search.
Args:
query: The search query
num_results: Number of results
type: Search type
category: Data category filter
Returns:
Dict with search results
"""
import asyncio
return await asyncio.get_event_loop().run_in_executor(
None,
lambda: exa_search(query, num_results, type, category)
)
async def exa_search_contents_async(
query: str,
text: bool = True,
highlights: bool = False,
num_results: int = 10
) -> Dict[str, Any]:
"""Async version of exa_search_contents.
Args:
query: The search query
text: Include full text
highlights: Include highlights
num_results: Number of results
Returns:
Dict with results containing content
"""
import asyncio
return await asyncio.get_event_loop().run_in_executor(
None,
lambda: exa_search_contents(query, text, highlights, num_results)
)
async def exa_answer_async(
query: str,
text: bool = False
) -> Dict[str, Any]:
"""Async version of exa_answer.
Args:
query: The question to answer
text: Include citation text
Returns:
Dict with answer and citations
"""
import asyncio
return await asyncio.get_event_loop().run_in_executor(
None,
lambda: exa_answer(query, text)
)
# Alias for simple usage
exa = exa_search
if __name__ == "__main__":
# Example usage
print("=" * 60)
print("Exa Tools - Example Usage")
print("=" * 60)
is_available, error = _check_exa_available()
if not is_available:
print(f"\nExa is not available: {error}")
print("\nTo use Exa tools:")
print("1. Install the package: pip install exa_py")
print("2. Set environment variable: export EXA_API_KEY=your_api_key")
else:
print("Exa SDK is available!")
# Example search
print("\n1. Basic Search")
print("-" * 30)
results = exa_search("Latest AI developments 2024", num_results=3)
if "error" not in results:
print(f"Found {len(results.get('results', []))} results")
for r in results.get("results", [])[:3]:
print(f" - {r.get('title', 'No title')[:50]}...")
print(f" URL: {r.get('url', 'No URL')}")
else:
print(f"Error: {results['error']}")
print("\n" + "=" * 60)
print("Demonstration Complete")
print("=" * 60)
@@ -0,0 +1,591 @@
"""Tavily search and extraction tools.
Tavily provides AI-powered web search, content extraction, and crawling capabilities.
This module requires the TAVILY_API_KEY environment variable to be set.
Usage:
from praisonaiagents.tools import tavily_search, tavily_extract
# Search the web
results = tavily_search("AI news 2024")
# Extract content from URLs
content = tavily_extract("https://example.com")
# Or use the class directly
from praisonaiagents.tools import TavilyTools
tavily = TavilyTools()
results = tavily.search("AI news")
"""
from typing import List, Dict, Any, Optional, Union
import logging
import os
from importlib import util
def _check_tavily_available() -> tuple[bool, Optional[str]]:
"""Check if Tavily is available and API key is set.
Returns:
Tuple of (is_available, error_message)
"""
# Check if tavily package is installed
if util.find_spec("tavily") is None:
return False, "Tavily package is not installed. Please install it using: pip install tavily-python"
# Check if API key is set
api_key = os.environ.get("TAVILY_API_KEY")
if not api_key:
return False, "TAVILY_API_KEY environment variable is not set. Please set it to use Tavily tools."
return True, None
class TavilyTools:
"""Comprehensive tools for web search and content extraction using Tavily API.
Tavily provides AI-powered search capabilities optimized for LLM applications.
Features:
- Web search with advanced filtering and ranking
- Content extraction from URLs
- Website crawling with intelligent navigation
- Site mapping
Requires:
- tavily-python package: pip install tavily-python
- TAVILY_API_KEY environment variable
"""
def __init__(self, api_key: Optional[str] = None):
"""Initialize TavilyTools.
Args:
api_key: Optional API key. If not provided, uses TAVILY_API_KEY env var.
"""
self._api_key = api_key
self._client = None
self._async_client = None
def _get_api_key(self) -> Optional[str]:
"""Get API key from instance or environment."""
return self._api_key or os.environ.get("TAVILY_API_KEY")
def _get_client(self):
"""Get or create synchronous Tavily client."""
if self._client is None:
is_available, error = _check_tavily_available()
if not is_available:
return None, error
from tavily import TavilyClient
api_key = self._get_api_key()
if not api_key:
return None, "TAVILY_API_KEY not set"
self._client = TavilyClient(api_key=api_key)
return self._client, None
def _get_async_client(self):
"""Get or create asynchronous Tavily client."""
if self._async_client is None:
is_available, error = _check_tavily_available()
if not is_available:
return None, error
from tavily import AsyncTavilyClient
api_key = self._get_api_key()
if not api_key:
return None, "TAVILY_API_KEY not set"
self._async_client = AsyncTavilyClient(api_key=api_key)
return self._async_client, None
def search(
self,
query: str,
search_depth: str = "basic",
topic: str = "general",
max_results: int = 5,
include_answer: bool = False,
include_raw_content: bool = False,
include_images: bool = False,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
time_range: Optional[str] = None,
) -> Dict[str, Any]:
"""Search the web using Tavily's AI-powered search.
Args:
query: The search query
search_depth: "basic" or "advanced" (advanced uses more credits but better results)
topic: "general", "news", or "finance"
max_results: Maximum number of results (1-20)
include_answer: Include LLM-generated answer based on results
include_raw_content: Include cleaned HTML content of each result
include_images: Include query-related images
include_domains: List of domains to include (max 300)
exclude_domains: List of domains to exclude (max 150)
time_range: Filter by time - "day", "week", "month", "year" or "d", "w", "m", "y"
Returns:
Dict containing search results with keys:
- results: List of search results with title, url, content, score
- query: The search query
- response_time: Time taken for search
- answer: (optional) LLM-generated answer if include_answer=True
- images: (optional) List of image URLs if include_images=True
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
kwargs = {
"query": query,
"search_depth": search_depth,
"topic": topic,
"max_results": max_results,
"include_answer": include_answer,
"include_raw_content": include_raw_content,
"include_images": include_images,
}
if include_domains:
kwargs["include_domains"] = include_domains
if exclude_domains:
kwargs["exclude_domains"] = exclude_domains
if time_range:
kwargs["time_range"] = time_range
response = client.search(**kwargs)
return response
except Exception as e:
error_msg = f"Tavily search error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
def extract(
self,
urls: Union[str, List[str]],
include_images: bool = False,
extract_depth: str = "basic",
) -> Dict[str, Any]:
"""Extract content from one or more URLs.
Args:
urls: Single URL or list of URLs (max 20)
include_images: Include extracted images
extract_depth: "basic" or "advanced" (advanced retrieves more data)
Returns:
Dict containing:
- results: List of successful extractions with url, raw_content
- failed_results: List of URLs that failed with error messages
- response_time: Time taken for extraction
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
response = client.extract(
urls=urls,
include_images=include_images,
extract_depth=extract_depth,
)
return response
except Exception as e:
error_msg = f"Tavily extract error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
def crawl(
self,
url: str,
max_depth: int = 1,
max_breadth: int = 20,
limit: int = 50,
instructions: Optional[str] = None,
include_images: bool = False,
extract_depth: str = "basic",
) -> Dict[str, Any]:
"""Crawl a website starting from a base URL.
Args:
url: The root URL to begin crawling
max_depth: Maximum depth from base URL (default: 1)
max_breadth: Max links to follow per page (default: 20)
limit: Total links to process before stopping (default: 50)
instructions: Natural language instructions for the crawler
include_images: Extract image URLs from pages
extract_depth: "basic" or "advanced"
Returns:
Dict containing:
- base_url: The starting URL
- results: List of crawled pages with url, raw_content, images
- response_time: Time taken for crawl
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
kwargs = {
"url": url,
"max_depth": max_depth,
"max_breadth": max_breadth,
"limit": limit,
"include_images": include_images,
"extract_depth": extract_depth,
}
if instructions:
kwargs["instructions"] = instructions
response = client.crawl(**kwargs)
return response
except Exception as e:
error_msg = f"Tavily crawl error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
def map(
self,
url: str,
max_depth: int = 1,
max_breadth: int = 20,
limit: int = 50,
instructions: Optional[str] = None,
) -> Dict[str, Any]:
"""Get a sitemap starting from a base URL.
Args:
url: The root URL to begin mapping
max_depth: Maximum depth from base URL (default: 1)
max_breadth: Max links to follow per page (default: 20)
limit: Total links to process before stopping (default: 50)
instructions: Natural language instructions for the mapper
Returns:
Dict containing:
- base_url: The starting URL
- results: List of discovered URLs
- response_time: Time taken for mapping
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
kwargs = {
"url": url,
"max_depth": max_depth,
"max_breadth": max_breadth,
"limit": limit,
}
if instructions:
kwargs["instructions"] = instructions
response = client.map(**kwargs)
return response
except Exception as e:
error_msg = f"Tavily map error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
# Async methods
async def asearch(
self,
query: str,
search_depth: str = "basic",
topic: str = "general",
max_results: int = 5,
include_answer: bool = False,
include_raw_content: bool = False,
include_images: bool = False,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
time_range: Optional[str] = None,
) -> Dict[str, Any]:
"""Async version of search. See search() for documentation."""
client, error = self._get_async_client()
if error:
logging.error(error)
return {"error": error}
try:
kwargs = {
"query": query,
"search_depth": search_depth,
"topic": topic,
"max_results": max_results,
"include_answer": include_answer,
"include_raw_content": include_raw_content,
"include_images": include_images,
}
if include_domains:
kwargs["include_domains"] = include_domains
if exclude_domains:
kwargs["exclude_domains"] = exclude_domains
if time_range:
kwargs["time_range"] = time_range
response = await client.search(**kwargs)
return response
except Exception as e:
error_msg = f"Tavily async search error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
async def aextract(
self,
urls: Union[str, List[str]],
include_images: bool = False,
extract_depth: str = "basic",
) -> Dict[str, Any]:
"""Async version of extract. See extract() for documentation."""
client, error = self._get_async_client()
if error:
logging.error(error)
return {"error": error}
try:
response = await client.extract(
urls=urls,
include_images=include_images,
extract_depth=extract_depth,
)
return response
except Exception as e:
error_msg = f"Tavily async extract error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
# Standalone functions for direct import
def tavily_search(
query: str,
search_depth: str = "basic",
topic: str = "general",
max_results: int = 5,
include_answer: bool = False,
include_raw_content: bool = False,
include_images: bool = False,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
time_range: Optional[str] = None,
) -> Dict[str, Any]:
"""Search the web using Tavily's AI-powered search.
This is a convenience function that creates a TavilyTools instance.
For repeated calls, consider using TavilyTools class directly.
Args:
query: The search query
search_depth: "basic" or "advanced"
topic: "general", "news", or "finance"
max_results: Maximum number of results (1-20)
include_answer: Include LLM-generated answer
include_raw_content: Include cleaned HTML content
include_images: Include query-related images
include_domains: List of domains to include
exclude_domains: List of domains to exclude
time_range: Filter by time - "day", "week", "month", "year"
Returns:
Dict containing search results
"""
tools = TavilyTools()
return tools.search(
query=query,
search_depth=search_depth,
topic=topic,
max_results=max_results,
include_answer=include_answer,
include_raw_content=include_raw_content,
include_images=include_images,
include_domains=include_domains,
exclude_domains=exclude_domains,
time_range=time_range,
)
def tavily_extract(
urls: Union[str, List[str]],
include_images: bool = False,
extract_depth: str = "basic",
) -> Dict[str, Any]:
"""Extract content from one or more URLs using Tavily.
Args:
urls: Single URL or list of URLs (max 20)
include_images: Include extracted images
extract_depth: "basic" or "advanced"
Returns:
Dict containing extraction results
"""
tools = TavilyTools()
return tools.extract(
urls=urls,
include_images=include_images,
extract_depth=extract_depth,
)
def tavily_crawl(
url: str,
max_depth: int = 1,
max_breadth: int = 20,
limit: int = 50,
instructions: Optional[str] = None,
include_images: bool = False,
extract_depth: str = "basic",
) -> Dict[str, Any]:
"""Crawl a website starting from a base URL using Tavily.
Args:
url: The root URL to begin crawling
max_depth: Maximum depth from base URL
max_breadth: Max links to follow per page
limit: Total links to process before stopping
instructions: Natural language instructions for the crawler
include_images: Extract image URLs from pages
extract_depth: "basic" or "advanced"
Returns:
Dict containing crawl results
"""
tools = TavilyTools()
return tools.crawl(
url=url,
max_depth=max_depth,
max_breadth=max_breadth,
limit=limit,
instructions=instructions,
include_images=include_images,
extract_depth=extract_depth,
)
def tavily_map(
url: str,
max_depth: int = 1,
max_breadth: int = 20,
limit: int = 50,
instructions: Optional[str] = None,
) -> Dict[str, Any]:
"""Get a sitemap starting from a base URL using Tavily.
Args:
url: The root URL to begin mapping
max_depth: Maximum depth from base URL
max_breadth: Max links to follow per page
limit: Total links to process before stopping
instructions: Natural language instructions for the mapper
Returns:
Dict containing map results with discovered URLs
"""
tools = TavilyTools()
return tools.map(
url=url,
max_depth=max_depth,
max_breadth=max_breadth,
limit=limit,
instructions=instructions,
)
# Async standalone functions
async def tavily_search_async(
query: str,
search_depth: str = "basic",
topic: str = "general",
max_results: int = 5,
include_answer: bool = False,
include_raw_content: bool = False,
include_images: bool = False,
include_domains: Optional[List[str]] = None,
exclude_domains: Optional[List[str]] = None,
time_range: Optional[str] = None,
) -> Dict[str, Any]:
"""Async version of tavily_search. See tavily_search() for documentation."""
tools = TavilyTools()
return await tools.asearch(
query=query,
search_depth=search_depth,
topic=topic,
max_results=max_results,
include_answer=include_answer,
include_raw_content=include_raw_content,
include_images=include_images,
include_domains=include_domains,
exclude_domains=exclude_domains,
time_range=time_range,
)
async def tavily_extract_async(
urls: Union[str, List[str]],
include_images: bool = False,
extract_depth: str = "basic",
) -> Dict[str, Any]:
"""Async version of tavily_extract. See tavily_extract() for documentation."""
tools = TavilyTools()
return await tools.aextract(
urls=urls,
include_images=include_images,
extract_depth=extract_depth,
)
if __name__ == "__main__":
# Example usage
print("\n==================================================")
print("Tavily Tools Demonstration")
print("==================================================\n")
# Check if API key is available
is_available, error = _check_tavily_available()
if not is_available:
print(f"Error: {error}")
print("\nTo use Tavily tools:")
print("1. Install: pip install tavily-python")
print("2. Set environment variable: export TAVILY_API_KEY=your_api_key")
else:
print("Tavily is available!")
# Example search
print("\n1. Basic Search")
print("-" * 30)
results = tavily_search("Latest AI developments 2024", max_results=3)
if "error" not in results:
print(f"Found {len(results.get('results', []))} results")
for r in results.get("results", [])[:3]:
print(f" - {r.get('title', 'No title')}")
print(f" URL: {r.get('url', 'No URL')}")
else:
print(f"Error: {results['error']}")
print("\n==================================================")
print("Demonstration Complete")
print("==================================================")
# Alias for simple usage: from praisonaiagents.tools import tavily
tavily = tavily_search
@@ -0,0 +1,533 @@
"""You.com search and content tools.
You.com provides AI-powered web search, news search, content extraction, and image search.
This module requires the YDC_API_KEY environment variable to be set.
Usage:
from praisonaiagents.tools import ydc_search, ydc_news, ydc_contents
# Search the web (unified web + news)
results = ydc_search("AI news 2024")
# Get live news
news = ydc_news("technology trends")
# Extract content from URLs
content = ydc_contents(["https://example.com"])
# Or use the class directly
from praisonaiagents.tools import YouTools
you = YouTools()
results = you.search("AI news")
"""
from typing import List, Dict, Any, Optional, Union
import logging
import os
from importlib import util
def _check_youdotcom_available() -> tuple[bool, Optional[str]]:
"""Check if You.com SDK is available and API key is set.
Returns:
Tuple of (is_available, error_message)
"""
# Check if youdotcom package is installed
if util.find_spec("youdotcom") is None:
return False, "youdotcom package is not installed. Please install it using: pip install youdotcom"
# Check if API key is set
api_key = os.environ.get("YDC_API_KEY")
if not api_key:
return False, "YDC_API_KEY environment variable is not set. Please set it to use You.com tools."
return True, None
class YouTools:
"""Comprehensive tools for web search and content using You.com API.
You.com provides AI-powered search capabilities optimized for LLM applications.
Features:
- Unified web and news search with LLM-ready snippets
- Live news search from authoritative sources
- Content extraction from URLs (HTML/Markdown)
- Image search
- Advanced search operators support
- Geographic and language targeting
- Freshness controls
Requires:
- youdotcom package: pip install youdotcom
- YDC_API_KEY environment variable
"""
def __init__(self, api_key: Optional[str] = None):
"""Initialize YouTools.
Args:
api_key: Optional API key. If not provided, uses YDC_API_KEY env var.
"""
self._api_key = api_key
self._client = None
def _get_api_key(self) -> Optional[str]:
"""Get API key from instance or environment."""
return self._api_key or os.environ.get("YDC_API_KEY")
def _get_client(self):
"""Get or create You.com client."""
if self._client is None:
is_available, error = _check_youdotcom_available()
if not is_available:
return None, error
from youdotcom import You
api_key = self._get_api_key()
if not api_key:
return None, "YDC_API_KEY not set"
self._client = You(api_key_auth=api_key)
return self._client, None
def search(
self,
query: str,
count: int = 10,
freshness: Optional[str] = None,
country: Optional[str] = None,
language: Optional[str] = None,
offset: int = 0,
safesearch: str = "moderate",
livecrawl: Optional[str] = None,
livecrawl_formats: Optional[str] = None,
) -> Dict[str, Any]:
"""Search the web using You.com's unified search API.
Returns both web and news results in a single call with LLM-ready snippets.
Args:
query: Search query (supports operators like site:, filetype:, +term, -term)
count: Max results per section (default 10, max 100)
freshness: Filter by recency - "day", "week", "month", "year" or "YYYY-MM-DDtoYYYY-MM-DD"
country: ISO country code (e.g., "US", "GB", "FR")
language: BCP 47 language code (e.g., "EN", "JP", "DE")
offset: Pagination offset (0-9), results = offset * count
safesearch: Content filter - "off", "moderate", "strict"
livecrawl: Fetch full content - "web", "news", or "all"
livecrawl_formats: Content format - "html" or "markdown"
Returns:
Dict containing:
- results: {web: [...], news: [...]}
- metadata: {query, search_uuid, latency}
Search Operators:
- site:domain.com - Search within specific domain
- filetype:pdf - Filter by file type
- +term - Must include term
- -term - Exclude term
- AND, OR, NOT - Boolean operators
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
kwargs = {"query": query, "count": count, "offset": offset}
if freshness:
kwargs["freshness"] = freshness
if country:
kwargs["country"] = country
if language:
kwargs["language"] = language
if safesearch:
kwargs["safesearch"] = safesearch
if livecrawl:
kwargs["livecrawl"] = livecrawl
if livecrawl_formats:
kwargs["livecrawl_formats"] = livecrawl_formats
response = client.search.unified(**kwargs)
# Convert response to dict if it's a model object
if hasattr(response, 'model_dump'):
return response.model_dump()
elif hasattr(response, 'to_dict'):
return response.to_dict()
elif hasattr(response, '__dict__'):
return self._convert_response(response)
return response
except Exception as e:
error_msg = f"You.com search error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
def _convert_response(self, response) -> Dict[str, Any]:
"""Convert SDK response object to dictionary."""
result = {}
if hasattr(response, 'results'):
results = response.results
result['results'] = {}
if hasattr(results, 'web') and results.web:
result['results']['web'] = [
{
'url': getattr(r, 'url', ''),
'title': getattr(r, 'title', ''),
'description': getattr(r, 'description', ''),
'snippets': getattr(r, 'snippets', []),
'thumbnail_url': getattr(r, 'thumbnail_url', None),
'page_age': getattr(r, 'page_age', None),
'favicon_url': getattr(r, 'favicon_url', None),
'authors': getattr(r, 'authors', []),
}
for r in results.web
]
if hasattr(results, 'news') and results.news:
result['results']['news'] = [
{
'url': getattr(r, 'url', ''),
'title': getattr(r, 'title', ''),
'description': getattr(r, 'description', ''),
'thumbnail_url': getattr(r, 'thumbnail_url', None),
'page_age': getattr(r, 'page_age', None),
}
for r in results.news
]
if hasattr(response, 'metadata'):
meta = response.metadata
result['metadata'] = {
'query': getattr(meta, 'query', ''),
'search_uuid': getattr(meta, 'search_uuid', ''),
'latency': getattr(meta, 'latency', 0),
}
return result
def get_contents(
self,
urls: Union[str, List[str]],
format: str = "markdown",
) -> Dict[str, Any]:
"""Fetch content from URLs using You.com's content extraction API.
Args:
urls: Single URL or list of URLs to fetch content from
format: Content format - "html" or "markdown" (default: markdown)
Returns:
Dict containing array of results with:
- url: The fetched URL
- title: Page title
- html: HTML content (if format="html")
- markdown: Markdown content (if format="markdown")
- metadata: {site_name, favicon_url}
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
if isinstance(urls, str):
urls = [urls]
# Use the contents endpoint
response = client.search.contents(urls=urls, format=format)
# Convert response
if hasattr(response, 'model_dump'):
return {"results": response.model_dump()}
elif isinstance(response, list):
return {"results": [
{
'url': getattr(r, 'url', ''),
'title': getattr(r, 'title', ''),
'html': getattr(r, 'html', None),
'markdown': getattr(r, 'markdown', None),
'metadata': {
'site_name': getattr(getattr(r, 'metadata', None), 'site_name', None) if hasattr(r, 'metadata') else None,
'favicon_url': getattr(getattr(r, 'metadata', None), 'favicon_url', None) if hasattr(r, 'metadata') else None,
}
}
for r in response
]}
return {"results": response}
except Exception as e:
error_msg = f"You.com contents error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
def live_news(
self,
query: str,
count: Optional[int] = None,
) -> Dict[str, Any]:
"""Search for live news using You.com's news API.
Note: This API may require early access. Contact [email protected] for access.
Args:
query: News search query
count: Maximum number of news results to return
Returns:
Dict containing:
- news: {query, results: [...], metadata}
- Each result has: title, description, url, page_age, source_name, thumbnail
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
kwargs = {"q": query}
if count:
kwargs["count"] = count
# Try to access live news endpoint
if hasattr(client, 'livenews'):
response = client.livenews.get(**kwargs)
else:
# Fallback: use unified search with news focus
return self.search(query=query, count=count or 10)
if hasattr(response, 'model_dump'):
return response.model_dump()
elif hasattr(response, 'news'):
news = response.news
return {
'news': {
'query': getattr(getattr(news, 'query', None), 'original', query) if hasattr(news, 'query') else query,
'results': [
{
'title': getattr(r, 'title', ''),
'description': getattr(r, 'description', ''),
'url': getattr(r, 'url', ''),
'page_age': getattr(r, 'page_age', None),
'age': getattr(r, 'age', None),
'source_name': getattr(r, 'source_name', None),
'thumbnail': getattr(getattr(r, 'thumbnail', None), 'src', None) if hasattr(r, 'thumbnail') else None,
}
for r in getattr(news, 'results', [])
],
'metadata': {
'request_uuid': getattr(getattr(news, 'metadata', None), 'request_uuid', None) if hasattr(news, 'metadata') else None,
}
}
}
return response
except Exception as e:
error_msg = f"You.com live news error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
def images(
self,
query: str,
) -> Dict[str, Any]:
"""Search for images using You.com's image search API.
Note: This API may require early access. Contact [email protected] for access.
Args:
query: Image search query
Returns:
Dict containing:
- images: {results: [...]}
- metadata: {query, search_uuid}
- Each result has: title, page_url, image_url
"""
client, error = self._get_client()
if error:
logging.error(error)
return {"error": error}
try:
if hasattr(client, 'images'):
response = client.images.get(q=query)
else:
return {"error": "Image search not available in this SDK version"}
if hasattr(response, 'model_dump'):
return response.model_dump()
elif hasattr(response, 'images'):
images = response.images
return {
'images': {
'results': [
{
'title': getattr(r, 'title', ''),
'page_url': getattr(r, 'page_url', ''),
'image_url': getattr(r, 'image_url', ''),
}
for r in getattr(images, 'results', [])
]
},
'metadata': {
'query': getattr(getattr(response, 'metadata', None), 'query', query) if hasattr(response, 'metadata') else query,
'search_uuid': getattr(getattr(response, 'metadata', None), 'search_uuid', None) if hasattr(response, 'metadata') else None,
}
}
return response
except Exception as e:
error_msg = f"You.com images error: {str(e)}"
logging.error(error_msg)
return {"error": error_msg}
def __enter__(self):
"""Context manager entry."""
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit."""
if self._client is not None:
# Close client if it has a close method
if hasattr(self._client, 'close'):
self._client.close()
elif hasattr(self._client, '__exit__'):
self._client.__exit__(exc_type, exc_val, exc_tb)
return False
# Standalone functions for direct import
def ydc_search(
query: str,
count: int = 10,
freshness: Optional[str] = None,
country: Optional[str] = None,
language: Optional[str] = None,
offset: int = 0,
safesearch: str = "moderate",
livecrawl: Optional[str] = None,
livecrawl_formats: Optional[str] = None,
) -> Dict[str, Any]:
"""Search the web using You.com's unified search API.
This is a convenience function that creates a YouTools instance.
For repeated calls, consider using YouTools class directly.
Args:
query: Search query (supports operators like site:, filetype:, +term, -term)
count: Max results per section (default 10, max 100)
freshness: Filter by recency - "day", "week", "month", "year"
country: ISO country code (e.g., "US", "GB")
language: BCP 47 language code (e.g., "EN", "JP")
offset: Pagination offset (0-9)
safesearch: Content filter - "off", "moderate", "strict"
livecrawl: Fetch full content - "web", "news", or "all"
livecrawl_formats: Content format - "html" or "markdown"
Returns:
Dict containing search results with web and news sections
"""
tools = YouTools()
return tools.search(
query=query,
count=count,
freshness=freshness,
country=country,
language=language,
offset=offset,
safesearch=safesearch,
livecrawl=livecrawl,
livecrawl_formats=livecrawl_formats,
)
def ydc_contents(
urls: Union[str, List[str]],
format: str = "markdown",
) -> Dict[str, Any]:
"""Fetch content from URLs using You.com's content extraction API.
Args:
urls: Single URL or list of URLs to fetch content from
format: Content format - "html" or "markdown"
Returns:
Dict containing extracted content for each URL
"""
tools = YouTools()
return tools.get_contents(urls=urls, format=format)
def ydc_news(
query: str,
count: Optional[int] = None,
) -> Dict[str, Any]:
"""Search for live news using You.com's news API.
Args:
query: News search query
count: Maximum number of results
Returns:
Dict containing news results
"""
tools = YouTools()
return tools.live_news(query=query, count=count)
def ydc_images(
query: str,
) -> Dict[str, Any]:
"""Search for images using You.com's image search API.
Args:
query: Image search query
Returns:
Dict containing image results
"""
tools = YouTools()
return tools.images(query=query)
if __name__ == "__main__":
# Example usage
print("\n" + "=" * 60)
print("You.com Tools Demonstration")
print("=" * 60 + "\n")
# Check if API key is available
is_available, error = _check_youdotcom_available()
if not is_available:
print(f"Error: {error}")
print("\nTo use You.com tools:")
print("1. Install: pip install youdotcom")
print("2. Set environment variable: export YDC_API_KEY=your_api_key")
else:
print("You.com SDK is available!")
# Example search
print("\n1. Basic Search")
print("-" * 30)
results = ydc_search("Latest AI developments 2024", count=3)
if "error" not in results:
web_results = results.get('results', {}).get('web', [])
print(f"Found {len(web_results)} web results")
for r in web_results[:3]:
print(f" - {r.get('title', 'No title')[:50]}...")
print(f" URL: {r.get('url', 'No URL')}")
else:
print(f"Error: {results['error']}")
print("\n" + "=" * 60)
print("Demonstration Complete")
print("=" * 60)
# Alias for simple usage: from praisonaiagents.tools import ydc
ydc = ydc_search
+3 -2
View File
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "praisonaiagents"
version = "0.0.184"
version = "0.0.189"
description = "Praison AI agents for completing complex tasks with Self Reflection Agents"
requires-python = ">=3.10"
authors = [
@@ -22,7 +22,8 @@ dependencies = [
mcp = [
"mcp>=1.6.0",
"fastapi>=0.115.0",
"uvicorn>=0.34.0"
"uvicorn>=0.34.0",
"websockets>=12.0"
]
memory = [
@@ -0,0 +1,18 @@
from praisonaiagents import Agent
def search_web(query: str) -> str:
return "AI trends: LLMs, multimodal AI, autonomous agents"
agent = Agent(
name="AI Assistant",
role="Research and Writing Specialist",
llm="gpt-4o-mini",
planning=True,
planning_tools=[search_web],
planning_reasoning=True,
verbose=False # Quiet mode
)
result = agent.start("Research AI trends in 2025 and write a 2 sentence summary")
print("\n=== FINAL RESULT ===")
print(result)
@@ -0,0 +1,62 @@
from praisonaiagents import Agent
def search_web(query: str) -> str:
return "AI trends: LLMs, multimodal AI, autonomous agents, edge AI"
print("=" * 60)
print("TEST 1: Planning ONLY (no tools, no reasoning)")
print("=" * 60)
agent1 = Agent(
name="Assistant",
llm="gpt-4o-mini",
planning=True,
verbose=False
)
result1 = agent1.start("Write 2 sentences about AI in healthcare")
print(f"\nResult: {result1[:200]}...")
print("\n" + "=" * 60)
print("TEST 2: Planning + Tools (no reasoning)")
print("=" * 60)
agent2 = Agent(
name="Assistant",
llm="gpt-4o-mini",
planning=True,
planning_tools=[search_web],
verbose=False
)
result2 = agent2.start("Write 2 sentences about AI trends")
print(f"\nResult: {result2[:200]}...")
print("\n" + "=" * 60)
print("TEST 3: Planning + Tools + Reasoning")
print("=" * 60)
agent3 = Agent(
name="Assistant",
llm="gpt-4o-mini",
planning=True,
planning_tools=[search_web],
planning_reasoning=True,
verbose=False
)
result3 = agent3.start("Write 2 sentences about AI trends")
print(f"\nResult: {result3[:200]}...")
print("\n" + "=" * 60)
print("TEST 4: No Planning (direct execution)")
print("=" * 60)
agent4 = Agent(
name="Assistant",
llm="gpt-4o-mini",
verbose=False
)
result4 = agent4.start("Write 2 sentences about AI trends")
print(f"\nResult: {result4[:200]}...")
print("\n" + "=" * 60)
print("COMPARISON COMPLETE")
print("=" * 60)
@@ -0,0 +1,122 @@
"""Tests for MCP Backward Compatibility - TDD approach.
These tests ensure backward compatibility with older MCP servers
and the deprecated HTTP+SSE transport from protocol version 2024-11-05.
Backward compatibility features:
- Legacy SSE endpoint support (/sse suffix)
- Fallback detection for older servers
- Automatic transport negotiation
"""
import pytest
class TestLegacySSESupport:
"""Test legacy SSE transport support."""
def test_sse_url_detection(self):
"""Test that /sse URLs are detected as legacy SSE."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("http://localhost:8080/sse") == "sse"
assert get_transport_type("https://example.com/api/sse") == "sse"
def test_non_sse_http_uses_streamable(self):
"""Test that non-/sse HTTP URLs use Streamable HTTP."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("http://localhost:8080/mcp") == "http_stream"
assert get_transport_type("https://example.com/api") == "http_stream"
class TestTransportNegotiation:
"""Test automatic transport negotiation."""
def test_detect_streamable_http_support(self):
"""Test detection of Streamable HTTP support."""
from praisonaiagents.mcp.mcp_compat import detect_transport_support
# Successful POST to MCP endpoint indicates Streamable HTTP
result = detect_transport_support(
response_status=200,
content_type="application/json"
)
assert result == "http_stream"
def test_detect_legacy_sse_fallback(self):
"""Test fallback to legacy SSE on specific error codes."""
from praisonaiagents.mcp.mcp_compat import detect_transport_support
# 400, 404, 405 indicate need for legacy SSE
assert detect_transport_support(response_status=400) == "sse"
assert detect_transport_support(response_status=404) == "sse"
assert detect_transport_support(response_status=405) == "sse"
def test_detect_sse_from_event_stream(self):
"""Test detection of SSE from content type."""
from praisonaiagents.mcp.mcp_compat import detect_transport_support
result = detect_transport_support(
response_status=200,
content_type="text/event-stream"
)
# SSE response indicates legacy transport
assert result in ["sse", "http_stream"]
class TestProtocolVersionCompatibility:
"""Test protocol version compatibility."""
def test_supports_2024_11_05(self):
"""Test support for protocol version 2024-11-05."""
from praisonaiagents.mcp.mcp_session import is_valid_protocol_version
assert is_valid_protocol_version("2024-11-05") is True
def test_supports_2025_03_26(self):
"""Test support for protocol version 2025-03-26."""
from praisonaiagents.mcp.mcp_session import is_valid_protocol_version
assert is_valid_protocol_version("2025-03-26") is True
def test_supports_2025_11_25(self):
"""Test support for protocol version 2025-11-25."""
from praisonaiagents.mcp.mcp_session import is_valid_protocol_version
assert is_valid_protocol_version("2025-11-25") is True
class TestMCPClassBackwardCompat:
"""Test MCP class backward compatibility."""
def test_mcp_accepts_sse_url(self):
"""Test MCP class accepts /sse URLs."""
# This is a design test - actual connection would require server
from praisonaiagents.mcp.mcp_transport import get_transport_type
# Verify URL would be routed to SSE transport
assert get_transport_type("http://localhost:8080/sse") == "sse"
def test_mcp_accepts_command_string(self):
"""Test MCP class accepts command strings (stdio)."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("python server.py") == "stdio"
assert get_transport_type("npx @mcp/server") == "stdio"
def test_mcp_accepts_http_url(self):
"""Test MCP class accepts HTTP URLs."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("http://localhost:8080/mcp") == "http_stream"
def test_mcp_accepts_websocket_url(self):
"""Test MCP class accepts WebSocket URLs."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("ws://localhost:8080/mcp") == "websocket"
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -0,0 +1,227 @@
"""Tests for MCP Resumability - TDD approach.
These tests define the expected behavior of SSE stream resumability
per MCP Protocol Revision 2025-11-25.
Resumability features:
- SSE event ID tracking
- Last-Event-ID header support
- Message replay on reconnection
- Retry field handling
"""
import pytest
class TestResumabilityManager:
"""Test ResumabilityManager class."""
def test_manager_creation(self):
"""Test ResumabilityManager can be created."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
assert manager.get_retry_delay() == 3000 # Default 3 seconds
def test_set_and_get_last_event_id(self):
"""Test setting and getting last event ID for a stream."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
# Initially no event ID
assert manager.get_last_event_id("stream-1") is None
# Set event ID
manager.set_last_event_id("stream-1", "event-123")
assert manager.get_last_event_id("stream-1") == "event-123"
def test_multiple_streams(self):
"""Test tracking multiple streams independently."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
manager.set_last_event_id("stream-1", "event-a")
manager.set_last_event_id("stream-2", "event-b")
assert manager.get_last_event_id("stream-1") == "event-a"
assert manager.get_last_event_id("stream-2") == "event-b"
def test_update_event_id(self):
"""Test updating event ID for a stream."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
manager.set_last_event_id("stream-1", "event-1")
manager.set_last_event_id("stream-1", "event-2")
assert manager.get_last_event_id("stream-1") == "event-2"
def test_set_retry_delay(self):
"""Test setting retry delay from SSE retry field."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
manager.set_retry_delay(5000)
assert manager.get_retry_delay() == 5000
def test_get_resume_headers_with_event_id(self):
"""Test getting resume headers when event ID is available."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
manager.set_last_event_id("stream-1", "event-123")
headers = manager.get_resume_headers("stream-1")
assert headers == {'Last-Event-ID': 'event-123'}
def test_get_resume_headers_without_event_id(self):
"""Test getting resume headers when no event ID is stored."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
headers = manager.get_resume_headers("stream-1")
assert headers == {}
def test_clear_stream(self):
"""Test clearing tracking for a specific stream."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
manager.set_last_event_id("stream-1", "event-a")
manager.set_last_event_id("stream-2", "event-b")
manager.clear_stream("stream-1")
assert manager.get_last_event_id("stream-1") is None
assert manager.get_last_event_id("stream-2") == "event-b"
def test_clear_all(self):
"""Test clearing all stream tracking."""
from praisonaiagents.mcp.mcp_session import ResumabilityManager
manager = ResumabilityManager()
manager.set_last_event_id("stream-1", "event-a")
manager.set_last_event_id("stream-2", "event-b")
manager.clear_all()
assert manager.get_last_event_id("stream-1") is None
assert manager.get_last_event_id("stream-2") is None
class TestSSEEventParsing:
"""Test SSE event parsing utilities."""
def test_parse_sse_event_with_id(self):
"""Test parsing SSE event with ID field."""
from praisonaiagents.mcp.mcp_session import parse_sse_event
event_text = "id: event-123\ndata: {\"test\": 1}\n\n"
event = parse_sse_event(event_text)
assert event['id'] == 'event-123'
assert event['data'] == '{"test": 1}'
def test_parse_sse_event_with_retry(self):
"""Test parsing SSE event with retry field."""
from praisonaiagents.mcp.mcp_session import parse_sse_event
event_text = "retry: 5000\ndata: {}\n\n"
event = parse_sse_event(event_text)
assert event['retry'] == 5000
def test_parse_sse_event_data_only(self):
"""Test parsing SSE event with only data."""
from praisonaiagents.mcp.mcp_session import parse_sse_event
event_text = "data: {\"message\": \"hello\"}\n\n"
event = parse_sse_event(event_text)
assert event['data'] == '{"message": "hello"}'
assert event.get('id') is None
def test_parse_sse_event_multiline_data(self):
"""Test parsing SSE event with multiline data."""
from praisonaiagents.mcp.mcp_session import parse_sse_event
event_text = "data: line1\ndata: line2\n\n"
event = parse_sse_event(event_text)
assert event['data'] == 'line1\nline2'
def test_parse_sse_event_empty_data(self):
"""Test parsing SSE event with empty data (for priming)."""
from praisonaiagents.mcp.mcp_session import parse_sse_event
event_text = "id: prime-123\ndata: \n\n"
event = parse_sse_event(event_text)
assert event['id'] == 'prime-123'
assert event['data'] == ''
class TestEventStore:
"""Test optional event store for message redelivery."""
def test_event_store_creation(self):
"""Test EventStore can be created."""
from praisonaiagents.mcp.mcp_session import EventStore
store = EventStore(max_events=100)
assert store.max_events == 100
def test_store_and_retrieve_event(self):
"""Test storing and retrieving events."""
from praisonaiagents.mcp.mcp_session import EventStore
store = EventStore()
store.store("stream-1", "event-1", {"data": "test1"})
store.store("stream-1", "event-2", {"data": "test2"})
events = store.get_events_after("stream-1", "event-1")
assert len(events) == 1
assert events[0] == {"data": "test2"}
def test_store_respects_max_events(self):
"""Test that store respects max_events limit."""
from praisonaiagents.mcp.mcp_session import EventStore
store = EventStore(max_events=3)
for i in range(5):
store.store("stream-1", f"event-{i}", {"data": i})
# Should only have last 3 events
all_events = store.get_all_events("stream-1")
assert len(all_events) <= 3
def test_store_per_stream_isolation(self):
"""Test that events are isolated per stream."""
from praisonaiagents.mcp.mcp_session import EventStore
store = EventStore()
store.store("stream-1", "event-a", {"data": "a"})
store.store("stream-2", "event-b", {"data": "b"})
events_1 = store.get_all_events("stream-1")
events_2 = store.get_all_events("stream-2")
assert len(events_1) == 1
assert len(events_2) == 1
assert events_1[0] == {"data": "a"}
assert events_2[0] == {"data": "b"}
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -0,0 +1,224 @@
"""Tests for MCP Security Features - TDD approach.
These tests define the expected behavior of security features
per MCP Protocol Revision 2025-11-25.
Security features:
- Origin header validation (DNS rebinding prevention)
- Localhost binding recommendation
- Authentication header support
- Secure session ID handling
"""
import pytest
class TestOriginValidation:
"""Test Origin header validation for DNS rebinding prevention."""
def test_valid_localhost_origins(self):
"""Test that localhost origins are accepted."""
from praisonaiagents.mcp.mcp_security import is_valid_origin
assert is_valid_origin("http://localhost:8080", allowed_origins=["localhost"]) is True
assert is_valid_origin("http://127.0.0.1:3000", allowed_origins=["127.0.0.1"]) is True
assert is_valid_origin("http://localhost", allowed_origins=["localhost"]) is True
def test_valid_custom_origins(self):
"""Test that configured custom origins are accepted."""
from praisonaiagents.mcp.mcp_security import is_valid_origin
allowed = ["example.com", "api.example.com"]
assert is_valid_origin("https://example.com", allowed_origins=allowed) is True
assert is_valid_origin("https://api.example.com/path", allowed_origins=allowed) is True
def test_invalid_origins_rejected(self):
"""Test that invalid origins are rejected."""
from praisonaiagents.mcp.mcp_security import is_valid_origin
allowed = ["localhost", "example.com"]
assert is_valid_origin("https://evil.com", allowed_origins=allowed) is False
assert is_valid_origin("https://example.com.evil.com", allowed_origins=allowed) is False
def test_missing_origin_handling(self):
"""Test handling of missing Origin header."""
from praisonaiagents.mcp.mcp_security import is_valid_origin
# Missing origin should be rejected by default
assert is_valid_origin(None, allowed_origins=["localhost"]) is False
assert is_valid_origin("", allowed_origins=["localhost"]) is False
def test_allow_missing_origin_option(self):
"""Test option to allow missing Origin header."""
from praisonaiagents.mcp.mcp_security import is_valid_origin
# Can optionally allow missing origin for non-browser clients
assert is_valid_origin(None, allowed_origins=["localhost"], allow_missing=True) is True
class TestLocalhostBinding:
"""Test localhost binding recommendations."""
def test_is_localhost_address(self):
"""Test detection of localhost addresses."""
from praisonaiagents.mcp.mcp_security import is_localhost_address
assert is_localhost_address("127.0.0.1") is True
assert is_localhost_address("localhost") is True
assert is_localhost_address("::1") is True # IPv6 localhost
assert is_localhost_address("0.0.0.0") is False # All interfaces
assert is_localhost_address("192.168.1.1") is False
def test_should_bind_localhost_only(self):
"""Test recommendation for localhost-only binding."""
from praisonaiagents.mcp.mcp_security import should_bind_localhost_only
# Local servers should bind to localhost only
assert should_bind_localhost_only(is_local=True) is True
# Remote/production servers may bind to all interfaces
assert should_bind_localhost_only(is_local=False) is False
class TestAuthenticationHeaders:
"""Test authentication header support."""
def test_create_bearer_auth_header(self):
"""Test creating Bearer authentication header."""
from praisonaiagents.mcp.mcp_security import create_auth_header
header = create_auth_header("my-secret-token", auth_type="bearer")
assert header == {"Authorization": "Bearer my-secret-token"}
def test_create_basic_auth_header(self):
"""Test creating Basic authentication header."""
from praisonaiagents.mcp.mcp_security import create_auth_header
import base64
header = create_auth_header("user:pass", auth_type="basic")
expected = base64.b64encode(b"user:pass").decode()
assert header == {"Authorization": f"Basic {expected}"}
def test_create_custom_auth_header(self):
"""Test creating custom authentication header."""
from praisonaiagents.mcp.mcp_security import create_auth_header
header = create_auth_header("custom-value", auth_type="custom", header_name="X-API-Key")
assert header == {"X-API-Key": "custom-value"}
def test_validate_auth_header(self):
"""Test validating authentication header presence."""
from praisonaiagents.mcp.mcp_security import validate_auth_header
headers_with_auth = {"Authorization": "Bearer token"}
headers_without_auth = {"Content-Type": "application/json"}
assert validate_auth_header(headers_with_auth) is True
assert validate_auth_header(headers_without_auth) is False
class TestSecureSessionHandling:
"""Test secure session ID handling."""
def test_generate_secure_session_id(self):
"""Test generating cryptographically secure session ID."""
from praisonaiagents.mcp.mcp_security import generate_secure_session_id
session_id = generate_secure_session_id()
# Should be non-empty
assert len(session_id) > 0
# Should be unique
session_id2 = generate_secure_session_id()
assert session_id != session_id2
# Should contain only valid characters (visible ASCII)
for char in session_id:
assert 0x21 <= ord(char) <= 0x7E
def test_session_id_length(self):
"""Test session ID has sufficient length for security."""
from praisonaiagents.mcp.mcp_security import generate_secure_session_id
session_id = generate_secure_session_id()
# Should be at least 32 characters for security
assert len(session_id) >= 32
def test_session_id_entropy(self):
"""Test session ID has sufficient entropy."""
from praisonaiagents.mcp.mcp_security import generate_secure_session_id
# Generate multiple IDs and check they're all different
ids = [generate_secure_session_id() for _ in range(100)]
unique_ids = set(ids)
assert len(unique_ids) == 100 # All should be unique
class TestDNSRebindingPrevention:
"""Test DNS rebinding attack prevention."""
def test_extract_origin_host(self):
"""Test extracting host from Origin header."""
from praisonaiagents.mcp.mcp_security import extract_origin_host
assert extract_origin_host("http://localhost:8080") == "localhost"
assert extract_origin_host("https://example.com:443/path") == "example.com"
assert extract_origin_host("http://127.0.0.1") == "127.0.0.1"
def test_detect_dns_rebinding_attempt(self):
"""Test detection of potential DNS rebinding attempts."""
from praisonaiagents.mcp.mcp_security import is_potential_dns_rebinding
# External origin trying to access localhost server
assert is_potential_dns_rebinding(
origin="https://evil.com",
server_host="localhost"
) is True
# Same origin is safe
assert is_potential_dns_rebinding(
origin="http://localhost:8080",
server_host="localhost"
) is False
class TestSecurityConfig:
"""Test security configuration."""
def test_security_config_defaults(self):
"""Test default security configuration."""
from praisonaiagents.mcp.mcp_security import SecurityConfig
config = SecurityConfig()
# Secure defaults
assert config.validate_origin is True
assert config.allow_missing_origin is False
assert "localhost" in config.allowed_origins
assert "127.0.0.1" in config.allowed_origins
def test_security_config_custom(self):
"""Test custom security configuration."""
from praisonaiagents.mcp.mcp_security import SecurityConfig
config = SecurityConfig(
allowed_origins=["example.com"],
allow_missing_origin=True,
require_auth=True
)
assert "example.com" in config.allowed_origins
assert config.allow_missing_origin is True
assert config.require_auth is True
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -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,242 @@
"""Tests for MCP Session Management - TDD approach.
These tests define the expected behavior of session management for
Streamable HTTP transport per MCP Protocol Revision 2025-11-25.
Session management features:
- MCP-Session-Id header handling
- Session persistence across requests
- Session expiration handling (HTTP 404)
- Session termination via HTTP DELETE
- Session ID validation (visible ASCII 0x21-0x7E)
"""
import pytest
class TestSessionIdValidation:
"""Test session ID validation per MCP spec."""
def test_valid_session_id_ascii(self):
"""Test that valid ASCII session IDs are accepted."""
from praisonaiagents.mcp.mcp_session import is_valid_session_id
# Valid session IDs (visible ASCII 0x21-0x7E)
assert is_valid_session_id("abc123") is True
assert is_valid_session_id("session-id-12345") is True
assert is_valid_session_id("a1b2c3d4e5f6") is True
assert is_valid_session_id("!@#$%^&*()") is True # Special chars in range
assert is_valid_session_id("UUID-1234-5678-90ab") is True
def test_invalid_session_id_with_spaces(self):
"""Test that session IDs with spaces are rejected."""
from praisonaiagents.mcp.mcp_session import is_valid_session_id
assert is_valid_session_id("session id") is False
assert is_valid_session_id(" leading") is False
assert is_valid_session_id("trailing ") is False
def test_invalid_session_id_with_control_chars(self):
"""Test that session IDs with control characters are rejected."""
from praisonaiagents.mcp.mcp_session import is_valid_session_id
assert is_valid_session_id("session\x00id") is False # NULL
assert is_valid_session_id("session\nid") is False # Newline
assert is_valid_session_id("session\tid") is False # Tab
def test_invalid_session_id_empty(self):
"""Test that empty session IDs are rejected."""
from praisonaiagents.mcp.mcp_session import is_valid_session_id
assert is_valid_session_id("") is False
assert is_valid_session_id(None) is False
def test_invalid_session_id_non_ascii(self):
"""Test that non-ASCII characters are rejected."""
from praisonaiagents.mcp.mcp_session import is_valid_session_id
assert is_valid_session_id("session\x7fid") is False # DEL char
assert is_valid_session_id("session\x80id") is False # Extended ASCII
class TestSessionManager:
"""Test SessionManager class for managing MCP sessions."""
def test_session_manager_creation(self):
"""Test SessionManager can be created."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
assert manager.session_id is None
assert manager.is_active is False
def test_session_manager_set_session_id(self):
"""Test setting session ID."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
manager.set_session_id("test-session-123")
assert manager.session_id == "test-session-123"
assert manager.is_active is True
def test_session_manager_rejects_invalid_id(self):
"""Test that invalid session IDs are rejected."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
with pytest.raises(ValueError, match="Invalid session ID"):
manager.set_session_id("invalid session") # Contains space
def test_session_manager_clear_session(self):
"""Test clearing session."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
manager.set_session_id("test-session-123")
manager.clear()
assert manager.session_id is None
assert manager.is_active is False
def test_session_manager_get_headers(self):
"""Test getting headers with session ID."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
# No session - no header
headers = manager.get_headers()
assert 'Mcp-Session-Id' not in headers
# With session - include header
manager.set_session_id("test-session-123")
headers = manager.get_headers()
assert headers['Mcp-Session-Id'] == "test-session-123"
class TestSessionFromResponse:
"""Test extracting session ID from HTTP response headers."""
def test_extract_session_id_from_headers(self):
"""Test extracting session ID from response headers."""
from praisonaiagents.mcp.mcp_session import extract_session_id
headers = {'Mcp-Session-Id': 'new-session-456'}
session_id = extract_session_id(headers)
assert session_id == 'new-session-456'
def test_extract_session_id_case_insensitive(self):
"""Test that header name is case-insensitive."""
from praisonaiagents.mcp.mcp_session import extract_session_id
# Various case combinations
assert extract_session_id({'mcp-session-id': 'test'}) == 'test'
assert extract_session_id({'MCP-SESSION-ID': 'test'}) == 'test'
assert extract_session_id({'Mcp-Session-Id': 'test'}) == 'test'
def test_extract_session_id_missing(self):
"""Test when session ID header is missing."""
from praisonaiagents.mcp.mcp_session import extract_session_id
headers = {'Content-Type': 'application/json'}
session_id = extract_session_id(headers)
assert session_id is None
class TestSessionExpiration:
"""Test handling of session expiration (HTTP 404)."""
def test_is_session_expired_404(self):
"""Test that HTTP 404 indicates session expiration."""
from praisonaiagents.mcp.mcp_session import is_session_expired
assert is_session_expired(404) is True
assert is_session_expired(200) is False
assert is_session_expired(400) is False
assert is_session_expired(500) is False
def test_session_manager_marks_expired(self):
"""Test that session manager can mark session as expired."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
manager.set_session_id("test-session")
manager.mark_expired()
assert manager.is_expired is True
assert manager.is_active is False
class TestProtocolVersion:
"""Test MCP-Protocol-Version header handling."""
def test_default_protocol_version(self):
"""Test default protocol version is 2025-03-26 for compatibility."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
headers = manager.get_headers()
# Default version for backward compatibility
assert headers.get('Mcp-Protocol-Version') == '2025-03-26'
def test_set_protocol_version(self):
"""Test setting negotiated protocol version."""
from praisonaiagents.mcp.mcp_session import SessionManager
manager = SessionManager()
manager.set_protocol_version('2025-11-25')
headers = manager.get_headers()
assert headers['Mcp-Protocol-Version'] == '2025-11-25'
def test_valid_protocol_versions(self):
"""Test that valid protocol versions are accepted."""
from praisonaiagents.mcp.mcp_session import is_valid_protocol_version
assert is_valid_protocol_version('2025-03-26') is True
assert is_valid_protocol_version('2025-06-18') is True
assert is_valid_protocol_version('2025-11-25') is True
assert is_valid_protocol_version('2024-11-05') is True
def test_invalid_protocol_versions(self):
"""Test that invalid protocol versions are rejected."""
from praisonaiagents.mcp.mcp_session import is_valid_protocol_version
assert is_valid_protocol_version('invalid') is False
assert is_valid_protocol_version('2025') is False
assert is_valid_protocol_version('') is False
class TestHTTPStreamSessionIntegration:
"""Test session management integration with HTTPStreamTransport."""
def test_transport_uses_session_manager(self):
"""Test that HTTPStreamTransport uses SessionManager."""
# This will be implemented when we update HTTPStreamTransport
pass
def test_transport_handles_session_from_init_response(self):
"""Test that transport extracts session ID from InitializeResult."""
pass
def test_transport_includes_session_in_requests(self):
"""Test that transport includes session ID in subsequent requests."""
pass
def test_transport_handles_session_expiration(self):
"""Test that transport handles HTTP 404 session expiration."""
pass
def test_transport_can_terminate_session(self):
"""Test that transport can terminate session via HTTP DELETE."""
pass
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -0,0 +1,167 @@
"""Tests for MCP Transport Abstraction Layer - TDD approach.
These tests define the expected behavior of the transport abstraction
layer that provides a common interface for all MCP transports.
Transport abstraction features:
- BaseTransport abstract class
- Common interface (connect, send, receive, close)
- Transport factory for automatic selection
"""
import pytest
from abc import ABC
class TestBaseTransport:
"""Test BaseTransport abstract class."""
def test_base_transport_is_abstract(self):
"""Test that BaseTransport cannot be instantiated directly."""
from praisonaiagents.mcp.mcp_transport import BaseTransport
with pytest.raises(TypeError):
BaseTransport()
def test_base_transport_defines_interface(self):
"""Test that BaseTransport defines required interface methods."""
from praisonaiagents.mcp.mcp_transport import BaseTransport
import inspect
# Check required abstract methods exist
assert hasattr(BaseTransport, 'connect')
assert hasattr(BaseTransport, 'send')
assert hasattr(BaseTransport, 'receive')
assert hasattr(BaseTransport, 'close')
assert hasattr(BaseTransport, 'is_connected')
def test_concrete_transport_must_implement_interface(self):
"""Test that concrete transports must implement all methods."""
from praisonaiagents.mcp.mcp_transport import BaseTransport
# Incomplete implementation should fail
class IncompleteTransport(BaseTransport):
pass
with pytest.raises(TypeError):
IncompleteTransport()
class TestTransportFactory:
"""Test transport factory for automatic selection."""
def test_factory_selects_stdio_for_command(self):
"""Test factory selects stdio transport for command strings."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("python /path/to/server.py") == "stdio"
assert get_transport_type("npx @modelcontextprotocol/server") == "stdio"
def test_factory_selects_websocket_for_ws_url(self):
"""Test factory selects WebSocket transport for ws:// URLs."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("ws://localhost:8080/mcp") == "websocket"
assert get_transport_type("wss://example.com/mcp") == "websocket"
def test_factory_selects_sse_for_sse_url(self):
"""Test factory selects SSE transport for /sse URLs."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("http://localhost:8080/sse") == "sse"
assert get_transport_type("https://example.com/sse") == "sse"
def test_factory_selects_http_stream_for_http_url(self):
"""Test factory selects HTTP Stream transport for HTTP URLs."""
from praisonaiagents.mcp.mcp_transport import get_transport_type
assert get_transport_type("http://localhost:8080/mcp") == "http_stream"
assert get_transport_type("https://example.com/mcp") == "http_stream"
class TestTransportConfig:
"""Test transport configuration."""
def test_transport_config_creation(self):
"""Test TransportConfig can be created."""
from praisonaiagents.mcp.mcp_transport import TransportConfig
config = TransportConfig(
timeout=30,
debug=True,
retry_count=3
)
assert config.timeout == 30
assert config.debug is True
assert config.retry_count == 3
def test_transport_config_defaults(self):
"""Test TransportConfig has sensible defaults."""
from praisonaiagents.mcp.mcp_transport import TransportConfig
config = TransportConfig()
assert config.timeout == 60 # Default timeout
assert config.debug is False
assert config.retry_count == 3
class TestTransportRegistry:
"""Test transport registration mechanism."""
def test_register_custom_transport(self):
"""Test registering a custom transport."""
from praisonaiagents.mcp.mcp_transport import (
TransportRegistry, BaseTransport
)
class CustomTransport(BaseTransport):
async def connect(self): pass
async def send(self, message): pass
async def receive(self): pass
async def close(self): pass
@property
def is_connected(self): return False
registry = TransportRegistry()
registry.register("custom", CustomTransport)
assert "custom" in registry.list_transports()
def test_get_registered_transport(self):
"""Test getting a registered transport class."""
from praisonaiagents.mcp.mcp_transport import (
TransportRegistry, BaseTransport
)
class MyTransport(BaseTransport):
async def connect(self): pass
async def send(self, message): pass
async def receive(self): pass
async def close(self): pass
@property
def is_connected(self): return False
registry = TransportRegistry()
registry.register("my_transport", MyTransport)
transport_class = registry.get("my_transport")
assert transport_class is MyTransport
def test_default_transports_registered(self):
"""Test that default transports are pre-registered."""
from praisonaiagents.mcp.mcp_transport import get_default_registry
registry = get_default_registry()
transports = registry.list_transports()
# Default transports should be available
assert "stdio" in transports
assert "sse" in transports
assert "http_stream" in transports
assert "websocket" in transports
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"])
@@ -0,0 +1,296 @@
"""Tests for MCP WebSocket Transport - TDD approach.
These tests define the expected behavior of the WebSocket transport module.
WebSocket transport is an optional feature that requires the 'websockets' package.
Performance considerations:
- WebSocket is lazy-loaded only when ws:// or wss:// URLs are used
- No impact on existing stdio/SSE/HTTP transports
- Uses asyncio for non-blocking I/O
"""
import pytest
from unittest.mock import Mock
# Skip all tests if websockets is not installed
pytest.importorskip("websockets", reason="websockets package not installed")
class TestWebSocketURLDetection:
"""Test URL detection for WebSocket transport."""
def test_detects_ws_url(self):
"""Test that ws:// URLs are detected as WebSocket."""
from praisonaiagents.mcp.mcp_websocket import is_websocket_url
assert is_websocket_url("ws://localhost:8080") is True
assert is_websocket_url("ws://example.com/mcp") is True
assert is_websocket_url("ws://127.0.0.1:3000/ws") is True
def test_detects_wss_url(self):
"""Test that wss:// URLs are detected as WebSocket (secure)."""
from praisonaiagents.mcp.mcp_websocket import is_websocket_url
assert is_websocket_url("wss://localhost:8080") is True
assert is_websocket_url("wss://example.com/mcp") is True
assert is_websocket_url("wss://secure.server.com:443/ws") is True
def test_rejects_http_urls(self):
"""Test that HTTP URLs are not detected as WebSocket."""
from praisonaiagents.mcp.mcp_websocket import is_websocket_url
assert is_websocket_url("http://localhost:8080") is False
assert is_websocket_url("https://example.com/mcp") is False
assert is_websocket_url("http://localhost:8080/sse") is False
def test_rejects_non_url_strings(self):
"""Test that non-URL strings are not detected as WebSocket."""
from praisonaiagents.mcp.mcp_websocket import is_websocket_url
assert is_websocket_url("python") is False
assert is_websocket_url("/path/to/script.py") is False
assert is_websocket_url("npx @modelcontextprotocol/server") is False
class TestWebSocketMCPTool:
"""Test WebSocketMCPTool wrapper class."""
def test_tool_has_required_attributes(self):
"""Test that tool wrapper has all required attributes for Agent."""
from praisonaiagents.mcp.mcp_websocket import WebSocketMCPTool
mock_session = Mock()
tool = WebSocketMCPTool(
name="test_tool",
description="A test tool",
session=mock_session,
input_schema={"type": "object", "properties": {"query": {"type": "string"}}}
)
assert tool.name == "test_tool"
assert tool.__name__ == "test_tool"
assert tool.__qualname__ == "test_tool"
assert tool.__doc__ == "A test tool"
assert tool.description == "A test tool"
assert hasattr(tool, '__signature__')
def test_tool_generates_correct_signature(self):
"""Test that tool generates correct function signature from schema."""
from praisonaiagents.mcp.mcp_websocket import WebSocketMCPTool
import inspect
mock_session = Mock()
tool = WebSocketMCPTool(
name="search",
description="Search tool",
session=mock_session,
input_schema={
"type": "object",
"properties": {
"query": {"type": "string"},
"max_results": {"type": "integer"}
},
"required": ["query"]
}
)
sig = tool.__signature__
params = list(sig.parameters.keys())
assert "query" in params
assert "max_results" in params
assert sig.parameters["query"].default == inspect.Parameter.empty
assert sig.parameters["max_results"].default is None # Optional
def test_tool_converts_to_openai_format(self):
"""Test conversion to OpenAI tool format."""
from praisonaiagents.mcp.mcp_websocket import WebSocketMCPTool
mock_session = Mock()
tool = WebSocketMCPTool(
name="search",
description="Search the web",
session=mock_session,
input_schema={
"type": "object",
"properties": {"query": {"type": "string"}},
"required": ["query"]
}
)
openai_tool = tool.to_openai_tool()
assert openai_tool["type"] == "function"
assert openai_tool["function"]["name"] == "search"
assert openai_tool["function"]["description"] == "Search the web"
assert "parameters" in openai_tool["function"]
def test_tool_fixes_array_schemas(self):
"""Test that array schemas without items are fixed for OpenAI compatibility."""
from praisonaiagents.mcp.mcp_websocket import WebSocketMCPTool
mock_session = Mock()
tool = WebSocketMCPTool(
name="process",
description="Process items",
session=mock_session,
input_schema={
"type": "object",
"properties": {
"items": {"type": "array"} # Missing 'items' attribute
}
}
)
openai_tool = tool.to_openai_tool()
items_schema = openai_tool["function"]["parameters"]["properties"]["items"]
assert "items" in items_schema # Should be added
assert items_schema["items"]["type"] == "string" # Default
class TestWebSocketTransport:
"""Test WebSocketTransport low-level implementation."""
def test_transport_handles_session_id(self):
"""Test that transport handles MCP session ID."""
from praisonaiagents.mcp.mcp_websocket import WebSocketTransport
transport = WebSocketTransport("ws://localhost:8080/mcp")
assert transport.session_id is None
transport.session_id = "test-session-123"
assert transport.session_id == "test-session-123"
def test_transport_initialization(self):
"""Test transport initializes with correct parameters."""
from praisonaiagents.mcp.mcp_websocket import WebSocketTransport
transport = WebSocketTransport(
"wss://example.com/mcp",
auth_token="secret",
max_retries=5,
timeout=30.0
)
assert transport.url == "wss://example.com/mcp"
assert transport.auth_token == "secret"
assert transport.max_retries == 5
assert transport.timeout == 30.0
def test_transport_send_requires_connection(self):
"""Test that send raises error when not connected."""
from praisonaiagents.mcp.mcp_websocket import WebSocketTransport
import asyncio
transport = WebSocketTransport("ws://localhost:8080/mcp")
with pytest.raises(RuntimeError, match="WebSocket not connected"):
asyncio.get_event_loop().run_until_complete(transport.send({"test": 1}))
class TestWebSocketReconnection:
"""Test WebSocket reconnection with exponential backoff."""
def test_exponential_backoff_calculation(self):
"""Test exponential backoff delay calculation."""
from praisonaiagents.mcp.mcp_websocket import calculate_backoff
# First retry: base delay
assert calculate_backoff(attempt=0, base_delay=1.0, max_delay=60.0) == 1.0
# Second retry: 2x
assert calculate_backoff(attempt=1, base_delay=1.0, max_delay=60.0) == 2.0
# Third retry: 4x
assert calculate_backoff(attempt=2, base_delay=1.0, max_delay=60.0) == 4.0
# Should cap at max_delay
assert calculate_backoff(attempt=10, base_delay=1.0, max_delay=60.0) == 60.0
class TestWebSocketMCPClient:
"""Test WebSocketMCPClient high-level interface."""
def test_client_initialization(self):
"""Test client can be initialized with WebSocket URL."""
# This test will be implemented after the basic structure is in place
pass
def test_client_lists_tools(self):
"""Test client can list available tools from server."""
pass
def test_client_calls_tool(self):
"""Test client can call a tool and get result."""
pass
def test_client_handles_timeout(self):
"""Test client handles timeout correctly."""
pass
def test_client_is_iterable(self):
"""Test client tools are iterable."""
pass
def test_client_converts_to_openai_tools(self):
"""Test client can convert all tools to OpenAI format."""
pass
class TestMCPClassWebSocketIntegration:
"""Test MCP class integration with WebSocket transport."""
def test_mcp_detects_ws_url(self):
"""Test MCP class detects ws:// URL and uses WebSocket transport."""
# Will be implemented after WebSocketMCPClient is ready
pass
def test_mcp_detects_wss_url(self):
"""Test MCP class detects wss:// URL and uses WebSocket transport."""
pass
def test_mcp_websocket_tools_work_with_agent(self):
"""Test WebSocket tools work correctly with Agent class."""
pass
class TestWebSocketKeepalive:
"""Test WebSocket keepalive/ping-pong handling."""
def test_ping_interval_configurable(self):
"""Test that ping interval is configurable."""
from praisonaiagents.mcp.mcp_websocket import WebSocketTransport
transport = WebSocketTransport(
"ws://localhost:8080/mcp",
ping_interval=60.0,
ping_timeout=20.0
)
assert transport.ping_interval == 60.0
assert transport.ping_timeout == 20.0
class TestWebSocketPerformance:
"""Test WebSocket transport performance characteristics."""
def test_lazy_import_websocket_module(self):
"""Test that mcp_websocket module can be imported."""
# Just verify the module imports without errors
from praisonaiagents.mcp.mcp_websocket import (
is_websocket_url,
calculate_backoff,
WebSocketTransport,
WebSocketMCPTool
)
# Verify functions exist
assert callable(is_websocket_url)
assert callable(calculate_backoff)
if __name__ == "__main__":
pytest.main([__file__, "-v"])
@@ -0,0 +1,142 @@
"""Test Tavily tools integration."""
import os
import sys
# Test 1: Import test - verify lazy loading works
print("=" * 60)
print("Test 1: Import Test (Lazy Loading)")
print("=" * 60)
try:
from praisonaiagents.tools import tavily_search, tavily_extract, tavily_crawl, tavily_map
print("✓ Successfully imported tavily_search, tavily_extract, tavily_crawl, tavily_map")
except ImportError as e:
print(f"✗ Import failed: {e}")
sys.exit(1)
try:
from praisonaiagents.tools import TavilyTools
print("✓ Successfully imported TavilyTools class")
except ImportError as e:
print(f"✗ TavilyTools import failed: {e}")
sys.exit(1)
try:
from praisonaiagents.tools import tavily_search_async, tavily_extract_async
print("✓ Successfully imported async functions")
except ImportError as e:
print(f"✗ Async import failed: {e}")
sys.exit(1)
# Test 2: Check API key detection
print("\n" + "=" * 60)
print("Test 2: API Key Detection")
print("=" * 60)
api_key = os.environ.get("TAVILY_API_KEY")
if api_key:
print(f"✓ TAVILY_API_KEY is set (length: {len(api_key)})")
else:
print("✗ TAVILY_API_KEY is not set")
print(" To run full tests, set: export TAVILY_API_KEY=your_api_key")
# Test 3: Error handling without API key
print("\n" + "=" * 60)
print("Test 3: Error Handling (No API Key)")
print("=" * 60)
# Temporarily unset API key to test error handling
original_key = os.environ.pop("TAVILY_API_KEY", None)
try:
result = tavily_search("test query")
if "error" in result:
print(f"✓ Proper error returned when no API key: {result['error'][:50]}...")
else:
print("✗ Expected error but got results")
except Exception as e:
print(f"✗ Unexpected exception: {e}")
# Restore API key
if original_key:
os.environ["TAVILY_API_KEY"] = original_key
# Test 4: TavilyTools class instantiation
print("\n" + "=" * 60)
print("Test 4: TavilyTools Class")
print("=" * 60)
try:
tools = TavilyTools()
print("✓ TavilyTools instantiated successfully")
# Check methods exist
methods = ['search', 'extract', 'crawl', 'map', 'asearch', 'aextract']
for method in methods:
if hasattr(tools, method):
print(f" ✓ Method '{method}' exists")
else:
print(f" ✗ Method '{method}' missing")
except Exception as e:
print(f"✗ TavilyTools instantiation failed: {e}")
# Test 5: Full integration test (only if API key is available)
print("\n" + "=" * 60)
print("Test 5: Full Integration Test")
print("=" * 60)
if os.environ.get("TAVILY_API_KEY"):
print("Running search test...")
try:
result = tavily_search("Python programming", max_results=2)
if "error" not in result:
print(f"✓ Search successful!")
print(f" Query: {result.get('query', 'N/A')}")
print(f" Results: {len(result.get('results', []))} items")
if result.get('results'):
print(f" First result: {result['results'][0].get('title', 'N/A')[:50]}...")
else:
print(f"✗ Search returned error: {result['error']}")
except Exception as e:
print(f"✗ Search failed with exception: {e}")
else:
print("⊘ Skipped (TAVILY_API_KEY not set)")
# Test 6: Agent integration test
print("\n" + "=" * 60)
print("Test 6: Agent Integration Test")
print("=" * 60)
if os.environ.get("TAVILY_API_KEY") and os.environ.get("OPENAI_API_KEY"):
try:
from praisonaiagents import Agent
agent = Agent(
name="SearchAgent",
role="Web Researcher",
goal="Search the web for information",
tools=[tavily_search]
)
print("✓ Agent created with tavily_search tool")
# Check tool is properly registered
if agent.tools and tavily_search in agent.tools:
print("✓ tavily_search is in agent's tools list")
else:
print("⊘ Tool registration check inconclusive")
except Exception as e:
print(f"✗ Agent integration failed: {e}")
else:
missing = []
if not os.environ.get("TAVILY_API_KEY"):
missing.append("TAVILY_API_KEY")
if not os.environ.get("OPENAI_API_KEY"):
missing.append("OPENAI_API_KEY")
print(f"⊘ Skipped (missing: {', '.join(missing)})")
print("\n" + "=" * 60)
print("Test Summary")
print("=" * 60)
print("All import and basic tests passed!")
print("For full functionality, ensure TAVILY_API_KEY is set.")
@@ -0,0 +1,135 @@
"""Test You.com tools integration."""
import os
import sys
# Test 1: Import test - verify lazy loading works
print("=" * 60)
print("Test 1: Import Test (Lazy Loading)")
print("=" * 60)
try:
from praisonaiagents.tools import ydc_search, ydc_contents, ydc_news, ydc_images
print("✓ Successfully imported ydc_search, ydc_contents, ydc_news, ydc_images")
except ImportError as e:
print(f"✗ Import failed: {e}")
sys.exit(1)
try:
from praisonaiagents.tools import YouTools
print("✓ Successfully imported YouTools class")
except ImportError as e:
print(f"✗ YouTools import failed: {e}")
sys.exit(1)
# Test 2: Check API key detection
print("\n" + "=" * 60)
print("Test 2: API Key Detection")
print("=" * 60)
api_key = os.environ.get("YDC_API_KEY")
if api_key:
print(f"✓ YDC_API_KEY is set (length: {len(api_key)})")
else:
print("✗ YDC_API_KEY is not set")
print(" To run full tests, set: export YDC_API_KEY=your_api_key")
# Test 3: Error handling without API key
print("\n" + "=" * 60)
print("Test 3: Error Handling (No API Key)")
print("=" * 60)
# Temporarily unset API key to test error handling
original_key = os.environ.pop("YDC_API_KEY", None)
try:
result = ydc_search("test query")
if "error" in result:
print(f"✓ Proper error returned when no API key: {result['error'][:50]}...")
else:
print("✗ Expected error but got results")
except Exception as e:
print(f"✗ Unexpected exception: {e}")
# Restore API key
if original_key:
os.environ["YDC_API_KEY"] = original_key
# Test 4: YouTools class instantiation
print("\n" + "=" * 60)
print("Test 4: YouTools Class")
print("=" * 60)
try:
tools = YouTools()
print("✓ YouTools instantiated successfully")
# Check methods exist
methods = ['search', 'get_contents', 'live_news', 'images']
for method in methods:
if hasattr(tools, method):
print(f" ✓ Method '{method}' exists")
else:
print(f" ✗ Method '{method}' missing")
except Exception as e:
print(f"✗ YouTools instantiation failed: {e}")
# Test 5: Full integration test (only if API key is available)
print("\n" + "=" * 60)
print("Test 5: Full Integration Test")
print("=" * 60)
if os.environ.get("YDC_API_KEY"):
print("Running search test...")
try:
result = ydc_search("Python programming", count=2)
if "error" not in result:
print(f"✓ Search successful!")
web_results = result.get('results', {}).get('web', [])
print(f" Web results: {len(web_results)} items")
if web_results:
print(f" First result: {web_results[0].get('title', 'N/A')[:50]}...")
else:
print(f"✗ Search returned error: {result['error']}")
except Exception as e:
print(f"✗ Search failed with exception: {e}")
else:
print("⊘ Skipped (YDC_API_KEY not set)")
# Test 6: Agent integration test
print("\n" + "=" * 60)
print("Test 6: Agent Integration Test")
print("=" * 60)
if os.environ.get("YDC_API_KEY") and os.environ.get("OPENAI_API_KEY"):
try:
from praisonaiagents import Agent
agent = Agent(
name="SearchAgent",
role="Web Researcher",
goal="Search the web for information",
tools=[ydc_search]
)
print("✓ Agent created with ydc_search tool")
# Check tool is properly registered
if agent.tools and ydc_search in agent.tools:
print("✓ ydc_search is in agent's tools list")
else:
print("⊘ Tool registration check inconclusive")
except Exception as e:
print(f"✗ Agent integration failed: {e}")
else:
missing = []
if not os.environ.get("YDC_API_KEY"):
missing.append("YDC_API_KEY")
if not os.environ.get("OPENAI_API_KEY"):
missing.append("OPENAI_API_KEY")
print(f"⊘ Skipped (missing: {', '.join(missing)})")
print("\n" + "=" * 60)
print("Test Summary")
print("=" * 60)
print("All import and basic tests passed!")
print("For full functionality, ensure YDC_API_KEY is set.")
+4 -1
View File
@@ -2422,7 +2422,7 @@ wheels = [
[[package]]
name = "praisonaiagents"
version = "0.0.184"
version = "0.0.189"
source = { editable = "." }
dependencies = [
{ name = "aiohttp" },
@@ -2450,6 +2450,7 @@ all = [
{ name = "python-jose", extra = ["cryptography"] },
{ name = "python-multipart" },
{ name = "uvicorn" },
{ name = "websockets" },
]
api = [
{ name = "fastapi" },
@@ -2479,6 +2480,7 @@ mcp = [
{ name = "fastapi" },
{ name = "mcp" },
{ name = "uvicorn" },
{ name = "websockets" },
]
memory = [
{ name = "chromadb" },
@@ -2530,6 +2532,7 @@ requires-dist = [
{ name = "rich" },
{ name = "uvicorn", marker = "extra == 'api'", specifier = ">=0.34.0" },
{ name = "uvicorn", marker = "extra == 'mcp'", specifier = ">=0.34.0" },
{ name = "websockets", marker = "extra == 'mcp'", specifier = ">=12.0" },
]
provides-extras = ["mcp", "memory", "knowledge", "graph", "llm", "api", "telemetry", "mongodb", "auth", "all"]
+101 -1
View File
@@ -60,9 +60,14 @@ PraisonAI is a production-ready Multi-AI Agents framework with self-reflection,
- 💾 **Prompt Caching** (Reduce costs & latency - OpenAI, Anthropic, Bedrock, Deepseek)
- 🧠 **Claude Memory Tool** (Persistent cross-conversation memory - Anthropic Beta)
- 💾 **File-Based Memory** (Zero-dependency persistent memory for all agents)
- 🔍 **Built-in Search Tools** (Tavily, You.com, Exa - web search, news, content extraction)
- 📋 **Planning Mode** (Plan before execution - Agent & Multi-Agent)
- 🔧 **Planning Tools** (Research with tools during planning)
- 🧠 **Planning Reasoning** (Chain-of-thought planning)
- 🔌 **MCP Transports** (stdio, Streamable HTTP, WebSocket, SSE - Protocol 2025-11-25)
- 🌐 **WebSocket MCP** (Real-time bidirectional connections with auto-reconnect)
- 🔐 **MCP Security** (Origin validation, DNS rebinding prevention, secure sessions)
- 🔄 **MCP Resumability** (SSE stream recovery via Last-Event-ID)
## Using Python Code
@@ -414,6 +419,101 @@ praisonai research --query-rewrite --tools tools.py --save "Your research query"
praisonai research -v "Your research query"
```
### Planning Mode CLI:
```bash
# Enable planning mode - agent creates a plan before execution
praisonai "Research AI trends and write a summary" --planning
# Planning with tools for research
praisonai "Analyze market trends" --planning --planning-tools tools.py
# Planning with chain-of-thought reasoning
praisonai "Complex analysis task" --planning --planning-reasoning
# Auto-approve plans without confirmation
praisonai "Task" --planning --auto-approve-plan
```
### Memory CLI:
```bash
# Enable memory for agent (persists across sessions)
praisonai "My name is John" --memory
# Memory with user isolation
praisonai "Remember my preferences" --memory --user-id user123
# Memory management commands
praisonai memory show # Show memory statistics
praisonai memory add "User prefers Python" # Add to long-term memory
praisonai memory search "Python" # Search memories
praisonai memory clear # Clear short-term memory
praisonai memory clear all # Clear all memory
praisonai memory save my_session # Save session
praisonai memory resume my_session # Resume session
praisonai memory sessions # List saved sessions
praisonai memory checkpoint # Create checkpoint
praisonai memory restore <checkpoint_id> # Restore checkpoint
praisonai memory checkpoints # List checkpoints
praisonai memory help # Show all commands
```
### Rules CLI:
```bash
# List all loaded rules (from PRAISON.md, CLAUDE.md, etc.)
praisonai rules list
# Show specific rule details
praisonai rules show <rule_name>
# Create a new rule
praisonai rules create my_rule "Always use type hints"
# Delete a rule
praisonai rules delete my_rule
# Show rules statistics
praisonai rules stats
# Include manual rules with prompts
praisonai "Task" --include-rules security,testing
```
### Workflow CLI:
```bash
# List available workflows
praisonai workflow list
# Execute a workflow
praisonai workflow run deploy
# Execute with variables
praisonai workflow run deploy --workflow-var environment=staging --workflow-var branch=main
# Show workflow details
praisonai workflow show deploy
# Create a new workflow template
praisonai workflow create my_workflow
```
### Hooks CLI:
```bash
# List configured hooks
praisonai hooks list
# Show hooks statistics
praisonai hooks stats
# Create hooks.json template
praisonai hooks init
```
### Claude Memory Tool CLI:
```bash
# Enable Claude Memory Tool (Anthropic models only)
praisonai "Research and remember findings" --claude-memory --llm anthropic/claude-sonnet-4-20250514
```
## Using JavaScript Code
```bash
@@ -1045,7 +1145,7 @@ cd src/praisonai && uv publish
- 🎨 Interactive UIs
- 📄 YAML-based Configuration
- 🛠️ Custom Tool Integration
- 🔍 Internet Search Capability (using Crawl4AI and Tavily)
- 🔍 Internet Search Capability (Tavily, You.com, Exa, DuckDuckGo, Crawl4AI)
- 🖼️ Vision Language Model (VLM) Support
- 🎙️ Real-time Voice Interaction
+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.6.tar.gz"
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.6.tar.gz | shasum -a 256`.split.first
url "https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.10.tar.gz"
sha256 `curl -sL https://github.com/MervinPraison/PraisonAI/archive/refs/tags/v2.3.10.tar.gz | shasum -a 256`.split.first
license "MIT"
depends_on "[email protected]"
+619 -1
View File
@@ -549,7 +549,7 @@ class PraisonAI:
return default_args
# Define special commands
special_commands = ['chat', 'code', 'call', 'realtime', 'train', 'ui', 'context', 'research']
special_commands = ['chat', 'code', 'call', 'realtime', 'train', 'ui', 'context', 'research', 'memory', 'rules', 'workflow', 'hooks']
parser = argparse.ArgumentParser(prog="praisonai", description="praisonAI command-line interface")
parser.add_argument("--framework", choices=["crewai", "autogen", "praisonai"], help="Specify the framework")
@@ -588,6 +588,25 @@ class PraisonAI:
parser.add_argument("--web-fetch", action="store_true", help="Enable web fetch to retrieve URL content (Anthropic only)")
parser.add_argument("--prompt-caching", action="store_true", help="Enable prompt caching to reduce costs (OpenAI, Anthropic, Bedrock, Deepseek)")
# Planning Mode arguments
parser.add_argument("--planning", action="store_true", help="Enable planning mode - create plan before execution")
parser.add_argument("--planning-tools", type=str, help="Tools for planning research (path to tools.py or comma-separated tool names)")
parser.add_argument("--planning-reasoning", action="store_true", help="Enable chain-of-thought reasoning in planning")
parser.add_argument("--auto-approve-plan", action="store_true", help="Auto-approve generated plans without user confirmation")
# Memory arguments
parser.add_argument("--memory", action="store_true", help="Enable file-based memory for agent")
parser.add_argument("--user-id", type=str, help="User ID for memory isolation")
# Rules arguments
parser.add_argument("--include-rules", type=str, help="Include manual rules by name (comma-separated)")
# Workflow arguments
parser.add_argument("--workflow-var", action="append", help="Workflow variable in key=value format (can be used multiple times)")
# Claude Memory Tool arguments
parser.add_argument("--claude-memory", action="store_true", help="Enable Claude Memory Tool (Anthropic models only)")
# If we're in a test environment, parse with empty args to avoid pytest interference
if in_test_env:
args, unknown_args = parser.parse_known_args([])
@@ -734,6 +753,60 @@ class PraisonAI:
self.handle_research_command(research_query, research_model, verbose, save, query_rewrite, tools_path, rewrite_tools)
sys.exit(0)
elif args.command == 'memory':
if not PRAISONAI_AVAILABLE:
print("[red]ERROR: PraisonAI Agents is not installed. Install with:[/red]")
print("\npip install praisonaiagents\n")
sys.exit(1)
# Get action and arguments from remaining args
action = unknown_args[0] if unknown_args else 'help'
action_args = unknown_args[1:] if len(unknown_args) > 1 else []
user_id = getattr(args, 'user_id', None)
self.handle_memory_command(action, action_args, user_id)
sys.exit(0)
elif args.command == 'rules':
if not PRAISONAI_AVAILABLE:
print("[red]ERROR: PraisonAI Agents is not installed. Install with:[/red]")
print("\npip install praisonaiagents\n")
sys.exit(1)
# Get action and arguments from remaining args
action = unknown_args[0] if unknown_args else 'list'
action_args = unknown_args[1:] if len(unknown_args) > 1 else []
self.handle_rules_command(action, action_args)
sys.exit(0)
elif args.command == 'workflow':
if not PRAISONAI_AVAILABLE:
print("[red]ERROR: PraisonAI Agents is not installed. Install with:[/red]")
print("\npip install praisonaiagents\n")
sys.exit(1)
# Get action and arguments from remaining args
action = unknown_args[0] if unknown_args else 'list'
action_args = unknown_args[1:] if len(unknown_args) > 1 else []
workflow_vars = {}
if getattr(args, 'workflow_var', None):
for var in args.workflow_var:
if '=' in var:
key, value = var.split('=', 1)
workflow_vars[key] = value
self.handle_workflow_command(action, action_args, workflow_vars)
sys.exit(0)
elif args.command == 'hooks':
if not PRAISONAI_AVAILABLE:
print("[red]ERROR: PraisonAI Agents is not installed. Install with:[/red]")
print("\npip install praisonaiagents\n")
sys.exit(1)
# Get action from remaining args
action = unknown_args[0] if unknown_args else 'list'
self.handle_hooks_command(action)
sys.exit(0)
# Only check framework availability for agent-related operations
if not args.command and (args.init or args.auto or args.framework):
if not CREWAI_AVAILABLE and not AUTOGEN_AVAILABLE and not PRAISONAI_AVAILABLE:
@@ -936,6 +1009,520 @@ class PraisonAI:
verbose = getattr(self.args, 'verbose', False)
return self._expand_prompt(prompt, expand_tools, verbose)
def _load_tools(self, tools_path: str) -> list:
"""
Load tools from a file path or comma-separated tool names.
Args:
tools_path: Path to tools.py file or comma-separated tool names
Returns:
List of tool functions
"""
tools_list = []
if not tools_path:
return tools_list
if os.path.isfile(tools_path):
# Load from file
try:
import inspect
spec = importlib.util.spec_from_file_location("tools_module", tools_path)
if spec and spec.loader:
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
for name, obj in inspect.getmembers(module):
if inspect.isfunction(obj) and not name.startswith('_'):
tools_list.append(obj)
if tools_list:
print(f"[cyan]Loaded {len(tools_list)} tools from {tools_path}[/cyan]")
except Exception as e:
print(f"[yellow]Warning: Failed to load tools from {tools_path}: {e}[/yellow]")
else:
# Treat as comma-separated tool names
try:
from praisonaiagents.tools import TOOL_MAPPINGS
import praisonaiagents.tools as tools_module
tool_names = [t.strip() for t in tools_path.split(',')]
for tool_name in tool_names:
if tool_name in TOOL_MAPPINGS:
try:
tool = getattr(tools_module, tool_name)
tools_list.append(tool)
except Exception as e:
print(f"[yellow]Warning: Failed to load tool '{tool_name}': {e}[/yellow]")
else:
print(f"[yellow]Warning: Unknown tool '{tool_name}'[/yellow]")
if tools_list:
print(f"[cyan]Loaded {len(tools_list)} built-in tools[/cyan]")
except ImportError:
print("[yellow]Warning: Could not import tools module[/yellow]")
return tools_list
def handle_memory_command(self, action: str, action_args: list, user_id: str = None):
"""
Handle memory subcommand actions.
Args:
action: The memory action (show, add, search, clear, save, resume, etc.)
action_args: Additional arguments for the action
user_id: User ID for memory isolation
"""
try:
from praisonaiagents.memory import FileMemory
from rich import print
from rich.table import Table
from rich.console import Console
console = Console()
memory = FileMemory(user_id=user_id or "default")
if action == 'show':
stats = memory.get_stats()
table = Table(title="Memory Statistics")
table.add_column("Property", style="cyan")
table.add_column("Value", style="green")
for key, value in stats.items():
table.add_row(str(key), str(value))
console.print(table)
# Show recent memories
print("\n[bold]Recent Short-term Memories:[/bold]")
short_term = memory.get_short_term(limit=5)
for i, item in enumerate(short_term, 1):
content = item.get('content', str(item))[:100]
print(f" {i}. {content}")
elif action == 'add':
if not action_args:
print("[red]ERROR: Content required. Usage: praisonai memory add \"Your memory content\"[/red]")
return
content = ' '.join(action_args)
memory.add_long_term(content, importance=0.8)
print(f"[green]✅ Added to long-term memory: {content[:50]}...[/green]")
elif action == 'search':
if not action_args:
print("[red]ERROR: Query required. Usage: praisonai memory search \"query\"[/red]")
return
query = ' '.join(action_args)
results = memory.search(query, limit=10)
print(f"[bold]Search results for '{query}':[/bold]")
for i, result in enumerate(results, 1):
content = result.get('content', str(result))[:100]
print(f" {i}. {content}")
elif action == 'clear':
target = action_args[0] if action_args else 'short'
if target == 'all':
memory.clear_all()
print("[green]✅ All memory cleared[/green]")
else:
memory.clear_short_term()
print("[green]✅ Short-term memory cleared[/green]")
elif action == 'save':
if not action_args:
print("[red]ERROR: Session name required. Usage: praisonai memory save <session_name>[/red]")
return
session_name = action_args[0]
memory.save_session(session_name)
print(f"[green]✅ Session saved: {session_name}[/green]")
elif action == 'resume':
if not action_args:
print("[red]ERROR: Session name required. Usage: praisonai memory resume <session_name>[/red]")
return
session_name = action_args[0]
memory.resume_session(session_name)
print(f"[green]✅ Session resumed: {session_name}[/green]")
elif action == 'sessions':
sessions = memory.list_sessions()
if sessions:
print("[bold]Saved Sessions:[/bold]")
for s in sessions:
print(f" - {s.get('name', 'Unknown')} (saved: {s.get('saved_at', 'Unknown')})")
else:
print("[yellow]No saved sessions found[/yellow]")
elif action == 'compress':
print("[cyan]Compressing short-term memory...[/cyan]")
# Note: compress requires an LLM function, so we'll just show stats
stats = memory.get_stats()
print(f"[green]Short-term items: {stats.get('short_term_count', 0)}[/green]")
print("[yellow]Note: Full compression requires an LLM. Use programmatically with memory.compress(llm_func=...)[/yellow]")
elif action == 'checkpoint':
name = action_args[0] if action_args else None
checkpoint_id = memory.create_checkpoint(name)
print(f"[green]✅ Checkpoint created: {checkpoint_id}[/green]")
elif action == 'restore':
if not action_args:
print("[red]ERROR: Checkpoint ID required. Usage: praisonai memory restore <checkpoint_id>[/red]")
return
checkpoint_id = action_args[0]
memory.restore_checkpoint(checkpoint_id)
print(f"[green]✅ Checkpoint restored: {checkpoint_id}[/green]")
elif action == 'checkpoints':
checkpoints = memory.list_checkpoints()
if checkpoints:
print("[bold]Checkpoints:[/bold]")
for cp in checkpoints:
print(f" - {cp.get('id', 'Unknown')} ({cp.get('name', 'Unnamed')})")
else:
print("[yellow]No checkpoints found[/yellow]")
elif action == 'help' or action == '--help':
print("[bold]Memory Commands:[/bold]")
print(" praisonai memory show - Show memory statistics")
print(" praisonai memory add <content> - Add to long-term memory")
print(" praisonai memory search <query> - Search memories")
print(" praisonai memory clear [short|all] - Clear memory")
print(" praisonai memory save <session_name> - Save session")
print(" praisonai memory resume <session_name> - Resume session")
print(" praisonai memory sessions - List saved sessions")
print(" praisonai memory compress - Compress short-term memory")
print(" praisonai memory checkpoint [name] - Create checkpoint")
print(" praisonai memory restore <checkpoint_id> - Restore checkpoint")
print(" praisonai memory checkpoints - List checkpoints")
print("\n[bold]Options:[/bold]")
print(" --user-id <id> - User ID for memory isolation")
else:
print(f"[red]Unknown memory action: {action}[/red]")
print("Use 'praisonai memory help' for available commands")
except ImportError as e:
print(f"[red]ERROR: Failed to import memory module: {e}[/red]")
print("Make sure praisonaiagents is installed: pip install praisonaiagents")
except Exception as e:
print(f"[red]ERROR: Memory command failed: {e}[/red]")
def handle_rules_command(self, action: str, action_args: list):
"""
Handle rules subcommand actions.
Args:
action: The rules action (list, show, create, delete, stats)
action_args: Additional arguments for the action
"""
try:
from praisonaiagents.memory import RulesManager
from rich import print
from rich.table import Table
from rich.console import Console
console = Console()
rules = RulesManager(workspace_path=os.getcwd())
if action == 'list':
all_rules = rules.get_all_rules()
if all_rules:
table = Table(title="Loaded Rules")
table.add_column("Name", style="cyan")
table.add_column("Description", style="white")
table.add_column("Activation", style="green")
table.add_column("Priority", style="yellow")
for rule in all_rules:
table.add_row(
rule.name,
rule.description[:50] + "..." if len(rule.description) > 50 else rule.description,
rule.activation,
str(rule.priority)
)
console.print(table)
else:
print("[yellow]No rules found. Create PRAISON.md, CLAUDE.md, or files in .praison/rules/[/yellow]")
elif action == 'show':
if not action_args:
print("[red]ERROR: Rule name required. Usage: praisonai rules show <name>[/red]")
return
rule_name = action_args[0]
rule = rules.get_rule_by_name(rule_name)
if rule:
print(f"[bold cyan]Rule: {rule.name}[/bold cyan]")
print(f"[bold]Description:[/bold] {rule.description}")
print(f"[bold]Activation:[/bold] {rule.activation}")
print(f"[bold]Priority:[/bold] {rule.priority}")
if rule.globs:
print(f"[bold]Globs:[/bold] {', '.join(rule.globs)}")
print(f"\n[bold]Content:[/bold]\n{rule.content}")
else:
print(f"[red]Rule not found: {rule_name}[/red]")
elif action == 'create':
if len(action_args) < 2:
print("[red]ERROR: Name and content required. Usage: praisonai rules create <name> <content>[/red]")
return
rule_name = action_args[0]
content = ' '.join(action_args[1:])
rules.create_rule(
name=rule_name,
content=content,
description=f"Rule created via CLI: {rule_name}",
activation="always",
scope="workspace"
)
print(f"[green]✅ Rule created: {rule_name}[/green]")
elif action == 'delete':
if not action_args:
print("[red]ERROR: Rule name required. Usage: praisonai rules delete <name>[/red]")
return
rule_name = action_args[0]
rules.delete_rule(rule_name)
print(f"[green]✅ Rule deleted: {rule_name}[/green]")
elif action == 'stats':
stats = rules.get_stats()
table = Table(title="Rules Statistics")
table.add_column("Property", style="cyan")
table.add_column("Value", style="green")
for key, value in stats.items():
if isinstance(value, dict):
table.add_row(str(key), str(value))
else:
table.add_row(str(key), str(value))
console.print(table)
elif action == 'help' or action == '--help':
print("[bold]Rules Commands:[/bold]")
print(" praisonai rules list - List all loaded rules")
print(" praisonai rules show <name> - Show specific rule details")
print(" praisonai rules create <name> <content> - Create a new rule")
print(" praisonai rules delete <name> - Delete a rule")
print(" praisonai rules stats - Show rules statistics")
print("\n[bold]Supported Rule Files:[/bold]")
print(" PRAISON.md, CLAUDE.md, AGENTS.md, GEMINI.md")
print(" .cursorrules, .windsurfrules")
print(" .praison/rules/*.md, ~/.praison/rules/*.md")
else:
print(f"[red]Unknown rules action: {action}[/red]")
print("Use 'praisonai rules help' for available commands")
except ImportError as e:
print(f"[red]ERROR: Failed to import rules module: {e}[/red]")
print("Make sure praisonaiagents is installed: pip install praisonaiagents")
except Exception as e:
print(f"[red]ERROR: Rules command failed: {e}[/red]")
def handle_workflow_command(self, action: str, action_args: list, variables: dict = None):
"""
Handle workflow subcommand actions.
Args:
action: The workflow action (list, run, create, show)
action_args: Additional arguments for the action
variables: Workflow variables for substitution
"""
try:
from praisonaiagents.memory import WorkflowManager
from rich import print
from rich.table import Table
from rich.console import Console
console = Console()
manager = WorkflowManager(workspace_path=os.getcwd())
if action == 'list':
workflows = manager.list_workflows()
if workflows:
table = Table(title="Available Workflows")
table.add_column("Name", style="cyan")
table.add_column("Description", style="white")
table.add_column("Steps", style="green")
for workflow in workflows:
table.add_row(
workflow.name,
workflow.description[:50] + "..." if len(workflow.description) > 50 else workflow.description,
str(len(workflow.steps))
)
console.print(table)
else:
print("[yellow]No workflows found. Create files in .praison/workflows/[/yellow]")
elif action == 'run':
if not action_args:
print("[red]ERROR: Workflow name required. Usage: praisonai workflow run <name>[/red]")
return
workflow_name = action_args[0]
# Create a simple executor using PraisonAgent
def executor(prompt):
agent = PraisonAgent(
name="WorkflowExecutor",
role="Task Executor",
goal="Execute workflow steps"
)
return agent.chat(prompt)
print(f"[bold cyan]Running workflow: {workflow_name}[/bold cyan]")
result = manager.execute(
workflow_name,
executor=executor,
variables=variables or {}
)
if result.get("success"):
print("[green]✅ Workflow completed successfully![/green]")
for step_result in result.get("results", []):
status = "✅" if step_result.get("status") == "completed" else "❌"
print(f" {status} {step_result.get('step', 'Unknown step')}")
else:
print(f"[red]❌ Workflow failed: {result.get('error', 'Unknown error')}[/red]")
elif action == 'show':
if not action_args:
print("[red]ERROR: Workflow name required. Usage: praisonai workflow show <name>[/red]")
return
workflow_name = action_args[0]
workflow = manager.get_workflow(workflow_name)
if workflow:
print(f"[bold cyan]Workflow: {workflow.name}[/bold cyan]")
print(f"[bold]Description:[/bold] {workflow.description}")
print(f"\n[bold]Steps ({len(workflow.steps)}):[/bold]")
for i, step in enumerate(workflow.steps, 1):
print(f" {i}. {step.name}: {step.action[:80]}...")
if workflow.variables:
print(f"\n[bold]Variables:[/bold] {workflow.variables}")
else:
print(f"[red]Workflow not found: {workflow_name}[/red]")
elif action == 'create':
if not action_args:
print("[red]ERROR: Workflow name required. Usage: praisonai workflow create <name>[/red]")
return
workflow_name = action_args[0]
# Create a simple template workflow
manager.create_workflow(
name=workflow_name,
description=f"Workflow created via CLI: {workflow_name}",
steps=[
{"name": "Step 1", "action": "First step - edit this in .praison/workflows/"},
{"name": "Step 2", "action": "Second step - edit this in .praison/workflows/"}
]
)
print(f"[green]✅ Workflow created: {workflow_name}[/green]")
print(f"[cyan]Edit the workflow in .praison/workflows/{workflow_name}.md[/cyan]")
elif action == 'help' or action == '--help':
print("[bold]Workflow Commands:[/bold]")
print(" praisonai workflow list - List available workflows")
print(" praisonai workflow run <name> - Execute a workflow")
print(" praisonai workflow show <name> - Show workflow details")
print(" praisonai workflow create <name> - Create a new workflow")
print("\n[bold]Options:[/bold]")
print(" --workflow-var key=value - Set workflow variable (can be repeated)")
print("\n[bold]Example:[/bold]")
print(" praisonai workflow run deploy --workflow-var environment=staging")
else:
print(f"[red]Unknown workflow action: {action}[/red]")
print("Use 'praisonai workflow help' for available commands")
except ImportError as e:
print(f"[red]ERROR: Failed to import workflow module: {e}[/red]")
print("Make sure praisonaiagents is installed: pip install praisonaiagents")
except Exception as e:
print(f"[red]ERROR: Workflow command failed: {e}[/red]")
def handle_hooks_command(self, action: str):
"""
Handle hooks subcommand actions.
Args:
action: The hooks action (list, stats, init)
"""
try:
from praisonaiagents.memory import HooksManager
from rich import print
from rich.table import Table
from rich.console import Console
import json
console = Console()
hooks = HooksManager(workspace_path=os.getcwd())
if action == 'list':
stats = hooks.get_stats()
if stats.get('total_hooks', 0) > 0:
print("[bold]Configured Hooks:[/bold]")
for event in stats.get('events', []):
print(f" - {event}")
else:
print("[yellow]No hooks configured. Create .praison/hooks.json[/yellow]")
elif action == 'stats':
stats = hooks.get_stats()
table = Table(title="Hooks Statistics")
table.add_column("Property", style="cyan")
table.add_column("Value", style="green")
for key, value in stats.items():
table.add_row(str(key), str(value))
console.print(table)
elif action == 'init':
hooks_dir = os.path.join(os.getcwd(), ".praison")
os.makedirs(hooks_dir, exist_ok=True)
hooks_file = os.path.join(hooks_dir, "hooks.json")
if os.path.exists(hooks_file):
print(f"[yellow]hooks.json already exists at {hooks_file}[/yellow]")
else:
template = {
"enabled": True,
"timeout": 30,
"hooks": {
"pre_write_code": "./scripts/lint.sh",
"post_write_code": "./scripts/format.sh",
"pre_run_command": {
"command": "./scripts/validate.sh",
"timeout": 60,
"block_on_failure": True
}
}
}
with open(hooks_file, 'w') as f:
json.dump(template, f, indent=2)
print(f"[green]✅ Created hooks.json at {hooks_file}[/green]")
print("[cyan]Edit the file to configure your hooks[/cyan]")
elif action == 'help' or action == '--help':
print("[bold]Hooks Commands:[/bold]")
print(" praisonai hooks list - List configured hooks")
print(" praisonai hooks stats - Show hooks statistics")
print(" praisonai hooks init - Create hooks.json template")
print("\n[bold]Hook Events:[/bold]")
print(" pre_read_code, post_read_code")
print(" pre_write_code, post_write_code")
print(" pre_run_command, post_run_command")
print(" pre_user_prompt, post_user_prompt")
print(" pre_mcp_tool_use, post_mcp_tool_use")
else:
print(f"[red]Unknown hooks action: {action}[/red]")
print("Use 'praisonai hooks help' for available commands")
except ImportError as e:
print(f"[red]ERROR: Failed to import hooks module: {e}[/red]")
print("Make sure praisonaiagents is installed: pip install praisonaiagents")
except Exception as e:
print(f"[red]ERROR: Hooks command failed: {e}[/red]")
def _save_output(self, prompt: str, result: str):
"""
Save output to output/prompts/ folder.
@@ -994,6 +1581,37 @@ class PraisonAI:
agent_config["web_fetch"] = True
if getattr(self.args, 'prompt_caching', False):
agent_config["prompt_caching"] = True
# Planning Mode
if getattr(self.args, 'planning', False):
agent_config["planning"] = True
print("[bold cyan]Planning mode enabled - agent will create a plan before execution[/bold cyan]")
# Load planning tools if specified
if getattr(self.args, 'planning_tools', None):
planning_tools_list = self._load_tools(self.args.planning_tools)
if planning_tools_list:
agent_config["planning_tools"] = planning_tools_list
if getattr(self.args, 'planning_reasoning', False):
agent_config["planning_reasoning"] = True
# Memory
if getattr(self.args, 'memory', False):
agent_config["memory"] = True
print("[bold cyan]Memory enabled - agent will remember context across sessions[/bold cyan]")
if getattr(self.args, 'user_id', None):
agent_config["user_id"] = self.args.user_id
# Claude Memory Tool (Anthropic only)
if getattr(self.args, 'claude_memory', False):
llm = getattr(self.args, 'llm', '')
if llm and 'anthropic' in llm.lower():
agent_config["claude_memory"] = True
print("[bold cyan]Claude Memory Tool enabled - Claude will autonomously manage memories[/bold cyan]")
else:
print("[yellow]Warning: --claude-memory requires an Anthropic model (--llm anthropic/...)[/yellow]")
agent = PraisonAgent(**agent_config)
result = agent.start(prompt)
+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.6 gunicorn markdown\n")
file.write("RUN pip install flask praisonai==2.3.10 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.6"
__version__ = "2.3.10"
+1 -1
View File
@@ -12,7 +12,7 @@ dependencies = [
"rich>=13.7",
"markdown>=3.5",
"pyparsing>=3.0.0",
"praisonaiagents>=0.0.184",
"praisonaiagents>=0.0.189",
"python-dotenv>=0.19.0",
"instructor>=1.3.3",
"PyYAML>=6.0",
+1 -1
View File
@@ -365,7 +365,7 @@ This is **normal and expected** during development. The key metrics are:
2. **Mocking**: Mock external dependencies and APIs
3. **Naming**: Clear, descriptive test names
4. **Documentation**: Document complex test scenarios
5. **Performance**: Keep unit tests fast (<1s each)
5. **Performance**: Keep unit tests fast (under 1s each)
6. **Coverage**: Aim for high coverage of critical paths
7. **Maintainability**: Regular test maintenance and updates
+179
View File
@@ -0,0 +1,179 @@
#!/usr/bin/env python3
"""
Tests for new CLI features (fast, direct import, no subprocess):
- Planning Mode
- Memory CLI
- Rules CLI
- Workflow CLI
- Hooks CLI
- Claude Memory Tool
"""
import argparse
class TestCLIArgumentParsing:
"""Test CLI argument parsing directly (fast, no subprocess)."""
def get_parser(self):
"""Create argument parser matching CLI."""
parser = argparse.ArgumentParser(description="praisonAI CLI")
# Planning args
parser.add_argument("--planning", action="store_true")
parser.add_argument("--planning-tools")
parser.add_argument("--planning-reasoning", action="store_true")
parser.add_argument("--auto-approve-plan", action="store_true")
# Memory args
parser.add_argument("--memory", action="store_true")
parser.add_argument("--user-id")
# Rules args
parser.add_argument("--include-rules")
# Workflow args
parser.add_argument("--workflow-var", action="append")
# Claude memory
parser.add_argument("--claude-memory", action="store_true")
parser.add_argument("command", nargs="?")
return parser
def test_planning_args_parsed(self):
"""Test planning arguments are parsed correctly."""
parser = self.get_parser()
args = parser.parse_args(["--planning", "--planning-reasoning", "test"])
assert args.planning is True
assert args.planning_reasoning is True
assert args.command == "test"
def test_planning_tools_parsed(self):
"""Test planning-tools argument is parsed."""
parser = self.get_parser()
args = parser.parse_args(["--planning", "--planning-tools", "tools.py", "test"])
assert args.planning is True
assert args.planning_tools == "tools.py"
def test_memory_args_parsed(self):
"""Test memory arguments are parsed correctly."""
parser = self.get_parser()
args = parser.parse_args(["--memory", "--user-id", "user123", "test"])
assert args.memory is True
assert args.user_id == "user123"
def test_rules_args_parsed(self):
"""Test rules arguments are parsed correctly."""
parser = self.get_parser()
args = parser.parse_args(["--include-rules", "security,testing", "test"])
assert args.include_rules == "security,testing"
def test_workflow_vars_parsed(self):
"""Test workflow-var arguments are parsed correctly."""
parser = self.get_parser()
args = parser.parse_args([
"--workflow-var", "env=prod",
"--workflow-var", "branch=main",
"test"
])
assert args.workflow_var == ["env=prod", "branch=main"]
def test_claude_memory_parsed(self):
"""Test claude-memory argument is parsed correctly."""
parser = self.get_parser()
args = parser.parse_args(["--claude-memory", "test"])
assert args.claude_memory is True
def test_auto_approve_plan_parsed(self):
"""Test auto-approve-plan argument is parsed correctly."""
parser = self.get_parser()
args = parser.parse_args(["--planning", "--auto-approve-plan", "test"])
assert args.auto_approve_plan is True
class TestMemoryModule:
"""Test FileMemory module directly."""
def test_file_memory_import(self):
"""Test FileMemory can be imported."""
try:
from praisonaiagents.memory import FileMemory
assert FileMemory is not None
except ImportError:
pass # Skip if not installed
def test_file_memory_basic(self, tmp_path):
"""Test FileMemory basic operations."""
try:
from praisonaiagents.memory import FileMemory
memory = FileMemory(user_id="test_user")
# Test add
memory.add_long_term("Test fact")
# Test stats
stats = memory.get_stats()
assert stats["user_id"] == "test_user"
assert stats["long_term_count"] >= 1
except (ImportError, TypeError):
pass # Skip if not installed or API changed
class TestRulesModule:
"""Test RulesManager module directly."""
def test_rules_manager_import(self):
"""Test RulesManager can be imported."""
try:
from praisonaiagents.memory import RulesManager
assert RulesManager is not None
except ImportError:
pass # Skip if not installed
def test_rules_manager_basic(self):
"""Test RulesManager basic operations."""
try:
from praisonaiagents.memory import RulesManager
manager = RulesManager()
# Test stats
stats = manager.get_stats()
assert "total_rules" in stats
except ImportError:
pass # Skip if not installed
class TestWorkflowModule:
"""Test WorkflowManager module directly."""
def test_workflow_manager_import(self):
"""Test WorkflowManager can be imported."""
try:
from praisonaiagents.memory import WorkflowManager
assert WorkflowManager is not None
except ImportError:
pass # Skip if not installed
class TestHooksModule:
"""Test HooksManager module directly."""
def test_hooks_manager_import(self):
"""Test HooksManager can be imported."""
try:
from praisonaiagents.memory import HooksManager
assert HooksManager is not None
except ImportError:
pass # Skip if not installed
def test_hooks_manager_basic(self):
"""Test HooksManager basic operations."""
try:
from praisonaiagents.memory import HooksManager
hooks = HooksManager()
# Test stats
stats = hooks.get_stats()
assert "total_hooks" in stats
except ImportError:
pass # Skip if not installed
if __name__ == "__main__":
import pytest
pytest.main([__file__, "-v"])
+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.184" },
{ name = "praisonaiagents", specifier = ">=0.0.189" },
{ 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.184"
version = "0.0.189"
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/98/56/219e1f1eca6f0d521822f83fda970b44decba97dd49346c75cba00684bce/praisonaiagents-0.0.184.tar.gz", hash = "sha256:c09d036a35c5e34a3a40ffee01b94fa50ca6f6cc07f4a67a52ea1cbba42c139e", size = 412203, upload-time = "2025-12-15T22:13:47.78Z" }
sdist = { url = "https://files.pythonhosted.org/packages/62/ed/bd468ae67dea1489590fb306d50b02fbe2da5462745b2c8519a095b81fc1/praisonaiagents-0.0.189.tar.gz", hash = "sha256:a788b540b2b1f3418fb98013f1b2aba92d3010797d852507325735e2eb3fd944", size = 461258, upload-time = "2025-12-16T09:47:12.238Z" }
wheels = [
{ url = "https://files.pythonhosted.org/packages/93/a5/ffa998f4b99b763f9ff1d2ecc0a96b9669747b36563d9e453be3d4f9df3a/praisonaiagents-0.0.184-py3-none-any.whl", hash = "sha256:6aaa099cf181a9b7ac2ff446a6658ae8e2b687d55dc68a89a8eaa3f8ff1a0705", size = 398575, upload-time = "2025-12-15T22:13:46.253Z" },
{ url = "https://files.pythonhosted.org/packages/3a/70/2ea1d2a8baa02ae5a277616bbcc2f70c8a2a25a068d4cd5a8083eaf759cb/praisonaiagents-0.0.189-py3-none-any.whl", hash = "sha256:158693b6c68b9d23df64ee092648992222718279c47cac7b96e1007bf869ad8c", size = 437480, upload-time = "2025-12-16T09:47:09.846Z" },
]
[[package]]