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 URLargs: Optional[List[str]] = None
- Arguments for the commandenv: Optional[Dict[str, str]] = None
- Environment variablesdebug: bool = False
- Enable debug loggingtimeout: int = 60
- Timeout for server initialization
Properties
tools
- Dictionary of available tools from the MCP servertool_functions
- Dictionary of callable wrapper functions
Methods
get_tools()
- Get list of available tools with schemasget_openai_tools_schema()
- Get OpenAI function calling compatible schemaexecute_tool(tool_name, arguments)
- Execute a specific toolshutdown()
- Gracefully shutdown the MCP connection
SSEMCPClient
Client for Server-Sent Events (SSE) based MCP servers.Parameters
url: str
- SSE endpoint URLdebug: bool = False
- Enable debug logging
Methods
connect()
- Establish SSE connectiondisconnect()
- Close SSE connectionlist_tools()
- Get available toolscall_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
- Resource Management - Always call
shutdown()
when done - Error Handling - Wrap MCP operations in try-except blocks
- Timeout Configuration - Adjust timeout for slow servers
- Environment Isolation - Use env parameter for sensitive data
- Debug Logging - Enable debug mode during development
- 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