MCPcopy Index your code
hub / github.com/modelcontextprotocol/python-sdk / generate_poem

Function generate_poem

examples/snippets/servers/sampling.py:9–26  ·  view source on GitHub ↗

Generate a poem using LLM sampling.

(topic: str, ctx: Context)

Source from the content-addressed store, hash-verified

7
8@mcp.tool()
9async def generate_poem(topic: str, ctx: Context) -> str:
10 """Generate a poem using LLM sampling."""
11 prompt = f"Write a short poem about {topic}"
12
13 result = await ctx.session.create_message( # pyright: ignore[reportDeprecated]
14 messages=[
15 SamplingMessage(
16 role="user",
17 content=TextContent(type="text", text=prompt),
18 )
19 ],
20 max_tokens=100,
21 )
22
23 # Since we're not passing tools param, result.content is single content
24 if result.content.type == "text":
25 return result.content.text
26 return str(result.content)

Callers

nothing calls this directly

Calls 3

SamplingMessageClass · 0.90
TextContentClass · 0.90
create_messageMethod · 0.80

Tested by

no test coverage detected