FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/docs_src/client/tutorial001.py at main · modelcontextprotocol/python-sdk · GitHub
modelcontextprotocol
python-sdk
Repository navigation
Code
Issues
247
(247)
Pull requests
106
(106)
Actions
Projects
Security and quality
12
(12)
Insights
Expand file tree
Breadcrumbs
python-sdk
/
docs_src
/
client
/
tutorial001.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
56 lines (39 loc) · 1.66 KB
Breadcrumbs
python-sdk
/
docs_src
/
client
/
tutorial001.py
Copy path
File metadata and controls
56 lines (39 loc) · 1.66 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
51
52
53
54
55
56
from
pydantic
import
BaseModel
from
mcp
.
server
import
MCPServer
from
mcp
.
server
.
mcpserver
.
exceptions
import
ToolError
from
mcp
.
types
import
Completion
,
CompletionArgument
,
CompletionContext
,
PromptReference
,
ResourceTemplateReference
mcp
=
MCPServer
(
"Bookshop"
,
instructions
=
"Search the catalog before recommending a book."
)
GENRES
=
[
"fiction"
,
"non-fiction"
,
"poetry"
]
class
Book
(
BaseModel
):
title
:
str
author
:
str
year
:
int
@
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
}
)."
@
mcp
.
tool
()
def
lookup_book
(
title
:
str
)
->
Book
:
"""Look up a book by its exact title."""
if
title
!=
"Dune"
:
raise
ToolError
(
f"No book titled
{
title
!r
}
in the catalog."
)
return
Book
(
title
=
"Dune"
,
author
=
"Frank Herbert"
,
year
=
1965
)
@
mcp
.
resource
(
"catalog://genres"
)
def
genres
()
->
list
[
str
]:
"""The genres the catalog is organised by."""
return
GENRES
@
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
}
."
@
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."
@
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
)])
Back
|
FazBrowse Home
|
New Git URL