AI Agents·7 min read·

Building Production Model Context Protocol (MCP) Gateways for AI Agents

Standardize secure agent tool calling, resource inspection, and schema reflection using Anthropic's Model Context Protocol over Unix domain sockets.

NB

Netbay Developer Relations

Netbay Engineering

On this page

Until recently, connecting autonomous AI agents to internal company databases, file trees, and APIs meant writing brittle custom function-calling wrappers for every model provider. Each integration had its own error format, schema structure, and authorization model.

Anthropic's Model Context Protocol (MCP) has unified this landscape. MCP establishes an open, standard protocol enabling AI applications to discover capabilities, inspect context resources, and invoke tools across isolated processes. However, exposing enterprise infrastructure directly to an LLM via MCP requires an architectural gateway to enforce strict role-based access control, schema validation, and rate limiting.

The MCP Gateway Architecture

An enterprise-ready MCP architecture introduces an intermediary Security Gateway between the LLM client and internal services:

  1. Agent Host Client: Runs the model interface (e.g., Claude, local Llama 3.3, or Qwen 2.5) speaking standard JSON-RPC 2.0.
  2. Gateway Multiplexer: Intercepts all MCP protocol frames over Unix domain sockets or Server-Sent Events (SSE). It injects audit metadata, validates parameters against JSON Schemas, and checks tool allowlists.
  3. Domain Tool Servers: Isolated backend daemons exposing strictly bounded capabilities: PostgreSQL Reader, Kubernetes Inspector, or GitHub PR Reviewer.
Production Model Context Protocol (MCP) Gateway AI Agent Client Claude / Local Llama JSON-RPC 2.0 (mcp) MCP Security Gateway Schema Validation & Interception Audit Log & Rate Limiting Unix Socket: /run/mcp.sock Postgres MCP Read-Only SQL Queries Git Repo MCP Diff & Log Inspection Gateway Defense: Tool Injection & Parameter Poisoning Rejects unverified bash tools, validates regex constraints on filepaths Enforces per-session execution timeouts and mandatory human approvals

Building an MCP Server in TypeScript

The official @modelcontextprotocol/sdk makes implementing tools intuitive. Below is a production server exposing a safe, read-only system telemetry tool:

typescript
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
  CallToolRequestSchema,
  ListToolsRequestSchema
} from '@modelcontextprotocol/sdk/types.js';
import os from 'os';

const server = new Server(
  { name: 'netbay-telemetry-mcp', version: '1.0.0' },
  { capabilities: { tools: {} } }
);

// Register available tools
server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
    tools: [
      {
        name: 'get_host_telemetry',
        description: 'Returns real-time CPU, memory, and load averages for the node',
        inputSchema: {
          type: 'object',
          properties: {
            detailed: { type: 'boolean', description: 'Include per-core stats' }
          }
        }
      }
    ]
  };
});

// Handle tool execution with strict validation
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  if (request.params.name !== 'get_host_telemetry') {
    throw new Error('Unknown tool name: ' + request.params.name);
  }

  const telemetry = {
    uptime_sec: os.uptime(),
    load_avg: os.loadavg(),
    total_mem_mb: Math.round(os.totalmem() / 1024 / 1024),
    free_mem_mb: Math.round(os.freemem() / 1024 / 1024),
    hostname: os.hostname()
  };

  return {
    content: [
      {
        type: 'text',
        text: JSON.stringify(telemetry, null, 2)
      }
    ]
  };
});

async function run() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error('Netbay Telemetry MCP server initialized on stdio');
}

run().catch((err) => {
  console.error('Fatal error starting MCP:', err);
  process.exit(1);
});
Expand
Model Context Protocol Tool Integration
Code execution and protocol architecture

Video Reference on LLM Tool Calling Protocols

Explore the mechanics of language model tool calling and JSON-RPC protocol design:

Language Models and Tool Calling Protocols
YouTube

Implementing an MCP gateway gives engineering teams full control over agent access privileges, protecting sensitive data while unlocking autonomous tool workflows.

Deploy your AI agent microservices on high-performance Netbay Linux VPS instances with dedicated RAM and zero throttled CPU at netbayhosts.in.

Keep reading

Follow along on a real VPS

Deploy Linux in under 60 seconds

These guides are written against Ubuntu, Debian, and RHEL-family images — the same ones on NetBay.

Deploy an instance