How to Build an MCP Style Routed AI Agent System with Dynamic Tool Exposure Planning, Execution, and Context Injection
This tutorial demonstrates building a fully functional MCP-style routed agent system from scratch, combining tool discovery, intelligent routing, structured planning, and execution into a single cohesive workflow.

How to Build an MCP Style Routed AI Agent System with Dynamic Tool Exposure Planning, Execution, 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.', '', 'We begin by checking and installing all required Python packages so the tutorial runs smoothly in a single environment. We then import the core libraries for data handling, retrieval, structured schemas, web search, and rich console display.
We securely load the OpenAI API key, initialize the client, and define global settings for the model, tool calls, web results, and retrieval depth.', '', 'We define structured Pydantic models to represent tool specifications, tool calls, routing decisions, planning outputs, and tool results in a clean MCP-style format. We then create a small local knowledge base that explains concepts like MCP, dynamic capability exposure, context injection, router policies, and sandboxed execution. Finally, we built a TF-IDF-based local retriever that searches these documents and returns the most relevant snippets, along with their similarity scores.', '', 'We define the main tools our MCP-style agent can use, including web search, safe Python execution, dataset loading, and local vector retrieval.
We keep Python execution controlled by limiting built-in functions and capturing printed output, local variables, and errors. We also ensure that the dataset and retrieval tools return structured outputs so the agent can inspect data or retrieve relevant knowledge before generating a final answer.', '', 'We create an MCP-style tool server that stores each tool with its schema, description, tags, and callable function. We add methods for listing available tools and calling a selected tool with structured arguments while safely returning success or error outputs.
Source: MarkTechPost