Thursday, December 3, 2026 | London 11°C · Drizzle
DailyGlimpse

AI

Build a Scalable MCP-Style AI Agent with Dynamic Tool Routing and Context Injection

Build a fully functional MCP-style routed agent system from scratch with tool discovery, routing, planning, and execution.

MarkTechPost 2 min read

Build a Scalable MCP-Style AI Agent with Dynamic Tool Routing and Context Injection

In this tutorial, we build a fully functional MCP-style routed agent system from scratch, combining tool discovery, intelligent routing, structured planning, and execution into a single cohesive workflow.

We start by setting up a modular tool server that exposes capabilities such as web search, local retrieval, dataset loading, and Python execution, all defined through structured schemas. We then implement a hybrid router that uses both heuristics and LLM reasoning to dynamically decide which tools to expose for a given task, ensuring minimal yet effective capability exposure.

As we progress, we design an agent that plans tool usage, executes calls safely, and synthesizes final answers by injecting context from tool outputs. By the end, we demonstrate multiple real-world tasks and show how MCP principles such as context injection, routing policies, and restricted tool access come together to create a scalable, interpretable, and efficient agent system.

The tutorial begins by checking and installing all required Python packages so the code runs smoothly in a single environment. Core libraries for data handling, retrieval, structured schemas, web search, and rich console display are imported. The OpenAI API key is securely loaded, the client is initialized, and global settings for the model, tool calls, web results, and retrieval depth are defined.

Structured Pydantic models represent tool specifications, tool calls, routing decisions, planning outputs, and tool results in a clean MCP-style format. A small local knowledge base explains concepts like MCP, dynamic capability exposure, context injection, router policies, and sandboxed execution. Finally, a TF-IDF-based local retriever searches these documents and returns the most relevant snippets, along with their similarity scores.

React