Skip to main content

What This Tutorial Builds

A cargo 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, not Value indexing
  • 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:
The three 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.
In the finished program below this is inlined into 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 a Value and hoping, describe the response once and let serde enforce it.
Fields you do not declare are ignored, so you can start with the four you care about and grow the struct as you need more.

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.
Then check for failure before touching the payload. An upstream error — a bad key, a 429, a 422 — arrives as is_error: Some(true) with the detail in content, not as an Err:

Step 6: The Complete Program

src/main.rs:
Run it:
The first entry in 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

Every swarm run is billed per agent plus tokens. A three-agent SequentialWorkflow costs roughly three times a single agent call on the same task. Read usage.billing_info.total_cost on every response rather than estimating.

Next Steps