• bitcoinBitcoin(BTC)$79,077.00-2.95%
  • ethereumEthereum(ETH)$2,221.33-3.36%
  • tetherTether(USDT)$1.00-0.03%
  • binancecoinBNB(BNB)$672.36-1.25%
  • rippleXRP(XRP)$1.43-4.76%
  • usd-coinUSDC(USDC)$1.000.01%
  • solanaSolana(SOL)$89.14-3.96%
  • tronTRON(TRX)$0.351677-0.50%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.03-0.26%
  • dogecoinDogecoin(DOGE)$0.113388-2.53%
  • whitebitWhiteBIT Coin(WBT)$58.32-2.48%
  • USDSUSDS(USDS)$1.00-0.01%
  • HyperliquidHyperliquid(HYPE)$43.99-0.18%
  • cardanoCardano(ADA)$0.261387-4.09%
  • leo-tokenLEO Token(LEO)$10.13-0.62%
  • zcashZcash(ZEC)$514.34-8.51%
  • bitcoin-cashBitcoin Cash(BCH)$425.34-2.76%
  • chainlinkChainlink(LINK)$10.06-4.89%
  • moneroMonero(XMR)$379.91-5.50%
  • CantonCanton(CC)$0.157060-3.49%
  • the-open-networkToncoin(TON)$1.95-9.15%
  • stellarStellar(XLM)$0.154402-5.72%
  • USD1USD1(USD1)$1.00-0.02%
  • litecoinLitecoin(LTC)$57.34-2.32%
  • suiSui(SUI)$1.10-8.55%
  • daiDai(DAI)$1.000.02%
  • Ethena USDeEthena USDe(USDE)$1.00-0.05%
  • MemeCoreMemeCore(M)$3.24-2.79%
  • avalanche-2Avalanche(AVAX)$9.54-4.64%
  • hedera-hashgraphHedera(HBAR)$0.092777-2.95%
  • shiba-inuShiba Inu(SHIB)$0.000006-4.51%
  • RainRain(RAIN)$0.007491-1.06%
  • paypal-usdPayPal USD(PYUSD)$1.00-0.02%
  • crypto-com-chainCronos(CRO)$0.072863-4.45%
  • Global DollarGlobal Dollar(USDG)$1.000.01%
  • Circle USYCCircle USYC(USYC)$1.120.00%
  • BittensorBittensor(TAO)$285.04-7.65%
  • tether-goldTether Gold(XAUT)$4,539.44-2.56%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • uniswapUniswap(UNI)$3.59-4.55%
  • polkadotPolkadot(DOT)$1.31-5.45%
  • mantleMantle(MNT)$0.66-5.75%
  • pax-goldPAX Gold(PAXG)$4,542.27-2.42%
  • World Liberty FinancialWorld Liberty Financial(WLFI)$0.065997-5.60%
  • nearNEAR Protocol(NEAR)$1.54-3.09%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.13-0.30%
  • okbOKB(OKB)$85.19-0.04%
  • Falcon USDFalcon USD(USDF)$1.00-0.12%
  • HTX DAOHTX DAO(HTX)$0.000002-0.54%
  • OndoOndo(ONDO)$0.362688-8.03%
TradePoint.io
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop
No Result
View All Result
TradePoint.io
No Result
View All Result

How to Build an MCP Style Routed AI Agent System with Dynamic Tool Exposure Planning, Execution, and Context Injection

May 15, 2026
in AI & Technology
Reading Time: 4 mins read
A A
How to Build an MCP Style Routed AI Agent System with Dynamic Tool Exposure Planning, Execution, and Context Injection
ShareShareShareShareShare

YOU MAY ALSO LIKE

New Crash Data Highlights The Slow Progress Of Tesla’s Robotaxis

Claude’s next enterprise battle is not models: it’s the agent control plane

class RoutedAgent:
   def __init__(self, server: MCPToolServer, router: HybridMCPRouter, model: str):
       self.server = server
       self.router = router
       self.model = model


   def discover_exposed_tools(self, exposed_tool_names: List[str]) -> List[Dict[str, Any]]:
       return [t for t in self.server.tools_list() if t["name"] in exposed_tool_names]


   def plan(self, task: str, exposed_tools: List[Dict[str, Any]]) -> PlanOutput:
       instructions = """
You are a planning agent in an MCP-like architecture.
You can only use the exposed tools.
Decide whether tools are needed.
Return strict JSON only with keys:
requires_tools: boolean
tool_calls: array of objects with tool_name and arguments
direct_answer_allowed: boolean
planner_note: string


Rules:
- Use at most 3 tool calls.
- Only call tools from the exposed list.
- Arguments must match each tool's input schema conceptually.
- Prefer calling vector_retrieve for conceptual local knowledge.
- Prefer calling web_search for recent or external information.
- Prefer dataset_loader if the user asks about a named built-in dataset.
- Prefer python_exec only when computation or code execution is genuinely useful.
- Do not fabricate unavailable tools.
"""


       prompt = f"""
USER TASK:
{task}


EXPOSED TOOLS:
{json.dumps(exposed_tools, indent=2)}


Return JSON only.
"""
       obj = llm_json(instructions, prompt)


       raw_tool_calls = obj.get("tool_calls", [])
       parsed_calls = []
       allowed = {t["name"] for t in exposed_tools}


       for call in raw_tool_calls[:MAX_TOOL_CALLS]:
           name = call.get("tool_name", "")
           args = call.get("arguments", {})
           if name in allowed and isinstance(args, dict):
               parsed_calls.append(ToolCall(tool_name=name, arguments=args))


       return PlanOutput(
           requires_tools=bool(obj.get("requires_tools", False) or parsed_calls),
           tool_calls=parsed_calls,
           direct_answer_allowed=bool(obj.get("direct_answer_allowed", False)),
           planner_note=obj.get("planner_note", ""),
       )


   def run_tools(self, tool_calls: List[ToolCall]) -> List[ToolResult]:
       results = []
       for tc in tool_calls:
           result = self.server.tools_call(tc.tool_name, tc.arguments)
           results.append(result)
       return results


   def answer(self, task: str, route: RouteDecision, exposed_tools: List[Dict[str, Any]], plan: PlanOutput, results: List[ToolResult]) -> str:
       instructions = """
You are the final answering agent in an MCP-style routed tool system.
Use the routed tools and returned tool outputs to answer the user.
Be concrete, concise, and technically correct.
If tool outputs are partial, say so.
Do not mention hidden tools that were not exposed.
"""


       tool_result_payload = [r.model_dump() for r in results]


       prompt = f"""
USER TASK:
{task}


ROUTE DECISION:
{route.model_dump_json(indent=2)}


EXPOSED TOOLS:
{json.dumps(exposed_tools, indent=2)}


PLAN:
{plan.model_dump_json(indent=2)}


TOOL RESULTS:
{json.dumps(tool_result_payload, indent=2)}


Now answer the user clearly.
"""
       resp = client.responses.create(
           model=self.model,
           input=prompt,
           instructions=instructions,
           temperature=0.2
       )
       return resp.output_text


   def run(self, task: str, verbose: bool = True) -> Dict[str, Any]:
       route = self.router.route(task)
       exposed_tools = self.discover_exposed_tools(route.selected_tools)
       plan = self.plan(task, exposed_tools)
       results = self.run_tools(plan.tool_calls) if plan.requires_tools else []
       final_answer = self.answer(task, route, exposed_tools, plan, results)


       payload = {
           "task": task,
           "route_decision": route.model_dump(),
           "exposed_tools": exposed_tools,
           "plan": plan.model_dump(),
           "tool_results": [r.model_dump() for r in results],
           "final_answer": final_answer,
       }


       if verbose:
           console.print(Panel.fit(f"USER TASK\n{task}", title="Input"))
           pretty_tools_table(exposed_tools, "Tools Exposed By MCP Router")
           console.print(Panel(route.rationale or "No rationale provided", title="Router Rationale"))
           if route.policy_notes:
               console.print(Panel("\n".join(f"- {x}" for x in route.policy_notes), title="Policy Notes"))
           console.print(Panel(plan.planner_note or "No planner note provided", title="Planner Note"))


           if results:
               for r in results:
                   console.print(Panel.fit(RichJSON.from_data(r.model_dump()), title=f"Tool Result: {r.tool_name}"))
           console.print(Panel(final_answer, title="Final Answer"))


       return payload


def mcp_jsonrpc_tools_list(server: MCPToolServer) -> Dict[str, Any]:
   return {
       "jsonrpc": "2.0",
       "id": 1,
       "result": {
           "tools": server.tools_list()
       }
   }


def mcp_jsonrpc_tools_call(server: MCPToolServer, tool_name: str, arguments: Dict[str, Any]) -> Dict[str, Any]:
   result = server.tools_call(tool_name, arguments)
   return {
       "jsonrpc": "2.0",
       "id": 2,
       "result": result.model_dump()
   }


router = HybridMCPRouter(server=server, model=MODEL)
agent = RoutedAgent(server=server, router=router, model=MODEL)


console.print(Panel.fit("MCP-STYLE TOOL DISCOVERY", title="Step 1"))
console.print(RichJSON.from_data(mcp_jsonrpc_tools_list(server)))


demo_tasks = [
   "Explain how an MCP tool router should expose tools for an agent task about dynamic capability exposure.",
   "Search the web for recent examples of MCP-related developments and summarize them.",
   "Load the iris dataset, inspect its columns and basic stats, and tell me what kind of ML problem it is.",
   "Retrieve local knowledge about context injection and router policies, then explain why restricting tool access helps agent performance.",
   "Use Python to compute the average of [3, 5, 9, 10, 13] and then explain whether python execution was truly necessary.",
]


all_runs = []
for idx, task in enumerate(demo_tasks, start=1):
   console.print(Panel.fit(f"DEMO RUN {idx}", title="=" * 10))
   out = agent.run(task, verbose=True)
   all_runs.append(out)


custom_task = "Design a routed MCP workflow for an AI research assistant that should use retrieval for local protocol knowledge and web search only when the task explicitly asks for recent information."
custom_run = agent.run(custom_task, verbose=True)


print("\nPROGRAMMATIC EXAMPLE: tools/list")
print(json.dumps(mcp_jsonrpc_tools_list(server), indent=2))


print("\nPROGRAMMATIC EXAMPLE: tools/call for vector_retrieve")
print(json.dumps(mcp_jsonrpc_tools_call(server, "vector_retrieve", {"query": "dynamic capability exposure in MCP routers", "top_k": 2}), indent=2))


print("\nPROGRAMMATIC EXAMPLE: tools/call for dataset_loader")
print(json.dumps(mcp_jsonrpc_tools_call(server, "dataset_loader", {"name": "iris", "n_rows": 5}), indent=2))


print("\nPROGRAMMATIC EXAMPLE: custom final answer")
print(custom_run["final_answer"])

Credit: Source link

ShareTweetSendSharePin

Related Posts

New Crash Data Highlights The Slow Progress Of Tesla’s Robotaxis
AI & Technology

New Crash Data Highlights The Slow Progress Of Tesla’s Robotaxis

May 15, 2026
Claude’s next enterprise battle is not models: it’s the agent control plane
AI & Technology

Claude’s next enterprise battle is not models: it’s the agent control plane

May 15, 2026
The UK’s Tax Authority Is Turning To AI To Help Identify Fraud
AI & Technology

The UK’s Tax Authority Is Turning To AI To Help Identify Fraud

May 15, 2026
Google Teases The Android-Based Googlebook
AI & Technology

Google Teases The Android-Based Googlebook

May 15, 2026
Next Post
Your Mouse Pointer Is Getting an AI Brain | Latest in AI

Your Mouse Pointer Is Getting an AI Brain | Latest in AI

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

No Result
View All Result
My AI Channel Costs K/Month to Run

My AI Channel Costs $25K/Month to Run

May 13, 2026
Trump pulls Casey Means’ nomination for surgeon general and announces replacement

Trump pulls Casey Means’ nomination for surgeon general and announces replacement

May 11, 2026
Buy now or wait for a pullback? Adam Turnquist goes rapid-fire

Buy now or wait for a pullback? Adam Turnquist goes rapid-fire

May 12, 2026

About

Learn more

Our Services

Legal

Privacy Policy

Terms of Use

Bloggers

Learn more

Article Links

Contact

Advertise

Ask us anything

©2020- TradePoint.io - All rights reserved!

Tradepoint.io, being just a publishing and technology platform, is not a registered broker-dealer or investment adviser. So we do not provide investment advice. Rather, brokerage services are provided to clients of Tradepoint.io by independent SEC-registered broker-dealers and members of FINRA/SIPC. Every form of investing carries some risk and past performance is not a guarantee of future results. “Tradepoint.io“, “Instant Investing” and “My Trading Tools” are registered trademarks of Apperbuild, LLC.

This website is operated by Apperbuild, LLC. We have no link to any brokerage firm and we do not provide investment advice. Every information and resource we provide is solely for the education of our readers. © 2020 Apperbuild, LLC. All rights reserved.

No Result
View All Result
  • Main
  • AI & Technology
  • Stock Charts
  • Market & News
  • Business
  • Finance Tips
  • Trade Tube
  • Blog
  • Shop

© 2023 - TradePoint.io - All Rights Reserved!