What This Tutorial Builds
Acargo run binary that sends one research question to a swarm of three agents — a researcher, a skeptic, and an editor — and prints each agent’s contribution plus the run’s cost and wall time. Along the way:
- Connecting to the hosted MCP server with
rmcp, the official Rust MCP SDK - Sending credentials as transport headers rather than tool arguments
- Deserializing the tool result into typed structs with
serde, notValueindexing - Choosing a swarm architecture for the shape of your problem
Why a Swarm Instead of One Agent
A single agent with a long prompt tends to agree with itself. Asking one model to “research this, then critique your own research, then edit it” produces a fluent answer that has never actually been challenged. A swarm makes the critique a separate call with a separate system prompt and no stake in the earlier output.SequentialWorkflow passes each agent’s output to the next, so the skeptic sees the research and the editor sees both.
No installation required. The Swarms MCP server is hosted at
https://mcp.swarms.world/mcp. You need an API key from the API Keys page.Step 1: Create the Project
Cargo.toml:
rmcp features matter:
Step 2: Connect
rmcp builds a transport from a config, then serve turns it into a running client. The unit type () is a valid client handler when you only make requests and do not handle server-initiated calls.
main — the signature of a running rmcp service is awkward to name, and inlining avoids fighting it.
custom_headers takes a HashMap<HeaderName, HeaderValue>. Both sides come from .parse(), which is why the ? operators are there — a malformed header name is a runtime error, not a compile error.Step 3: Define the Result Types
This is where Rust pays off. Instead of indexing into aValue and hoping, describe the response once and let serde enforce it.
Step 4: Design the Swarm
Three agents, each with a job the next one cannot do for itself:SequentialWorkflow runs these in array order and feeds each output forward. Other architectures change that wiring:
The full list is in Available Architectures.
Step 5: Call the Tool
CallToolRequestParams is #[non_exhaustive], so build it with new plus with_arguments rather than a struct literal.
is_error: Some(true) with the detail in content, not as an Err:
Step 6: The Complete Program
src/main.rs:
output has role User and echoes your task — the agents follow after it, in execution order.
Step 7: Discovering Tools at Runtime
list_tools takes an Option<PaginatedRequestParams>, so Default::default() gives you the unpaginated call:
Common Errors
Next Steps
- Build an Agent CLI over MCP (TypeScript) — the single-agent version
- Run a Batch Pipeline over MCP (Python) — many records at once
- Available Architectures — every
swarm_typeexplained