Confirmation Required MCP Toolkit

Human-in-the-Loop: Adding User Confirmation to Tool Calls with MCP Servers.

1"""
2Confirmation Required MCP Toolkit
3=============================
4
5Human-in-the-Loop: Adding User Confirmation to Tool Calls with MCP Servers.
6"""
7
8import asyncio
9
10from kern.agent import Agent
11from kern.db.sqlite import SqliteDb
12from kern.models.openai import OpenAIResponses
13from kern.tools.mcp import MCPTools
14from rich.console import Console
15from rich.prompt import Prompt
16
17console = Console()
18
19# ---------------------------------------------------------------------------
20# Create Agent
21# ---------------------------------------------------------------------------
22mcp_tools = MCPTools(
23 transport="streamable-http",
24 url="https://kern.ndx.rocks/mcp",
25 requires_confirmation_tools=["SearchAgno"], # Note: Tool names are case-sensitive
26)
27
28agent = 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)
34
35
36# ---------------------------------------------------------------------------
37# Run Agent
38# ---------------------------------------------------------------------------
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 requirements
43 for requirement in run_event.active_requirements:
44 if requirement.needs_confirmation:
45 # Ask for confirmation
46 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 )
56
57 if message == "n":
58 requirement.reject()
59 else:
60 requirement.confirm()
61
62 # Continue the run after handling all confirmations
63 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 content
72 if run_event.content:
73 print(run_event.content, end="")
74
75 print() # Final newline
76
77
78if __name__ == "__main__":
79 asyncio.run(main())

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/10_human_in_the_loop
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python confirmation_required_mcp_toolkit.py