C.W.K.
Stream
Lesson 05 of 05 · published

Sampling · Roots · Elicitation

~22 min · sampling, roots, elicitation, client-features

Level 0호기심 많은 독자
0 XP0/48 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

Client 쪽 trio 가 MCP 를 conversational 하게 만드는 거. 각 capability 가 server 가 host 통해 — host 만 할 수 있는 일 — 으로 손 뻗게 함.

Sampling 은 server 가 host LLM 한테 자기 대신 inference 해달라고 요청. 클래식 케이스: 코드 분석 server 가 가능 refactor 셋 사이 추론하고 싶음 — 자기 LLM ship 대신 sampling request 보냄, host 가 inference 돌림. Host 는 forward 전 user 한테 MUST 물어봄 — sampling 이 'server 가 내 두뇌 빌리고 싶음' 이라 동의 필요. 승인되면 response 가 보통 sampling result 로 server 에 흘러감.

Roots 는 server 가 host 한테 — 현재 session 의 scope directory/URI 를 — 물음. Filesystem server 가 $HOME access 가정 금지; host 한테 root 물어 file:///Users/me/project 같은 거 받고 그 안에서 동작. Host 가 보통 현재 workspace 나 user pick 에서 root 도출; protocol 은 질문 어떻게 묻는지만 정의.

Elicitation 은 셋 중 가장 인간적: server 가 — workflow restart 안 하고 — mid-workflow 에 user 한테 input 추가 요청. '어느 환경에 deploy 해?' '테스트 파일 포함해?' Server 가 — input 을 작은 JSON Schema 로 — 묘사하는 elicitation/create request 보냄; host 가 form 렌더링, response 수집, 반환. Elicitation 이 — protocol 구조 안 깨고 — 일회성 tool call 을 interactive workflow 로 바꿔.

Code

Sampling — server 가 host 한테 생각해달라고·python
# MCP server 안 (Python SDK)
from mcp.types import CreateMessageRequest, SamplingMessage, TextContent

@app.tool()
async def analyze_diff(diff: str) -> list:
    # Host 한테 inference 돌려달라고
    completion = await app.request_context.session.create_message(
        CreateMessageRequest(
            messages=[SamplingMessage(role="user", content=TextContent(
                type="text",
                text=f"Pick the riskiest hunk in this diff. One paragraph.\n\n{diff}",
            ))],
            max_tokens=400,
        )
    )
    return [TextContent(type="text", text=completion.content.text)]
Roots — server 가 filesystem scope 물음·python
roots = await app.request_context.session.list_roots()
# roots = [{"uri": "file:///Users/me/project", "name": "project"}]
allowed = [r.uri for r in roots]
Elicitation — server 가 user 한테 구조화된 질문·python
from mcp.types import ElicitRequest

answer = await app.request_context.session.elicit(ElicitRequest(
    message="Which environment should I deploy to?",
    requestedSchema={
        "type": "object",
        "properties": {"env": {"type": "string", "enum": ["staging","production"]}},
        "required": ["env"],
    },
))
target = answer.content["env"]

External links

Exercise

셋 중 가장 낯선 거 (보통 elicitation) 골라 prototype: tool 호출 시 — 완료 전에 user 에게 single 구조화된 input elicit. Claude Desktop 으로 돌려 — tool call 중간에 elicitation form 등장하는 거 봐. 그게 protocol 이 일회성을 conversation 으로 바꾸는 거.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.