Generate a poem using LLM sampling.
(topic: str, ctx: Context)
| 7 | |
| 8 | @mcp.tool() |
| 9 | async 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) |
nothing calls this directly
no test coverage detected