FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/docs_src/middleware/tutorial001.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
/
middleware
/
tutorial001.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
49 lines (39 loc) · 1.47 KB
Breadcrumbs
python-sdk
/
docs_src
/
middleware
/
tutorial001.py
Copy path
File metadata and controls
49 lines (39 loc) · 1.47 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
import
logging
import
time
from
mcp
.
server
import
Server
,
ServerRequestContext
from
mcp
.
server
.
context
import
CallNext
,
HandlerResult
from
mcp
.
types
import
(
CallToolRequestParams
,
CallToolResult
,
ListToolsResult
,
PaginatedRequestParams
,
TextContent
,
Tool
,
)
logger
=
logging
.
getLogger
(
__name__
)
async
def
on_list_tools
(
ctx
:
ServerRequestContext
,
params
:
PaginatedRequestParams
|
None
)
->
ListToolsResult
:
return
ListToolsResult
(
tools
=
[
Tool
(
name
=
"search_books"
,
description
=
"Search the catalog by title or author."
,
input_schema
=
{
"type"
:
"object"
,
"properties"
: {
"query"
: {
"type"
:
"string"
}},
"required"
: [
"query"
],
},
)
]
)
async
def
on_call_tool
(
ctx
:
ServerRequestContext
,
params
:
CallToolRequestParams
)
->
CallToolResult
:
query
=
(
params
.
arguments
or
{})[
"query"
]
return
CallToolResult
(
content
=
[
TextContent
(
type
=
"text"
,
text
=
f"Found 3 books matching
{
query
!r
}
."
)])
async
def
log_timing
(
ctx
:
ServerRequestContext
,
call_next
:
CallNext
)
->
HandlerResult
:
start
=
time
.
perf_counter
()
try
:
return
await
call_next
(
ctx
)
finally
:
elapsed_ms
=
(
time
.
perf_counter
()
-
start
)
*
1000
logger
.
info
(
"%s took %.1f ms"
,
ctx
.
method
,
elapsed_ms
)
server
=
Server
(
"Bookshop"
,
on_list_tools
=
on_list_tools
,
on_call_tool
=
on_call_tool
)
server
.
middleware
.
append
(
log_timing
)
Back
|
FazBrowse Home
|
New Git URL