Confirmation Required MCP Toolkit
Human-in-the-Loop: Adding User Confirmation to Tool Calls with MCP Servers.
1"""2Confirmation Required MCP Toolkit3=============================45Human-in-the-Loop: Adding User Confirmation to Tool Calls with MCP Servers.6"""78import asyncio910from kern.agent import Agent11from kern.db.sqlite import SqliteDb12from kern.models.openai import OpenAIResponses13from kern.tools.mcp import MCPTools14from rich.console import Console15from rich.prompt import Prompt1617console = Console()1819# ---------------------------------------------------------------------------20# Create Agent21# ---------------------------------------------------------------------------22mcp_tools = MCPTools(23 transport="streamable-http",24 url="https://kern.ndx.rocks/mcp",25 requires_confirmation_tools=["SearchAgno"], # Note: Tool names are case-sensitive26)2728agent = Agent(29 model=OpenAIResponses(id="gpt-5.2"),30 tools=[mcp_tools],31 markdown=True,32 db=SqliteDb(db_file="tmp/confirmation_required_toolkit.db"),33)343536# ---------------------------------------------------------------------------37# Run Agent38# ---------------------------------------------------------------------------39async def main():40 async for run_event in agent.arun("What is Kern?", stream=True):41 if run_event.is_paused:42 # Handle confirmation requirements43 for requirement in run_event.active_requirements:44 if requirement.needs_confirmation:45 # Ask for confirmation46 console.print(47 f"Tool name [bold blue]{requirement.tool_execution.tool_name}({requirement.tool_execution.tool_args})[/] requires confirmation."48 )49 message = (50 Prompt.ask(51 "Do you want to continue?", choices=["y", "n"], default="y"52 )53 .strip()54 .lower()55 )5657 if message == "n":58 requirement.reject()59 else:60 requirement.confirm()6162 # Continue the run after handling all confirmations63 async for resp in agent.acontinue_run(64 run_id=run_event.run_id,65 requirements=run_event.requirements,66 stream=True,67 ):68 if resp.content:69 print(resp.content, end="")70 else:71 # Not paused - print the streaming content72 if run_event.content:73 print(run_event.content, end="")7475 print() # Final newline767778if __name__ == "__main__":79 asyncio.run(main())Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/02_agents/10_human_in_the_loop45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python confirmation_required_mcp_toolkit.py