Skip to main content

Module praisonaiagents.mcp

The MCP (Model Context Protocol) module provides integration with MCP servers, enabling AI agents to interact with external tools and services through standardized protocols with support for both STDIO and SSE transports.

Classes

MCP

The main class for connecting to and interacting with MCP servers.

Parameters

  • command: str - Command to execute or SSE endpoint URL
  • args: Optional[List[str]] = None - Arguments for the command
  • env: Optional[Dict[str, str]] = None - Environment variables
  • debug: bool = False - Enable debug logging
  • timeout: int = 60 - Timeout for server initialization

Properties

  • tools - Dictionary of available tools from the MCP server
  • tool_functions - Dictionary of callable wrapper functions

Methods

  • get_tools() - Get list of available tools with schemas
  • get_openai_tools_schema() - Get OpenAI function calling compatible schema
  • execute_tool(tool_name, arguments) - Execute a specific tool
  • shutdown() - Gracefully shutdown the MCP connection

SSEMCPClient

Client for Server-Sent Events (SSE) based MCP servers.

Parameters

  • url: str - SSE endpoint URL
  • debug: bool = False - Enable debug logging

Methods

  • connect() - Establish SSE connection
  • disconnect() - Close SSE connection
  • list_tools() - Get available tools
  • call_tool(name, arguments) - Execute a tool

Transport Mechanisms

STDIO Transport

For local MCP servers using subprocess communication.

SSE Transport

For remote MCP servers accessible via HTTP.

Usage Examples

Basic MCP Integration

Agent Integration

Custom Python MCP Server

SSE-Based MCP Server

Environment Variables

Error Handling

OpenAI Function Calling Integration

The MCP module automatically generates OpenAI-compatible function schemas:

Configuration Options

Debug Mode

Custom Timeout

Working Directory

Best Practices

  1. Resource Management - Always call shutdown() when done
  2. Error Handling - Wrap MCP operations in try-except blocks
  3. Timeout Configuration - Adjust timeout for slow servers
  4. Environment Isolation - Use env parameter for sensitive data
  5. Debug Logging - Enable debug mode during development
  6. Schema Validation - Validate tool schemas before use

Common MCP Servers

  • @modelcontextprotocol/server-filesystem - File system operations
  • @modelcontextprotocol/server-github - GitHub API integration
  • @modelcontextprotocol/server-postgres - PostgreSQL database access
  • @modelcontextprotocol/server-slack - Slack integration
  • @modelcontextprotocol/server-memory - Persistent memory storage