시작하기무료로 시작하기

Delegate the summary with sampling

Your server's summarize tool needs a language model to condense some text — but you don't want the server holding an API key. With sampling, the server asks the connected client to run the prompt instead, and the client returns the generated text. You also want the summary kept to a single sentence, so you'll steer the model with a system prompt.

FastMCP, Context, SamplingMessage, and TextContent are imported and the server mcp is created. run_tool(text_to_summarize) runs your tool through a real MCP client–server session; the connected client fulfills each sampling request by calling a real language model and returning its reply.

이 연습은 강의의 일부입니다

Model Context Protocol: Advanced Topics

강의 보기

연습 안내

  • Call ctx.session.create_message() to ask the client to run the prompt.
  • Steer the model by passing a system_prompt that tells it to reply with a single-sentence summary.
  • Return the generated text from the result with result.content.text.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

@mcp.tool()
async def summarize(text_to_summarize: str, ctx: Context):
    prompt = f"Please summarize the following text:\n{text_to_summarize}"

    # Ask the client's model to summarize, steered by a system prompt
    result = await ctx.session.____(
        messages=[
            SamplingMessage(role="user", content=TextContent(type="text", text=prompt))
        ],
        system_prompt=____,
        max_tokens=1000,
    )

    # Return the generated text
    return result.____.text


# Run the tool through a real MCP client-server session
summary = run_tool("lots of text")
print(summary)
코드 편집 및 실행