How MCP Server Integration in dcg Enables Multi-Agent Coordination

The destructive_command_guard (dcg) repository implements MCP server integration by exposing an asynchronous server that listens on stdio, routing hook requests to registered agents and aggregating their safety decisions into a unified policy response.

The destructive_command_guard (dcg) tool can operate as a Model Context Protocol (MCP) server, transforming from a standalone CLI into a coordinating hub for multiple agents. This integration allows dcg to receive commands from external systems like Claude Code, distribute evaluation work across specialized agents, and return a single definitive safety verdict. When running in MCP server mode, dcg leverages the rust-mcp-sdk crate to handle transport and protocol details while maintaining its core command analysis pipeline.

How dcg Implements MCP Server Mode

Bootstrap and Transport Layer

The MCP server functionality resides in src/mcp.rs, which initializes a McPServer instance using the rust-mcp-sdk crate. The server spawns a Tokio async runtime that listens for JSON-RPC requests over standard input and output channels, making it compatible with any MCP client that supports stdio transport.

Agent Registration and Discovery

When external agents connect, the server assigns unique identifiers and stores metadata in the AgentRegistry defined in src/agent.rs. Each registered agent advertises its capabilities and version information, enabling the server to route specific evaluation tasks to the most appropriate specialized agent.

Request Routing and Command Evaluation

Incoming requests arrive as HookInput payloads containing Claude Code's "PreToolUse" JSON. The src/hook.rs module parses these payloads into internal command representations, then delegates to src/evaluator.rs for pattern matching against destructive command signatures. The server maintains the full evaluation pipeline including quick-reject filtering and command normalization via src/normalize.rs.

Multi-Agent Coordination Architecture

Parallel Safety Evaluation

The MCP server enables concurrent evaluation by distributing command analysis across multiple agents simultaneously. One agent might verify Git repository safety while another checks filesystem impact, with each operating independently on the shared Context state.

Centralized Policy Enforcement

All agents read from and write to a shared in-memory Context structure defined in src/context.rs, which holds the current allow-list, pack configuration, and command history. This shared state ensures consistent policy enforcement across the entire system, with the MCP server applying final priority rules (e.g., critical severity overrides) before responding.

Result Aggregation and Response Formatting

After agents return their individual decisions (allow or deny), the server collates results in src/output/denial.rs. It generates a rich JSON denial payload containing explanations, suggested alternatives, and severity classifications, then returns this aggregated response to the original MCP client.

Running dcg as an MCP Server

To launch dcg in MCP server mode:

dcg --mcp

This command starts the async event loop, making the process listen for MCP requests on stdin and respond on stdout.

Example agent implementation using the rust-mcp-sdk:

use rust_mcp_sdk::{Client, Request};

#[tokio::main]
async fn main() {
    let mut client = Client::connect_stdio().await.unwrap();
    while let Some(req) = client.next_request().await {
        match req {
            Request::HookInput { json } => {
                // Process hook through dcg pipeline
                let decision = dcg::process_hook(json).await;
                client.respond(decision).await.unwrap();
            }
            _ => {}
        }
    }
}

Internal request handling in src/mcp.rs:

let server = McPServer::new();
server.on_request(|req| async move {
    if let Request::HookInput { json } = req {
        // Parse and evaluate the command
        let result = dcg::evaluate(json).await;
        // Return aggregated result to caller
        server.respond(req.id, result).await?;
    }
    Ok(())
});

Key Source Files and Functions

File Role Link
src/mcp.rs Provides the MCP server bootstrap, request handling, and routing logic. src/mcp.rs
src/agent.rs Defines the Agent struct, registration, and metadata used by the MCP server. src/agent.rs
src/hook.rs Parses Claude Code hook JSON and produces the internal command representation. src/hook.rs
src/evaluator.rs Core pattern-matching engine that decides whether a command is safe or destructive. src/evaluator.rs
src/context.rs Shared state (allow-list, pack config, history) accessed by all agents. src/context.rs
src/output/denial.rs Formats the JSON denial payload that agents return to the MCP server. src/output/denial.rs

Summary

  • MCP server mode converts dcg from a standalone CLI into a coordinating hub using src/mcp.rs and the rust-mcp-sdk crate.
  • The server listens on stdio transport via an async Tokio runtime, processing HookInput requests from MCP clients.
  • Multi-agent coordination works through parallel evaluation across specialized agents registered in src/agent.rs, with shared state managed in src/context.rs.
  • The evaluation pipeline remains consistent with the CLI mode, utilizing src/hook.rs, src/normalize.rs, and src/evaluator.rs for command analysis.
  • Result aggregation in src/output/denial.rs produces unified JSON responses that combine inputs from all participating agents.

Frequently Asked Questions

What is MCP server mode in dcg?

MCP server mode allows dcg to function as a Model Context Protocol server, receiving commands via JSON-RPC over standard input rather than command-line arguments. This enables integration with AI assistants like Claude Code that can send hook requests for real-time safety evaluation.

How does dcg route commands between multiple agents?

The McPServer struct in src/mcp.rs parses incoming requests and dispatches them to registered agents based on capability metadata stored in src/agent.rs. Each agent evaluates the command independently, and the server aggregates their decisions using the shared Context state before returning a final verdict.

Can custom agents be added to dcg's MCP server?

Yes, the architecture supports dynamic registration of new agents. Any agent implementing the MCP protocol can connect to dcg's stdio transport, register itself with the AgentRegistry, and begin receiving evaluation requests without modifying the core dcg codebase.

What transport protocol does dcg use for MCP communication?

Dcg uses stdio transport (standard input/output) for MCP communication, making it compatible with the rust-mcp-sdk crate and any MCP client that supports JSON-RPC over pipes. This transport method is specified in the server initialization code within src/mcp.rs.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →