docs: add a "What's new in v2" page (#3054)
This commit is contained in:
parent
220d362112
commit
e4d95e0d44
2 changed files with 348 additions and 2 deletions
0
docs_src/whats_new/__init__.py
Normal file
0
docs_src/whats_new/__init__.py
Normal file
37
docs_src/whats_new/tutorial001.py
Normal file
37
docs_src/whats_new/tutorial001.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
from mcp_types import (
|
||||
INVALID_PARAMS,
|
||||
CallToolRequestParams,
|
||||
CallToolResult,
|
||||
ListToolsResult,
|
||||
PaginatedRequestParams,
|
||||
TextContent,
|
||||
Tool,
|
||||
)
|
||||
|
||||
from mcp import MCPError
|
||||
from mcp.server import Server, ServerRequestContext
|
||||
|
||||
SEARCH_BOOKS = Tool(
|
||||
name="search_books",
|
||||
description="Search the catalog by title or author.",
|
||||
input_schema={ # (1)!
|
||||
"type": "object",
|
||||
"properties": {"query": {"type": "string"}},
|
||||
"required": ["query"],
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def list_tools(ctx: ServerRequestContext, params: PaginatedRequestParams | None) -> ListToolsResult: # (2)!
|
||||
return ListToolsResult(tools=[SEARCH_BOOKS]) # (3)!
|
||||
|
||||
|
||||
async def call_tool(ctx: ServerRequestContext, params: CallToolRequestParams) -> CallToolResult: # (4)!
|
||||
if params.name != "search_books":
|
||||
raise MCPError(INVALID_PARAMS, f"Unknown tool: {params.name}") # (5)!
|
||||
args = params.arguments or {} # (6)!
|
||||
text = f"Found 3 books matching {args['query']!r}."
|
||||
return CallToolResult(content=[TextContent(type="text", text=text)]) # (7)!
|
||||
|
||||
|
||||
server = Server("Bookshop", on_list_tools=list_tools, on_call_tool=call_tool) # (8)!
|
||||
Loading…
Reference in a new issue