FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/docs_src/lifespan/tutorial002.py at main · modelcontextprotocol/python-sdk · GitHub
modelcontextprotocol
python-sdk
Repository navigation
Code
Issues
245
(245)
Pull requests
106
(106)
Actions
Projects
Security and quality
12
(12)
Insights
Expand file tree
Breadcrumbs
python-sdk
/
docs_src
/
lifespan
/
tutorial002.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (29 loc) · 992 Bytes
Breadcrumbs
python-sdk
/
docs_src
/
lifespan
/
tutorial002.py
Copy path
File metadata and controls
44 lines (29 loc) · 992 Bytes
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
from
collections
.
abc
import
AsyncIterator
from
contextlib
import
asynccontextmanager
from
dataclasses
import
dataclass
from
mcp
.
server
import
MCPServer
from
mcp
.
server
.
mcpserver
import
Context
class
Database
:
def
__init__
(
self
)
->
None
:
self
.
connected
=
False
async
def
connect
(
self
)
->
None
:
self
.
connected
=
True
async
def
disconnect
(
self
)
->
None
:
self
.
connected
=
False
@
dataclass
class
AppContext
:
db
:
Database
database
=
Database
()
@
asynccontextmanager
async
def
app_lifespan
(
server
:
MCPServer
)
->
AsyncIterator
[
AppContext
]:
await
database
.
connect
()
try
:
yield
AppContext
(
db
=
database
)
finally
:
await
database
.
disconnect
()
mcp
=
MCPServer
(
"Bookshop"
,
lifespan
=
app_lifespan
)
@
mcp
.
tool
()
def
database_status
(
ctx
:
Context
[
AppContext
])
->
str
:
"""Report whether the database connection is up."""
db
=
ctx
.
request_context
.
lifespan_context
.
db
return
"connected"
if
db
.
connected
else
"disconnected"
Back
|
FazBrowse Home
|
New Git URL