FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/docs_src/extensions/tutorial004.py at main · stophobia/python-sdk · GitHub
stophobia
/
python-sdk
Public
forked from
modelcontextprotocol/python-sdk
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-sdk
/
docs_src
/
extensions
/
tutorial004.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
59 lines (40 loc) · 1.68 KB
Breadcrumbs
python-sdk
/
docs_src
/
extensions
/
tutorial004.py
Copy path
File metadata and controls
59 lines (40 loc) · 1.68 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
57
58
59
from
collections
.
abc
import
Sequence
from
typing
import
Any
,
Literal
from
pydantic
import
Field
import
mcp
.
types
as
types
from
mcp
import
Client
from
mcp
.
client
import
advertise
from
mcp
.
server
.
context
import
ServerRequestContext
from
mcp
.
server
.
extension
import
Extension
,
MethodBinding
from
mcp
.
server
.
mcpserver
import
MCPServer
,
require_client_extension
EXTENSION_ID
=
"com.example/search"
class
SearchParams
(
types
.
RequestParams
):
query
:
str
limit
:
int
=
Field
(
default
=
10
,
ge
=
1
,
le
=
100
)
class
SearchResult
(
types
.
Result
):
items
:
list
[
str
]
class
SearchRequest
(
types
.
Request
[
SearchParams
,
Literal
[
"com.example/search"
]]):
method
:
Literal
[
"com.example/search"
]
=
"com.example/search"
params
:
SearchParams
async
def
search
(
ctx
:
ServerRequestContext
[
Any
,
Any
],
params
:
SearchParams
)
->
SearchResult
:
require_client_extension
(
ctx
,
EXTENSION_ID
)
return
SearchResult
(
items
=
[
f"
{
params
.
query
}
-
{
n
}
"
for
n
in
range
(
params
.
limit
)])
class
Search
(
Extension
):
"""An extension that serves its own request method."""
identifier
=
EXTENSION_ID
def
methods
(
self
)
->
Sequence
[
MethodBinding
]:
return
[
MethodBinding
(
"com.example/search"
,
SearchParams
,
search
,
protocol_versions
=
frozenset
({
"2026-07-28"
}),
)
]
mcp
=
MCPServer
(
"catalog"
,
extensions
=
[
Search
()])
async
def
main
()
->
None
:
async
with
Client
(
mcp
,
extensions
=
[
advertise
(
EXTENSION_ID
)])
as
client
:
request
=
SearchRequest
(
params
=
SearchParams
(
query
=
"mcp"
,
limit
=
3
))
result
=
await
client
.
session
.
send_request
(
request
,
SearchResult
)
print
(
result
.
items
)
# ['mcp-0', 'mcp-1', 'mcp-2']
Back
|
FazBrowse Home
|
New Git URL