When an MCP server needs client input
Sometimes an MCP server cannot finish a tool call alone. It may need a model's answer (sampling), the folders it may use (roots) or a reply from the person (elicitation).
Since the 2026-07-28 edition it asks by answering "input required", and the client tries again with what was asked for. You will see both rounds in the lab.
By the end of this lesson
Note
I can answer an MCP server's input request and retry the call it belongs to.
Key words
| Word | What it means | Example |
|---|---|---|
| Sampling | a server asking the client's model for an answer | |
| Roots | the folders a client lets a server work in | file:///home/sam/notes |
| Elicitation | a server asking the person for information | |
| Input request | one thing a server asks for before it can finish | |
| Request state | an opaque string the client must send back unchanged | |
| Deprecated | still working, but due to be removed |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsInput required, then the same call again
The client calls a tool. The server answers with resultType set to input_required, a map of input requests, and often a request state.
The client gathers each answer, then sends the same call again with a new id, the answers in inputResponses and the state unchanged. The server keeps nothing between the two, so any copy of it can finish the call.
One tool call, two requests
sequenceDiagram
participant P as Person
participant C as Client
participant S as Server
participant M as Model
C->>S: tools/call, id 1
S-->>C: input_required, sampling request, requestState
C->>P: show the request
P-->>C: agree
C->>M: the prompt
M-->>C: the answer
C->>S: tools/call, id 2, inputResponses, requestState
S-->>C: complete, the resultCheck
Have a go
Check
3 questionsTry it: build a retry
# One tool call that needs something from the client takes two requests.
first = {"id": 1, "params": {"name": "explain_word", "arguments": {"word": "token"}}}
answer = {"resultType": "input_required", "requestState": "abc|123",
"inputRequests": {"model": {"method": "sampling/createMessage"}}}
retry = {"id": 2, "params": dict(first["params"])}
retry["params"]["inputResponses"] = {"model": {"role": "assistant", "content": {"type": "text", "text": "A small piece of text."}}}
retry["params"]["requestState"] = answer["requestState"]
print("first id", first["id"], "retry id", retry["id"])
print("keys asked for:", list(answer["inputRequests"]))
print("keys answered:", list(retry["params"]["inputResponses"]))
# Change the retry so it forgets requestState. What should the server do?
Make a free account to save your work and track your progress.
Three kinds of request, two of them deprecated
Sampling lets a server use the client's model, so the server needs no model key. The person should see the request first and may refuse. Roots tell a server which folders it may touch. Elicitation asks the person a question through the client.
The 2026-07-28 edition marks sampling and roots as deprecated. They still work for at least a year. New servers call a model provider directly and take folders as tool arguments.
Write a server that asks, part 1
docker exec tools sh -c "mkdir -p mcp && printf '%s\n' '\"\"\"A server whose tools need something from the client first: the answer of a model, or the roots.\"\"\"' 'import hmac' 'import json' 'import posixpath' 'KEY = b\"only-the-server-knows\"' '' '' 'def seal(data):' ' text = json.dumps(data)' ' return text + \"|\" + hmac.new(KEY, text.encode(), \"sha256\").hexdigest()[:16]' '' '' 'def unseal(state):' ' text, mac = state.rsplit(\"|\", 1)' ' if hmac.compare_digest(mac, hmac.new(KEY, text.encode(), \"sha256\").hexdigest()[:16]):' ' return json.loads(text)' ' return None' 'def reply(msg_id, result):' ' return {\"jsonrpc\": \"2.0\", \"id\": msg_id, \"result\": result}' '' '' 'def done(msg_id, text, failed=False):' ' return reply(msg_id, {\"resultType\": \"complete\", \"isError\": failed,' ' \"content\": [{\"type\": \"text\", \"text\": text}]})' '' '' 'def needs(msg_id, asks, state=None):' > mcp/asker.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a server that asks, part 2
docker exec tools sh -c "printf '%s\n' ' result = {\"resultType\": \"input_required\", \"inputRequests\": asks}' ' if state:' ' result[\"requestState\"] = state' ' return reply(msg_id, result)' 'def handle(msg):' ' p = msg[\"params\"]' ' got = p.get(\"inputResponses\", {})' ' if p[\"name\"] == \"explain_word\":' ' word = p[\"arguments\"][\"word\"]' ' if \"model\" not in got:' ' if \"sampling\" not in p[\"_meta\"][\"io.modelcontextprotocol/clientCapabilities\"]:' ' return done(msg[\"id\"], \"this client cannot ask a model\", True)' ' text = {\"type\": \"text\", \"text\": \"What is a \" + word + \"?\"}' ' ask = {\"method\": \"sampling/createMessage\", \"params\": {\"messages\": [{\"role\": \"user\", \"content\": text}],' ' \"systemPrompt\": \"Answer in one sentence.\", \"maxTokens\": 60}}' >> mcp/asker.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a server that asks, part 3
docker exec tools sh -c "printf '%s\n' ' return needs(msg[\"id\"], {\"model\": ask}, seal({\"word\": word}))' ' if unseal(p.get(\"requestState\", \"x|y\")) is None:' ' return {\"jsonrpc\": \"2.0\", \"id\": msg[\"id\"], \"error\": {\"code\": -32602, \"message\": \"requestState failed its check\"}}' ' return done(msg[\"id\"], word + \": \" + got[\"model\"][\"content\"][\"text\"])' ' if p[\"name\"] == \"read_note\":' ' if \"roots\" not in got:' ' return needs(msg[\"id\"], {\"roots\": {\"method\": \"roots/list\", \"params\": {}}})' ' path = posixpath.normpath(p[\"arguments\"][\"path\"])' ' for root in got[\"roots\"][\"roots\"]:' ' folder = root[\"uri\"][len(\"file://\"):]' ' if path.startswith(folder + \"/\"):' ' return done(msg[\"id\"], \"opened \" + path)' ' return done(msg[\"id\"], \"refused: \" + path + \" is outside every root\", True)' >> mcp/asker.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the client that answers, part 1
docker exec tools sh -c "mkdir -p mcp && printf '%s\n' '\"\"\"A client that answers the input requests of a server and retries the same call with a new id.\"\"\"' 'import json' 'import asker' 'import llm' 'CAPS = {\"sampling\": {}, \"roots\": {}}' 'ROOTS = {\"roots\": [{\"uri\": \"file:///home/sam/notes\", \"name\": \"Notes\"}]}' 'calls = 0' '' '' 'def call(tool, arguments, responses=None, state=None):' ' global calls' ' calls += 1' ' p = {\"name\": tool, \"arguments\": arguments, \"_meta\": {\"io.modelcontextprotocol/clientCapabilities\": CAPS}}' ' if responses:' ' p[\"inputResponses\"] = responses' ' if state:' ' p[\"requestState\"] = state' ' return asker.handle({\"jsonrpc\": \"2.0\", \"id\": calls, \"method\": \"tools/call\", \"params\": p})' 'def sample(ask):' ' \"\"\"The person sees the request first. Here they say yes. A real host shows it and waits.\"\"\"' > mcp/askclient.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the client that answers, part 2
docker exec tools sh -c "printf '%s\n' ' print(\" the person is shown:\", ask[\"params\"][\"messages\"][0][\"content\"][\"text\"], \"and agrees\")' ' chat = [{\"role\": \"system\", \"content\": ask[\"params\"][\"systemPrompt\"]}]' ' chat.append({\"role\": \"user\", \"content\": ask[\"params\"][\"messages\"][0][\"content\"][\"text\"]})' ' text = llm.chat(chat, max_tokens=ask[\"params\"][\"maxTokens\"])[\"choices\"][0][\"message\"][\"content\"]' ' return {\"role\": \"assistant\", \"content\": {\"type\": \"text\", \"text\": text}, \"model\": \"lab\", \"stopReason\": \"endTurn\"}' 'first = call(\"explain_word\", {\"word\": \"token\"})[\"result\"]' 'print(\"1. id 1 answered:\", json.dumps(first)[:150], \"...\")' 'answers = {\"model\": sample(first[\"inputRequests\"][\"model\"])}' 'again = call(\"explain_word\", {\"word\": \"token\"}, answers, first[\"requestState\"])' 'print(\"2. id 2, the retry:\", again[\"result\"][\"content\"][0][\"text\"])' >> mcp/askclient.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the client that answers, part 3
docker exec tools sh -c "printf '%s\n' 'changed = first[\"requestState\"].replace(\"token\", \"password\")' 'again = call(\"explain_word\", {\"word\": \"password\"}, answers, changed)' 'print(\"3. id 3, with the state changed by the client:\", again[\"error\"][\"message\"])' 'print(\"4. id 4 answered:\", call(\"read_note\", {\"path\": \"/home/sam/notes/monday.txt\"})[\"result\"][\"inputRequests\"])' 'for path in [\"/home/sam/notes/monday.txt\", \"/home/sam/notes/../.ssh/id_rsa\"]:' ' result = call(\"read_note\", {\"path\": path}, {\"roots\": ROOTS})[\"result\"]' ' print(\"5. id\", calls, result[\"content\"][0][\"text\"], \"isError\", result[\"isError\"])' >> mcp/askclient.py"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Run both rounds
docker exec tools python3 mcp/askclient.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
What askclient.py printed
| Request | What happened |
|---|---|
| id 1 explain_word | input_required with a sampling/createMessage request |
| The person | is shown "What is a token?" and agrees |
| id 2, the retry | token: A token is a small piece of text, often part of a word. |
| id 3, state changed | requestState failed its check |
| id 4 read_note | input_required with a roots/list request |
| id 5 and 6 | opened /home/sam/notes/monday.txt, refused /home/sam/.ssh/id_rsa |
The cells students fill in are left blank in this preview.
Check the Run
Have a go
Check the Run
3 questionsA common mistake
Warning
Many people think a client should send a sampling request to the model at once. The person should see it first, because the prompt comes from the server. A server could ask the model for anything. Show the request, let the person refuse, and show the answer before it goes back.
Match each request to what it asks
Have a go
Loading…
Your answers are marked here and are not kept. An account keeps your marks.
Assignment: answer and retry
Write the client's side of an input request.
pending(result) lists the keys the server asked for. retry(original, new_id, responses, state) builds the retried request without changing the original. inside_roots(path, roots) says whether a path, once cleaned with posixpath.normpath, is inside one of the roots.
import posixpath
def pending(result):
# The keys of result["inputRequests"], sorted, when result["resultType"] is
# "input_required". Otherwise an empty list.
return []
def retry(original, new_id, responses, state):
# A new request: the original with id new_id, and params holding everything the
# original params held plus "inputResponses": responses, and "requestState": state
# only when state is not None. Do not change the original.
return original
def inside_roots(path, roots):
# roots: a list like [{"uri": "file:///home/sam/notes", "name": "Notes"}].
# True when path, after posixpath.normpath, is inside one root folder.
return True
Make a free account to save your work and track your progress.
Exit quiz
Have a go