본문 바로가기
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

MCP 를 대화답게 만들어주는 건 client 쪽 셋이야. 이 셋은 저마다, server 가 host 를 거쳐 host 만 할 수 있는 일에 손을 뻗게 해줘.

Sampling 은 server 가 host 의 LLM 한테 대신 추론해달라고 부탁하는 거야. 전형적인 경우는 이래. 코드를 분석하는 server 가 가능한 리팩터링 몇 가지를 놓고 판단을 해야 해. 자기 LLM 을 싣는 대신 sampling 요청을 보내면 host 가 추론을 돌려줘. Host 는 그걸 넘기기 전에 반드시 user 한테 물어봐야 해. sampling 은 결국 'server 가 내 두뇌를 빌리고 싶다' 는 말이라 동의가 필요하거든. 승인이 나면 결과가 sampling 결과로 server 에 흘러가.

Roots 는 server 가 host 한테 이번 session 의 활동 범위를 묻는 거야. Filesystem server 는 $HOME 에 접근할 수 있다고 넘겨짚으면 안 돼. Host 한테 root 를 물어서 file:///Users/me/project 같은 답을 받고 그 안에서만 움직여야지. Host 는 보통 지금 열려 있는 workspace 나 user 가 고른 폴더에서 root 를 뽑아내. Protocol 은 그 질문을 어떻게 주고받는지만 정해줘.

Elicitation 은 셋 중 제일 인간적이야. Server 가 workflow 중간에, 처음부터 다시 돌리지 않고도 user 한테 입력을 더 달라고 하는 거지. '어느 환경에 배포할까?' '테스트 파일도 포함할까?' Server 가 원하는 입력을 작은 JSON Schema 로 그려서 elicitation/create 요청을 보내면, host 가 입력 폼을 띄우고 답을 모아 돌려줘. Elicitation 덕에, protocol 의 구조를 하나도 안 깨면서 한 번 부르고 끝나던 tool 이 오가는 대화로 바뀌어.

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 이야) 골라서 시제품을 만들어봐. Tool 을 부르면 끝나기 전에 user 한테 정해진 형식의 입력을 하나 받게 해. Claude Desktop 으로 돌려서 tool call 한복판에 입력 폼이 튀어나오는 걸 봐. 그게 바로 protocol 이 한 번짜리 호출을 대화로 바꾸는 순간이야.

Progress

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

댓글 0

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

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