FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk/docs_src/mrtr/tutorial005.py at main · Stars1233/python-sdk · GitHub
Stars1233
/
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
/
mrtr
/
tutorial005.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
38 lines (27 loc) · 1.48 KB
Breadcrumbs
python-sdk
/
docs_src
/
mrtr
/
tutorial005.py
Copy path
File metadata and controls
38 lines (27 loc) · 1.48 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
import
os
from
cryptography
.
exceptions
import
InvalidTag
from
cryptography
.
hazmat
.
primitives
.
ciphers
.
aead
import
AESGCM
from
mcp
.
server
import
MCPServer
from
mcp
.
server
.
mcpserver
import
InvalidRequestState
,
RequestStateSecurity
PREFIX
=
"kms1."
# format version; fed to GCM as associated data, so it is bound under the tag
def
unwrap_data_key
()
->
bytes
:
"""One KMS call at process start, kms.decrypt(CiphertextBlob=...); every token after that is local crypto."""
return
os
.
urandom
(
32
)
# stand-in for the unwrapped 32-byte data key
class
EnvelopeCodec
:
def
__init__
(
self
,
data_key
:
bytes
)
->
None
:
self
.
_aesgcm
=
AESGCM
(
data_key
)
def
seal
(
self
,
payload
:
bytes
)
->
str
:
nonce
=
os
.
urandom
(
12
)
return
PREFIX
+
(
nonce
+
self
.
_aesgcm
.
encrypt
(
nonce
,
payload
,
PREFIX
.
encode
())).
hex
()
def
unseal
(
self
,
token
:
str
)
->
bytes
:
if
not
token
.
startswith
(
PREFIX
):
raise
InvalidRequestState
(
"unknown token format"
)
body
=
token
[
len
(
PREFIX
) :]
try
:
raw
=
bytes
.
fromhex
(
body
)
if
raw
.
hex
()
!=
body
:
# only the exact string seal() produced verifies
raise
ValueError
(
"non-canonical hex"
)
return
self
.
_aesgcm
.
decrypt
(
raw
[:
12
],
raw
[
12
:],
PREFIX
.
encode
())
except
(
ValueError
,
InvalidTag
)
as
exc
:
raise
InvalidRequestState
(
"token failed verification"
)
from
exc
mcp
=
MCPServer
(
"Deployer"
,
request_state_security
=
RequestStateSecurity
(
codec
=
EnvelopeCodec
(
unwrap_data_key
())))
Back
|
FazBrowse Home
|
New Git URL