Extend resolver DI to sampling and roots requests (#3049)

This commit is contained in:
Max 2026-07-06 18:25:57 +01:00 • committed by GitHub
commit d287c9868f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 1134 additions and 200 deletions

View file

View file

@ -0,0 +1,22 @@
from typing import Annotated
from mcp_types import CreateMessageResult, SamplingMessage, TextContent
from mcp.server import MCPServer
from mcp.server.mcpserver import Resolve, Sample
mcp = MCPServer("Bookshop")
def draft_blurb(title: str) -> Sample:
prompt = f"Write a one-sentence blurb for the book {title!r}."
return Sample(
[SamplingMessage(role="user", content=TextContent(type="text", text=prompt))],
max_tokens=60,
)
@mcp.tool()
async def blurb(title: str, draft: Annotated[CreateMessageResult, Resolve(draft_blurb)]) -> str:
"""Draft a blurb for a book."""
return draft.content.text if draft.content.type == "text" else "No blurb."

View file

@ -0,0 +1,20 @@
from typing import Annotated
from mcp_types import ListRootsResult
from mcp.server import MCPServer
from mcp.server.mcpserver import ListRoots, Resolve
mcp = MCPServer("Bookshop")
def workspace_roots() -> ListRoots:
return ListRoots()
@mcp.tool()
async def catalog_folder(roots: Annotated[ListRootsResult, Resolve(workspace_roots)]) -> str:
"""Pick the folder the catalog export should go to."""
if not roots.roots:
return "No workspace folders shared."
return str(roots.roots[0].uri)