Slava Ukraine
Model Context Protocol - Structure for Building AI Agents
The Model Context Protocol (MCP) is a structured framework for designing intelligent systems powered by large language models (LLMs). By splitting an agent into four distinct layers— Model, Context, Tools, and Control Plane—MCP promotes modularity, scalability, and maintainability.

🧩 Core Components of MCP
- Model – The core LLM or reasoning engine used for decision-making and text generation.
- Context – Manages conversational history, memory, and dynamic runtime state.
- Tools – External APIs, databases, or utilities the agent can call (e.g., search, calculators, vector DBs).
- Control Plane – Provides admin access, configuration, observability, and governance for agent behaviors.
Architecture Deep-Dive
The MCP layers interact in a clear pipeline:
- **User Input** arrives and is passed first to the Context layer, which attaches relevant history and memory.
- The enriched prompt goes to the Model, which produces a candidate response or a call to a Tool.
- If a Tool is invoked (e.g., a search query), the result is fed back to the Model in a follow-up prompt.
- The Control Plane monitors each step—logging inputs/outputs and enforcing policies (rate limits, forbidden content).
Sample Workflow
Example: a user asks for stock prices.
- Context: retrieve last finance-related turns from memory store.
- Model: “What is the current price of AAPL?” → determines to call the finance API Tool.
- Tools: call getStockPrice("AAPL") → returns $175.23.
- Model: composes final answer → “AAPL is trading at $175.23.”
- Control Plane: logs the entire exchange and enforces API quotas.
🚀 Benefits of Using MCP
- 🔧 Modularity – Easily extend or replace components like tools or context memory.
- 📦 Maintainability – Clear code structure supports long-term development.
- 🌍 Integrability – Seamlessly plug in LangChain, LangGraph, or custom pipelines.
- 🔒 Governance – Introduce safety constraints and logging through the control plane.
TypeScript Code Example
Here’s a minimal bootstrap showing MCP interfaces and a simple agent loop:
// mcp.ts
/** 1) Define core interfaces **/
export interface ContextManager {
getHistory(userId: string): Promise<string[]>;
appendTurn(userId: string, turn: string): Promise<void>;
}
export interface Tool {
name: string;
call(args: any): Promise<any>;
}
export interface ControlPlane {
log(entry: any): void;
enforce(entry: any): void;
}
// 2) Agent class wiring components
export class MCPAgent {
constructor(
private model: (prompt: string) => Promise<string>,
private context: ContextManager,
private tools: Record<string, Tool>,
private control: ControlPlane
) {}
async handle(userId: string, userInput: string) {
// 2a) Enforce policy
this.control.enforce({ userId, userInput });
// 2b) Build prompt
const history = await this.context.getHistory(userId);
const prompt = [...history, userInput].join("\n");
// 2c) Model inference
const raw = await this.model(prompt);
// 2d) If tool call detected, route to tool
if (raw.startsWith("CALL_TOOL:")) {
const [_, toolName, argJson] = raw.split("|");
const result = await this.tools[toolName].call(JSON.parse(argJson));
return this.handle(userId, result);
}
// 2e) Log & update memory
this.control.log({ userId, prompt, response: raw });
await this.context.appendTurn(userId, userInput);
await this.context.appendTurn(userId, raw);
return raw;
}
}
// 3) Example instantiation omitted for brevity
Where MCP is Used
MCP is commonly adopted in modern AI frameworks such as LangChain, CrewAI, and LangGraph. It’s ideal for production-level agents, copilots, and data-driven chatbots.