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,18 @@
|
||||
from mcp import Client
|
||||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop", instructions="Search the catalog before recommending a book.")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def search_books(query: str) -> str:
|
||||
"""Search the catalog by title or author."""
|
||||
return f"Found 3 books matching {query!r}."
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
print(client.server_info)
|
||||
print(client.server_capabilities)
|
||||
print(client.protocol_version)
|
||||
print(client.instructions)
|
||||
@@ -0,0 +1,20 @@
|
||||
from mcp import Client
|
||||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
|
||||
@mcp.tool(title="Search the catalog")
|
||||
def search_books(query: str, limit: int = 10) -> str:
|
||||
"""Search the catalog by title or author."""
|
||||
return f"Found 3 books matching {query!r} (showing up to {limit})."
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
result = await client.list_tools()
|
||||
for tool in result.tools:
|
||||
print(tool.name)
|
||||
print(tool.title)
|
||||
print(tool.description)
|
||||
print(tool.input_schema)
|
||||
@@ -0,0 +1,33 @@
|
||||
from mcp_types import TextContent
|
||||
from pydantic import BaseModel
|
||||
|
||||
from mcp import Client
|
||||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
|
||||
class Book(BaseModel):
|
||||
title: str
|
||||
author: str
|
||||
year: int
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def lookup_book(title: str) -> Book:
|
||||
"""Look up a book by its exact title."""
|
||||
if title != "Dune":
|
||||
raise ValueError(f"No book titled {title!r} in the catalog.")
|
||||
return Book(title="Dune", author="Frank Herbert", year=1965)
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
result = await client.call_tool("lookup_book", {"title": "Dune"})
|
||||
|
||||
for block in result.content:
|
||||
if isinstance(block, TextContent):
|
||||
print(block.text)
|
||||
|
||||
print(result.structured_content)
|
||||
print(result.is_error)
|
||||
@@ -0,0 +1,32 @@
|
||||
from mcp_types import TextResourceContents
|
||||
|
||||
from mcp import Client
|
||||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
|
||||
@mcp.resource("catalog://genres")
|
||||
def genres() -> list[str]:
|
||||
"""The genres the catalog is organised by."""
|
||||
return ["fiction", "non-fiction", "poetry"]
|
||||
|
||||
|
||||
@mcp.resource("catalog://genres/{genre}")
|
||||
def books_in_genre(genre: str) -> str:
|
||||
"""Every title we stock in one genre."""
|
||||
return f"3 books filed under {genre}."
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
listed = await client.list_resources()
|
||||
print([resource.uri for resource in listed.resources])
|
||||
|
||||
templates = await client.list_resource_templates()
|
||||
print([template.uri_template for template in templates.resource_templates])
|
||||
|
||||
result = await client.read_resource("catalog://genres/poetry")
|
||||
for contents in result.contents:
|
||||
if isinstance(contents, TextResourceContents):
|
||||
print(contents.text)
|
||||
@@ -0,0 +1,20 @@
|
||||
from mcp import Client
|
||||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
|
||||
@mcp.prompt(title="Recommend a book")
|
||||
def recommend(genre: str) -> str:
|
||||
"""Ask for a recommendation in a genre."""
|
||||
return f"Recommend one {genre} book from the catalog and say why."
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
listed = await client.list_prompts()
|
||||
print(listed.prompts)
|
||||
|
||||
result = await client.get_prompt("recommend", {"genre": "poetry"})
|
||||
for message in result.messages:
|
||||
print(message.role, message.content)
|
||||
@@ -0,0 +1,32 @@
|
||||
from mcp_types import Completion, CompletionArgument, CompletionContext, PromptReference, ResourceTemplateReference
|
||||
|
||||
from mcp import Client
|
||||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
GENRES = ["fiction", "non-fiction", "poetry"]
|
||||
|
||||
|
||||
@mcp.prompt()
|
||||
def recommend(genre: str) -> str:
|
||||
"""Ask for a recommendation in a genre."""
|
||||
return f"Recommend one {genre} book from the catalog and say why."
|
||||
|
||||
|
||||
@mcp.completion()
|
||||
async def complete_genre(
|
||||
ref: PromptReference | ResourceTemplateReference,
|
||||
argument: CompletionArgument,
|
||||
context: CompletionContext | None,
|
||||
) -> Completion | None:
|
||||
return Completion(values=[genre for genre in GENRES if genre.startswith(argument.value)])
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
result = await client.complete(
|
||||
ref=PromptReference(type="ref/prompt", name="recommend"),
|
||||
argument={"name": "genre", "value": "p"},
|
||||
)
|
||||
print(result.completion.values)
|
||||
@@ -0,0 +1,31 @@
|
||||
from mcp_types import Tool
|
||||
|
||||
from mcp import Client
|
||||
from mcp.server import MCPServer
|
||||
|
||||
mcp = MCPServer("Bookshop")
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def search_books(query: str) -> str:
|
||||
"""Search the catalog by title or author."""
|
||||
return f"Found 3 books matching {query!r}."
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def reserve_book(title: str) -> str:
|
||||
"""Put a book on hold."""
|
||||
return f"Reserved {title!r}."
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
async with Client(mcp) as client:
|
||||
tools: list[Tool] = []
|
||||
cursor: str | None = None
|
||||
while True:
|
||||
page = await client.list_tools(cursor=cursor)
|
||||
tools.extend(page.tools)
|
||||
if page.next_cursor is None:
|
||||
break
|
||||
cursor = page.next_cursor
|
||||
print([tool.name for tool in tools])
|
||||
Reference in New Issue
Block a user