## Introduction
In modern microservices architectures, coordinating multiple AI agents efficiently is a challenge. Services often need to invoke specialized Claude-powered agents for tasks like data processing, validation, or external API calls, but direct integration can lead to tight coupling and scalability issues. Enter **Model Context Protocol (MCP) servers**—Anthropic's extensible tool ecosystem that allows Claude models (Opus, Sonnet, Haiku) to interact with remote tools and services dynamically.
This guide provides a step-by-step approach to using MCP servers for microservices orchestration. We'll build a multi-tool agent system where Claude agents communicate via MCP to handle a real-world e-commerce order fulfillment workflow. Expect architecture diagrams, code examples, and best practices tailored for developers and teams using the Claude API.
By the end, you'll have a production-ready setup for scaling AI agents across services.
## Understanding MCP Servers
MCP servers act as intermediaries between Claude and external tools or services. They expose a standardized protocol for Claude to discover, authenticate, and invoke tools remotely. Key benefits for microservices:
- **Decoupling**: Agents call tools via MCP without knowing implementation details.
- **Scalability**: Deploy MCP servers per microservice for horizontal scaling.
- **Security**: Token-based auth and scoped permissions.
- **Multi-model support**: Works with Opus for complex reasoning, Sonnet for speed, Haiku for lightweight tasks.
MCP leverages Claude's native tool-use capabilities (via XML-tagged function calls in prompts). When Claude requests a tool, the MCP server handles execution and returns results in the conversation context.
## Architecture Overview
Our example orchestrates three microservices:
1. **Order Service**: Validates incoming orders (Claude Haiku agent).
2. **Inventory Service**: Checks stock (Claude Sonnet agent with DB tools).
3. **Fulfillment Service**: Generates shipping labels (Claude Opus agent with external APIs).
A central **Orchestrator MCP Server** coordinates agents via pub/sub or direct MCP calls.
Here's a Mermaid diagram of the architecture:
```mermaid
graph TD
A[Client Request] --> B[Orchestrator MCP Server]
B --> C[Order Agent<br/>(Claude Haiku)]
B --> D[Inventory Agent<br/>(Claude Sonnet)]
B --> E[Fulfillment Agent<br/>(Claude Opus)]
C --> F[Order DB]
D --> G[Inventory DB]
E --> H[Shipping API]
B -.->|MCP Protocol| C
B -.->|MCP Protocol| D
B -.->|MCP Protocol| E
style B fill:#f9f
```
The Orchestrator uses Claude's API to plan workflows, then delegates via MCP endpoints.
## Prerequisites
- Node.js 18+ or Python 3.10+
- Claude API key (from console.anthropic.com)
- Docker for service isolation
- Familiarity with Claude's Messages API and tool definitions
Install Claude Code CLI for local development:
```bash
npm install -g @anthropic-ai/claude-code
claude-code init
```
## Step 1: Setting Up an MCP Server
MCP servers are lightweight HTTP servers exposing `/tools` and `/invoke` endpoints. Use the official MCP SDK.
### Node.js Example
Create `mcp-server.js`:
```javascript
import express from 'express';
import { Anthropic } from '@anthropic-ai/sdk';
const app = express();
app.use(express.json());
const claude = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
// Tool discovery endpoint
app.get('/tools', (req, res) => {
res.json([
{
name: 'validate_order',
description: 'Validate order details',
inputSchema: {
type: 'object',
properties: {
order: { type: 'object' }
}
}
}
]);
});
// Tool invocation
app.post('/invoke/:tool', async (req, res) => {
const { tool } = req.params;
if (tool === 'validate_order') {
const { order } = req.body;
const msg = await claude.messages.create({
model: 'claude-3-haiku-20240307',
max_tokens: 1024,
tools: [{ name: 'order_validator', ... }],
messages: [{ role: 'user', content: `Validate: ${JSON.stringify(order)}` }]
});
res.json({ result: msg.content[0].text });
}
});
app.listen(3000, () => console.log('MCP Server on port 3000'));
```
Run with `node mcp-server.js`.
For Python, use `anthropic` SDK similarly.
## Step 2: Building Claude Agents
Each microservice runs its own Claude agent via an MCP server.
### Inventory Agent (Sonnet)
Define tools for DB queries:
```json
{
"name": "check_inventory",
"description": "Check stock levels",
"inputSchema": {
"type": "object",
"properties": {
"sku": { "type": "string" },
"quantity": { "type": "integer" }
}
}
}
```
Agent prompt template:
```xml
<system>
You are an inventory agent. Use tools to check stock and respond with availability.
</system>
<user>Check SKU: ABC123, qty: 5</user>
```
Integrate with PostgreSQL via a custom tool handler in the MCP server.
## Step 3: Orchestrator Implementation
The orchestrator is a Claude Opus-powered service that sequences agents.
```javascript
// orchestrator.js
import { Anthropic } from '@anthropic-ai/sdk';
const client = new Anthropic();
async function orchestrateOrder(order) {
let context = `Order: ${JSON.stringify(order)}`;
// Step 1: Validate
const validateRes = await invokeMCP('http://order-service:3000', 'validate_order', { order });
context += `\
Validation: ${validateRes.result}`;
// Step 2: Inventory
const invRes = await invokeMCP('http://inventory-service:3000', 'check_inventory', { sku: order.sku, quantity: order.qty });
context += `\
Inventory: ${invRes.result}`;
if (invRes.result.available) {
// Step 3: Fulfill
const fulfillRes = await invokeMCP('http://fulfill-service:3000', 'generate_label', { order });
context += `\
Fulfillment: ${fulfillRes.result}`;
}
return context;
}
async function invokeMCP(baseUrl, tool, input) {
const res = await fetch(`${baseUrl}/invoke/${tool}`, {
method: 'POST',
headers: { 'Authorization': `Bearer ${process.env.MCP_TOKEN}`, 'Content-Type': 'application/json' },
body: JSON.stringify(input)
});
return res.json();
}
```
Feed the full context back to Claude for final decision-making:
```javascript
const finalMsg = await client.messages.create({
model: 'claude-3-opus-20240229',
max_tokens: 2000,
messages: [{ role: 'user', content: context }]
});
```
## Step 4: Deployment with Docker Compose
Scale with Docker:
```yaml
version: '3'
services:
order-mcp:
build: ./order-service
ports: ['3000:3000']
inventory-mcp:
build: ./inventory-service
ports: ['3001:3001']
orchestrator:
build: ./orchestrator
ports: ['8080:8080']
depends_on: [order-mcp, inventory-mcp]
```
Deploy to Kubernetes for production, using Horizontal Pod Autoscaler tied to Claude token usage.
## Real-World Example: E-Commerce Workflow
Simulate an order:
```bash
curl -X POST http://localhost:8080/orchestrate \
-H 'Content-Type: application/json' \
-d '{"sku":"ABC123","qty":5,"customer":"John Doe"}'
```
Output: Sequential results from each agent, culminating in a shipping label URL.
## Best Practices
- **Prompt Engineering**: Use XML tags for tools; chain-of-thought for orchestration.
- **Error Handling**: Implement retries in MCP invoke with exponential backoff.
- **Monitoring**: Log Claude token usage with Prometheus; alert on high latency.
- **Security**: Use short-lived JWTs for MCP auth; scope tools to least privilege.
- **Cost Optimization**: Route simple tasks to Haiku, complex to Opus.
- **Testing**: Use Claude Code CLI for local prompt testing: `claude-code run workflow.prompt`.
| Model | Use Case | Avg Latency | Cost/Token |
|-------|----------|-------------|------------|
| Haiku | Validation | 200ms | Low |
| Sonnet | Inventory | 800ms | Medium |
| Opus | Orchestration | 2s | High |
## Advanced: AI Agents with n8n Integration
Extend with no-code: Connect Orchestrator to n8n workflows via webhooks. Trigger MCP calls from Slack or Zapier for hybrid setups.
Example n8n node:
```json
{
"name": "Claude MCP",
"type": "n8n-nodes-base.httpRequest",
"url": "http://orchestrator:8080/invoke"
}
```
## Conclusion
MCP servers transform Claude into a powerhouse for microservices orchestration, enabling resilient, scalable AI agents. Start with the code above, iterate with your stack, and unlock enterprise-grade automation. For more, check Anthropic's MCP docs and Claude Directory's agent playbooks.
*Word count: ~1450*