• bitcoinBitcoin(BTC)$61,698.002.10%
  • ethereumEthereum(ETH)$1,732.742.75%
  • tetherTether(USDT)$1.00-0.01%
  • binancecoinBNB(BNB)$565.781.94%
  • usd-coinUSDC(USDC)$1.000.01%
  • rippleXRP(XRP)$0.972.46%
  • solanaSolana(SOL)$72.983.09%
  • Figure HelocFigure Heloc(FIGR_HELOC)$1.070.87%
  • tronTRON(TRX)$0.3054310.46%
  • HyperliquidHyperliquid(HYPE)$66.328.75%
  • zcashZcash(ZEC)$744.56-0.64%
  • dogecoinDogecoin(DOGE)$0.0581193.12%
  • moneroMonero(XMR)$466.22-11.74%
  • leo-tokenLEO Token(LEO)$9.070.91%
  • whitebitWhiteBIT Coin(WBT)$62.276.40%
  • chainlinkChainlink(LINK)$9.584.59%
  • USDSUSDS(USDS)$1.000.00%
  • cardanoCardano(ADA)$0.1530303.44%
  • Ethena USDeEthena USDe(USDE)$1.00-0.04%
  • stellarStellar(XLM)$0.1297911.31%
  • daiDai(DAI)$1.000.02%
  • bitcoin-cashBitcoin Cash(BCH)$213.112.54%
  • nearNEAR Protocol(NEAR)$3.101.44%
  • litecoinLitecoin(LTC)$44.272.20%
  • CantonCanton(CC)$0.076167-3.46%
  • avalanche-2Avalanche(AVAX)$6.771.95%
  • uniswapUniswap(UNI)$4.884.12%
  • tether-goldTether Gold(XAUT)$3,650.952.51%
  • USD1USD1(USD1)$1.000.01%
  • the-open-networkGram (prev. Toncoin)(GRAM)$0.981.10%
  • suiSui(SUI)$0.682.52%
  • Ripple USDRipple USD(RLUSD)$1.000.02%
  • Global DollarGlobal Dollar(USDG)$1.000.00%
  • hedera-hashgraphHedera(HBAR)$0.0557054.92%
  • Ondo US Dollar YieldOndo US Dollar Yield(USDY)$1.150.10%
  • paypal-usdPayPal USD(PYUSD)$1.000.01%
  • BittensorBittensor(TAO)$188.283.06%
  • quant-networkQuant(QNT)$151.262.12%
  • shiba-inuShiba Inu(SHIB)$0.0000042.13%
  • Spiko Amundi Overnight Swap Fund (EUR)Spiko Amundi Overnight Swap Fund (EUR)(EURSAFO)$1.120.12%
  • Circle USYCCircle USYC(USYC)$1.150.00%
  • crypto-com-chainCronos(CRO)$0.0402881.52%
  • MemeCoreMemeCore(M)$0.83-2.21%
  • okbOKB(OKB)$84.614.24%
  • RainRain(RAIN)$0.002415-9.38%
  • Falcon USDFalcon USD(USDF)$1.000.02%
  • pax-goldPAX Gold(PAXG)$3,648.772.44%
  • United StablesUnited Stables(U)$1.000.03%
  • BlackRock USD Institutional Digital Liquidity FundBlackRock USD Institutional Digital Liquidity Fund(BUIDL)$1.000.00%
  • pepePepe(PEPE)$0.0000043.60%
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
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

YOU MAY ALSO LIKE

Tencent Open-Sources DFly2: Speculative Decoding That Reuses the Target’s Sparse KV Cache

Backloggd Urges Users To Change Their Passwords After A Breach

ShareTweetSendSharePin

Related Posts

Tencent Open-Sources DFly2: Speculative Decoding That Reuses the Target’s Sparse KV Cache
AI & Technology

Tencent Open-Sources DFly2: Speculative Decoding That Reuses the Target’s Sparse KV Cache

November 27, 2026
Backloggd Urges Users To Change Their Passwords After A Breach
AI & Technology

Backloggd Urges Users To Change Their Passwords After A Breach

November 26, 2026
1Password Says It Was Hacked But Users’ Vaults Are Safe
AI & Technology

1Password Says It Was Hacked But Users’ Vaults Are Safe

November 26, 2026
Canada Invests 0 Million More to Expand AI Compute Access Fund – Unite.AI
AI & Technology

Canada Invests $700 Million More to Expand AI Compute Access Fund – Unite.AI

November 26, 2026
Next Post
Intercom, now called Fin, launches an AI agent whose only job is managing another AI agent

Intercom, now called Fin, launches an AI agent whose only job is managing another AI agent

Leave a Reply Cancel reply

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

Search

No Result
View All Result
Timeline: See when the major East Coast Thanksgiving week storm will impact your area – FOX Weather

Timeline: See when the major East Coast Thanksgiving week storm will impact your area – FOX Weather

November 25, 2026
Fort Hood shooter asks court to delay execution scheduled for next week – The Washington Post

Fort Hood shooter asks court to delay execution scheduled for next week – The Washington Post

November 25, 2026
Russian warship hit by Ukrainian drones in Libya, Zelensky says – BBC

Russian warship hit by Ukrainian drones in Libya, Zelensky says – BBC

November 23, 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!