Rebuild the docs around tested examples; shrink README.v2.md to a pitch (#2978)
This commit is contained in:
parent
4caa41f6d5
commit
411a6d3980
1 changed files with 9510 additions and 2639 deletions
42
docs_src/lowlevel/tutorial004.py
Normal file
42
docs_src/lowlevel/tutorial004.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
from mcp_types import (
|
||||
CallToolRequestParams,
|
||||
CallToolResult,
|
||||
ListToolsResult,
|
||||
PaginatedRequestParams,
|
||||
TextContent,
|
||||
Tool,
|
||||
)
|
||||
|
||||
from mcp.server import Server, ServerRequestContext
|
||||
|
||||
SEARCH_BOOKS = Tool(
|
||||
name="search_books",
|
||||
description="Search the catalog by title or author.",
|
||||
input_schema={
|
||||
"type": "object",
|
||||
"properties": {"query": {"type": "string"}, "limit": {"type": "integer"}},
|
||||
"required": ["query", "limit"],
|
||||
},
|
||||
output_schema={
|
||||
"type": "object",
|
||||
"properties": {"matches": {"type": "integer"}, "query": {"type": "string"}},
|
||||
"required": ["matches", "query"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def list_tools(ctx: ServerRequestContext, params: PaginatedRequestParams | None) -> ListToolsResult:
|
||||
return ListToolsResult(tools=[SEARCH_BOOKS])
|
||||
|
||||
|
||||
async def call_tool(ctx: ServerRequestContext, params: CallToolRequestParams) -> CallToolResult:
|
||||
args = params.arguments or {}
|
||||
data = {"matches": 3, "query": args["query"]}
|
||||
return CallToolResult(
|
||||
content=[TextContent(type="text", text=f"Found 3 books matching {args['query']!r}.")],
|
||||
structured_content=data,
|
||||
_meta={"bookshop/record_ids": ["bk_17", "bk_42", "bk_99"]},
|
||||
)
|
||||
|
||||
|
||||
server = Server("Bookshop", on_list_tools=list_tools, on_call_tool=call_tool)
|
||||
Loading…
Reference in a new issue