Co-authored-by: Felix Weinberger <fweinberger@anthropic.com> Co-authored-by: Claude <noreply@anthropic.com>
25 lines
794 B
Python
25 lines
794 B
Python
from mcp.server.fastmcp import Context, FastMCP
|
|
from mcp.server.session import ServerSession
|
|
from mcp.types import SamplingMessage, TextContent
|
|
|
|
mcp = FastMCP(name="Sampling Example")
|
|
|
|
|
|
@mcp.tool()
|
|
async def generate_poem(topic: str, ctx: Context[ServerSession, None]) -> str:
|
|
"""Generate a poem using LLM sampling."""
|
|
prompt = f"Write a short poem about {topic}"
|
|
|
|
result = await ctx.session.create_message(
|
|
messages=[
|
|
SamplingMessage(
|
|
role="user",
|
|
content=TextContent(type="text", text=prompt),
|
|
)
|
|
],
|
|
max_tokens=100,
|
|
)
|
|
|
|
if all(c.type == "text" for c in result.content_as_list):
|
|
return "\n".join(c.text for c in result.content_as_list if c.type == "text")
|
|
return str(result.content)
|