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
- Never Expose Raw SQL Execution to Untrusted Prompts: Instead of a generic `execute_sql(query)` tool, expose parameterized domain tools (`lookup_customer_invoices`, `get_deployment_health`) backed by read-only database roles.
- Human-in-the-Loop Confirmation for Write Tools: Any MCP tool that dispatches external messages, modifies billing state, or mutates production infrastructure must return a staged preview token requiring explicit human confirmation.
3. Related AI Application Engineering Guides
- Hire a Senior AI Application Engineer: LLM Agents, RAG, MCP, Google ADK & LangGraph Pillar Guide
- AI Application Engineer Blueprint: Production MCP Servers & Hybrid RRF RAG
⚡ 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) →