Skip to main content
Model-Context-Protocol (MCP) rethinks how AI agents integrate with external systems. Instead of having developers write bespoke API integrations for every use case, MCP lets services register as callable tools that agents can invoke. This shifts the integration burden from application code to the agent, enabling more flexible, composable workflows. In practice, an MCP server exposes one or more well-defined functions (endpoints) with explicit input and output schemas. When an agent runs, it discovers and calls these functions to query or mutate external state. That makes it straightforward to extend an assistant’s capabilities—plug in an MCP server for a system and the agent can use it without additional glue code. For example, a TechDocs assistant could query customer, order, inventory, or ticketing systems via MCP endpoints. If a user asks, “What’s the status of order 1234?”, the agent can call an MCP that queries the order-management system, receive the structured response, and compose a natural-language reply that includes the order state.
A hand-drawn system diagram showing a user asking "What's the status of order #1234" to Tech Corp's AI chat assistant and agent. The agent connects to internal knowledge (a vector DB) and external systems (customer database, inventory/support) via an MCP/API to fetch the information.

Minimal FastAPI MCP server example

Below is a concise, practical example: a FastAPI-based MCP that exposes a simple customer lookup function. It demonstrates the typical pieces of an MCP server:
  • A web app that exposes a function endpoint the agent can call.
  • Typed request/response models so agents know how to call the function.
  • A persistence layer (here, an in-memory dict) — replace with your production DB.
Use this as a template for a real integration (SQL, MongoDB, or any service).
# server.py
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional, Dict

app = FastAPI(title="customer-db-mcp", version="0.1.0")


# Request schema: what the MCP client (the AI agent) will send
class GetCustomerRequest(BaseModel):
    customer_id: str


# Response schema: what the MCP server returns
class Customer(BaseModel):
    customer_id: str
    name: str
    email: Optional[str] = None
    status: str  # e.g., 'shipped', 'processing', 'closed' (use values appropriate for your domain)


# Fake in-memory database (replace with real DB in production)
customers: Dict[str, Customer] = {
    "1234": Customer(customer_id="1234", name="Alice Johnson", email="alice@example.com", status="shipped"),
    "2345": Customer(customer_id="2345", name="Bob Smith", email="bob@example.com", status="processing"),
}


@app.post("/mcp/get_customer", response_model=Customer)
async def get_customer(req: GetCustomerRequest):
    """
    MCP function: returns customer information by customer_id.
    The agent can call this endpoint to retrieve customer state.
    """
    cust = customers.get(req.customer_id)
    if not cust:
        raise HTTPException(status_code=404, detail="customer not found")
    return cust
Run the server with uvicorn:
uvicorn server:app --host 0.0.0.0 --port 8000 --reload
Example MCP client request (what an agent or other client would send):
curl -X POST "http://localhost:8000/mcp/get_customer" \
  -H "Content-Type: application/json" \
  -d '{"customer_id": "1234"}'
Example JSON response:
{
  "customer_id": "1234",
  "name": "Alice Johnson",
  "email": "alice@example.com",
  "status": "shipped"
}

Key components and best practices

ComponentPurposeExample / Notes
Endpoint (function surface)Defines callable operations for agentsPOST /mcp/get_customer
Typed schemasMakes discovery and validation reliablePydantic models, OpenAPI schemas
PersistenceStore and retrieve real-world stateSQL, MongoDB, managed services
SecurityProtect data and control accessAPI keys, OAuth, RBAC, rate limits
Agent discoveryHow agents find and interpret functionsOpenAPI, function registry, metadata
This structure—define endpoints and schemas once—lets any compatible agent call your MCP server to fetch or modify external state. Many ecosystems publish reusable MCP adapters for common services (e.g., source control, databases, productivity tools), letting you plug them into agents without custom integration work.
MCP servers should clearly define input and output schemas (e.g., via OpenAPI / Pydantic). This makes it easy for agents to discover and call them reliably. In production, secure these endpoints (authentication, authorization, rate-limiting) before exposing them to agents.
Use this pattern to make your AI assistants actionable: define function surfaces, provide strict schemas, and secure endpoints—then let the agent do the integration work.

Watch Video