Skip to main content
The Model Context Protocol (MCP) is a standardized protocol that enables AI agents to interact with external tools and services through MCP servers. Swarms provides first-class support for MCP integration, allowing your agents to dynamically discover and execute tools.

What is MCP?

MCP (Model Context Protocol) provides:
  • Standardized Tool Interface: Unified protocol for tool integration
  • Dynamic Discovery: Automatically discover available tools from MCP servers
  • Multi-Server Support: Connect to multiple MCP servers simultaneously
  • Type Safety: Automatic schema validation for tool calls
  • Flexible Transport: Support for HTTP, WebSocket, and stdio transports

Quick Start

Basic MCP Integration

Connect an agent to an MCP server:

Async Tool Loading

For async environments, use the async API:

MCP Connection Configuration

Using MCPConnection Object

For advanced configuration, use the MCPConnection schema:

Transport Options

Swarms supports multiple transport protocols:

Multi-Server Integration

Connect to Multiple MCP Servers

Fetch tools from multiple servers simultaneously:

Execute Tools Across Multiple Servers

Real-World Example

Building a Multi-Tool Agent

Here’s a complete example from the source code:

Tool Execution

Execute Individual Tools

Batch Tool Execution

Error Handling

Retry Logic and Timeouts

The MCP client includes automatic retry with exponential backoff:

Custom Error Handling

Advanced Features

Auto-Detect Transport

The client automatically detects the appropriate transport from the URL:

Tool Schema Transformation

Convert between MCP and OpenAI tool formats:

Best Practices

Connection Pooling

Reuse MCP connections when fetching tools multiple times

Timeout Configuration

Set appropriate timeouts based on server response times

Error Recovery

Implement fallback strategies for MCP server failures

Verbose Logging

Enable verbose mode during development for debugging

Troubleshooting

Common Issues

Connection Timeouts
Authentication Failures
Tool Not Found

Next Steps

Model Providers

Configure different LLM providers

Custom Tools

Create your own tool integrations