FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/docs_src/lowlevel/tutorial005.py at main · uuzzrm/python-sdk · GitHub
uuzzrm
python-sdk
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-sdk
/
docs_src
/
lowlevel
/
tutorial005.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
50 lines (36 loc) · 1.46 KB
Breadcrumbs
python-sdk
/
docs_src
/
lowlevel
/
tutorial005.py
Copy path
File metadata and controls
50 lines (36 loc) · 1.46 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from
collections
.
abc
import
AsyncIterator
from
contextlib
import
asynccontextmanager
from
dataclasses
import
dataclass
from
mcp
.
server
import
Server
,
ServerRequestContext
from
mcp
.
types
import
(
CallToolRequestParams
,
CallToolResult
,
ListToolsResult
,
PaginatedRequestParams
,
TextContent
,
Tool
,
)
@
dataclass
class
Catalog
:
books
:
list
[
str
]
def
search
(
self
,
query
:
str
)
->
list
[
str
]:
return
[
title
for
title
in
self
.
books
if
query
.
lower
()
in
title
.
lower
()]
@
asynccontextmanager
async
def
lifespan
(
server
:
Server
[
Catalog
])
->
AsyncIterator
[
Catalog
]:
yield
Catalog
(
books
=
[
"Dune"
,
"Dune Messiah"
,
"Children of Dune"
])
SEARCH_BOOKS
=
Tool
(
name
=
"search_books"
,
description
=
"Search the catalog by title or author."
,
input_schema
=
{
"type"
:
"object"
,
"properties"
: {
"query"
: {
"type"
:
"string"
}},
"required"
: [
"query"
],
},
)
async
def
list_tools
(
ctx
:
ServerRequestContext
[
Catalog
],
params
:
PaginatedRequestParams
|
None
)
->
ListToolsResult
:
return
ListToolsResult
(
tools
=
[
SEARCH_BOOKS
])
async
def
call_tool
(
ctx
:
ServerRequestContext
[
Catalog
],
params
:
CallToolRequestParams
)
->
CallToolResult
:
matches
=
ctx
.
lifespan_context
.
search
((
params
.
arguments
or
{})[
"query"
])
text
=
f"Found
{
len
(
matches
)
}
books:
{
', '
.
join
(
matches
)
}
."
return
CallToolResult
(
content
=
[
TextContent
(
type
=
"text"
,
text
=
text
)])
server
=
Server
(
"Bookshop"
,
lifespan
=
lifespan
,
on_list_tools
=
list_tools
,
on_call_tool
=
call_tool
)
Back
|
FazBrowse Home
|
New Git URL