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.

Diagram

MCP Architecture

🤖 Agent

🔌 MCP Client

🖥️ MCP Server

🔧 Tools

Quick Start

  1. Install MCP Support
    bash
    pip install praisonaiagents[mcp]
  2. 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

Diagram

MCP Transports

Local servers

Legacy HTTP

Modern HTTP

Real-time

📟 stdio
Subprocess

📡 SSE
Server-Sent Events

🌐 HTTP Stream
Current Standard

🔗 WebSocket
SEP-1288

Use Case

TransportURL PatternUse Case
stdioCommand stringLocal subprocess servers
SSEhttp://...​/sseLegacy HTTP+SSE servers
HTTP Streamhttp://...Modern HTTP servers
WebSocketws://... or wss://...Real-time bidirectional

Connection Methods

Stdio (Subprocess)

python
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

python
# SSE endpoint (legacy)
tools = MCP("http://localhost:8080/sse")

# HTTP Stream (modern)
tools = MCP("http://localhost:8080/mcp")

WebSocket

python
# WebSocket connection
tools = MCP("wss://mcp.example.com/ws")

# With authentication
tools = MCP(
    "wss://mcp.example.com/ws",
    auth_token="your-token"
)

Configuration Options

python
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
)
OptionTypeDefaultDescription
command_or_stringstrRequiredCommand, URL, or command string
argslistNoneArguments for stdio transport
timeoutint60Timeout in seconds
debugboolFalseEnable debug logging
auth_tokenstrNoneAuthentication token (WebSocket)

How It Works

Diagram
MCP ServerMCP ClientAgentMCP ServerMCP ClientAgentInitializeConnect (stdio/HTTP/WS)List toolsTools availableCall toolExecute toolResultTool result

Tool Discovery

MCP automatically discovers tools from the server:

python
from praisonaiagents.mcp import MCP

mcp = MCP("python server.py")

# Tools are automatically available
print(mcp.tools)  # List of callable functions

Tool Execution

Tools are executed through the MCP protocol:

python
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:

python
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)

python
# 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

python
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.


Related