49b9bb6724
Deploy Docs / deploy-docs (push) Failing after 1s
Conformance Tests / client-conformance (push) Failing after 3s
Conformance Tests / server-conformance (push) Failing after 1s
GitHub Actions Security Analysis / zizmor (push) Failing after 1s
CI / checks (push) Failing after 59m20s
CI / all-green (push) Waiting to run
26 lines
887 B
Python
26 lines
887 B
Python
"""Sampling primitive: a tool asks the client's LLM for a completion mid-call."""
|
|
|
|
from mcp_types import SamplingMessage, TextContent
|
|
|
|
from mcp.server.mcpserver import Context, MCPServer
|
|
from stories._hosting import run_server_from_args
|
|
|
|
|
|
def build_server() -> MCPServer:
|
|
mcp = MCPServer("sampling-example")
|
|
|
|
@mcp.tool(description="Summarize text by asking the host's LLM via sampling/createMessage.")
|
|
async def summarize(text: str, ctx: Context) -> str:
|
|
result = await ctx.session.create_message( # pyright: ignore[reportDeprecated]
|
|
messages=[SamplingMessage(role="user", content=TextContent(text=f"Summarize in one sentence:\n\n{text}"))],
|
|
max_tokens=200,
|
|
)
|
|
assert isinstance(result.content, TextContent)
|
|
return result.content.text
|
|
|
|
return mcp
|
|
|
|
|
|
if __name__ == "__main__":
|
|
run_server_from_args(build_server)
|