AI Application Engineer Guide: Building Custom Model Context Protocol (MCP) Servers for Claude, Gemini & Enterprise Databases

The Model Context Protocol (MCP) standardizes how AI agents connect to internal PostgreSQL databases, FHIR healthcare servers, CRM/ERP APIs, and cloud infrastructure tools. As a Senior AI Application Engineer and Backend Engineer who builds production MCP servers across Python and TypeScript, here is how I design deterministic, permission-scoped MCP tools that agents can invoke reliably.

1. Anatomy of a Production Python MCP Tool Server

A well-engineered MCP tool provides a self-documenting JSON Schema, enforces strict tenant isolation from the authenticated session context, and returns structured, token-efficient results:

from typing import Any, Dict
import psycopg2.extras

def mcp_tool_lookup_customer_invoices(
    db_conn, tenant_id: str, customer_email: str, limit: int = 10
) -> Dict[str, Any]:
    """
    Read-only MCP tool handler with strict tenant_id scoping and bounded row limit.
    """
    safe_limit = max(1, min(limit, 25))
    with db_conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
        cur.execute(
            """
            SELECT invoice_number, status, amount_cents, currency, issued_at
            FROM billing_invoices
            WHERE tenant_id = %s AND LOWER(customer_email) = LOWER(%s)
            ORDER BY issued_at DESC
            LIMIT %s
            """,
            (tenant_id, customer_email.strip(), safe_limit),
        )
        rows = cur.fetchall()
    return {
        "tenant_id": tenant_id,
        "count": len(rows),
        "invoices": [dict(r) for r in rows],
    }

2. Security & Reliability Guardrails for Enterprise MCP Deployments

3. Related AI Application Engineering Guides


⚡ Hire Aldian Fazrihady — Senior Cloud, GCP, Backend, AI Application & Full Stack Engineer

Need production cloud architecture, GCP/AWS infrastructure, high-throughput backend systems, AI agents, or full stack web engineering? Book a 1-Day Programming Sprint for $100 USD (International Card / Apple Pay) or Rp 1,000,000 (Indonesian QRIS / Bank Virtual Account), or hire directly on Upwork:

Book $100 / Rp 1M 1-Day Sprint → Hire on Upwork (100% Job Success) →


About the Author — Hire Aldian Fazrihady (Cloud Engineer, GCP Professional Cloud Architect, GCP Professional Data Engineer, Backend, AI & Full Stack Engineer)

Aldian Fazrihady is a Top-Rated Senior Cloud Engineer, GCP Professional Cloud Architect (Google Cloud Certified), GCP Professional Data Engineer (Google Cloud Certified), Backend Engineer (Python, Django, FastAPI, Go, PostgreSQL), AI Application Engineer (LLMs, RAG, MCP, Google ADK, LangGraph), and Full Stack Engineer (React, Next.js, TypeScript) with 15+ years of production software engineering experience.

Hire Aldian Fazrihady on Upwork | Book 1-Day Programming Service ($100 USD / Rp 1.000.000) | View Live AI Portfolio | Send a Direct Message

Related Engineering Articles

Engineering Role Pillar Guides & Category Hubs