chore: import upstream snapshot with attribution
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
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
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
from typing import Annotated
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from mcp.server import MCPServer
|
||||
from mcp.server.mcpserver import Resolve
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
INVENTORY = {"Dune": 7, "Neuromancer": 0}
|
||||
|
||||
|
||||
class Stock(BaseModel):
|
||||
title: str
|
||||
copies: int
|
||||
|
||||
|
||||
async def check_stock(title: str) -> Stock:
|
||||
return Stock(title=title, copies=INVENTORY.get(title, 0))
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def reserve_book(title: str, stock: Annotated[Stock, Resolve(check_stock)]) -> str:
|
||||
"""Reserve a copy of a book."""
|
||||
if stock.copies == 0:
|
||||
return f"{title!r} is out of stock."
|
||||
return f"Reserved {title!r} ({stock.copies - 1} copies left)."
|
||||
@@ -0,0 +1,35 @@
|
||||
from typing import Annotated
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
from mcp.server import MCPServer
|
||||
from mcp.server.mcpserver import Resolve
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
INVENTORY = {"Dune": 7, "Neuromancer": 0}
|
||||
|
||||
|
||||
class Stock(BaseModel):
|
||||
title: str
|
||||
copies: int
|
||||
|
||||
|
||||
async def check_stock(title: str) -> Stock:
|
||||
return Stock(title=title, copies=INVENTORY.get(title, 0))
|
||||
|
||||
|
||||
async def estimate_delivery(stock: Annotated[Stock, Resolve(check_stock)]) -> str:
|
||||
return "tomorrow" if stock.copies > 0 else "in 2-3 weeks"
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def order_book(
|
||||
title: str,
|
||||
stock: Annotated[Stock, Resolve(check_stock)],
|
||||
delivery: Annotated[str, Resolve(estimate_delivery)],
|
||||
) -> str:
|
||||
"""Order a book from the shop."""
|
||||
if stock.copies == 0:
|
||||
return f"{title!r} is on backorder; it would arrive {delivery}."
|
||||
return f"Ordered {title!r}; it arrives {delivery}."
|
||||
@@ -0,0 +1,46 @@
|
||||
from typing import Annotated
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from mcp.server import MCPServer
|
||||
from mcp.server.mcpserver import Elicit, Resolve
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
INVENTORY = {"Dune": 7, "Neuromancer": 0}
|
||||
|
||||
|
||||
class Stock(BaseModel):
|
||||
title: str
|
||||
copies: int
|
||||
|
||||
|
||||
class Backorder(BaseModel):
|
||||
confirm: bool = Field(description="Order anyway and wait?")
|
||||
|
||||
|
||||
async def check_stock(title: str) -> Stock:
|
||||
return Stock(title=title, copies=INVENTORY.get(title, 0))
|
||||
|
||||
|
||||
async def confirm_backorder(
|
||||
title: str,
|
||||
stock: Annotated[Stock, Resolve(check_stock)],
|
||||
) -> Backorder | Elicit[Backorder]:
|
||||
if stock.copies > 0:
|
||||
return Backorder(confirm=True) # in stock: nothing to ask
|
||||
return Elicit(f"{title!r} is out of stock (2-3 weeks). Order anyway?", Backorder)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def order_book(
|
||||
title: str,
|
||||
stock: Annotated[Stock, Resolve(check_stock)],
|
||||
backorder: Annotated[Backorder, Resolve(confirm_backorder)],
|
||||
) -> str:
|
||||
"""Order a book from the shop."""
|
||||
if not backorder.confirm:
|
||||
return "No order placed."
|
||||
if stock.copies == 0:
|
||||
return f"Backordered {title!r}; it ships in 2-3 weeks."
|
||||
return f"Ordered {title!r}."
|
||||
@@ -0,0 +1,26 @@
|
||||
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 suggest_title(genre: str) -> Sample:
|
||||
prompt = f"Suggest one {genre} book title. Answer with the title only."
|
||||
return Sample(
|
||||
[SamplingMessage(role="user", content=TextContent(type="text", text=prompt))],
|
||||
max_tokens=50,
|
||||
)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def recommend_book(
|
||||
genre: str,
|
||||
suggestion: Annotated[CreateMessageResult, Resolve(suggest_title)],
|
||||
) -> str:
|
||||
"""Recommend a book in the given genre."""
|
||||
title = suggestion.content.text if suggestion.content.type == "text" else "the classics"
|
||||
return f"Today's {genre} pick: {title}"
|
||||
Reference in New Issue
Block a user