Agent MCP Integration
Connect agents to external tools and services via Model Context Protocol
MCP (Model Context Protocol) enables agents to connect to external tool servers using standardized transports – stdio, SSE, HTTP stream, or WebSocket.
Quick Start
- Install MCP Support
bash pip install praisonaiagents[mcp] - Connect to MCP Server
python from praisonaiagents import Agent from praisonaiagents.mcp import MCP # Connect to an MCP server agent = Agent( instructions="You are a helpful assistant", tools=MCP("python /path/to/mcp_server.py") ) agent.start("Use the available tools to help me")
Transport Types
| Transport | URL Pattern | Use Case |
|---|---|---|
| stdio | Command string | Local subprocess servers |
| SSE | http://.../sse | Legacy HTTP+SSE servers |
| HTTP Stream | http://... | Modern HTTP servers |
| WebSocket | ws://... or wss://... | Real-time bidirectional |
Connection Methods
Stdio (Subprocess)
from praisonaiagents.mcp import MCP
# Method 1: Single command string
tools = MCP("python /path/to/server.py")
# Method 2: Separate command and args
tools = MCP(
command="/usr/bin/python",
args=["/path/to/server.py"]
)
# Method 3: NPX for npm packages
tools = MCP("npx -y @anthropic/mcp-server-fetch")HTTP/SSE
# SSE endpoint (legacy)
tools = MCP("http://localhost:8080/sse")
# HTTP Stream (modern)
tools = MCP("http://localhost:8080/mcp")WebSocket
# WebSocket connection
tools = MCP("wss://mcp.example.com/ws")
# With authentication
tools = MCP(
"wss://mcp.example.com/ws",
auth_token="your-token"
)Configuration Options
from praisonaiagents.mcp import MCP
mcp = MCP(
command_or_string="python server.py", # Command or URL
args=None, # Arguments (for stdio)
timeout=60, # Initialization timeout
debug=False, # Enable debug logging
# HTTP Stream options
responseMode="streaming", # Response mode
headers={"Authorization": "Bearer ..."},
# WebSocket options
auth_token="token", # Auth token
)| Option | Type | Default | Description |
|---|---|---|---|
command_or_string | str | Required | Command, URL, or command string |
args | list | None | Arguments for stdio transport |
timeout | int | 60 | Timeout in seconds |
debug | bool | False | Enable debug logging |
auth_token | str | None | Authentication token (WebSocket) |
How It Works
Tool Discovery
MCP automatically discovers tools from the server:
from praisonaiagents.mcp import MCP
mcp = MCP("python server.py")
# Tools are automatically available
print(mcp.tools) # List of callable functionsTool Execution
Tools are executed through the MCP protocol:
agent = Agent(
instructions="You can search the web",
tools=MCP("npx -y @anthropic/mcp-server-fetch")
)
# Agent automatically uses MCP tools
agent.start("Fetch the content from example.com")Creating MCP Servers
Expose your tools as an MCP server:
from praisonaiagents.mcp import ToolsMCPServer, launch_tools_mcp_server
def my_tool(query: str) -> str:
"""Search for information."""
return f"Results for: {query}"
# Create and launch server
server = ToolsMCPServer(tools=[my_tool])
launch_tools_mcp_server(server)Common Patterns
NPX Servers (Smithery)
# Fetch server
tools = MCP("npx -y @anthropic/mcp-server-fetch")
# Filesystem server
tools = MCP("npx -y @anthropic/mcp-server-filesystem /path/to/dir")
# GitHub server
tools = MCP("npx -y @anthropic/mcp-server-github")Multi-Server Setup
from praisonaiagents import Agent
from praisonaiagents.mcp import MCP
# Combine tools from multiple servers
fetch_tools = MCP("npx -y @anthropic/mcp-server-fetch")
fs_tools = MCP("npx -y @anthropic/mcp-server-filesystem .")
agent = Agent(
instructions="You can fetch web content and manage files",
tools=fetch_tools + fs_tools # Combine tool lists
)Best Practices
Use appropriate transport
- stdio: Best for local tools, simple setup
- HTTP Stream: Best for remote servers, modern standard
- WebSocket: Best for real-time, bidirectional communication
Set reasonable timeouts
Increase timeout for slow-starting servers or network latency.
Enable debug for troubleshooting
Use debug=True to see MCP protocol messages when debugging.
Handle server availability
MCP servers may not always be available. Handle initialization failures gracefully.