Production-grade guide to mcp server architecture covering architecture patterns, implementation strategies, testing approaches, and operational best practices for enterprise engineering teams.
Mcp Server Architecture is a scalable, modular, event-driven framework for hosting AI agents that communicate through a central message control plane. It matters when agents must coordinate across services, share state, and react to real-time signals—such as in a customer support pipeline, autonomous data processing, or real-time collaboration systems—where consistency, latency, and composability are critical.
The Mcp Server Architecture orchestrates AI agents through a message-based control plane that decouples agent logic from execution environments. Agents register with a central broker, publish events, subscribe to topics, and process messages in a persistent, ordered, and fault-tolerant way. The architecture assumes a single source of truth for agent state, a shared event log, and a predictable lifecycle for agent execution.
Key components include:
Agents are deployed as containerized services, each running a single entrypoint binary. The agent starts by registering with the control plane via a POST /agents/register request.
POST /agents/register HTTP/1.1
Host: mcp-control-plane.example.com
Content-Type: application/json
{
"agent_id": "support-agent-01",
"image": "registry.example.com/support-agent:v3.2",
"entrypoint": "main.py",
"config": {
"max_concurrent_tasks": 16,
"timeout_seconds": 300,
"initial_state": {
"status": "idle",
"last_seen": "2024-04-05T10:00:00Z"
}
}
}
The control plane responds with a 201 Created and assigns a unique agent_id and registration_token. The agent uses this token to authenticate future requests.
When an agent fails to heartbeat (GET /agents/heartbeat), the control plane marks it as unhealthy and triggers a restart via POST /agents/restart.
Heartbeats are sent every 30 seconds. A missed heartbeat is not immediately failure. The server waits for three consecutive heartbeats before marking an agent as offline.
If an agent crashes during a task and fails to send a heartbeat, the control plane may reassign the task to another agent. This leads to double-processing when a task is picked up by two agents simultaneously. To avoid this, agents must use idempotent task assignment:
{
"task_id": "task-7a2b9c",
"agent_id": "support-agent-01",
"assign_timestamp": "2024-04-05T10:02:15Z",
"retries": 2,
"status": "in_progress"
}
The agent updates the task status in a shared tasks table. The assign_timestamp ensures that a second agent cannot claim the same task if the first has already updated it.
Agents process events from a central topic, such as events.customer.support.requested. The control plane publishes events to the broker using a PRODUCER API.
POST /events HTTP/1.1
Host: mcp-broker.example.com
Content-Type: application/json
[
{
"event_type": "customer.support.requested",
"payload": {
"customer_id": "cust-1043",
"request_text": "My order #20435 is missing the invoice.",
"timestamp": "2024-04-05T10:01:30Z"
},
"metadata": {
"source": "webform",
"priority": "high"
}
}
]
Agents consume events using a CONSUMER API that returns batches of events.
GET /events/consume?topic=events.customer.support.requested&batch_size=10&timeout=30s HTTP/1.1
Host: mcp-broker.example.com
Authorization: Bearer <registration_token>
Response:
{
"events": [
{
"event_id": "evt-1234",
"event_type": "customer.support.requested",
"payload": {
"customer_id": "cust-1043",
"request_text": "My order #20435 is missing the invoice.",
"timestamp": "2024-04-05T10:01:30Z"
},
"metadata": {
"source": "webform",
"priority": "high"
},
"received_at": "2024-04-05T10:02:15Z"
}
],
"next_offset": 12345
}
After processing, the agent sends a POST /events/ack request:
POST /events/ack HTTP/1.1
Host: mcp-broker.example.com
Authorization: Bearer <registration_token>
Content-Type: application/json
{
"event_ids": [
"evt-1234",
"evt-1235"
],
"status": "completed",
"details": {
"processed_by": "support-agent-01",
"duration_ms": 4231,
"notes": "Assigned to agent: alice, added to SLA tracker."
}
}
If the agent crashes before acknowledging, the control plane reprocesses the event. This leads to at-least-once processing, but agents must be idempotent.
next_offset but fails to persist it. On restart, it reprocesses events from the wrong position.evt-1234, but fails before sending the ack response. The broker receives ack, but the control plane does not record the completed state.event_id or payload_hash.To resolve this, the agent must:
event_id in a processed_events table.event_id as a primary key with a processed_at timestamp.event_id already exists in processed_events. If so, skip processing.-- Table: processed_events
CREATE TABLE processed_events (
event_id TEXT PRIMARY KEY,
processed_at TIMESTAMP WITH TIME ZONE,
agent_id TEXT,
details JSONB
);
Each agent maintains state across executions using a shared state store. The state is stored as a JSON document, versioned, and updated atomically.
To retrieve state:
GET /state/{agent_id}/{key} HTTP/1.1
Host: mcp-state.example.com
Authorization: Bearer <registration_token>
Response:
{
"key": "current_conversation",
"value": {
"messages": [
{
"role": "user",
"text": "My order #20435 is missing the invoice.",
"timestamp": "2024-04-05T10:01:30Z"
},
{
"role": "assistant",
"text": "Thank you for your request. I’m checking your order now.",
"timestamp": "2024-04-05T10:02:10Z"
}
],
"current_step": "invoice_search",
"status": "in_progress"
},
"version": 3,
"last_updated": "2024-04-05T10:02:15Z"
}
To update state:
PATCH /state/{agent_id}/{key} HTTP/1.1
Host: mcp-state.example.com
Authorization: Bearer <registration_token>
Content-Type: application/json
{
"value": {
"messages": [
// ... updated messages
],
"current_step": "invoice_search",
"status": "waiting_for_response"
},
"expected_version": 3,
"metadata": {
"source": "customer_support_workflow"
}
}
The server enforces optimistic concurrency: if the expected_version does not match the current version in the store, it returns 409 Conflict:
{
"error": "conflict",
"expected_version": 3,
"actual_version": 5,
"current_value": {
"messages": [ ... ],
"current_step": "invoice_search",
"status": "waiting_for_response"
},
"details": "Another agent updated the state between reads."
}
The agent must retry the update with the new expected_version until success.
expected_version = 3, but version 5 is current. The update is rejected, and the client must re-read and re-apply.PATCH response. The control plane sees the state, but the agent has not recorded the update.To mitigate, agents should:
expected_version in all updates.merge strategy when updating complex state.Agents can trigger workflows by publishing a workflow.triggered event.
{
"event_type": "workflow.triggered",
"payload": {
"workflow_id": "order_fulfillment_v2",
"input": {
"order_id": "ord-20435",
"customer_id": "cust-1043"
},
"triggered_at": "2024-04-05T10:05:00Z"
},
"metadata": {
"source": "payment_system",
"priority": "critical"
}
}
The control plane schedules a workflow instance, which consists of a sequence of steps, each executed by a registered agent.
Each step is described in a workflows table:
CREATE TABLE workflows (
workflow_id TEXT PRIMARY KEY,
steps JSONB,
created_at TIMESTAMP WITH TIME ZONE,
status TEXT,
current_step_index INTEGER
);
Example steps:
[
{
"step_id": "step-1",
"agent_id": "inventory-checker",
"input": {
"order_id": "{{input.order_id}}"
},
"timeout_seconds": 60
},
{
"step_id": "step-2",
"agent_id": "payment-processor",
"input": {
"customer_id": "{{input.customer_id}}"
},
"required": true
}
]
The control plane dispatches each step by:
step.started event with the step details.POST /steps/assign.POST /steps/completed.The agent must:
step.started.input from the event payload.POST /steps/completed.If a step fails, the control plane may:
max_retries.step.failed event.workflow.retried event.Agents are deployed via Docker and managed by systemctl.
sudo systemctl enable mcp-agent-support
sudo systemctl start mcp-agent-support
# Check status
sudo systemctl status mcp-agent-support
# View logs
sudo journalctl -u mcp-agent-support -f
The agent reads configuration from a file at /etc/mcp-agent/config.json:
{
"control_plane_url": "https://mcp-control-plane.example.com",
"broker_url": "https://mcp-broker.example.com",
"state_store_url": "https://mcp-state.example.com",
"registry": {
"type": "database",
"connection_string": "host=postgres.example.com dbname=mcp_agents user=agent user password=secret"
},
"logging": {
"level": "info",
"format": "json",
"output": "stdout"
},
"telemetry": {
"enabled": true,
"interval_seconds": 60
}
}
The agent uses the control_plane_url to register and heartbeat. If the control plane is unreachable, the agent falls back to a local cache of configuration and continues processing.
/etc/mcp-agent/config.json. It uses default values but logs config.file_not_found.registry.type is database, but the connection_string is malformed. The agent logs registry.connection_failed, and the control plane marks it unhealthy.The control plane logs all errors to a central errors table:
CREATE TABLE errors (
error_id SERIAL PRIMARY KEY,
agent_id TEXT,
event_id TEXT,
workflow_id TEXT,
error_type TEXT,
message TEXT,
stack_trace TEXT,
occurred_at TIMESTAMP WITH TIME ZONE,
metadata JSONB,
resolved_at TIMESTAMP WITH TIME ZONE
);
When an agent fails, it logs an error:
POST /errors HTTP/1.1
Host: mcp-control-plane.example.com
Authorization: Bearer <registration_token>
Content-Type: application/json
{
"agent_id": "support-agent-01",
"error_type": "task_failed",
"message": "Failed to fetch invoice PDF: HTTP 502",
"stack_trace": "Traceback (most recent call last):\n File \"main.py\", line 42, in process_task\n response = requests.get(pdf_url)\nrequests.exceptions.ConnectionError: HTTP 502",
"metadata": {
"task_id": "task-7a2b9c",
"retry_count": 3
}
}
The control plane triggers alerts for:
error_type = "agent_unreachable"error_type = "task_failed", error.message LIKE '%timeout%'error_type = "state_version_conflict"Agents can also subscribe to error events to perform self-healing.
task_failed, the agent queues the task again with increased retry_delay.max_retries are moved to events.dead_letter for manual inspection.state_version_conflict and runs a repair_state workflow to merge changes from multiple sources.These patterns ensure the Mcp Server Architecture is resilient, composable, and predictable—even when agents are distributed across clouds, regions, and networks.
This page was rewritten on 13 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.
We use cookies for analytics (Google Analytics) and advertising (Google AdSense) to improve your experience and support free content. Privacy Policy